如何在sqlite中总结一个联接表?

时间:2019-04-12 04:17:40

标签: sql sqlite

如果我想对联接表中的人数列求和怎么办?

我尝试使用此查询:

SELECT A.Job, A.Site, count(B.Job) HeadCount FROM tble_1 A LEFT join tble_2 B ON A.Job = B.Job AND A.Site = B.Site GROUP BY A.Job

RESULT:
+----------------+----------------+----------------+
| Jobs           | Site           | Headcount      |
+----------------+----------------+----------------+
|       Doctor   |             US |             10 |
+----------------+----------------+----------------+
|       Artist   |         Mexico |             10 | 
+----------------+----------------+----------------+
|       Doctor   |          Japan |             10 | 
+----------------+----------------+----------------+
|       Doctor   |        Germany |             10 | 
+----------------+----------------+----------------+
|       Doctor   |         Russia |             10 | 
+----------------+----------------+----------------+
|       Actor    |          India |             10 | 
+----------------+----------------+----------------+

如何获得此结果:

+----------------+----------------+----------------+----------------+
| Jobs           | Site           | Headcount      | Total          |
+----------------+----------------+----------------+----------------+
|       Doctor   |             US |             10 |             30 |
+----------------+----------------+----------------+----------------+
|       Artist   |         Mexico |             10 |             10 |
+----------------+----------------+----------------+----------------+
|       Doctor   |          Japan |             10 |             30 |
+----------------+----------------+----------------+----------------+
|       Doctor   |        Germany |             10 |             30 |
+----------------+----------------+----------------+----------------+
|       Actor    |         Russia |             10 |             20 |
+----------------+----------------+----------------+----------------+
|       Actor    |          India |             10 |             20 |
+----------------+----------------+----------------+----------------+

注意:总数栏是人员总数。

3 个答案:

答案 0 :(得分:0)

使用标量子查询

SELECT A.Job, A.Site, count(B.Job) HeadCount,
(select count(*) FROM tble_1 A1 where A.job=A1.job) as total
FROM tble_1 A LEFT join tble_2 B ON A.Job = B.Job AND A.Site = B.Site 
GROUP BY A.Job

答案 1 :(得分:0)

我们可以为每个计数尝试使用两个单独的子查询:

SELECT
    t1.Job,
    t1.Site,
    COALESCE(t3.HeadCount, 0) AS HeadCount,
    COALESCE(t2.SiteCount, 0) AS Total
FROM tble_1 t1
LEFT JOIN
(
    SELECT Job, COUNT(*) AS SiteCount
    FROM tble_1 
    GROUP BY Job
) t2
    ON t1.Job = t2.Job
LEFT JOIN
(
    SELECT A.Job, A.Site, COUNT(*) AS HeadCount
    FROM tble_1 A
    LEFT JOIN tble_2 B
        ON A.Job = B.Job AND A.Site = B.Site
    GROUP BY A.Job, A.Site
) t3
    ON t1.Job = t3.Job AND t1.Site = t3.Site;

Demo

(演示数据由@Shawn提供)

答案 2 :(得分:0)

您的示例输出不可能来自帖子中的查询(您只能在一个列上分组,结果是基于在两列上分组;根据您的查询,每个作业应该只有一行),而且没有可供测试的示例数据,因此修复该问题,我认为这将满足您的要求(需要Sqlite 3.25或更高版本):

SELECT a.job AS Job
     , a.site AS Site
     , count(b.job) AS Headcount
     , sum(count(b.job)) OVER (PARTITION BY a.job) AS Total
FROM tble_1 AS a
LEFT JOIN tble_2 AS b ON a.job = b.job AND a.site = b.site
GROUP BY a.job, a.site;

给出以下示例数据:

CREATE TABLE tble_1(job text, site text);
INSERT INTO tble_1 VALUES('Doctor','US');
INSERT INTO tble_1 VALUES('Doctor','Japan');
INSERT INTO tble_1 VALUES('Doctor','Germany');
INSERT INTO tble_1 VALUES('Artist','Mexico');
INSERT INTO tble_1 VALUES('Actor','Russia');
INSERT INTO tble_1 VALUES('Actor','India');
INSERT INTO tble_1 VALUES('Actor','Mexico');
CREATE TABLE tble_2(job text, site text);
INSERT INTO tble_2 VALUES('Doctor','US');
INSERT INTO tble_2 VALUES('Doctor','Japan');
INSERT INTO tble_2 VALUES('Doctor','Germany');
INSERT INTO tble_2 VALUES('Artist','Mexico');
INSERT INTO tble_2 VALUES('Actor','Russia');
INSERT INTO tble_2 VALUES('Actor','India');
INSERT INTO tble_2 VALUES('Doctor','Germany');
CREATE INDEX tble_1_idx ON tble_1(job, site);
CREATE INDEX tble_2_idx ON tble_2(job, site);

这将产生:

Job         Site        Headcount   Total     
----------  ----------  ----------  ----------
Actor       India       1           2         
Actor       Mexico      0           2         
Actor       Russia      1           2         
Artist      Mexico      1           1         
Doctor      Germany     2           4         
Doctor      Japan       1           4         
Doctor      US          1           4