sql server 2008 - How to select all records after a certain record within groups in sql? -


given following table structure

col1, col2, eventtype,  datetime 

how can select records per grouping of col1, col2 occur after top record eventtype = 3 particular group of col1, col2.

for example following data

col1, col2, eventtype, datetime       b       1      2012-1-1       b       3      2011-1-1       b       1      2010-1-1 c       d       1      2012-1-1 c       d       2      2011-1-1 c       d       2      2010-1-1 c       d       3      2009-1-1 c       d       2      2008-1-1 c       d       3      2007-1-1 c       d       1      2006-1-1 c       d       2      2005-1-1 

i want select

col1, col2, eventtype, datetime       b       1      2012-1-1  c       d       1      2012-1-1 c       d       2      2011-1-1 c       d       2      2010-1-1 

you can use max function on subquery:

select col1, col2, eventtype, datetime  thetable datetime >   (select  max(datetime)    thetable sub   eventtype = 3 , sub.col1 = a.col1 , sub.col2 = a.col2) 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -