초보입니다 그룹별 쿼리좀 가르쳐 주세요
일자별 상황별 주말여부별 로 그룹바이해서 통계를 내야합니다
데이터는 아래와 같습니다
WITH T AS(
select '001' S_ID , 'L2010103' r_id , '2014-12-10' d_date , 'N' week_yn , '3' type from dual union all
select '002' S_ID ,'L2010202' r_id , '2014-12-20' d_date , 'Y' week_yn , '4' type from dual union all
select '003' S_ID ,'L2010102' r_id , '2015-01-14' d_date , 'N' week_yn , '2' type from dual union all
select '004' S_ID ,'L2010103' r_id , '2014-12-10' d_date , 'N' week_yn , '1' type from dual union all
select '005' S_ID ,'L2010103' r_id , '2014-12-10' d_date , 'N' week_yn , '1' type from dual
)
SELECT R_ID, D_DATE, WEEK_YN , TYPE , COUNT(*)
, NVL( MAX(DECODE (WEEK_YN, 'Y', 1 )) , 0 ) WEEKEND_Y_CNT
, NVL( MAX(DECODE (WEEK_YN, 'N', 1 )) , 0 ) WEEKEND_N_CNT
FROM T
GROUP BY R_ID, D_DATE, TYPE , WEEK_YN
위 쿼리 결과
도로ID 일자 주말여부 상황코드 COUNT YCNT NCNT
L2010103 2014-12-10 N 3 1 0 1
L2010102 2015-01-14 N 2 1 0 1
L2010202 2014-12-20 Y 4 1 1 0
L2010103 2014-12-10 N 1 2 0 1
위처럼 나오는데요
2014-12-10일에 총건수가 2이고 YCNT 1 NCNT 1이 나와야되는데
현재는 YCNT 0 NCNT 1 로나오죠 뭐 디코드를 저렇게 했으니 당연한 결과겠지만.......
저부분을 어떻게 처리해야될지 모르겠습니다..
즉 14년12월10일은 그룹바이시 2건인데 주말여부가 Y일때 건수 N일때 건수를 따로 뿌리고싶거든요
조언좀 부탁드리겠습니다
음....올려주신 쿼리의 2014-12-10 인 자료의 WEEK_YN은 모두 Y네요.
그래서 그냥 COUNT(DECODE())로 하시면 될거 같은데 이거 아닌가요?
그런데.. 날짜 하나에 주말여부가 다를 수가 있나요? 이건 그냥;;;
WITH T AS( select '001' S_ID ,'L2010103' r_id , '2014-12-10' d_date , 'N' week_yn , '3' type from dual union all select '002' S_ID ,'L2010202' r_id , '2014-12-20' d_date , 'Y' week_yn , '4' type from dual union all select '003' S_ID ,'L2010102' r_id , '2015-01-14' d_date , 'N' week_yn , '2' type from dual union all select '004' S_ID ,'L2010103' r_id , '2014-12-10' d_date , 'N' week_yn , '1' type from dual union all select '005' S_ID ,'L2010103' r_id , '2014-12-10' d_date , 'N' week_yn , '1' type from dual ) SELECT R_ID, D_DATE, WEEK_YN , TYPE , COUNT(*) , COUNT(DECODE(WEEK_YN, 'Y', 1)) AS WEEKEND_Y_CNT , COUNT(DECODE(WEEK_YN, 'N', 1)) AS WEEKEND_N_CNT FROM T GROUP BY R_ID, D_DATE, TYPE , WEEK_YN
질문과 예제가 뭔가 이상한거 같은데요..
결과를 기준으로 한다면 다음과 같이 해야 하지 않을까요?
WITH T AS( select '001' S_ID ,'L2010103' r_id , '2014-12-10' d_date , 'N' week_yn , '3' type from dual union all select '002' S_ID ,'L2010202' r_id , '2014-12-20' d_date , 'Y' week_yn , '4' type from dual union all select '003' S_ID ,'L2010102' r_id , '2015-01-14' d_date , 'N' week_yn , '2' type from dual union all select '004' S_ID ,'L2010103' r_id , '2014-12-10' d_date , 'N' week_yn , '1' type from dual union all select '005' S_ID ,'L2010103' r_id , '2014-12-10' d_date , 'Y' week_yn , '1' type from dual ) SELECT R_ID, D_DATE, TYPE , COUNT(*) , COUNT(DECODE(WEEK_YN, 'Y', 1)) AS WEEKEND_Y_CNT , COUNT(DECODE(WEEK_YN, 'N', 1)) AS WEEKEND_N_CNT FROM T GROUP BY R_ID, D_DATE, TYPE