左联接两个表,并按两个表之间的公共ID以及所有三个表之间的公共ID进行分组

时间:2018-09-16 21:24:13

标签: sql sql-server join group-by

我有三个表(其中两个实际上是“视图”)。我想离开联接表2和3,并按它们的两个公用列对其进行分组,然后进行一些计数。我整夜都在挣扎,我正要把笔记本电脑丢到窗外

这是我的三个桌子:

+-----------------------+
| Features              |
+-----------------------+
| featureId | title     |
+-----------+-----------+
| feature1  | Feature 1 |
+-----------+-----------+
| feature2  | Feature 2 |
+-----------+-----------+
| feature3  | Feature 2 |
+-----------+-----------+


+--------------------------+--------+
| Tasks                    |        |
+--------------------------+--------+
| featureId | workstreamId | title  |
+-----------+--------------+--------+
| feature1  | workstream1  | Task 1 |
+-----------+--------------+--------+
| feature2  | workstream2  | Task 2 |
+-----------+--------------+--------+
| feature2  | workstream3  | Task 3 |
+-----------+--------------+--------+
| feature2  | workstream3  | Task 3 |
+-----------+--------------+--------+

+--------------------------+-------+
| Jobs                     |       |
+--------------------------+-------+
| featureId | workstreamId | title |
+-----------+--------------+-------+
| feature3  | workstream1  | Job 1 |
+-----------+--------------+-------+
| feature2  | workstream2  | Job 2 |
+-----------+--------------+-------+
| feature2  | workstream3  | Job 3 |
+-----------+--------------+-------+

这是我想要得到的结果:

+-----------+--------------+-----------+----------+
| featureId | workstreamId | taskCount | jobCount |
+-----------+--------------+-----------+----------+
| feature1  | workstream1  | 1         | 0        |
+-----------+--------------+-----------+----------+
| feature2  | workstream2  | 1         | 1        |
+-----------+--------------+-----------+----------+
| feature2  | workstream3  | 2         | 1        |
+-----------+--------------+-----------+----------+
| feature3  | workstream1  | 0         | 1        |
+-----------+--------------+-----------+----------+

我想按featureId和workstreamId分组-此组合在结果中应该是唯一的。我觉得这应该真的很容易,但我只是想不通。

这是我到目前为止所拥有的:

SELECT 
    f.title, 
    t.workstreamId,
    t.taskCount,
    j.jobCount
FROM features f
LEFT JOIN (
        SELECT  
                featureId, 
                workstreamId,
                COUNT(workstreamId) AS taskCount
        FROM tasks
        GROUP BY workstreamId, featureId
    ) AS t
ON t.featureId = f.featureId
LEFT JOIN 
    (
        SELECT  
                featureId,
                workstreamId,
                COUNT(workstreamId) AS jobCount
        FROM jobs
        GROUP BY featureId, workstreamId
    ) AS j
ON j.featureId = f.featureId

欢迎任何帮助:-)

3 个答案:

答案 0 :(得分:1)

最简单的方法是将features连接到tasksjobs表的UNION上,并对不同的标题进行COUNTS。诀窍在于,在UNION中,NULL表中的任务标题为tasks,而NULL表中的任务标题为jobs,因此允许正确添加计数对于每种类型:

SELECT f.featureId, u.workstreamId, COUNT(u.tasktitle) AS taskCount, COUNT(u.jobtitle) AS jobCOunt
FROM features f
JOIN (SELECT featureId, workstreamId, 'Task' AS tasktitle, NULL AS jobtitle
      FROM tasks
      UNION ALL
      SELECT featureId, workstreamId, NULL AS tasktitle, 'Job' AS jobtitle
      FROM jobs) u
ON u.featureId = f.featureId
GROUP BY f.featureId, u.workstreamId
ORDER BY f.featureId

输出:

featureId   workstreamId    taskCount   jobCOunt
feature1    workstream1     1           0
feature2    workstream2     1           1
feature2    workstream3     2           0
feature3    workstream1     0           1

SQLFiddle Demo

答案 1 :(得分:0)

您需要按function get_price() { $url = "https://steamcommunity.com/market/priceoverview/?country=PL&currency=3&appid=730&callback=?&market_hash_name=Operation%20Vanguard%20Weapon%20Case"; $json = file_get_contents($url); $price = json_decode($json); $price_case = $price->{"lowest_price"}; return number_format($price_case, 2); } Tasks表创建完整表。

下一步在完整表的基础上为JobsLEFT JOIN编写两个Tasks子查询,然后获取Jobs

count

sqlfiddle

答案 2 :(得分:0)

FULL JOIN首先查询子查询,然后LEFT JOIN将结果查询到features

SELECT f.featureid,
       coalesce(t.workstreamid, j.workstreamid) workstreamid,
       coalesce(t.count, 0) taskcount,
       coalesce(j.count, 0) jobcount
       FROM features f
            LEFT JOIN ((SELECT featureid,
                               workstreamid,
                               count(*) count
                               FROM tasks
                               GROUP BY featureid,
                                        workstreamid) t
                       FULL JOIN (SELECT featureid,
                                         workstreamid,
                                         count(*) count
                                         FROM jobs
                                         GROUP BY featureid,
                                                  workstreamid) j
                                 ON t.featureid = j.featureid
                                    AND t.workstreamid = j.workstreamid)
                      ON coalesce(t.featureid, j.featureid)  = f.featureid;

db<>fiddle

当标题可为空并且包含空值时,它也可以工作:db<>fiddle