Link to home
Start Free TrialLog in
Avatar of qinyan
qinyan

asked on

T-SQL: running total of unique ids by date?

i am trying to find the cumulative # of ids by date:
e.g. id "1" and "2" were on day 1, id "1" "3" on day 2, "4" on day 3
i want to return 2 unique ids on day 1, 3 unique ids on day 2 and 4 unique ids on day 3.

What's the best way to do that?
Thanks!
Avatar of Emes
Emes
Flag of United States of America image

select count(ids),Date
from table
group by date

Avatar of qinyan
qinyan

ASKER

that's not cumulative.
Here ya go, let me know if this works for ya.  What it does is count the number of id's for each date.  You will want to change the field names to those that your table uses.
SELECT
    date,
    COUNT(*) AS num_ids
FROM
    table_name
GROUP BY
    date

Open in new window

you will need to put the ids that you want into a list.

declare @IDs varchar(5000)
select @IDs = COALESCE(@IDs + ',', '') + cast (id as varchar(5))
from info1  
Where date = '1/2/2008'
group by id
select @IDs as 'IDs'

Avatar of qinyan

ASKER

NEVER MIND, I figured it out. thx
Avatar of qinyan

ASKER

this is also not cumulative:
SELECT
    date,
    COUNT(*) AS num_ids
FROM
    table_name
GROUP BY
    date

---------------------------------
i don't want to put them in list, i want to make them rowsets.
ASKER CERTIFIED SOLUTION
Avatar of qinyan
qinyan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial