Author |
Topic |
cvipin
Yak Posting Veteran
51 Posts |
Posted - 2015-03-05 : 20:27:50
|
Hi,I have following data with me. I want to write a sql to generated the output mentioned below:CREATE TABLE #CrDr (Sequence int, DBCR varchar(1), Amount decimal(18,2), RevisedSeq int) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (310,'C',200) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (315,'D',100) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (320,'D',100) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (325,'C',350) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (330,'D',350) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (335,'C',2797.04) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (340,'D',2797.04) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (345,'C',100) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (350,'C',50) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (355,'D',150) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (360,'C',264.56) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (365,'C',300) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (370,'D',490) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (375,'D',74.56) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (380,'C',50) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (385,'D',50) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (390,'C',278.59) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (395,'D',256.97) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (400,'D',17.23) INSERT INTO #CrDr (Sequence,DBCR,Amount) VALUES (405,'D',4.39)Expected output: (Generate ParentSeq #)Sequence DBCR Amount ParentSeq310 C 200 1315 D 100 1320 D 100 1325 C 350 2330 D 350 2335 C 2797.04 3340 D 2797.04 3345 C 100 4350 C 50 4355 D 150 4360 C 264.56 5365 C 300 5370 D 490 5375 D 74.56 5380 C 50 6385 D 50 6390 C 278.59 7395 D 256.97 7400 D 17.23 7405 D 4.39 7Can you please help with this query.ThanksVipin |
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2015-03-06 : 00:43:08
|
Can you explain the column ParentSeq ? what is the logic for it ?sabinWeb MCP |
|
|
cvipin
Yak Posting Veteran
51 Posts |
Posted - 2015-03-06 : 12:22:01
|
The ParentSeq # should group the Credit and Debit entries ordered by Sequence. The next ParentSeq should be next number when there is a change from D to C. Basically the Credit and Debit entris should balnce to each other when we order the data by Sequence. |
|
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2015-03-09 : 02:16:44
|
[code];WITH aCTEAS ( SELECT A.Sequence,ROW_NUMBER() OVER(ORDER BY A.Sequence) AS RevisedSeq FROM #CrDr AS A OUTER APPLY (SELECT TOP(1) B.DBCR FROM #CrDr AS B WHERE A.Sequence>B.Sequence ORDER BY B.Sequence DESC)B WHERE A.DBCR ='C' AND (B.DBCR ='D' OR B.DBCR IS NULL) )SELECT A.Sequence,A.DBCR,A.Amount , B.RevisedSeq FROM #CrDr AS A CROSS APPLY ( SELECT TOP(1) RevisedSeq FROM ACTE as b WHERE B.Sequence <= A.Sequence ORDER BY B.Sequence dESC )B [/code]I think will exits a better solution, but for the moment this is what I come upsabinWeb MCP |
|
|
cvipin
Yak Posting Veteran
51 Posts |
Posted - 2015-03-09 : 15:14:32
|
Thank you very much sabinWeb. This works perfect for now. Do post if you find other solution :) |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2015-03-10 : 01:38:20
|
[code]-- SwePesoSELECT [Sequence], DBCR, Amount, DENSE_RANK() OVER (PARTITION BY DBCR ORDER BY grp) AS grpFROM ( SELECT [Sequence], DBCR, Amount, ROW_NUMBER() OVER (ORDER BY [Sequence]) - DENSE_RANK() OVER (PARTITION BY DBCR ORDER BY [Sequence]) AS grp FROM #crdr ) AS dORDER BY [Sequence];[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
cvipin
Yak Posting Veteran
51 Posts |
Posted - 2015-03-10 : 15:39:09
|
Thanks SwePeso, that was very innovative. |
|
|
|
|
|