简体   繁体   English

与 group by (Sum) 的多个联接

[英]Multiple joins with group by (Sum)

在此处输入图像描述

When I using multiple JOIN, I hope to get the sum of some column in joined tables.当我使用多个 JOIN 时,我希望得到连接表中某些列的总和。

SELECT 
    A.*, 
    SUM(C.purchase_price) AS purcchase_total, 
    SUM(D.sales_price) AS sales_total, 
    B.user_name
FROM
    PROJECT AS A 
LEFT JOIN 
    USER AS B ON A.user_idx = B.user_idx 
LEFT JOIN 
    PURCHASE AS C ON A.project_idx = C.project_idx
LEFT JOIN 
    SALES AS D ON A.project_idx = D.project_idx
GROUP BY 
    ????

You need to use subquery as follows:您需要按如下方式使用子查询:

SELECT A.project_idx,
       a.project_name, 
       A.project_category,
       sum(C.purchase_price) AS purcchase_total, 
       sum(D.sales_price) as sales_total,
       B.user_name
FROM PROJECT AS A 
LEFT JOIN USER AS B ON A.user_idx = B.user_idx 
LEFT JOIN (select project_idx, sum(purchase_price) as purchase_price 
             from PURCHASE group by project_idx ) AS C ON A.project_idx = C.project_idx
LEFT JOIN (select project_idx, sum(sale_price) as sale_price 
             from SALES group by project_idx) AS D ON A.project_idx = D.project_idx

I am not sure but you can use inner join of project with user instead of left join .我不确定,但您可以将projectinner joinuser一起使用,而不是left join

SELECT A.project_idx,
       a.project_name, 
       A.project_category,
       purcchase_total, 
       sales_total,
       B.user_name
FROM PROJECT AS A 
LEFT JOIN USER AS B ON A.user_idx = B.user_idx 
LEFT JOIN (select project_idx, sum(purchase_price) as purchase_total 
             from PURCHASE group by project_idx ) AS C ON A.project_idx = C.project_idx
LEFT JOIN (select project_idx, sum(sale_price) as sale_total 
             from SALES group by project_idx) AS D ON A.project_idx = D.project_idx

This is working correctly on MS-SQL Server.这在 MS-SQL Server 上正常工作。 Thanks to Popeye感谢大力水手

You are attempting to aggregate over two unrelated dimensions, and that throws off all the calculations.您正在尝试聚合两个不相关的维度,这会引发所有计算。

Correlated subqueries are an alternative:相关子查询是一种替代方法:

SELECT p.*, 
       (SELECT SUM(pu.purchase_price)
        FROM PURCHASE pu
        WHERE p.project_idx = pu.project_idx
       ) as purchase_total,
       (SELECT SUM(s.sales_price)
        FROM SALES s
        WHERE p.project_idx = s.project_idx
       ) as sales_total, 
       u.user_name
FROM PROJECT p LEFT JOIN
     USER u
     ON p.user_idx = u.user_idx ;

Note that this uses meaningful table aliases so the query is easier to read.请注意,这使用有意义的表别名,因此查询更易于阅读。 Arbitrary letters are really no better (and perhaps worse) than using the entire table name.任意字母实际上并不比使用整个表名更好(甚至可能更糟)。

Correlated subqueries avoid the outer aggregation as well -- and let you select all the columns from the first table, which is what you want.相关子查询也避免了外部聚合——让你 select 第一个表中的所有列,这就是你想要的。 They also often have better performance with the right indexes.使用正确的索引,它们通常也有更好的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM