Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Getting column change history...

$
0
0
Hi,I've a table that stores the history from another table, all the records updates made.I need to write a query to get just the price changes..If I write the following query:[code="sql"]SELECT historyDate, price FROM history.vehicles WHERE idVehicle = 123 ORDER BY historyDate[/code]I get the following result...[code="plain"]historyDate price2015-12-02 00:00:00.000 57502015-12-06 00:00:00.000 57502015-12-07 00:00:00.000 57502015-12-08 00:00:00.000 57502015-12-10 00:00:00.000 57002015-12-13 00:00:00.000 56502015-12-15 00:00:00.000 49902015-12-17 00:00:00.000 50002015-12-19 00:00:00.000 50002015-12-20 00:00:00.000 50002015-12-27 00:00:00.000 49902016-01-02 00:00:00.000 49902016-01-09 03:36:47.000 4990[/code]To get just the price changes I tried:[code="sql"]WITH data (SELECT historyDate, price, row = ROW_NUMBER() OVER (PARTITION BY price ORDER BY historyDate) FROM history.vehicles WHERE idVehicle = 123)SELECT * FROM data WHERE row = 1 ORDER BY historyDate[/code]But I get:[code="plain"]historyDate price row2015-12-02 00:00:00.000 5750 12015-12-10 00:00:00.000 5700 12015-12-13 00:00:00.000 5650 12015-12-15 00:00:00.000 4990 12015-12-17 00:00:00.000 5000 1[/code]It's what I wrote but the price 4990 occurred again at 2015-12-27 and I'd want it to show the record: 2015-12-27 4990 to show...I need to compare the next row price with the current row and if it's the same ignore.... How can I do that??!Thanks,Pedro

Viewing all articles
Browse latest Browse all 3145

Trending Articles