Click here to Skip to main content
15,909,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I need help in writing a SQL query that gives the difference in values for two consecutive dates.

My table structure is as follows :
Symbol Name   Dates       Outstanding values
VAN Equity     12/3/2011   0.7995
VAN Equity     12/4/2011   0.7995
VAN Equity     12/5/2011   0.7000
VAN Equity     12/8/2011   0.7000


I want the output in the following form :
Symbol Name   Dates       Difference
VAN Equity     12/3/2011   0.7995
VAN Equity     12/4/2011   0
VAN Equity     12/5/2011   -0.0995
VAN Equity     12/8/2011   0

I came up with the below query.
SQL
WITH LHP AS
      (
      SELECT  *, ROW_NUMBER() OVER(ORDER BY Dates ) AS rn
      FROM    test_table as LI
      )
SELECT   mc.[Outstanding Values] - mp.[Outstanding Values],
        mc.[Outstanding Values] , mp.[Outstanding Values]
FROM    LHP mc
inner JOIN    LHP mp
ON    mc.rn = mp.rn - 1

The above query works fine if I have just one set of Symbol Names.
However, if I have my data in the below format :
Symbol Name       Dates            Outstanding Values
VAN Equity   2011-12-03 00:00:00.000   0.7995
VAN Equity   2011-12-04 00:00:00.000   0.7995
VAN Equity   2011-12-05 00:00:00.000   0.7
VAN Equity   2011-12-08 00:00:00.000   0.7
VAN Equity   2011-12-09 00:00:00.000   0.6
VIN Equity   2011-12-03 00:00:00.000   0.1
VIN Equity   2011-12-04 00:00:00.000   0.2
VIN Equity   2011-12-05 00:00:00.000   0.7
VIN Equity   2011-12-08 00:00:00.000   0.7
VIN Equity   2011-12-09 00:00:00.000   0.6
VAT Equity   2011-12-03 00:00:00.000   0.1
VAT Equity   2011-12-04 00:00:00.000   0.2
VAT Equity   2011-12-05 00:00:00.000   0.7
VAT Equity   2011-12-08 00:00:00.000   0.7
VAT Equity   2011-12-09 00:00:00.000   0.6

i.e. multiple sets of Symbol Names distributed across the same set of dates my query gives me the results as shown below : which is not as expected.
No      Outstanding   Outstanding
Name    Values        Values
0.6995   0.7995         0.1
-0.1     0.1            0.2
-0.5995  0.2            0.7995
0.0995   0.7995         0.7
0        0.7            0.7
0.1      0.7            0.6

Any help would be greatly appreciated.
Posted
Updated 25-Dec-11 6:31am
v3

[After your comment]

Change this line :
SQL
SELECT  *, ROW_NUMBER() OVER(ORDER BY Dates ) AS rn

To this :
SQL
SELECT  *, ROW_NUMBER() OVER(PARTITION BY [Symbol Name] ORDER BY [Symbol Name], Dates ) AS rn


It should solve the problem.
 
Share this answer
 
v2
Hi Amir,
Your solution does not work as the ranking takes place as shown below :

CSS
VAN Equity  2011-12-03 00:00:00.000 0.7995  1
VAN Equity  2011-12-04 00:00:00.000 0.7995  2
VAN Equity  2011-12-05 00:00:00.000 0.7 3
VAN Equity  2011-12-08 00:00:00.000 0.7 4
VAN Equity  2011-12-09 00:00:00.000 0.6 5
VIN Equity  2011-12-03 00:00:00.000 0.1 1
VIN Equity  2011-12-04 00:00:00.000 0.2 2




as a result of which, the condition
VB
mc.rn = mp.rn - 1

holds true for rows across the subs groups as well.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900