2016-06-23 23 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

2番目の画像は*希望の*出力ですか、または*現在の出力ですか? *望ましい*出力でない場合は、他の画像で使用したデータに基づいて出力できますか?また、画像は使用しないでください。ただし、平置きテキスト(固定幅の4つのスペースでインデントされています - すべてを選択し、Ctrl + Kを押してください)を入力してください。 – trincot

+0

クエリと結果の画像を更新しました。 "どのように私はそれが表示されますが、すべての列の最後の行のために働いているロールアップは、私も0としてnullを持っていると思いますが、これは助けることを願って – 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

関連する問題