Click here to Skip to main content
16,015,809 members
Home / Discussions / Database
   

Database

 
QuestionMicrosoft SQL Server 2008 R2 (between) Pin
Framework .l.27-Dec-11 16:59
Framework .l.27-Dec-11 16:59 
AnswerRe: Microsoft SQL Server 2008 R2 (between) Pin
R. Giskard Reventlov27-Dec-11 17:17
R. Giskard Reventlov27-Dec-11 17:17 
QuestionRe: Microsoft SQL Server 2008 R2 (between) Pin
Framework .l.27-Dec-11 17:21
Framework .l.27-Dec-11 17:21 
AnswerRe: Microsoft SQL Server 2008 R2 (between) Pin
thatraja27-Dec-11 17:46
professionalthatraja27-Dec-11 17:46 
AnswerRe: Microsoft SQL Server 2008 R2 (between) Pin
Shameel27-Dec-11 17:59
professionalShameel27-Dec-11 17:59 
QuestionFailed to open malformed assembly 'mscorlib' with HRESULT 0x80070008. Pin
yousefshokati26-Dec-11 19:41
yousefshokati26-Dec-11 19:41 
AnswerRe: Failed to open malformed assembly 'mscorlib' with HRESULT 0x80070008. Pin
Luc Pattyn26-Dec-11 23:52
sitebuilderLuc Pattyn26-Dec-11 23:52 
QuestionHelp in finding the differences in values across sub groups. Pin
aakar25-Dec-11 9:17
aakar25-Dec-11 9:17 
CSS
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 :

 Collapse | Copy Code
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 :

 Collapse | Copy Code
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.

 Collapse | Copy Code
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 :

 Collapse | Copy Code
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.

 Collapse | Copy Code
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.

AnswerRe: Help in finding the differences in values across sub groups. Pin
SilimSayo29-Dec-11 10:36
SilimSayo29-Dec-11 10:36 
AnswerRe: Help in finding the differences in values across sub groups. Pin
datakeyword8-Feb-12 15:57
datakeyword8-Feb-12 15:57 
AnswerRe: Help in finding the differences in values across sub groups. Pin
datakeyword8-Feb-12 18:57
datakeyword8-Feb-12 18:57 
QuestionUsing where condition in Linked server with ms access in sql server. Pin
yesu prakash23-Dec-11 19:55
yesu prakash23-Dec-11 19:55 
AnswerRe: Using where condition in Linked server with ms access in sql server. Pin
coded00725-Dec-11 19:23
professionalcoded00725-Dec-11 19:23 
AnswerRe: Using where condition in Linked server with ms access in sql server. Pin
Shameel25-Dec-11 22:43
professionalShameel25-Dec-11 22:43 
QuestionChecking database has a table. Pin
yuvarajujogi22-Dec-11 19:50
yuvarajujogi22-Dec-11 19:50 
AnswerRe: Checking database has a table. Pin
Shameel22-Dec-11 23:40
professionalShameel22-Dec-11 23:40 
AnswerRe: Checking database has a table. Pin
Eddy Vluggen23-Dec-11 1:16
professionalEddy Vluggen23-Dec-11 1:16 
AnswerRe: Checking database has a table. Pin
RDBurmon23-Dec-11 2:42
RDBurmon23-Dec-11 2:42 
AnswerRe: Checking database has a table. Pin
PIEBALDconsult23-Dec-11 2:49
mvePIEBALDconsult23-Dec-11 2:49 
AnswerRe: Checking database has a table. Pin
jschell23-Dec-11 10:07
jschell23-Dec-11 10:07 
GeneralRe: Checking database has a table. Pin
yuvarajujogi27-Dec-11 23:43
yuvarajujogi27-Dec-11 23:43 
GeneralRe: Checking database has a table. Pin
Karthik Harve28-Dec-11 22:02
professionalKarthik Harve28-Dec-11 22:02 
AnswerRe: Checking database has a table. Pin
Mohibur Rashid26-Dec-11 22:44
professionalMohibur Rashid26-Dec-11 22:44 
GeneralRe: Checking database has a table. Pin
yuvarajujogi27-Dec-11 19:33
yuvarajujogi27-Dec-11 19:33 
GeneralRe: Checking database has a table. Pin
Mohibur Rashid27-Dec-11 20:38
professionalMohibur Rashid27-Dec-11 20:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.