GROUP BY 완전정복

Kyle_Kim·2024년 4월 4일
0
post-thumbnail

기본적인 GROUP BY절

select
    pcb.id
    ,count(pcb.total_component_count) as count
from ks_inspection_result.t_result_aoi_pcb pcb
group by pcb.id

GROUP BY 절의 칼럼은 SELECT 절에 존재해야 사용할 수 있다.

**SELECT 절에서 집계 함수를 제외한 칼럼을 GROUP BY 절에 기술한다고 생각하면 된다.

Group을 여러개 지정하는 경우.

select
    pcb.id
     ,wfmi.array_value
    ,count(wfmi.pcb_id) as count
from ks_inspection_result.t_result_aoi_pcb pcb
left join ks_inspection_result.t_aoi_whole_wfmi_result wfmi
    on pcb.id = wfmi.pcb_id
group by pcb.id, wfmi.array_value
having count(wfmi.pcb_id) > 0 and wfmi.array_value > 0

pcb.id로 먼저 묶이고, wfmi.array_value로 한번 더 묶인다.

Using Order By

select
    pcb.id
     ,wfmi.array_value as arrayNum
    ,count(wfmi.pcb_id) as count
from ks_inspection_result.t_result_aoi_pcb pcb
left join ks_inspection_result.t_aoi_whole_wfmi_result wfmi
    on pcb.id = wfmi.pcb_id
group by pcb.id, wfmi.array_value
having count(wfmi.pcb_id) > 0 and wfmi.array_value > 0
order by count desc

order By절에서 집계함수절을 사용할수 있다.

profile
Make Things Right

0개의 댓글