12강. 집약

1. 여러개의 레코드를 하나로 집약

<CASE식과 GROUP BY 응용>

NonAggTbl:

id data_type data_1 data_2 data_3 data_4 data_5 data_6
Jim A 100 10 34 346 54
Jim B 45 2 167 77 90 157
Jim C 3 687 1355 324 457
Ken A 78 5 724 457 1
Ken B 123 12 178 346 85 235
Ken C 45 23 46 687 33
Beth A 75 0 190 25 356
Beth B 435 0 183 4 325
Beth C 96 128 0 0 12
SELECT id, MAX(CASE WHEN data_type='A' THEN data_1 ELSE NULL END) AS data_1,
           MAX(CASE WHEN data_type='A' THEN data_2 ELSE NULL END) AS data_2,
           MAX(CASE WHEN data_type='B' THEN data_3 ELSE NULL END) AS data_3,
           MAX(CASE WHEN data_type='B' THEN data_4 ELSE NULL END) AS data_4,
           MAX(CASE WHEN data_type='B' THEN data_5 ELSE NULL END) AS data_5,
           MAX(CASE WHEN data_type='C' THEN data_6 ELSE NULL END) AS data_6,
    FROM NonAggTbl
    GROUP BY id;
id data_1 data_2 data_3 data_4 data_5 data_6
Jim 100 10 167 77 90 457
Ken 78 5 178 346 85 33
Beth 78 0 183 4 12

<집약, 해시, 정렬>