这个组合预期结果的SQL查询 - 枢轴?

时间:2012-08-29 07:17:43

标签: sql database

很抱歉这个冗长的问题,但我不知道如何构建我想要的结果所需的SQL查询。我将概述我目前运行的两个查询并正常工作,我将概述我需要的结果。任何帮助将不胜感激。

第一次查询:

SELECT c.name AS name, count(*) AS total, sum(a.views) AS total_views, sum(a.views) / count(*) as average_views
FROM table_a a
JOIN table_b b ON b.id = a.b_id
JOIN table_c c ON c.id = b.c_id
WHERE a.status = 0 AND a.type in (2, 4, 5)
GROUP BY c.name ORDER BY c.name;

结果:

--------------------------------------------
name | total | total_views | average_views |
--------------------------------------------
aaaa |     2 |         150 |            75 |
bbbb |     1 |          75 |            75 |
dddd |     1 |          25 |            25 |
--------------------------------------------

第二次查询:

SELECT c.name AS name, count(*) AS total, sum(a.views) AS total_views, sum(a.views) / count(*) as average_views
FROM table_a a
JOIN table_b b ON b.id = a.b_id
JOIN table_c c ON c.id = b.c_id
WHERE a.status = 0 AND a.type in (1, 3)
GROUP BY c.name ORDER BY c.name;

第二个结果:

--------------------------------------------
name | total | total_views | average_views |
--------------------------------------------
aaaa |     2 |         200 |           100 |
bbbb |     1 |         100 |           100 |
dddd |     1 |          25 |            25 |
--------------------------------------------

根据这些数据给出这些表格: 表table_a:

-----------------------------------
id | b_id | views | type | status |
-----------------------------------
 1 |  100 |   100 |    2 |      0 |
 2 |  200 |    75 |    4 |      0 |
 3 |  300 |    50 |    5 |      0 |
 4 |  400 |    25 |    2 |      0 |
 5 |  500 |   100 |    1 |      0 |
 6 |  600 |   100 |    1 |      0 |
 7 |  700 |   100 |    3 |      0 |
 8 |  800 |    25 |    3 |      0 |
-----------------------------------

表table_b:

-------------
id  | c_id  |
-------------
100 |  1000 |
200 |  2000 |
300 |  1000 |
400 |  4000 |
500 |  1000 |
600 |  2000 |
700 |  4000 |
800 |  1000 |
-------------

表table_c:

-------------
id   | name |
-------------
1000 | aaaa |
2000 | bbbb |
3000 | cccc |
4000 | dddd |
-------------

这是我真正想要的表,这只是上面两个表的一致,公共列是名称列。

-------------------------------------------------------------------------------------------------------------------------------
name | total_type245 | total_views_type245 | average_views_type245 | total_type13 | total_views_type13 | average_views_type13 |
-------------------------------------------------------------------------------------------------------------------------------
aaaa |             2 |                 150 |                    75 |            2 |                200 |                  100 |
bbbb |             1 |                  75 |                    75 |            1 |                100 |                  100 |
dddd |             1 |                  25 |                    25 |            1 |                 25 |                   25 |
-------------------------------------------------------------------------------------------------------------------------------

这很可能是一个非常简单的查询,但我无法弄清楚如何去做。

感谢。

2 个答案:

答案 0 :(得分:0)

只需将结果加在一起;

SELECT ResultsA.name, 
total_type245, 
total_views_type245, 
average_views_type245,
total_type13,
total_views_type13,
average_views_type13

FROM
(
    SELECT c.name AS name, count(*) AS total_type245, sum(a.views) AS total_views_type245, sum(a.views) / count(*) as average_views_type245
    FROM table_a a
    JOIN table_b b ON b.id = a.b_id
    JOIN table_c c ON c.id = b.c_id
    WHERE a.status = 0 AND a.type in (2, 4, 5)
    GROUP BY name

) as ResultsA

JOIN 
(
    SELECT c.name AS name, count(*) AS total_type13, sum(a.views) AS total_views_type13, sum(a.views) / count(*) as average_views_type13
    FROM table_a a
    JOIN table_b b ON b.id = a.b_id
    JOIN table_c c ON c.id = b.c_id
    WHERE a.status = 0 AND a.type in (1, 3)
    GROUP BY name

) as ResultsB ON ResultsA.name = ResultsB.name

ORDER BY ResultsA.name

答案 1 :(得分:0)

好的,所以有了Matt的帮助,这个查询有效:

SELECT c.name, total_type245, total_views_type245, average_views_type245, total_type13, total_views_type13, average_views_type13
FROM table_c c
LEFT JOIN (
    SELECT c.name AS name, count(*) AS total_type245, sum(a.views) AS total_views_type245, sum(a.views) / count(*) as average_views_type245
    FROM table_a a
    JOIN table_b b ON b.id = a.b_id
    JOIN table_c c ON c.id = b.c_id
    WHERE a.status = 0 AND a.type in (2, 4, 5)
    GROUP BY name
) as ResultsA ON ResultsA.name = c.name
LEFT JOIN (
    SELECT c.name AS name, count(*) AS total_type13, sum(a.views) AS total_views_type13, sum(a.views) / count(*) as average_views_type13
    FROM table_a a
    JOIN table_b b ON b.id = a.b_id
    JOIN table_c c ON c.id = b.c_id
    WHERE a.status = 0 AND a.type in (1, 3)
    GROUP BY name
) as ResultsB ON ResultsB.name = c.name;

这是对这份工作最有效的查询吗?似乎我重复了很多查询,唯一的变化是a.type值是差异。