소개
게시글
고민있어요
2023.01.03 17:42
[추가 문제] 제품별 총 판매액을 보고 싶다면? (+내림차순)
- 0
- 2
- 255
질문&답변
2023.01.03
[서브쿼리] 예제 문제 Shipper_ID 총합 칼럼 추가 방법 문의
스스로 정답을 찾아서 내용 공유합니다! WITH(임시 테이블)라는 것을 사용하여 해결할 수 있었네요.With Temp_table as ( SELECT C.CustomerID, IFNULL(O1.ShipperID_1, 0) AS S1, IFNULL(O2.ShipperID_2, 0) AS S2, IFNULL(O3.ShipperID_3, 0) AS S3 FROM Customers AS C LEFT JOIN ( SELECT CustomerID, Count(OrderID) AS ShipperID_1 FROM Orders WHERE ShipperID = 1 Group By CustomerID ) AS O1 ON O1.CustomerID = C.CustomerID LEFT JOIN ( SELECT CustomerID, Count(OrderID) AS ShipperID_2 FROM Orders WHERE ShipperID = 2 Group By CustomerID ) AS O2 ON O2.CustomerID = C.CustomerID LEFT JOIN ( SELECT CustomerID, Count(OrderID) AS ShipperID_3 FROM Orders WHERE ShipperID = 3 Group By CustomerID ) AS O3 ON O3.CustomerID = C.CustomerID ) Select CustomerID, S1, S2, S3, S1+S2+S3 AS SUM From Temp_table
- 1
- 1
- 276