带CASE的mysql COUNT()

时间:2016-10-19 12:52:25

标签: mysql

我遇到了此查询如何返回数据的问题。 timestamp返回正确,newpost返回正确,但已解决的帖子和已关闭的帖子无法恢复正确。我遇到的问题是,我今天有一个实例,今天有人关闭了一堆票,但是因为它们比过去7天发现的更早(这是为了制图),所以不算数他们因为我使用按组创建的顺序。创建的东西是创建它的日期,用作图表的timestamp以及故障单的创建时间以及newpost

在一天结束时,第18天应该有12 newposts,1 solved和22 closed,但它不起作用

SELECT
  DATE(created) timestamp,
  COUNT(created) newpost,
  COUNT(CASE WHEN DATE(closed) AND (sent = 0 OR sent = 1) THEN closed END) solvedpost,
  COUNT(CASE WHEN DATE(closed) AND (sent = 2) THEN closed END) closedpost
FROM 
  tickets
GROUP BY DATE(created)
ORDER BY DATE(created) DESC
LIMIT 7

示例数据: 门票     ---

| post_id| sent |     created      |        closed  
| 546nby | 2 | 2016-09-23 19:58:46 | 2016-10-18 19:00:53 | 
| 540r72 | 2 | 2016-09-22 19:33:07 | 2016-10-18 18:00:56 | 
| 54jjc2 | 2 | 2016-09-26 07:41:45 | 2016-10-18 18:00:54 | 
| 5720p8 | 2 | 2016-10-12 02:55:35 | 2016-10-18 18:00:52 | 
| 57a5qo | 2 | 2016-10-13 13:09:54 | 2016-10-18 18:00:50 | 
| 4zp83k | 2 | 2016-08-26 15:40:55 | 2016-10-18 14:00:47 | 
| 4zpu3c | 2 | 2016-08-26 17:36:19 | 2016-10-18 14:00:45 | 
| 545qvv | 2 | 2016-09-23 17:03:08 | 2016-10-18 13:01:17 | 
| 5475fv | 2 | 2016-09-23 21:41:17 | 2016-10-18 13:01:15 | 
| 549u2z | 2 | 2016-09-24 12:01:35 | 2016-10-18 13:01:13 | 
| 54ct4l | 2 | 2016-09-25 00:07:39 | 2016-10-18 13:01:11 | 
| 54gksl | 2 | 2016-09-25 19:07:58 | 2016-10-18 13:01:09 | 
| 54ippx | 2 | 2016-09-26 03:15:33 | 2016-10-18 13:01:07 | 
| 54mxvf | 2 | 2016-09-26 21:09:21 | 2016-10-18 13:01:05 | 
| 54yhfh | 2 | 2016-09-28 20:35:32 | 2016-10-18 13:01:03 | 
| 55gxw7 | 2 | 2016-10-02 04:58:23 | 2016-10-18 13:01:01 | 
| 55l2sz | 2 | 2016-10-03 00:11:01 | 2016-10-18 13:00:59 | 
| 55ozq6 | 2 | 2016-10-03 18:04:24 | 2016-10-18 13:00:57 | 
| 55txmx | 2 | 2016-10-04 15:09:11 | 2016-10-18 13:00:55 | 
| 55vi46 | 2 | 2016-10-04 20:06:39 | 2016-10-18 13:00:53 | 
| 55yw80 | 2 | 2016-10-05 10:56:29 | 2016-10-18 13:00:51 | 
| 561wzj | 2 | 2016-10-05 21:26:43 | 2016-10-18 13:00:49 | 
| 56fzlm | 0 | 2016-10-08 07:25:22 | 2016-10-18 13:00:45 | 
| 56sjln | 1 | 2016-10-10 15:34:01 | 2016-10-18 01:00:48 | 
| 57luzw | 1 | 2016-10-15 12:34:50 | 2016-10-17 18:00:44 | 
| 56tlp8 | 1 | 2016-10-10 18:44:46 | 2016-10-17 16:00:44 | 
| 578e6y | 0 | 2016-10-13 03:56:29 | 2016-10-17 05:00:47 | 
| 56931q | 1 | 2016-10-07 02:01:46 | 2016-10-17 04:00:44 | 
| 55wjpf | 1 | 2016-10-04 23:38:30 | 2016-10-16 23:00:47 | 
| 56tye6 | 1 | 2016-10-10 19:45:46 | 2016-10-16 23:00:45 | 
| 56wvge | 1 | 2016-10-11 07:07:50 | 2016-10-16 23:00:43 | 
| 57sfd6 | 2 | 2016-10-16 17:16:28 | 2016-10-16 18:00:36 |

