반응형
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | select WorkGrName , substring(YearMonth ,1,4) Years , SUM(CASE WHEN right(YearMonth,2) ='01' THEN ISNULL(SumAmt,0) else 0 end) 'Amt01' , SUM(CASE WHEN right(YearMonth,2) ='01' THEN ISNULL(SumQty,0) else 0 end) 'Qty01' , SUM(CASE WHEN right(YearMonth,2) ='02' THEN ISNULL(SumAmt,0) else 0 end) 'Amt02' , SUM(CASE WHEN right(YearMonth,2) ='02' THEN ISNULL(SumQty,0) else 0 end) 'Qty02' , SUM(CASE WHEN right(YearMonth,2) ='03' THEN ISNULL(SumAmt,0) else 0 end) 'Amt03' , SUM(CASE WHEN right(YearMonth,2) ='03' THEN ISNULL(SumQty,0) else 0 end) 'Qty03' , SUM(CASE WHEN right(YearMonth,2) ='04' THEN ISNULL(SumAmt,0) else 0 end) 'Amt04' , SUM(CASE WHEN right(YearMonth,2) ='04' THEN ISNULL(SumQty,0) else 0 end) 'Qty04' , SUM(CASE WHEN right(YearMonth,2) ='05' THEN ISNULL(SumAmt,0) else 0 end) 'Amt05' , SUM(CASE WHEN right(YearMonth,2) ='05' THEN ISNULL(SumQty,0) else 0 end) 'Qty05' , SUM(CASE WHEN right(YearMonth,2) ='06' THEN ISNULL(SumAmt,0) else 0 end) 'Amt06' , SUM(CASE WHEN right(YearMonth,2) ='06' THEN ISNULL(SumQty,0) else 0 end) 'Qty06' , SUM(CASE WHEN right(YearMonth,2) ='07' THEN ISNULL(SumAmt,0) else 0 end) 'Amt07' , SUM(CASE WHEN right(YearMonth,2) ='07' THEN ISNULL(SumQty,0) else 0 end) 'Qty07' , SUM(CASE WHEN right(YearMonth,2) ='08' THEN ISNULL(SumAmt,0) else 0 end) 'Amt08' , SUM(CASE WHEN right(YearMonth,2) ='08' THEN ISNULL(SumQty,0) else 0 end) 'Qty08' , SUM(CASE WHEN right(YearMonth,2) ='09' THEN ISNULL(SumAmt,0) else 0 end) 'Amt09' , SUM(CASE WHEN right(YearMonth,2) ='09' THEN ISNULL(SumQty,0) else 0 end) 'Qty09' , SUM(CASE WHEN right(YearMonth,2) ='10' THEN ISNULL(SumAmt,0) else 0 end) 'Amt10' , SUM(CASE WHEN right(YearMonth,2) ='10' THEN ISNULL(SumQty,0) else 0 end) 'Qty10' , SUM(CASE WHEN right(YearMonth,2) ='11' THEN ISNULL(SumAmt,0) else 0 end) 'Amt11' , SUM(CASE WHEN right(YearMonth,2) ='11' THEN ISNULL(SumQty,0) else 0 end) 'Qty11' , SUM(CASE WHEN right(YearMonth,2) ='12' THEN ISNULL(SumAmt,0) else 0 end) 'Amt12' , SUM(CASE WHEN right(YearMonth,2) ='12' THEN ISNULL(SumQty,0) else 0 end) 'Qty12' FROM temp2 GROUP BY substring(YearMonth ,1,4) , WorkGrName , WorkGrCode order by WorkGrCode | cs |
GROUP BY 절과 SUM 함수를 이용하여 웝별 데이터를 가로로 출력해 보여주는 방법!
주로 통계 쪽에서 많이 사용할 것 같다.
반응형
'프로그램 개발 > DB(MSSQL)' 카테고리의 다른 글
서로다른 DB간 테이블 복사 ( SQL Management Studio ) (0) | 2017.10.25 |
---|---|
MSSQL 날짜 데이터 형식 ( Getdate() ) 자유롭게 변환 (0) | 2017.10.12 |
프로시저로 RecordSet 넘기기 (0) | 2017.08.22 |
MSSQL IF문 WHERE 절에서 사용하기 (0) | 2017.07.27 |
동적 쿼리 예제 (0) | 2017.07.27 |