2012-08-29 2 views
0

이 긴 질문에 대해 유감스럽게 생각합니다.하지만 원하는 결과를 얻기 위해 필요한 SQL 쿼리를 작성하는 방법을 모르겠습니다. 현재 실행 중이며 정상적으로 작동하는 두 가지 쿼리를 요약하고 필요한 결과를 간략하게 설명합니다. 어떤 도움을 주시면 감사하겠습니다.결합 된 예상 결과에 대한 SQL 쿼리 - 피벗?

1 조회 :

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 | 
-------------------------------------------- 

2 쿼리

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; 

2 결과 :

-------------------------------------------- 
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 | 
------------- 

, 인 위의 두 테이블의 concantenation은 공통 컬럼이 이름 컬럼이됩니다.

------------------------------------------------------------------------------------------------------------------------------- 
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 | 
------------------------------------------------------------------------------------------------------------------------------- 

가장 간단한 쿼리 일 가능성이 높습니다.하지만 어떻게 할 수 있습니까?

감사합니다.

+0

두 쿼리 모두에서 내부 조인을 만들 수 있습니다. –

+0

http://sqlfiddle.com/#!3/aa1d5/2 테스트 여기 – BeNdErR

답변

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 
+0

매트, 고맙습니다. 작동하지만 스패너를 던지기 만하면됩니다. 이름 열 사이에 불일치가 있다면 어떻게 될까요? 즉, 이름 열은 각 결과마다 다를 수 있으므로 이름에 대한 결과가없는 경우 해당 열은 0이어야합니다. 위의 BeNdErR 덕분에 SQL 실행기 앱에 대한 의미는 여기를 참조하십시오. http://sqlfiddle.com/#!1/b819b/1 이름이 eeeee 인 행이 있어야하지만 그렇지 않습니다. 산출. – mangrovejack

+0

자, 이제 테이블 c에서 직접 이름을 선택하고 그 결과를 그대로 남겨 둡니다. 을보십시오. 이것은 MySQL에서 실행되고 있으며, Postgres에서 이상한 예외를 던지고있었습니다. 그리고 그것을 디버그하기위한 포스트그레스에 대해 충분히 알지 못합니다. –

0

, 그래서이 쿼리가 작동합니다

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 값만 변경하면 많은 쿼리가 반복되고있는 것처럼 보입니다.