如何在没有通用“ GROUP BY”元素的情况下合并2个查询的结果

时间:2019-01-13 17:15:08

标签: mysql select

我正在尝试计算不同类型的实例数,并按表示位置的字段将其分组。但是,第二个查询中的位置是通过另一个字段的子字符串获得的,并且比所需的数字小300。

目标是按位置汇总电话类型。对于IP电话(type 9608,b189和9611),位置由nr(网络区域)确定。对于模拟设备(type无绳电话,2500和传真机),位置由port上的前3个字符确定。

在大多数情况下,我都会根据需要运行每个查询。我有2个问题:

  1. 第二个查询中的nr需要添加300个结果。
  2. 我无法将这两个查询组合在一起以获得所需的单个结果。

示例数据集

+------+--------------+---------+-----------------------------+------+------+-----------+------+------+--------+-----------+------+----------------+------+
| id   | extension    | port    | name                        | cp1  | cor  | type      | cp2  | cos  | tenent | prod_id   | tcp  | ip             | nr   |
+------+--------------+---------+-----------------------------+------+------+-----------+------+------+--------+-----------+------+----------------+------+
|   62 | 111-000-0201 | S00215  | Baker Mail Rm               |      |    2 | 9608      |      |    1 |      4 | IP_Phone  | tcp  | 172.20.94.76   | 308  |
|   63 | 111-000-0202 | S00216  | Baker Copy Rm               |      |    2 | 9608      |      |    1 |      4 |           |      |                |      |
|   66 | 111-000-0205 | S00235  | Baker Conference Rm 2       |      |    2 | b189      |      |    1 |      4 | IP_Phone  | tcp  | 172.20.94.22   | 308  |
|  123 | 111-000-0626 | 008V301 | Baker Cordless              |      |    2 | cordless  |      |    1 |      1 |           |      |                |      |
|  145 | 111-000-1200 | S12329  | JEF-MAIN Dental 1           |      |    2 | 9611      |      |    1 |      1 | IP_Phone  | tcp  | 172.20.195.160 | 490  |
|  885 | 999-888-3025 | 190V203 | JEF WP Admin Conf Rm        |      |    2 | 2500      |      |    1 |      1 |           |      |                |      |
|  890 | 999-888-1561 | 190V201 | JEF-GATO Clinic Fax         |      |    2 | fax       |      |    1 |      1 |           |      |                |      |
|  993 | 111-777-0202 | S00256  | Test Rm                     |      |    2 | 9608      |      |    1 |      4 | IP_Phone  |      | 172.20.190.45  | 303  |
|  994 | 111-777-0212 | S00217  | Test Rm  2                  |      |    2 | 9608      |      |    1 |      4 | IP_Phone  |      | 172.20.190.46  | 303  |

查询1

SELECT nr, 
        sum(type='9608') '9608', 
        sum(type='9611') '9611', 
        sum(type='b189') 'b189', 
        count(*) 'Total IP' 
FROM station GROUP BY nr;

结果1

+------+------+------+------+----------+
| nr   | 9608 | 9611 | b189 | Total IP |
+------+------+------+------+----------+
| 308  |    1 |    0 |   1  |        2 |
| 490  |    0 |    1 |   0  |        1 |
| 303  |    2 |    0 |   0  |        2 |

查询2

SELECT SUBSTR(port, 1, 3) AS 'nr', 
        sum(type='fax') 'fax', 
        sum(type='2500') 'analog', 
        sum(type='cordless') 'cordless', 
        count(*) 'Total Analog' 
FROM station 
WHERE port LIKE '___V%' 
GROUP BY SUBSTR(port, 1, 3);

结果2

+------+------+--------+----------+--------------+
| nr   | fax  | analog | cordless | Total Analog |
+------+------+--------+----------+--------------+
| 008  |    0 |      0 |        1 |            1 |
| 190  |    1 |      1 |        0 |            2 |

所需的结果是

+------+------+------+------+----------+------+--------+----------+--------------+
| nr   | 9608 | 9611 | b189 | Total IP | fax  | analog | cordless | Total Analog |
+------+------+------+------+----------+------+--------+----------+--------------+
| 308  |    1 |    0 |   1  |        2 |    0 |      0 |        1 |            1 |
| 490  |    0 |    1 |   0  |        1 |    1 |      1 |        0 |            2 |
| 303  |    2 |    0 |   0  |        2 |    0 |      0 |        0 |            0 |

1 个答案:

答案 0 :(得分:0)

这是您需要的吗?我使用了内部联接。检查我的答案。

SELECT s1.nr, 
        sum(type='9608') '9608', 
        sum(type='9611') '9611', 
        sum(type='b189') 'b189', 
        count(*) 'Total IP' ,
        fax,
        analog,
        cordless,
        TotalAnalog
FROM station s1
INNER JOIN (
  SELECT SUBSTR(port, 1, 3) AS 'nr', 
          sum(type='fax') 'fax', 
          sum(type='2500') 'analog', 
          sum(type='cordless') 'cordless', 
          count(*) 'TotalAnalog' 
  FROM station 
  WHERE port LIKE '___V%' 
  GROUP BY SUBSTR(port, 1, 3)
) as s2
ON s1.nr = s2.nr + 300
GROUP BY nr;

工作Fiddle