I have the following example dataset and as you can see the column X is the class ID, column Y is each class name, and finally column Z is the other universities class ID used for each subject. The goal is to get X, and Z with a grouping by X and get the most common ID used by Z (Other universities).
Sheet 1:
X | Y | Z |
---|---|---|
123 | 22 | 122 |
123 | 22 | 123 |
123 | 22 | 122 |
256 | 21 | 256 |
256 | 21 | 255 |
341 | 33 | 400 |
341 | 33 | 400 |
The outcome should be:
X | Z |
---|---|
123 | 122 |
256 | 255 |
341 | 400 |
I tried by adding the following query, but it is only returning the maximum from all the the table but not only for each value in column Z.
SELECT Sheet1.X, Sheet1.Z
FROM Sheet1
GROUP BY Sheet1.X, Sheet1.Z
HAVING COUNT(Sheet1.Z) =
(SELECT MAX(sheet2.count) FROM
(SELECT Sheet1.X, Sheet1.Z, COUNT(Sheet1.Z) AS count
FROM Sheet1
GROUP BY Sheet1.X, Sheet1.Z) as sheet2);
Any suggestion of what I am doing wrong?