具有多个组和子组的MySQL和计算

时间:2019-01-31 11:40:14

标签: mysql

我有一个查询,查询不输出我想要的结果。我为此花了几天时间,但没有找到解决方法。

我的代码

    SELECT *, IFNULL(type, 'N/A') AS groupField, IFNULL(type, 'N/A') AS displayField, `pmonth`, `pyear`, count(prj.code) as projects_num 
FROM `tblProjecats` `prj` 
LEFT JOIN ( select cda, pyear, pmonth, sum(venituri) venituri, sum(cheltuieli) cheltuieli, sum(if(cont3 between 500 and 550,cheltuieli,0)) cheltuieliD, sum(if(cont3 > 550,cheltuieli,0)) cheltuieliI from buffer_cda group by pyear, pmonth, cda ) cda ON `cda`.`cda`=`prj`.`code` 
-- more simple left jons --
WHERE (concat(pyear,lpad(pmonth,2,0)) BETWEEN 201811 AND 201911) 
GROUP BY `type`, concat_ws('', `cda`.`pyear`, lpad(cda.pmonth, 2, '0')) 
ORDER BY `type` ASC, `pyear` ASC, `pmonth` ASC, `cda` ASC

表tblProjects

code    type
66666   N/A
66667   Custom
66668   Simple
66669   Simple
66610   N/A
66611   Custom
66612   N/A

表buffer_cda(已加入项目类型,以便于查看)

cda pmonth  pyear   type    cheltuieliD
66666   1   2019    N/A     1
66667   1   2019    Custom  10
66668   1   2019    Simple  100
66669   12  2018    Simple  200
66610   12  2018    N/A     2
66611   12  2018    Custom  20
66612   12  2018    N/A     3
66612   12  2018    N/A     4

预期结果

Type    pmonth  pyear   count_prj   cheltuieliD
N/A     12      2018        2       9
N/A     1       2019        1       1
Custom  12      2018        1       20
Custom  1       2019        1       10
Simple  12      2018        1       200
Simple  1       2019        1       100

实际结果

Type    pmonth  pyear   count_prj   cheltuieliD
N/A     12      2018        2       7
N/A     1       2019        8       1
Custom  12      2018        1       20
Custom  1       2019        3       10
Simple  12      2018        1       200
Simple  1       2019        1       100

我在分组时做错了什么,但我不知道该怎么办。在其他情况下,我会使用cheltuieliD = 2和cheltuieliD = 7(而不是cheltuieliD = 9的一种类型)进行双倍类型结果

下一步是计算每种项目类型,期间组(月/年)的总成本指标,然后是总计。单个查询可以做到吗?

3 个答案:

答案 0 :(得分:0)

使用cda,pyear,pmonth来更改您的groupby序列...

如果您只想每月返回项目计数,则删除cda frim groupby子句...

如果您可以解释要执行的逻辑操作,我们将为您提供帮助。

答案 1 :(得分:0)

您的数据似乎并不代表您的问题,但鉴于所提供的示例数据,我将像这样编写代码

 drop table if exists tblprojects,buffer_cda;
 create table tblProjects
(code int,   type varchar(20));
insert into tblprojects values
(66666  , 'N/A'),
(66667  , 'Custom'),
(66668  , 'Simple'),
(66669  , 'Simple'),
(66610  , 'N/A'),
(66611  , 'Custom'),
(66612  , 'N/A');

create Table buffer_cda 
(cda int,pmonth int, pyear int, cheltuieliD int);
insert into buffer_cda values
(66666  , 1   ,2019  , 1),
(66667  , 1   ,2019  , 10),
(66668  , 1   ,2019  , 100),
(66669  , 12  ,2018  , 200),
(66610  , 12  ,2018  , 2),
(66611  , 12  ,2018  , 20),
(66612  , 12  ,2018  , 3),
(66612  , 12  ,2018  , 4);

select type,pmonth,pyear,count(type),sum(cheltuieliD) 
from tblprojects tp
join buffer_cda bc on bc.cda = tp.code
group by type,pyear,pmonth;

+--------+--------+-------+-------------+------------------+
| type   | pmonth | pyear | count(type) | sum(cheltuieliD) |
+--------+--------+-------+-------------+------------------+
| Custom |     12 |  2018 |           1 |               20 |
| Custom |      1 |  2019 |           1 |               10 |
| N/A    |     12 |  2018 |           3 |                9 |
| N/A    |      1 |  2019 |           1 |                1 |
| Simple |     12 |  2018 |           1 |              200 |
| Simple |      1 |  2019 |           1 |              100 |
+--------+--------+-------+-------------+------------------+

答案 2 :(得分:0)

我找到了正确的方法!

谢谢大家的回应!

我必须将项目上的加入buffer_cda交换为加入到buffer_cda的项目。 更进一步,我不得不消除其他联接。

在我的答案旁边,写给codeigniter!

select 
  X, Y, 
  case 
    when Z is null then (select something)
    else (select something else)
  end as col
from tablename

我也按类型,年,月进行分组。