2016-11-27 15 views
1

このSQLの実行合計に関する助けが必要です。私は提案のいくつかを試してみましたが、それを100%稼働させることはできませんでした。私の合計は順調ではありません。毎月のMySQLの累積合計

クエリは、最初に5カラットを超える石、次に5カラット以下の石について、毎日生成される石の数とカラットの数を示します。したがって、最初の列である日付の後に、4つの列が表示されます。

次に、次の2つの列は、石を一緒にまとめて、カラットを合計して1日合計にします。

その後、私は石とカラットの実行合計を作成する2つの列を追加したいと思います。

ご協力いただければ幸いです。ありがとうございました!

select 
    `dbo_List_Dates`.`Full_Date` AS `Full_Date`, 
    if(isnull(`qry_Register_Over5ct`.`StonesO5`), 
    0,`qry_Register_Over5ct`.`StonesO5`) AS `StonesO5`, 
    if(isnull(`qry_Register_Over5ct`.`CaratsO5`), 
    0,format(`qry_Register_Over5ct`.`CaratsO5`,3)) AS `CaratsO5`, 
    if(isnull(`qry_Register_Under5ct`.`StonesU5`), 
    0,`qry_Register_Under5ct`.`StonesU5`) AS `StonesU5`, 
    if(isnull(`qry_Register_Under5ct`.`CaratsU5`), 
    0,format(`qry_Register_Under5ct`.`CaratsU5`,3)) AS `CaratsU5`, 

    (if(isnull(`qry_Register_Over5ct`.`StonesO5`), 
    0,`qry_Register_Over5ct`.`StonesO5`) + if(isnull(`qry_Register_Under5ct`.`StonesU5`), 
    0,`qry_Register_Under5ct`.`StonesU5`)) AS `Stones`, 

    format((if(isnull(`qry_Register_Over5ct`.`CaratsO5`), 
    0,`qry_Register_Over5ct`.`CaratsO5`) + if(isnull(`qry_Register_Under5ct`.`CaratsU5`), 
    0,`qry_Register_Under5ct`.`CaratsU5`)),3) AS `Carats`, 

    date_format(`dbo_List_Dates`.`Full_Date`,'%Y-%m') AS `Date_Filter` 

from 
    (
    (`dbo_List_Dates` 
    left join `qry_Register_Over5ct` 
    on((`dbo_List_Dates`.`Full_Date`=qry_Register_Over5ct`.`Shift_Date`)) 
    ) 
    left join `qry_Register_Under5ct` 
    on((`dbo_List_Dates`.`Full_Date`=`qry_Register_Under5ct`.`Shift_Date`)) 
) 
where 
    (date_format(`dbo_List_Dates`.`Full_Date`,'%Y-%m') = date_format(now(),'%Y-%m')) 
order by 
    `dbo_List_Dates`.`Full_Date` 
limit 0,31 
+0

参照[Sqlfiddle、なぜ私は気にしなければならないのは何ですか?](http://stackoverflow.com/q/38899464) – Drew

+0

なぜMySQLのタグ? – Strawberry

+0

'qry_Register_Over5ct'と' qry_Register_Under5ct'ビューはありますか?私は石サイズを決定するために同じテーブルを2回スキャンするので、これは既に非効率的だと思います。 –

答えて

0

mysqlで合計を実行するには、変数を使用する必要があります。例えば

create table register (id int, dt date, carats int); 
insert into register values 
(1,'2016-11-01',10),(2,'2016-11-01',10),(3,'2016-11-01',1), 
(4,'2016-11-02',1), 
(5,'2016-11-03',10), 
(6,'2016-11-05',10),(7,'2016-11-05',1); 

この

+------------+ 
| dte  | 
+------------+ 
| 2016-11-01 | 
| 2016-11-02 | 
| 2016-11-03 | 
| 2016-11-04 | 
| 2016-11-05 | 
+------------+ 

select s.* ,@RunningTotal:[email protected] + s.LT5_GE5 RunningTotal 
from 
(
select d.dte, 
     sum(case when r.dt is not null and r.carats >= 5 then 1 else 0 end) GE5, 
     sum(case when r.dt is not null and r.carats < 5 then 1 else 0 end) LT5, 
     sum(case when r.dt is not null and r.carats >= 5 then 1 else 0 end) + 
     sum(case when r.dt is not null and r.carats < 5 then 1 else 0 end) LT5_GE5 
from dates d 
left join register r on r.dt = d.dte 
where dte between '2016-11-01' and '2016-11-05' 
group by dte 
) s ,(select @RunningTotal:=0) rt 

結果

+------------+------+------+---------+--------------+ 
| dte  | GE5 | LT5 | LT5_GE5 | RunningTotal | 
+------------+------+------+---------+--------------+ 
| 2016-11-01 | 2 | 1 |  3 |   3 | 
| 2016-11-02 | 0 | 1 |  1 |   4 | 
| 2016-11-03 | 1 | 0 |  1 |   5 | 
| 2016-11-04 | 0 | 0 |  0 |   5 | 
| 2016-11-05 | 1 | 1 |  2 |   7 | 
+------------+------+------+---------+--------------+ 

などの日付のテーブルを与えそして、あなたがしたい場合は、列はあなたが

により、グループ内のロールアップを含めることができます合計
select s.* , 
      cast(if(dte is null,@RunningTotal,@RunningTotal:[email protected] + s.LT5_GE5) as int) RunningTotal 
from 
(
select d.dte, 
     sum(case when r.dt is not null and r.carats >= 5 then 1 else 0 end) GE5, 
     sum(case when r.dt is not null and r.carats < 5 then 1 else 0 end) LT5, 
     sum(case when r.dt is not null and r.carats >= 5 then 1 else 0 end) + 
     sum(case when r.dt is not null and r.carats < 5 then 1 else 0 end) LT5_GE5 
from dates d 
left join register r on r.dt = d.dte 
where dte between '2016-11-01' and '2016-11-05' 
group by dte with rollup 
) s ,(select @RunningTotal:=0) rt 

結果

+------------+------+------+---------+--------------+ 
| dte  | GE5 | LT5 | LT5_GE5 | RunningTotal | 
+------------+------+------+---------+--------------+ 
| 2016-11-01 | 2 | 1 |  3 |   3 | 
| 2016-11-02 | 0 | 1 |  1 |   4 | 
| 2016-11-03 | 1 | 0 |  1 |   5 | 
| 2016-11-04 | 0 | 0 |  0 |   5 | 
| 2016-11-05 | 1 | 1 |  2 |   7 | 
| NULL  | 4 | 3 |  7 |   7 | 
+------------+------+------+---------+--------------+ 
6 rows in set (0.02 sec)