上个月和本月总计使用MySQL CASE来汇总

时间:2018-02-16 10:11:09

标签: mysql sql sum case

我有一个查询来生成2个表中的课程名称和总计列表。它工作正常,但现在我想在同一个查询中得到本月和上个月的总数,所以我添加了几个案例。问题是案例返回空值。

这是我的疑问:

$Sql = "SELECT t1.coursename, t1.id, SUM(t2.amount) AS total,

CASE 
WHEN YEAR(t2.date) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(t2.date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) THEN SUM(t2.amount)
ELSE 0 END 
AS lastMonthTotal,      

CASE
WHEN YEAR(t2.date) = YEAR(CURRENT_DATE) AND MONTH(t2.date) = MONTH(CURRENT_DATE) THEN SUM(t2.amount) 
ELSE 0 END 
AS thisMonthTotal

FROM t1
LEFT JOIN t2 ON t2.courseid = t1.courseid
WHERE $pdoCoursesString GROUP BY t1.courseid";

5 个答案:

答案 0 :(得分:2)

我认为你应该将CASE语句放在SUM函数中。

SQL小提琴:http://sqlfiddle.com/#!9/4e10e2/5

SQL:

测试数据:

create table t1(courseid int, coursename varchar(10));
create table t2(courseid int, date date, amount int);
insert into t1 values(1,'courseA');
insert into t1 values(2,'courseB');
insert into t2 values(1,'20180211',12);
insert into t2 values(1,'20180111',16);
insert into t2 values(2,'20180101',1);
insert into t2 values(2,'20180201',1);
insert into t2 values(2,'20180101',1);    

查询:

SELECT t1.coursename, t1.courseid, SUM(t2.amount) AS total,
SUM(
  CASE 
  WHEN YEAR(t2.date) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) 
  AND MONTH(t2.date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) 
  THEN 
    t2.amount
  ELSE 
    0 
  END
) AS lastmonthtotal,
SUM(
  CASE 
  WHEN YEAR(t2.date) = YEAR(CURRENT_DATE) 
  AND MONTH(t2.date) = MONTH(CURRENT_DATE) 
  THEN 
    t2.amount
  ELSE 
    0 
  END
) AS thismonthtotal
FROM t1
LEFT JOIN t2 ON t2.courseid = t1.courseid
GROUP BY t1.courseid
;

结果:

| coursename | courseid | total | lastmonthtotal | thismonthtotal |
|------------|----------|-------|----------------|----------------|
|    courseA |        1 |    28 |             16 |             12 |
|    courseB |        2 |     3 |              2 |              1 |

答案 1 :(得分:1)

sum(case when {condition} then {field} else 0 end)

答案 2 :(得分:1)

将案件置于总和之内。

SUM(CASE WHEN ......)

答案 3 :(得分:1)

CASE放在SUM内,而不是相反。

$Sql = "SELECT t1.coursename, t1.id, SUM(t2.amount) AS total,

SUM(CASE WHEN YEAR(t2.date) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(t2.date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) 
         THEN t2.amount
         ELSE 0 END) AS lastMonthTotal,      

SUM(CASE
    WHEN YEAR(t2.date) = YEAR(CURRENT_DATE) AND MONTH(t2.date) = MONTH(CURRENT_DATE) 
    THEN t2.amount 
    ELSE 0 END) AS thisMonthTotal

FROM t1
LEFT JOIN t2 ON t2.courseid = t1.courseid
WHERE $pdoCoursesString GROUP BY t1.courseid";

答案 4 :(得分:0)

您应该在select子句中为没有聚合函数的相同列进行分组,例如t1.coursename,t1.id 并使用sum(case ...)例如:

$Sql = "SELECT t1.coursename, t1.id, SUM(t2.amount) AS total,

   sum(  CASE 
    WHEN YEAR(t2.date) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(t2.date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) THEN t2.amount
    ELSE 0 END )
    AS lastMonthTotal,      

    sum( CASE
    WHEN YEAR(t2.date) = YEAR(CURRENT_DATE) AND MONTH(t2.date) = MONTH(CURRENT_DATE) THEN t2.amount
    ELSE 0 END )
    AS thisMonthTotal
FROM t1
LEFT JOIN t2 ON t2.courseid = t1.courseid
WHERE $pdoCoursesString 
GROUP BY t1.coursename, t1.id";