Laboratory/MSSQL

Group By, Having, Rollup, Cube,Compute

theking 2008. 3. 11. 22:12

① Group By : 그룹으로 묶어 추출하고자 할때

    ┗select * from 테이블명 Group By 그룹을만들기준열

예) select * From test Group By color

 

② Having : Having 이후에 나오는 조건문에 해당하는것만 그룹으로 추출하고자 할때

    ┗select * from 테이블명 Group By 그룹만들기준열 Having 조건

예) select * From test Group By color Having qty>=20

 

③ Rollup : 형태별 합과 전체 합이 추가

    ┗select * from 테이블명 Group By 그룹만들기준열 With Rollup

예) Select type,color,sum(qty) From test Group By type With Rollup

 

④ Cube : Group By에 대한 모든 집계치를 보여줌

    ┗select * from 테이블명 Group By 그룹만들기준열 With Cube

예) Select type,color,sum(qty) From test Group By type With Cube

 

⑤ Compute : 전체 집계행을 만들 수 있음

    ┗select * From 테이블명 Where 조건식 compute[조건식]

예) select qty from test COMPUTE avg(qty)

 

⑥ Compute By: 집계행을 별도의 결과집합으로 표형

    ┗select * From 테이블명 Where 조건식 compute[조건식] By [그룹지을컬럼]

예) select qty from test COMPUTE avg(qty) By qty