Count Delta

  • I have attached an excel spreadsheet with sample data and results I am looking for. I'm trying to create a column with a running difference in counts. So for each event_id I want the difference in counts of member_id that have event = open. Any help greatly appreciated.

  • If I understand your requirement correctly, you would need something like this:

    ;With ID_TotalCount(Int_ID, Int_IDTotalCount)

    AS

    (

    SELECT Int_ID, COUNT(Int_ID)

    FROM dbo.tblData

    GROUP BY Int_ID

    ),

    ID_OpenCount(Int_ID, Int_IDOpenCount)

    AS

    (

    SELECT Int_ID, COUNT(Int_ID)

    FROM dbo.tblData

    WHERE Event = 'Open'

    GROUP BY Int_ID

    )

    SELECT DISTINCT td.Int_ID

    ,ISNULL(Int_IDOpenCount, 0) AS Opened

    ,ISNULL(Int_IDTotalCount - Int_IDOpenCount, -1) AS Diff

    FROM ID_TotalCount tc

    JOIN ID_OpenCount oc

    ON tc.Int_ID = oc.Int_ID

    RIGHT JOIN dbo.tblData td

    ON td.Int_ID = oc.Int_ID

    Btw - not sure if your columns are actually named Event, Date etc. but if they are, you may want to revisit your naming convention and eliminate key/reserved words usage.







    **ASCII stupid question, get a stupid ANSI !!!**

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply