2016-06-23 8 views
0

이 쿼리는 엔지니어가 시간, 일, 주, 월로 세분화 한 총 테스트 수를 얻기 위해 동일한 테이블 (qcheck)에 대해 여러 카운트를 실행합니다. 또한 결과와 데이터의 스크린 샷을 포함 시켰습니다.하위 쿼리로 롤업 사용

은 내가 합계를 얻을 수 있도록 롤업 싶어하지만 난 아무것도

picture of data

Picture of results

코드 전체에 가입 할 수 있습니다으로 하위 쿼리를 사용할 때이 작업을 수행하는 방법을 잘 :

select coalesce(main.checkby, 'Total') as checkby_or_total, 
     lfaulty, 
     lfully, 
     ltotal, 
     dfaulty, 
     dfully, 
     dtotal, 
     wfaulty, 
     wfully, 
     wtotal, 
     mfaulty, 
     mfully, 
     mtotal 
from (
     select qcheck.checkby, 
       count(case result when 'fully tested & working' then 1 end)  as mfully, 
       count(case result when 'faulty' then 1 end)      as mfaulty, 
       count(*) as mtotal 
     from  qcheck 
     where YEAR(finishdate) = YEAR(CURDATE()) AND MONTH(finishdate) = MONTH(CURDATE()) 
     and  qcheck.checkby not like 'michael' 
     and  qcheck.checkby not like 'chaz' 
     group by qcheck.checkby with rollup 
     ) as main 
Left join (select qcheck.checkby, 
       count(case result when 'fully tested & working' then 1 end)  as dfully, 
       count(case result when 'faulty' then 1 end)      as dfaulty, 
       count(*) as dtotal 
     from  qcheck 
     where finishdate >= now()-interval 12 hour 
     and  qcheck.checkby not like 'michael' 
     and  qcheck.checkby not like 'chaz' 
     group by qcheck.checkby with rollup) as today on today.checkby =main.checkby 
Left join (select qcheck.checkby, 
       count(case result when 'fully tested & working' then 1 end)  as wfully, 
       count(case result when 'faulty' then 1 end)      as wfaulty, 
       count(*) as wtotal 
     from  qcheck 
     where YEARWEEK(finishdate)=YEARWEEK(NOW()) 
     and  qcheck.checkby not like 'michael' 
     and  qcheck.checkby not like 'chaz' 
     group by qcheck.checkby with rollup) as week on week.checkby =main.checkby 
Left join (select qcheck.checkby, 
       count(case result when 'fully tested & working' then 1 end)  as lfully, 
       count(case result when 'faulty' then 1 end)      as lfaulty, 
       count(*) as ltotal 
     from  qcheck 
     where finishdate >= now()-interval 1 hour 
     and  qcheck.checkby not like 'michael' 
     and  qcheck.checkby not like 'chaz' 
     group by qcheck.checkby with rollup) as month on month.checkby =main.checkby 
order by main.checkby is null, 
      mtotal desc 
+0

두 번째 그림은 * 원하는 * 출력입니까, 아니면 * 현재 * 출력입니까? * 원하는 * 출력이 아닌 경우 다른 이미지에서 사용한 데이터를 기반으로 출력 할 수 있습니까? 또한, 이미지를 사용하지 말고 일반 텍스트 (단폭형 레이아웃의 경우 4 칸 들여 쓰기 - 모두 선택, Ctrl + K를 누르십시오) – trincot

+0

쿼리 및 결과 그림을 업데이트했습니다. "결과 그림 "어떻게 표시하고 싶지만 모든 열의 마지막 행에 대해 작동하지 않는 롤, 나는 또한 null로 0 싶습니다,이 도움이되기를 바랍니다. – troy

답변

0

조인 조건때문에 하위 쿼리에서 생성 된 롤업 행이 손실됩니다.. 양측이 null 일 때이 조건은 false입니다. 특히 : null = nullfalse입니다.

또한 국경일이 2 개월 이상인 경우에 한해 주당 계산이 현재 달의 계산보다 커질 수 있습니다. 이것은 그 자체로 문제는 아니지만 join 체인이 매월 카운트로 시작하는 방식으로, 매주 카운트의 일부 레코드를 놓칠 수 있습니다. 즉, 이번 달에 아직 기록이없는 사람에 대한 주간 기록은 주 통계에 포함되지 않습니다.

null 값은 바깥 쪽 (left) 조인의 결과입니다. 외부 조인 조건이 false이면 참조 할 때 라이터 조인 된 테이블의 모든 필드 (개수)도 null이됩니다.

위의 모든 문제

where 조건 case when 조건 대신 더 정교한 사용하여, 하나 개의 큰 서브 쿼리를 수행하여 해결할 수 있습니다

select * 
from  (
      select coalesce(checkby, 'Total') as checkby_or_total, 
        count(case when last_day(finishdate) = last_day(curdate()) 
           and result = 'fully tested & working' 
         then 1 end) as mfully, 
        count(case when last_day(finishdate) = last_day(curdate()) 
           and result = 'faulty' 
         then 1 end) as mfaulty, 
        count(case when last_day(finishdate) = last_day(curdate()) 
         then 1 end) as mtotal, 
        count(case when date(finishdate) = curdate() 
           and result = 'fully tested & working' 
         then 1 end) as dfully, 
        count(case when date(finishdate) = curdate() 
           and result = 'faulty' 
         then 1 end) as dfaulty, 
        count(case when date(finishdate) = curdate() 
         then 1 end) as dtotal, 
        count(case when yearweek(finishdate) = yearweek(curdate()) 
           and result = 'fully tested & working' 
         then 1 end) as wfully, 
        count(case when yearweek(finishdate) = yearweek(curdate()) 
           and result = 'faulty' 
         then 1 end) as wfaulty, 
        count(case when yearweek(finishdate) = yearweek(curdate()) 
         then 1 end) as wtotal, 
        count(case when finishdate >= now() - interval 1 hour 
           and result = 'fully tested & working' 
         then 1 end) as lfully, 
        count(case when finishdate >= now() - interval 1 hour 
           and result = 'faulty' 
         then 1 end) as lfaulty, 
        count(case when finishdate >= now() - interval 1 hour 
         then 1 end) as ltotal 
      from  qcheck 
      where checkby not in ('michael', 'chaz') 
      group by checkby with rollup) as main 
order by checkby_or_total = 'Total', 
      mtotal desc 
나는 또한 어떤 다른 조건을 도입

:

  • 달 조건을보다 효율적으로 만들 수 있습니다. last_day(finishdate) = last_day(curdate())
  • 당일 조건은 date(finishdate) = curdate() 일 수 있지만 당신이 사용한 조건과 정확히 같지 않습니다. 하지만 아마도 이것을 고려하고 싶을 것입니다.
+0

도움을 주셔서 감사합니다, 잘 작동합니다. – troy