按日期分组多个表

时间:2016-01-15 17:58:30

标签: sql postgresql join group-by union

(我正在使用postgres)

我从不同的表中得到了两个查询。每个按日期分组。 将它们连接到一个查询(UNION或JOIN)的最佳方法是什么? 提前谢谢。

查询№1:

SELECT to_char(date,'MM') as mon,
       extract(year from date) as yyyy,
       SUM("table_1"."user_money") AS user_sum,
       SUM("table_1"."system_money") AS system_sum
FROM "table_1"
GROUP BY 1,2
ORDER BY 2,1;

查询结果№1:

 mon | yyyy |     user_sum     |   system_sum    
-----+------+------------------+-----------------
 11  | 2015 |        10        |       50        
 12  | 2015 |        20        |       60        
(2 rows)

查询№2:

SELECT to_char(created_at,'MM') as mon,
       extract(year from created_at) as yyyy,
       SUM("table_2"."amount") AS payments_sum
FROM "table_2"
GROUP BY 1,2
ORDER BY 2,1;

查询结果2:

 mon | yyyy | payments_sum
-----+------+--------------     
 10  | 2015 |     500      
 11  | 2015 |     600      
 12  | 2015 |     700      
 01  | 2016 |     800      
(4 rows)

必填结果:

 mon | yyyy | payments_sum |     user_sum     |   system_sum   
-----+------+--------------+------------------+----------------
 10  | 2015 |     500      |                  |                
 11  | 2015 |     600      |        10        |       50       
 12  | 2015 |     700      |        20        |       60      
 01  | 2016 |     800      |                  |                
(4 rows)

3 个答案:

答案 0 :(得分:0)

试试这个。

SELECT 
    to_char(date,'MM') as mon,
    extract(year from date) as yyyy,
    SUM("table_2"."amount") AS payments_sum,
    SUM("table_1"."user_money") AS user_sum,
    SUM("table_1"."system_money") AS system_sum
FROM 
    "table_1"
LEFT JOIN 
    "table_2" ON (to_char("table_1"."date", 'MM') = to_char("table_2"."date", 'MM') AND extract(year from "table_1"."date") = extract(year from "table_2"."date"))
GROUP BY 1,2
ORDER BY 2,1;

答案 1 :(得分:0)

我认为您可以将每个查询视为子查询,然后与完整的外部联接相结合。联盟可能不是你想要的。

这样的事情:

SELECT * FROM
(SELECT to_char(date,'MM') as mon,
   extract(year from date) as yyyy,
   SUM("table_1"."user_money") AS user_sum,
   SUM("table_1"."system_money") AS system_sum
FROM "table_1"
GROUP BY 1,2) AS q1
FULL OUTER JOIN 
(SELECT to_char(created_at,'MM') as mon,
   extract(year from created_at) as yyyy,
   SUM("table_2"."amount") AS payments_sum
FROM "table_2"
GROUP BY 1,2) AS q2
ON q1.mon = q2.mon AND q1.yyyy = q2.yyyy
ORDER BY 2,1

加入mon和yyyy值匹配的地方。

答案 2 :(得分:0)

这是PostgreSQL特定语法中的另一个:

WITH query_one AS (
    SELECT 
        to_char(date,'MM') as mon,
        extract(year from date) as yyyy,
        SUM("table_1"."user_money") AS user_sum,
        SUM("table_1"."system_money") AS system_sum
    FROM 
        "table_1"
    GROUP BY 
        1,2
), query_two AS (
    SELECT 
        to_char(created_at,'MM') as mon,
        extract(year from created_at) as yyyy,
        SUM("table_2"."amount") AS payments_sum
    FROM 
        "table_2"
    GROUP BY 
        1,2
)
SELECT
    *
FROM
    query_one
    LEFT JOIN query_two USING (mon, yyyy)
ORDER BY 
    yyyy
    , mon;

USING子句更易于使用,并阻止monyyyy在输出中两次,其余只是为了清晰视图。