Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


All the rows before this one


All the rows before this one

Author
Message
Hany Helmy
Hany Helmy
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2514 Visits: 1108
sestell1 (12/20/2013)
LAG works for a fixed number of rows before the current row.
For all the rows prior to the current row, wouldn't you use something like ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW in the OVER clause?

https://www.simple-talk.com/sql/learn-sql-server/window-functions-in-sql-server-part-2-the-frame/

http://sqlmag.com/sql-server-2012/how-use-microsoft-sql-server-2012s-window-functions-part-1

https://www.simple-talk.com/sql/t-sql-programming/sql-server-2012-window-function-basics/

+1 True.
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8319 Visits: 11548
So how exactly does one "aggregate (...) those rows that had been processed BEFORE the current row" with LAG?
(I emphasized two important words in that quote from the question - rows is plural and aggregate suggests aggregating multiple rows into one result).

I picked PREVIOUS, thinking that the author had misspelled PRECEDING, which would have been the only correct answer to this question.


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Revenant
Revenant
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5771 Visits: 4706
Hugo Kornelis (12/21/2013)
So how exactly does one "aggregate (...) those rows that had been processed BEFORE the current row" with LAG? . . .

http://technet.microsoft.com/en-us/library/hh231256.aspx
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8319 Visits: 11548
Revenant (12/21/2013)
Hugo Kornelis (12/21/2013)
So how exactly does one "aggregate (...) those rows that had been processed BEFORE the current row" with LAG? . . .

http://technet.microsoft.com/en-us/library/hh231256.aspx

Not sure why you post that link. I know what LAG is, and what it does.
It does not answer the question on how to aggregate rows (watch the plural) with a function that is designed to return the value from one of the preceding rows.


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Revenant
Revenant
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5771 Visits: 4706
Hugo Kornelis (12/21/2013)
Revenant (12/21/2013)
Hugo Kornelis (12/21/2013)
So how exactly does one "aggregate (...) those rows that had been processed BEFORE the current row" with LAG? . . .

http://technet.microsoft.com/en-us/library/hh231256.aspx

Not sure why you post that link. I know what LAG is, and what it does.
It does not answer the question on how to aggregate rows (watch the plural) with a function that is designed to return the value from one of the preceding rows.

Gotcha now. I will try to come up with an example of my own but it will have to wait until tomorrow - my pre-Christmas Saturday is rather busy.
matt.akers.1
matt.akers.1
SSC Journeyman
SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)

Group: General Forum Members
Points: 93 Visits: 31
Not crazy about this question as it is misleading. LEAD / LAG are not generally used to retrieve "all rows before this one" without doing some additional work to the query.

Really the purpose of LEAD / LAG is to provide an easy way to access 1 forward / 1 back of the current resultset without the need for a self join.

Lead: http://msdn.microsoft.com/en-us/library/hh213125.aspx
Lag: http://msdn.microsoft.com/en-us/library/hh231256.aspx

They don't really make any mention of a full result set being returned as a product of this analytic function alone.
pchirags
pchirags
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2166 Visits: 613
Nice and easy one..
Thanks Steve.
TomThomson
TomThomson
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10700 Visits: 11991
Steve Jones - SSC Editor (12/20/2013)
You would use unbounded preceding for all the rows. The title did say all the rows, but the question did not.

Steve, that's special pleading. LAG references a single row. the question says "those rows ...", not "a row ...". It also talks about an aggregate, and it's a bit odd to think of an aggregate applying to a single row. Can you give a sensible example of how LAG can be used to cause an aggregate to apply to rows (as opposed to a row)?

Tom

steve.jacobs
steve.jacobs
SSChasing Mays
SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)SSChasing Mays (622 reputation)

Group: General Forum Members
Points: 622 Visits: 279
Good question. Thanks. I see some good conversation stemming from this question. ;-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search