MySQL:左连接计数总数低于范围

时间:2015-02-15 08:16:23

标签: mysql sql left-join

我正在尝试从我的数据库中的下表生成以下表格输出。

期望的输出:

Table-pax     Q-Length      Avg wait time            Cust ID
=====         ========      ======================   =======
1-2           7             5 minutes                4
3-4           2             6 minutes                12
5-6           0             7 minutes                NULL
7-8           0             7 minutes                NULL

所以我有以下查询用于尝试生成所需的输出,这是在我的数据库表上执行的:

SELECT concat(s.min_pax, "-", s.max_pax) as Table-pax, d.CustCount as Q-Length, s.avg_q_time, d2.cust_id
FROM `Stat_table` as s
LEFT JOIN (
    SELECT est_id, COUNT(cust_id) as CustCount, pax, q_id
    FROM `Data_table`
    WHERE STATUS = "QQQ")
    GROUP BY est_id, pax) d
on d.pax >= s.min_pax AND d.pax <= s.max_pax
LEFT JOIN (
    SELECT est_id, pax, cust_id, q_id
    FROM `Data_table`
    WHERE status = "QQQ")
    GROUP BY pax
    ORDER BY time ASC) d2
on d2.q_id = d.q_id
WHERE s.est_id = 1
GROUP BY concat(s.min_pax, "-", s.max_pax)

数据库表1 - Stat_table

est_id     min_pax     max_pax     avg_q_time
======     =======     =======     ==========
1          1           2           5
1          3           4           6
1          5           6           7
1          7           8           7
2          1           4           4

数据库表2 - Data_table

est_id     cust_id     pax         status     q_id     time
======     =======     ===         ======     ====     ====
1          13          3           QQQ        22       12:32:01
1          12          3           QQQ        21       11:00:41
1          1           2           QQQ        20       12:12:33
1          11          1           QQQ        19       11:12:10
1          1           1           CXL        18       12:11:07
1          10          1           QQQ        17       12:59:45
1          7           1           QQQ        16       11:05:30
1          6           1           QQQ        15       12:18:32
1          5           1           QQQ        14       12:22:12
1          4           1           QQQ        13       10:15:02

但是,我从上述查询得到的是

Table-pax     Q-Length      Avg wait time            Cust ID
=====         ========      ======================   =======
1-2           1             5 minutes                1
3-4           2             6 minutes                12
5-6           0             7 minutes                NULL
7-8           0             7 minutes                NULL

尽管有7个条目状态&#34; QQQ&#34;对于1和2的人。

我几乎已经接近解决方案,但我无法绕过每个类别的表格聚合,而不是我想要的,即对于所有pax 1和2下的& #39; Data_table&#39;,我希望那些在table-pax 1-2的输出下聚合,所有pax 3和4在table-pax 3-4下聚合,依此类推。

1 个答案:

答案 0 :(得分:0)

你可以试试这个,但我不确定最后一栏cust_id,也许你能解释一下吗?

SELECT
  concat(s.min_pax, '-', s.max_pax) AS Table_pax,
  sum(d.CustCount)                  AS CustCount,
  s.avg_q_time,
  d2.cust_id
FROM Stat_table AS s
  LEFT JOIN (
              SELECT
                COUNT(cust_id) AS CustCount,
                pax,
                q_id
              FROM Data_table
              WHERE status = 'QQQ'
              GROUP BY pax) d
    ON d.pax >= s.min_pax AND d.pax <= s.max_pax
  LEFT JOIN (
              SELECT
                est_id,
                pax,
                cust_id,
                q_id
              FROM Data_table
              WHERE status = 'QQQ'
              GROUP BY pax
              ORDER BY time ASC) d2
    ON d2.q_id = d.q_id
WHERE s.est_id = 1
GROUP BY Table_pax;