将两个sql查询合并为一个分组

时间:2013-12-27 18:03:42

标签: sqlite

所以我在底部有两个以下的查询。现在我怎么能把它们组合在一个表中呢?也是如此:

+-------+-----------------+----------------+
| Date  | Tickets created | Tickets closed |
+-------+-----------------+----------------+
| 10/25 |              50 |             45 |
| 10/26 |              40 |             40 |
| 10/27 |              30 |             30 |
| 10/28 |              10 |              0 |
+-------+-----------------+----------------+

票证表如下所示:

+----+----------------------+----------------------+------------------+
| id |      created_at      |      closed_at       | master_ticket_id |
+----+----------------------+----------------------+------------------+
|  1 | 2013-10-12 00:00:00' | 2013-10-12 06:00:00' |                  |
|  2 | 2013-11-11 00:00:00' |                      |                  |
|  3 | 2013-11-12 00:00:00' |                      |                2 |
|  4 | 2013-11-13 10:00:00' | 2013-11-13 12:00:00' |                  |
|  5 | 2013-12-12 00:00:00' | 2013-12-12 07:00:00' |                  |
+----+----------------------+----------------------+------------------+

查询#1

select strftime("%m-%d",tickets.created_at) as 'Date',
count(tickets.created_at)as 'Tickets created'
from tickets 
WHERE tickets.created_at >= '2013-10-01 00:00:00' 
and tickets.created_at< '2013-10-31 00:00:00'
and tickets.master_ticket_id is Null
Group by strftime("%m-%d",tickets.created_at) 

产生:

+-------+-----------------+
| Date  | Tickets created |
+-------+-----------------+
| 10/25 |              50 |
| 10/26 |              40 |
| 10/27 |              30 |
+-------+-----------------+

查询#2

select strftime("%m-%d",tickets.closed_at) as 'Date',
count(tickets.closed_at)as 'Tickets closed'
from tickets 
WHERE tickets.closed_at >= '2013-10-01 00:00:00' 
and tickets.closed_at< '2013-10-31 00:00:00'
and tickets.master_ticket_id is Null
Group by strftime("%m-%d",tickets.closed_at) 

产生:

+-------+----------------+
| Date  | Tickets closed |
+-------+----------------+
| 10/25 |             50 |
| 10/26 |             40 |
| 10/27 |             30 |
+-------+----------------+

2 个答案:

答案 0 :(得分:2)

[编辑因为以前的版本有一些错误]

似乎更难以避免UNION而不是我希望的,但就我发现以下情况而言应该有效。

SELECT Date, SUM(created) AS 'Tickets created', SUM(closed) AS 'Tickets closed' FROM
(
    SELECT strftime("%m-%d",tickets.created_at) AS 'Date',
    COUNT(tickets.created_at) AS 'created', 
    0 AS 'closed'
    FROM tickets 
    WHERE tickets.created_at >= '2013-10-01 00:00:00' 
    AND tickets.created_at< '2013-10-31 00:00:00'
    AND tickets.master_ticket_id IS NULL
    GROUP BY Date

UNION ALL

    SELECT strftime("%m-%d",tickets.closed_at) AS 'Date',
    0 AS 'created',  
    COUNT(tickets.closed_at) AS 'closed'
    FROM tickets 
    WHERE tickets.closed_at >= '2013-10-01 00:00:00' 
    AND tickets.closed_at< '2013-10-31 00:00:00'
    AND tickets.master_ticket_id IS NULL
    GROUP BY Date
)
GROUP BY Date

答案 1 :(得分:1)

您可以尝试JOIN表,例如:

      SELECT *
      FROM (query1) q1
      LEFT JOIN (query) q2
      ON q1.date = q2.date

但mySql没有完全加入,结果不会很好。你必须使用LEFT + RIGHT JOIN组合,结果是2个查询的UNION。

但试试这个(未经测试):

 SELECT 
     strftime("%m-%d",`date`) AS `date`, 
     SUM(created) `created`, 
     SUM(closed)`closed`
  FROM
  (
     SELECT 
     CASE
         WHEN (tickets.created_at >= '2013-10-01 00:00:00'  and tickets.created_at< '2013-10-31 00:00:00'  and tickets.master_ticket_id is Null )  THEN created_at
         WHEN (tickets.closed_at >= '2013-10-01 00:00:00'  and tickets.closed_at< '2013-10-31 00:00:00' and tickets.master_ticket_id is Null)  THEN closed_at
         END AS `date` ,    
     CASE
         WHEN (tickets.closed_at >= '2013-10-01 00:00:00'  and tickets.closed_at< '2013-10-31 00:00:00' and tickets.master_ticket_id is Null) THEN 1
         ELSE 0
         END AS `closed`,
     CASE
          WHEN (tickets.created_at >= '2013-10-01 00:00:00'  and tickets.created_at< '2013-10-31 00:00:00'  and tickets.master_ticket_id is Null) THEN 1
          ELSE 0
          END AS `created`
     FROM tickets 
 ) AS results
 Group by strftime("%m-%d",`date`)