.안녕하세요~!
오늘도 질문이 있어서 찾아왔습니다!
아래와 같이 A,B,C 값 별로 행을 가지고 오는 쿼리문을 작성하였는데
추가로 ABC의 합친 수량과, 근무시간을 더해서 수치를 구하는 쿼리를 추가하고 싶습니다.
검색하여서 grouping / rollup 을 사용하려 하니 이런 에러가 떠서 도움이 필요합니다ㅠ
line 15:8: The arguments to GROUPING() must be expressions referenced by the GROUP BY at the associated query level. Mismatch due to date .
현재 Zeppelin 사용하고 있으며 정확하게 mssql / mysql 인지를 모르겠습니다.......
오라클 : NVL
MSSQL : ISNULL
MySQL : IFNULL
공통 : COALESCE
지난번 이 4개 알려주셨을때 공통 coalesce 빼고는 전부 작동이 되지 않았었습니다.
(select 문 만 가져옴)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | select ct.code, week, case when date between 6 and 16 then 'A' when date between 17 and 25 then 'B' when date between 26 and 35 then 'C' end as WORKTIME, case when grouping ( date ) = 1 then 'total' end as total, sum ( case when task = '작업' then 수량 end ) as 수량 sum (cworkhours) as cworkhours, round( sum ( case when task = '작업' then 수량 end ) / nullif ( sum (수량) ,0) , 2) as 수치 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | WITH Date AS ( SELECT iyyyy_1||mm AS mm , iyyyy_1_wk AS week , dates from d. date ) select c.code, week, case when u. hour between 6 and 16 then 'a' when u. hour between 17 and 25 then 'b' when u. hour between 26 and 35 then 'c' end as hour , sum ( case when u.task = 'p' then u.qty end ) as 수량, sum (u.workhour) as 시간, round( sum ( case when u.task = 'p' then u.qty end ) / nullif ( sum (u.workhour) ,0) , 2) as 수치 from u.u join ( select center) c on c.id = u.id join date on u. hour = date .dates where c.code = 'Joy1' and u.type= 'joy2' and u. hour between date ( '2023-07-01' ) and date (now()) and u.deleted = 0 group by 1, 2, 3 order by 1, 2, 3 |
보안 문제로 문구를 좀 변경하였습니다. 해당 쿼리로 a,b,c 별 수량, 시간, 수치까지 구하는 쿼리는 정상 작동되었습니다!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | WITH Date AS ( SELECT iyyyy_1||mm AS mm , iyyyy_1_wk AS week , dates from d. date ) select c.code, week, case when u.시간 between 6 and 16 then 'a' when u.시간 between 17 and 25 then 'b' when u.시간 between 26 and 35 then 'c' end as 근무시간, sum ( case when u.task = 'p' then u.qty end ) as 수량, sum (u.workhour) as 시간, round( sum ( case when u.task = 'p' then u.qty end ) / nullif ( sum (u.workhour) ,0) , 2) as 수치 from u.u join ( select center) c on c.id = u.id join date on u. hour = date .dates where c.code = 'Joy1' and u.type= 'joy2' and u. hour between date ( '2023-07-01' ) and date (now()) and u.deleted = 0 group by 1, 2, 3 order by 1, 2, 3 |
code | week | 근무시간 | 수량 | 시간 | 수치 |
Joy1 | 47 | a | 500 | 40 | 12.5 |
Joy1 | 47 | b | 600 | 50 | 12 |
Joy1 | 47 | c | 700 | 60 | 11.67 |
Joy1 | 47 | total | 1800 | 150 | 12 |
Joy1 | 48 | a | 805 | 540 | 1.491 |
Joy1 | 48 | b | 540 | 124 | 4.355 |
Joy1 | 48 | c | 668 | 241 | 2.772 |
Joy1 | 48 | total | 2013 | 905 | 2.224 |
49 | a | ||||
49 | b | ||||
49 | c | ||||
49 | total |
이런식의 a,b,c 의 값이 나왔고, 중간 토탈값이 필요합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | -- 간단한 쿼리로 롤업 가능성 테스트 해보세요. -- GROUPING() 함수 사용도 가능한지 테스트 해보세요. -- 1. 기본 유형 SELECT a, b, c , SUM (d) d FROM t GROUP BY ROLLUP (a, b, c) ; -- 2. 부분 사용 유형 SELECT a, b, c , SUM (d) d FROM t GROUP BY a, b, ROLLUP (c) ; -- 3. 괄호 사용 유형 SELECT a, b, c , SUM (d) d FROM t GROUP BY ROLLUP ((a, b, c)) ; -- 4. MySQL 유형 SELECT a, b, c , SUM (d) d FROM t GROUP BY a, b, c WITH ROLLUP ; |