SELECT TOP 3 *
FROM SalesOrder
ORDER BY OrderDate desc;
SELECT TOP은 WHERE 및 ORDER BY 까지 적용된 최종 결과에 대하여 상위 N개 (혹은 N퍼센트) 선택
SELECT TOP 3 WITH TIES *
FROM SalesOrder
ORDER BY OrderDate desc;
TOP 3개를 로출했으나 3번째 & 4번째 같은 경우 같은 날짜가 있어서 TOP 4가 됨
/* 주문금액 기준 상위 5개 주문 ID, 주문금액*/
SELECT TOP 5
SalesOrderID, SUM(SubTotal) AS Order_Total
FROM SalesOrderDetail
GROUP BY SalesOrderID
ORDER BY Order_Total DESC;
/* 총 주문금액 상위 10% 고객 ID, 금액*/
SELECT TOP 10 PERCENT
so.CustomerID,
SUM(sod.SubTotal) AS total
FROM SalesOrder so
INNER JOIN SalesOrderDetail sod
ON (so.SalesOrderID = sod.SalesOrderID)
GROUP BY so.CustomerID
ORDER BY total DESC