Getting Started with Adobe After Effects - Part 6: Motion Blur
Ask
Ask questions, discuss or help others by answering
Related Posts · View All
SQL Server 142
TSQL 76
SSRS 70
SSIS 66
XML 54

Top Categories · View All
SQL Server 142
TSQL 76
SSRS 70
SSIS 66
XML 54

Find sequencial total of group

May 18 2011 12:00AM by Parth Patel   

Hi all,

I need urgent solution of the following query.

Input

Id, Vacation, Grp

1, N, 1

2, Y, 1

3, N, 1

4, Y, 1

5, N, 1

6, Y, 2

7, N, 2

8, N, 3

Output

Id, Vacation, Grp, TotalVacationCurrentBeforeInGrp

1, N, 1, 0

2, Y, 1, 1

3, N, 1, 1

4, Y, 1, 2

5, N, 1, 2

6, Y, 2, 1

7, N, 2, 1

8, N, 3, 0

Submitted under: Microsoft SQL Server · TSQL ·  ·  · 


Parth Patel
103 · 2% · 510

3 Replies

  • Hi parth, try this query select ,(select count() from TableName where Grp = t.Grp and Vacation = 'Y' and ID <= t.ID) from TableName t

    commented on May 18 2011 11:40AM
    Ramireddy
    2 · 41% · 12972
  • Hi Rami,

    Thanks for your reply. But I am already doing so. But it requires inline query executed each time. Is there any other way to do so?

    commented on May 19 2011 2:06AM
    Parth Patel
    103 · 2% · 510
  • Oh.. How stupid I was. It was so easy..

    ;WITH tmp (Id, Vacation, Grp) AS
    (
        SELECT 1, 'N', 1
        UNION ALL
        SELECT 2, 'Y', 1
        UNION ALL
        SELECT 3, 'N', 1
        UNION ALL
        SELECT 4, 'Y', 1
        UNION ALL
        SELECT 5, 'N', 1
        UNION ALL
        SELECT 6, 'Y', 2
        UNION ALL
        SELECT 7, 'N', 2
        UNION ALL
        SELECT 8, 'N', 3
    ),
    tmp1 AS
    (
        SELECT Id, Vacation, Grp,
        VacationOrder = ROW_NUMBER() OVER (PARTITION BY Vacation ORDER BY Id),
        RowNo = ROW_NUMBER() OVER (ORDER BY Id)
        FROM tmp
    )
    SELECT Id, Vacation, Grp, 
    TotalVacationTillDate = CASE WHEN Vacation = 'Y' THEN VacationOrder ELSE RowNo - VacationOrder END
    FROM tmp1 ORDER BY Id;
    
    commented on May 19 2011 2:23AM
    Parth Patel
    103 · 2% · 510

Your Reply


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]