Question concerning counting unique combination of fields across two tables in SQL. MSaccess 2003.
table1: id, fld1, fld2
table2: id, dateAndTime
id is not unique; tables are joined across "id".
I need a count, by date (dd/mm/yy - ignoring time) of distinct combinations of fld1
and fld2
(int).
More specifically, I need to know which date has the most distinct combinations of fld1
and fld2
.
If table1 has
1, 101, 101 2, 101, 101 3, 101, 101 4, 101, 102 5, 101, 102 6, 101, 103
And table 2 has
1, 12/1/2010 2, 12/1/2010 3, 12/1/2010 1, 12/2/2010 2, 12/2/2010 4, 12/2/2010 5, 12/2/2010 6, 12/2/2010
I need
12/1/2010, 1 'only 1 unique combinatin of fld1 and fld2
12/2/2010, 3 'only 3 unique combinations of fld1 and fld2
But I only need "12/2/2010, 3" output as I just need the date and count of the largest count.
Couldn't figure out how to format this in the Commment to the correct answer below - so here it is for MS Access 2003.
Select TOP 5 theDay, count(*) AS theCount
FROM (
Select cdate(int(date_col)) As theDay
From tbl1 Inner Join tbl2 on tbl1.id=tbl2.id
Group By cdate(int(date_col)), fld1, fld2
) As X
Group By theDay
Order By 2 Desc;
This returns the TOP 5 combinations by Date (disregarding any time value)