调整两个查询

时间:2017-12-01 21:56:53

标签: mysql sql

再次邀请社区。

我最近收到了解决由List grouped data by month including months in which there are no results

组成的问题的解决方案的帮助

现在我有相同的情况,但不同的是有一个条件要评估。我需要调整这个查询

select
    date_format(a.date_created, '%b') month,
    month(a.date_created) pivot,
    sum(case when a.state = 'created' then 1 else 0 end) created,
    sum(case when a.state = 'notified' then 1 else 0 end) notified,
    sum(case when a.state = 'confirmed' then 1 else 0 end) confirmed,
    sum(case when a.state = 'approved' then 1 else 0 end) approved,
    sum(case when a.state = 'authorized' then 1 else 0 end) authorized,
    sum(case when a.state = 'attended' then 1 else 0 end) attended,
    sum(case when a.state = 'canceled' then 1 else 0 end) canceled,
    count(a.id) as total
from
    activities a
        inner join
    employees e on a.employee_id = e.id
where
    e.id = 8
group by 1, 2 order by pivot desc;

此查询

select
    s.name month,
    s.m pivot,
    sum(case when a.state = 'created' then 1 else 0 end) created,
    sum(case when a.state = 'notified' then 1 else 0 end) notified,
    sum(case when a.state = 'confirmed' then 1 else 0 end) confirmed,
    sum(case when a.state = 'approved' then 1 else 0 end) approved,
    sum(case when a.state = 'authorized' then 1 else 0 end) authorized,
    sum(case when a.state = 'attended' then 1 else 0 end) attended,
    sum(case when a.state = 'canceled' then 1 else 0 end) canceled,
    count(a.id) as total
from (
      SELECT  1 m, 'Jan' AS name 
      UNION SELECT 2, 'Feb' 
      UNION SELECT 3, 'Mar'
      UNION SELECT 4, 'Apr'
      UNION SELECT 5, 'May'
      UNION SELECT 6, 'Jun'
      UNION SELECT 7, 'Jul'
      UNION SELECT 8, 'Aug'
      UNION SELECT 9, 'Sep'
      UNION SELECT 10, 'Oct'
      UNION SELECT 11, 'Nov'
      UNION SELECT 12, 'Dec'
) s
LEFT JOIN activities a
    ON s.m = month(date_created)
group by 1, 2 order by pivot desc;

我不知道在上一个问题的解决方案的上下文中如何添加条件

我测试了以下内容,但未列出没有结果的月份

select
    s.name month,
    s.m pivot,
    sum(case when a.state = 'created' then 1 else 0 end) created,
    sum(case when a.state = 'notified' then 1 else 0 end) notified,
    sum(case when a.state = 'confirmed' then 1 else 0 end) confirmed,
    sum(case when a.state = 'approved' then 1 else 0 end) approved,
    sum(case when a.state = 'authorized' then 1 else 0 end) authorized,
    sum(case when a.state = 'attended' then 1 else 0 end) attended,
    sum(case when a.state = 'canceled' then 1 else 0 end) canceled,
    count(a.id) as total
from (
      SELECT  1 m, 'Jan' AS name 
      UNION SELECT 2, 'Feb' 
      UNION SELECT 3, 'Mar'
      UNION SELECT 4, 'Apr'
      UNION SELECT 5, 'May'
      UNION SELECT 6, 'Jun'
      UNION SELECT 7, 'Jul'
      UNION SELECT 8, 'Aug'
      UNION SELECT 9, 'Sep'
      UNION SELECT 10, 'Oct'
      UNION SELECT 11, 'Nov'
      UNION SELECT 12, 'Dec'
) s
LEFT JOIN activities a
  ON s.m = month(date_created)
inner join employees e on a.employee_id = e.id
group by 1, 2 order by pivot desc;

我分享了以下sqlfiddle,其中包括员工与活动之间的关系

我期待id为2的用户结果如下

enter image description here

再次,非常感谢你。

2 个答案:

答案 0 :(得分:0)

我认为这个版本可以满足您的需求:

select s.name as month, s.m as pivot,
       sum(case when a.state = 'created' then 1 else 0 end) created,
       sum(case when a.state = 'notified' then 1 else 0 end) notified,
       sum(case when a.state = 'confirmed' then 1 else 0 end) confirmed,
       sum(case when a.state = 'approved' then 1 else 0 end) approved,
       sum(case when a.state = 'authorized' then 1 else 0 end) authorized,
       sum(case when a.state = 'attended' then 1 else 0 end) attended,
       sum(case when a.state = 'canceled' then 1 else 0 end) canceled,
       count(a.id) as total
from (SELECT  1 m, 'Jan' AS name UNION ALL
      SELECT 2, 'Feb'  UNION ALL
      SELECT 3, 'Mar' UNION ALL
      SELECT 4, 'Apr' UNION ALL
      SELECT 5, 'May' UNION ALL
      SELECT 6, 'Jun' UNION ALL
      SELECT 7, 'Jul' UNION ALL
      SELECT 8, 'Aug' UNION ALL
      SELECT 9, 'Sep' UNION ALL
      SELECT 10, 'Oct' UNION ALL
      SELECT 11, 'Nov' UNION ALL
      SELECT 12, 'Dec'
     ) s LEFT JOIN activities a
     ON s.m = month(date_created) left join
     employees e
     on a.employee_id = e.id AND e.id = 2
group by 1, 2
order by pivot desc;

Here是SQL小提琴。

主要更改是将e.id上的条件移至ON子句,并将第二个联接更改为LEFT JOIN。此外,我将UNION更改为UNION ALL,以消除删除重复项的开销。

答案 1 :(得分:0)

你刚刚加入混乱。对于要被视为INNER联接的活动和员工之间的联接,必须将其括在括号中。让我们知道它是否有效!

这是解决方案

select
    s.name month,
    s.m pivot,
    sum(case when a.state = 'created' then 1 else 0 end) created,
    sum(case when a.state = 'notified' then 1 else 0 end) notified,
    sum(case when a.state = 'confirmed' then 1 else 0 end) confirmed,
    sum(case when a.state = 'approved' then 1 else 0 end) approved,
    sum(case when a.state = 'authorized' then 1 else 0 end) authorized,
    sum(case when a.state = 'attended' then 1 else 0 end) attended,
    sum(case when a.state = 'canceled' then 1 else 0 end) canceled,
    count(a.id) as total
from (
      SELECT  1 m, 'Jan' AS name 
      UNION SELECT 2, 'Feb' 
      UNION SELECT 3, 'Mar'
      UNION SELECT 4, 'Apr'
      UNION SELECT 5, 'May'
      UNION SELECT 6, 'Jun'
      UNION SELECT 7, 'Jul'
      UNION SELECT 8, 'Aug'
      UNION SELECT 9, 'Sep'
      UNION SELECT 10, 'Oct'
      UNION SELECT 11, 'Nov'
      UNION SELECT 12, 'Dec'
) s
LEFT JOIN ( activities a INNER JOIN employees e 
                                 ON a.employee_id = e.id AND e.id = 2)
        ON (s.m = month(a.date_created))
group by 1, 2 order by pivot desc;