I've got two solutions for my problem, but not sure if there's a better way. I need to count up the number of dates there are for a CardId. But I only want to count one, so if there's more than one row for a CardId and a date, I only want one. Here's the sample and my two solutions:SET NOCOUNT ONDECLARE @Event TABLE(CardId int NOT NULL, TimeDate datetime NOT NULL)INSERT INTO @EventSELECT 475, '12-01-2004 05:00' UNION ALLSELECT 475, '12-01-2004 07:00' UNION ALLSELECT 475, '12-01-2004 08:00' UNION ALLSELECT 475, '12-02-2004 12:00' UNION ALLSELECT 475, '12-02-2004 15:00' UNION ALLSELECT 475, '12-02-2004 21:00' UNION ALLSELECT 475, '12-03-2004 05:00' UNION ALLSELECT 475, '12-04-2004 05:00' UNION ALLSELECT 475, '12-05-2004 05:15' UNION ALLSELECT 475, '12-06-2004 05:21' UNION ALLSELECT 476, '12-01-2004 05:00' UNION ALLSELECT 476, '12-06-2004 07:33' UNION ALLSELECT 477, '11-30-2004 18:22' SELECT CardId, COUNT(TimeDate) AS DateCountFROM ( SELECT CardId, CONVERT(varchar(10), TimeDate, 101) AS TimeDate FROM @Event GROUP BY CardId, CONVERT(varchar(10), TimeDate, 101)) tGROUP BY CardIdSELECT CardId, COUNT(TimeDate) AS DateCountFROM ( SELECT DISTINCT CardId, CONVERT(varchar(10), TimeDate, 101) AS TimeDate FROM @Event) tGROUP BY CardId
I don't like the GROUP BY solution as it's not using an aggregate function. I don't like the DISTINCT way either, not sure why though it just doesn't seem right. Is there a better solution for this? Here's the expected result set:CardId DateCount ----------- ----------- 475 6476 2477 1
Tara