@drapp

Results
---
| timestamp | newpost | solvedpost | closedpost |  
| 2016-10-19 | 4 | 0 | 0 | 
| 2016-10-18 | 12 | 0 | 0 | 
| 2016-10-17 | 8 | 0 | 0 | 
| 2016-10-16 | 6 | 0 | 1 | 
| 2016-10-15 | 8 | 1 | 0 | 
| 2016-10-14 | 11 | 2 | 1 | 
| 2016-10-13 | 12 | 3 | 1 | 

查询损坏的结果 Heres The results

3 个答案:

答案 0 :(得分:1)

好的,我认为我明白你想要什么,我会澄清。希望我的写作能帮助你解释未来的帖子。

对于您的数据,您拥有创建时,关闭时以及关闭状态的故障单。

我需要根据相关活动的每个日期检索摘要。因此,即使机票可能已于10月9日创建并于10月18日关闭,我也需要将截止日期活动显示在第18条摘要记录中。

要执行此操作,您需要一个子查询来汇总所有已关闭的活动,并根据相关日期对主要活动进行汇总。但是现在,如果你有一个没有创建门票的日期怎么办,但有些已关闭...我知道,异常情况,我不打算查询。

所以,调整后的查询为

SELECT
      DATE(T1.created) DateActivity,
      COUNT(T1.*) newpost,
      MAX( COALESCE( ClosedActivity.ClosedEntries, 0 )) as ClosedEntries,
      MAX( COALESCE( ClosedActivity.ClosedPost, 0 )) as ClosedPosts,
      MAX( COALESCE( ClosedActivity.SolvedPost, 0 )) as SolvedPosts
   FROM 
      tickets T1
         LEFT JOIN 
         ( select 
                 DATE( closed ) DateClosed,
                 COUNT(*) as ClosedEntries,
                 SUM( case when sent = 2 then 1 else 0 end ) as ClosedPost,
                 SUM( case when sent = 0 OR sent = 1 then 1 else 0 end ) as SolvedPost
              from
                 tickets
              where
                 closed >= date_add( current_date(), interval -7 day )
              GROUP BY 
                 DATE(closed) ) ClosedActivity
            ON 
               DATE( T1.Created ) = ClosedActivity.DateClosed
   where
      T1.created >= date_add( current_date(), interval -7 day )
   GROUP BY 
      DATE(T1.created)
   ORDER BY 
      DATE(T1.created) DESC
   LIMIT 7

注意我应用日期数学从当前减去7天。这样,它只会抓取过去7天内创建故障单的活动。如果您的数据有多年的数据,那么它将全部剔除,然后只返回最近的数据。我只从最近7天的活动开始。

由于已关闭的票证永远不会早于创建票证,因此该票证在7天内是合格的。

现在,对于内部查询的每个日期的单独行,我正在应用该结果的MAX(),因为它将永远不会根据创建的票证的外部条目数进行更改。否则,引擎可能会唠叨不属于不是聚合列的group by子句的列。

我认为这就是你要找的东西。

答案 1 :(得分:0)

DATE(已关闭)返回DATE而不是布尔值,您应该考虑以下内容:

closed>=curdate()

覆盖今天的日期并避免通过函数运行字段值

答案 2 :(得分:0)

更改

COUNT(CASE WHEN DATE(closed) AND (sent = 2) THEN closed END) closedpost

SUM(DATE(closed) = DATE(created) AND (sent = 2)) closedpost

(另一个柜台也是如此。)