開發日誌

컬럼별으로 묶어서 카운트..

jethL 2012. 5. 30. 00:07

1)번은 그룹별로 묶어만 놓은 상태

2), 3)은 결과가 같지만 2)번의 distinct를 쓰면 정렬 후 값을 비교하기 때문에 많은 양의 데이터에 쓰기에 불리함

4)번은 null이나 공백등으로 인해 값이 누락되는 것을 막기위해 NVL과 아우터조인을 이용해 필드값이 비어있어도

출력가능함.

 

1)

    select swid, macaddr, count(1)

      from pif_logmaster

    where inputday >= '2012-05-28'

       and inputday < '2012-05-29'

group by swid, macaddr

 

2)

    select swid, count(distinct macaddr)

      from pif_logmaster

    where inputday >= '2012-05-28'

       and inputday < '2012-05-29'

group by swid

 

3)

    select b.swid, sum(a.macCnt)

      from (

                  select swid, macaddr, case when count(1) = 1 then 1 else 1 end as macCnt

                    from pif_logmaster

                  where inputday >= '2012-05-28'

                     and inputday < '2012-05-29'

              group by swid, macaddr)

group by b.swid

 

4)

    select b.swid, nvl(sum(a.macCnt), 0) cnt

      from (

                  select swid, macaddr, case when count(1) = 1 then 1 else 1 end as macCnt

                    from pif_logmaster

                  where inputday >= '2012-05-28'

                     and inputday < '2012-05-29'

              group by swid, macaddr) a, pif_swinfo b

    where a.swid(+) = b.swid   --  a 1,2,3,4,5

group by b.swid                   --  b 1,2,4,5,6 일경우 (+) 있는 1,2,3,4,5인 a출력시킨다.