Hello
I have data set with below formatting and i am trying create query to get top two of each group(group by div and sub div)
Suppose I have this data:
DIVISION |
SUBDIVISION |
TRADE |
SumOfTotProblems |
IND |
INDA |
P1 |
20 |
IND |
INDA |
P3 |
12 |
IND |
INDB |
P1 |
15 |
IND |
INDA |
P2 |
25 |
IND |
INDB |
P3 |
15 |
IND |
INDB |
P2 |
30 |
AUS |
AUSA |
P1 |
23 |
AUS |
AUSA |
P3 |
37 |
AUS |
AUSA |
P2 |
27 |
Then result should be:
DIVISION |
SUBDIVISION |
TRADE |
SumOfTotProblems |
IND |
INDA |
P2 |
25 |
IND |
INDA |
P1 |
20 |
IND |
INDB |
P2 |
30 |
IND |
INDB |
P1 |
15 |
AUS |
AUSA |
P3 |
37 |
AUS |
AUSA |
P2 |
27 |
I tried this query....but its not working and throwing error:
Code:
Error:
this expression is typed incorrectly or it is too complex to be evaluated
Code:
Query:
SELECT Division, Subdivision , Trade, SumOfTotProblems
FROM Helpertop5
WHERE
SumOfTotProblems IN
(SELECT TOP 3 SumOfTotProblems
FROM Helpertop5 HT WHERE Helpertop5.Division = HT.Division AND Helpertop5.Subdivision = HT.Subdivision
ORDER BY Helpertop5.SumOfTotProblems DESC)
ORDER BY Helpertop5.SumOfTotProblems DESC;
Please help me to figure out how I can do this.
Thanks