sql server - SQL Query - Query on Current Date but condition from the past -


i looking following scenario:

i have sql server db, table stores historical data. example lets use following sample set.

car, service date, findings 1234, 21/01/2001, fuel filter 1234, 23/09/2009, oil change 1234, 30/09/2015, tyres 3456, 30/09/2015, clutch 

i following sample bring result shows service of car brought in on give date, e.g. 30/09/2015 if had oil change in past.

the query bring back:

1234, 30/09/2015, tyres 

since car on date services had oil change.

any appreciated.

use exists clause:

select cur.car,     cur.[service date],     cur.findings tablename cur cur.[service date] = @mydate     , exists (         select 1         tablename past         past.car = cur.car            , past.[service date] < cur.[service date]            , past.findings = 'oil change'     ) 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -