April 14, 2015 at 10:30 pm
Comments posted to this topic are about the item Using LAG
Aunt Kathi Data Platform MVP
Author of Expert T-SQL Window Functions
Simple-Talk Editor
April 15, 2015 at 1:19 am
But note that supplying an offset only produces the expected results if you can be sure that there is exactly one row per month in the sales table. (Or, if you use the PARTITION BY clause, exactly one row per distinct set of values of the partition columns and per month).
LAG simply counts rows, it does not look at the actual values.
April 15, 2015 at 1:20 am
Hugo Kornelis (4/15/2015)
But note that supplying an offset only produces the expected results if you can be sure that there is exactly one row per month in the sales table. (Or, if you use the PARTITION BY clause, exactly one row per distinct set of values of the partition columns and per month).LAG simply counts rows, it does not look at the actual values.
+1
April 15, 2015 at 1:46 am
Carlo Romagnano (4/15/2015)
Hugo Kornelis (4/15/2015)
But note that supplying an offset only produces the expected results if you can be sure that there is exactly one row per month in the sales table. (Or, if you use the PARTITION BY clause, exactly one row per distinct set of values of the partition columns and per month).LAG simply counts rows, it does not look at the actual values.
+1
+1
---------------------------------------------------
"Thare are only 10 types of people in the world:
Those who understand binary, and those who don't."
April 15, 2015 at 2:43 am
This was removed by the editor as SPAM
April 15, 2015 at 5:35 am
Nice question. Of all the things we get when we upgrade to 2012 or 2014, LEAD and LAG are the ones I'm looking forward to the most. Yes, I know, it's 2015.
April 15, 2015 at 6:27 am
free_mascot (4/15/2015)
Carlo Romagnano (4/15/2015)
Hugo Kornelis (4/15/2015)
But note that supplying an offset only produces the expected results if you can be sure that there is exactly one row per month in the sales table. (Or, if you use the PARTITION BY clause, exactly one row per distinct set of values of the partition columns and per month).LAG simply counts rows, it does not look at the actual values.
+1
+1
Triple ditto; the only reason I picked the right answer was because the other three were definitely wrong, so I guessed that was the author's intent.
April 15, 2015 at 6:33 am
Thanks for the question. I didn't realize this even existed.
April 15, 2015 at 6:59 am
Xavon (4/15/2015)
free_mascot (4/15/2015)
Carlo Romagnano (4/15/2015)
Hugo Kornelis (4/15/2015)
But note that supplying an offset only produces the expected results if you can be sure that there is exactly one row per month in the sales table. (Or, if you use the PARTITION BY clause, exactly one row per distinct set of values of the partition columns and per month).LAG simply counts rows, it does not look at the actual values.
+1
+1
Triple ditto; the only reason I picked the right answer was because the other three were definitely wrong, so I guessed that was the author's intent.
+1
April 15, 2015 at 7:01 am
[Using XML in Computed Columns], hmmm, that`s somthin new. Thanx
This is 4 the Computed Column question, don`t know how it came here :hehe::cool:
Thanks & Best Regards,
Hany Helmy
SQL Server Database Consultant
April 15, 2015 at 7:18 am
Ed Wagner (4/15/2015)
Nice question. Of all the things we get when we upgrade to 2012 or 2014, LEAD and LAG are the ones I'm looking forward to the most. Yes, I know, it's 2015.
+1 - Our minimum support version is 2008.
And thanks Aunt Kathi for the question as I wasn't aware of the analytic functions.
April 15, 2015 at 7:37 am
Hugo Kornelis (4/15/2015)
But note that supplying an offset only produces the expected results if you can be sure that there is exactly one row per month in the sales table. (Or, if you use the PARTITION BY clause, exactly one row per distinct set of values of the partition columns and per month).LAG simply counts rows, it does not look at the actual values.
Not only that, but you need an ORDER BY clause. Otherwise, there is no guarantee that 20140101 is exactly 12 rows before 20150101
Gerald Britton, Pluralsight courses
April 15, 2015 at 7:38 am
Hugo Kornelis (4/15/2015)
But note that supplying an offset only produces the expected results if you can be sure that there is exactly one row per month in the sales table. (Or, if you use the PARTITION BY clause, exactly one row per distinct set of values of the partition columns and per month).LAG simply counts rows, it does not look at the actual values.
While we're at it, it also only works if you order by the months in ascending order and if the query that has a total per month is not missing any months.
April 15, 2015 at 8:45 am
Outer join to a calendar table to ensure that there are no missing months, order by the date, and it should work.
April 15, 2015 at 11:56 am
Luckily there was only one correct answer so I didn't have to play the "guess the author's intention" game. 😛 LEAD and LAG have been very helpful in simplifying some of our queries since we upgraded to 2012. Thanks for the question Kathi.
Viewing 15 posts - 1 through 15 (of 26 total)
You must be logged in to reply to this topic. Login to reply