2016-11-27 91 views
1

我需要一些帮助,这个SQL运行总数。我尝试了一些建议,但没有得到它的工作100%,我的总数不是为了。本月的每一天的MySQL累计总数

查询显示每天产生的石头数和克拉数,首先是5克拉以上的石头,然后是5克拉以下的石头,所以在第一列即日期之后,出现这4列。

然后接下来的两列简单地将石头和克拉一起加入日常总计中。

在此之后,我想添加两列,创建石头和克拉总数运行。

任何帮助,将不胜感激。谢谢!

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'的意见?我猜想这已经是低效了,因为你正在扫描相同的表格两次以确定石材的大小。 –

回答

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)