Idmpc
Programmer
- Jun 25, 2008
- 16
Hi everyone,
As usual my thread title might be confusing, so here's what I've been trying to do but with no success:
I have a table with the fields ERROR and DETAILS.
here's an example of the data inside the table:
What I need to do is a query that can count which of the errors (A, B or C) has appeared more times on the table and order them on a descending order. Then, it needs to count how many times each detail happened for each error, and order them on descending order too.
From the table above, we can see that Error "C" has 5 records, Error "A" has 4 records and Error "B" has 3 records. Then, for Error "C", we can see that Detail "D2" has 3 records, while "D1" and "D3" only have 1 record each, and so on for Errors "A" and "B".
I tried to use the following Query:
but I got the following error:
Cannot have aggregate function in ORDER BY clause (SUM(COUNT(APN)))
I also tried using:
but that way, the results would NOT be ordered together by ERROR count, they would just be ordered by DETAIL count, for example:
The results I need should be:
It is very important for the Error's to be ordered by their total count (C = 5, A = 4, B = 3) and then, for each Error, we order the "Detail" count in descending order.
Does anyone know how should I write the SQL query for this?
Thank you very much!
As usual my thread title might be confusing, so here's what I've been trying to do but with no success:
I have a table with the fields ERROR and DETAILS.
here's an example of the data inside the table:
Code:
ERROR DETAIL
A D1
A D1
B D2
C D1
B D2
B D3
C D2
A D3
A D3
C D3
C D2
C D2
What I need to do is a query that can count which of the errors (A, B or C) has appeared more times on the table and order them on a descending order. Then, it needs to count how many times each detail happened for each error, and order them on descending order too.
From the table above, we can see that Error "C" has 5 records, Error "A" has 4 records and Error "B" has 3 records. Then, for Error "C", we can see that Detail "D2" has 3 records, while "D1" and "D3" only have 1 record each, and so on for Errors "A" and "B".
I tried to use the following Query:
Code:
SELECT ERROR, DETAIL, COUNT (DETAIL) AS [COUNT]
FROM TABLE
GROUP BY ERROR, DETAIL
ORDER BY SUM (COUNT (ERROR)) DESC, COUNT(DETAIL) DESC
but I got the following error:
Cannot have aggregate function in ORDER BY clause (SUM(COUNT(APN)))
I also tried using:
Code:
ORDER BY COUNT (ERROR) DESC, COUNT (DETAIL) DESC
but that way, the results would NOT be ordered together by ERROR count, they would just be ordered by DETAIL count, for example:
Code:
ERROR DETAIL COUNT
C D2 3
B D2 2
A D3 2
A D1 2
C D3 1
C D1 1
B D3 1
The results I need should be:
Code:
ERROR DETAIL COUNT
C D2 3
C D3 1
C D1 1
A D3 2
A D1 2
B D2 2
B D3 1
It is very important for the Error's to be ordered by their total count (C = 5, A = 4, B = 3) and then, for each Error, we order the "Detail" count in descending order.
Does anyone know how should I write the SQL query for this?
Thank you very much!