Customer
및 AlertName
을 그룹화하여 각 고객에게 몇 개의 경고가 발생했는지 파악한 다음 결과를 No_Alerts
으로 주문해야합니다.SQL Server 2008 - 그룹화 된 데이터 주문
SELECT Customer, AlertName, COUNT(AlertName) as No_Alerts
FROM Alerts
GROUP BY Customer, AlertName
ORDER BY Customer, No_Alerts DESC
결과는 다음과 같습니다 : 데이터가 위의 No_Alerts
으로 정렬되어
Customer AlertName No_Alerts
----------------------------------
1 Cust1 Alert1 12
2 Cust1 Alert7 5
3 Cust1 Alert5 3
4 Cust2 Alert8 32
5 Cust2 Alert4 17
6 Cust2 Alert2 2
7 Cust3 Alert3 234
8 Cust3 Alert4 22
9 Cust3 Alert6 7
하지만 어떻게 그 결과 다음과 같은 얻을 수있는, 그래서 나는이 SQL 쿼리를 사용하고 있습니다?
Customer AlertName No_Alerts
----------------------------------
1 Cust3 Alert3 234
2 Cust3 Alert4 22
3 Cust3 Alert6 7
4 Cust2 Alert8 32
5 Cust2 Alert4 17
6 Cust2 Alert2 2
7 Cust1 Alert1 12
8 Cust1 Alert7 5
9 Cust1 Alert5 3
미리 감사드립니다.
고마워요 - MAX 기능이 내가 찾고있는 기능입니다! 정렬되었습니다! 잘 했어! –