Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Question about SQL Query
Message
From
28/06/2007 08:16:01
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, United States
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2000
Miscellaneous
Thread ID:
01235766
Message ID:
01236235
Views:
11
>>I have a table like so:
>>
>>
>>ID |Date      |Rate
>>1  |1/1/2007  |1
>>2  |1/2/2007  |1
>>3  |1/3/2007  |1
>>4  |1/4/2007  |1.25
>>5  |1/5/2007  |1.25
>>6  |1/6/2007  |1.5
>>7  |1/7/2007  |1
>>8  |1/8/2007  |1
>>9  |1/9/2007  |1
>>10 |1/10/2007 |1.25
>>11 |1/11/2007 |1.25
>>12 |1/12/2007 |1.25
>>13 |1/13/2007 |1.25
>>14 |1/14/2007 |1
>>15 |1/15/2007 |1
>>16 |1/16/2007 |1
>>
>>
>>I want a query that shows only when the rate changes... this would be my intended result:
>>
>>ID |Date      |Rate
>>1  |1/1/2007  |1
>>4  |1/4/2007  |1.25
>>6  |1/6/2007  |1.5
>>7  |1/7/2007  |1
>>10 |1/10/2007 |1.25
>>14 |1/14/2007 |1
>>
>>
>>Would the best way be to include an inline query to query the current id - 1? Or is there some magic I can do to make it easier?
>
>Mike,
>I asked about this in other forum and here it is the query:
>
>DECLARE @Temp TABLE (Id int, Date datetime, Rate float)
>INSERT INTO @Temp VALUES (1,'1/1/2007',1)
>INSERT INTO @Temp VALUES (19,'1/2/2007',1)
>INSERT INTO @Temp VALUES (3,'1/3/2007',1)
>INSERT INTO @Temp VALUES (4,'1/4/2007',1.25)
>INSERT INTO @Temp VALUES (5,'1/5/2007',1.25)
>INSERT INTO @Temp VALUES (6,'1/6/2007',1.5)
>INSERT INTO @Temp VALUES (7,'1/7/2007',1)
>INSERT INTO @Temp VALUES (8,'1/8/2007',1)
>INSERT INTO @Temp VALUES (9,'1/9/2007',1)
>INSERT INTO @Temp VALUES (10,'1/10/2007',1.25)
>INSERT INTO @Temp VALUES (11,'1/11/2007',1.25)
>INSERT INTO @Temp VALUES (12,'1/12/2007',1.25)
>INSERT INTO @Temp VALUES (13,'1/13/2007',1.25)
>INSERT INTO @Temp VALUES (14,'1/14/2007',1)
>INSERT INTO @Temp VALUES (15,'1/15/2007',1)
>INSERT INTO @Temp VALUES (16,'1/16/2007',1)
>
>SELECT Tmp.*
>       FROM  @Temp Tmp
>LEFT JOIN @Temp t on Tmp.Date = t.Date + 1
>          and Tmp.Rate = t.Rate
>where t.ID is null
>
>Of course this query will work only if Dates are in sequence.
>Thanks to AlexCuse.

There won't necessarily be a record for every single day, but they will be in sequence. Thanks for checking!
Very fitting: http://xkcd.com/386/
Previous
Reply
Map
View

Click here to load this message in the networking platform