Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • That was an amazing explanation, thanks for that. The code I was trying was giving me a error saying that it couldn't bound the joins! I was doing SELECT ... INTO ... FROM ... WHERE ... though. Let me give it a try tomorrow morning (late here!) and I'll revert back to you, but this looks solid. Commented Jul 3, 2017 at 21:42
  • One last question: What if I want to amend the code to insert rows only if records does not exist (like the current NOT EXISTS statement, OR if the value the [value] column is 0? Commented Jul 3, 2017 at 21:45
  • Also is there a significant performance increase when using SELECT 1 in the NOT EXISTS statement (I reckon the same would go for EXISTS) instead of doing SELECT *? Commented Jul 3, 2017 at 21:48
  • I believe the DB engine will basically ignore the SELECT list in an EXISTS (or NOT EXISTS) query. I mostly use the SELECT 1 to help make it more obvious that this isn't a query that's actually selecting data. At one point, the engine might have actually pulled data from whatever columns were specified, which would be were the SELECT 1 usage would have originally come from. Commented Jul 3, 2017 at 21:55
  • Updated to cover replacing new month values that are currently 0 with previous month values. Commented Jul 3, 2017 at 22:04