1.

Solve : SQL: Counting duplicates within combinations of groups?

Answer»

Hi All,

I have a database table which contains approx 20,000 records in the following format:
Code: [Select]DISK Track
=====================
CD1 Unique to CD1
CD1 On all three
CD1 On CD1 and CD2
CD1 On CD1 and CD3

CD2 Unique to CD2
CD2 On all three
CD2 On CD1 and CD2
CD2 On CD2 and CD3
CD2 Another on CD2 and CD3

CD3 Unique to CD3
CD3 On all three
CD3 On CD1 and CD3
CD3 On CD2 and CD3
CD3 Another on CD2 and CD3

Is it POSSIBLE to create an SQL statement to list all combinations of two disks, for example:
Code: [Select]DiskA DiskB
==============
CD1 CD2
CD1 CD3
CD2 CD3

Is it possible to create an SQL statement to list "counts of duplicates" WITHIN (or between) each combination of two disks, for example:
Code: [Select]DiskA DiskB CountOfDuplicatedTracks
=====================================
CD1 CD2 2
CD1 CD3 2
CD2 CD3 3

Thank you for your help,
Best REGARDS,
JAMES

P.S. This is a 'real question', with a real table, and not just a 'homework question'Hi All,

I have managed to solve this problem, (or atleast part 2 of the problem which is what I was really interested in).

Just in case anyone else is interested, my solution uses two SQL statements...

Make_B:
SELECT CD AS CD_B, Track AS Track_B FROM MyTable;

Joiner:
SELECT CD, CD_B, COUNT(Track) AS CountOfDuplicates
FROM MyTable INNER JOIN Make_B ON MyTable.Track=Make_B.Track_B
GROUP BY CD, CD_B HAVING CD_B>CD;

Regards,
James



Discussion

No Comment Found