JOIN之前的SQL GROUP BY - 查询时的序列

时间:2013-08-15 19:01:18

标签: sql

SELECT 
    Income.point, Income.date, SUM(out), SUM(inc)
FROM 
    Income 
LEFT JOIN
    Outcome ON Income.point = Outcome.point 
           AND Income.date = Outcome.date
GROUP BY 
    Income.point, Income.date

UNION

SELECT 
    Outcome.point, Outcome.date, SUM(out), SUM(inc)
FROM 
    Outcome 
LEFT JOIN
    Income ON Income.point = Outcome.point 
           AND Income.date = Outcome.date
GROUP BY 
    Outcome.point, Outcome.date;

我有这个代码,我想要做的是在加入之前分组。 “假设我们有一个包含连接和分组的SQL查询。评估此类查询的标准方法是首先执行所有连接,然后执行分组操作。但是,可以执行组 - 早期,即推动分组操作超过一个或多个连接。早期分组可以通过减少参与连接的数据量来降低查询处理成本。“ 所以我需要解释如何做到这一点

在这种情况下,

运动如下:

  

假设每个出口(点)的钱的收入(inc)和费用(out)每天登记任意次数,得到的结果集包括:出口,日期,费用,收入。

     

请注意,单个记录必须与每个日期的每个商店对应。

     

使用收入和结果表。

6 个答案:

答案 0 :(得分:2)

试试此代码

SELECT ip,id,ii,oo FROM
(SELECT I.point ip, I.date id, SUM(I.inc) ii FROM Income I GROUP BY I.point, I.date ) in1
LEFT JOIN
(SELECT O.point op, O.date od, SUM(O.out) oo FROM Outcome O GROUP BY O.point, O.date ) ou1 
ON op=ip AND od=id

UNION

SELECT ip,id,ii,oo FROM
(SELECT I.point ip, I.date id, SUM(I.inc) ii FROM Income I GROUP BY I.point, I.date ) in1
RIGHT JOIN
(SELECT O.point op, O.date od, SUM(O.out) oo FROM Outcome O GROUP BY O.point, O.date ) ou1 
ON op=ip AND od=id

也许有人可以给它起个名字。我甚至不知道你如何在括号中称这些SELECTS ......: - /

修改

那么,考虑到Luis LL的想法并将其与“早期分组”相结合,可以获得以下内容:

SELECT COALESCE(ip,op) point,COALESCE(id,od) date,ii inc,oo out FROM
(SELECT point ip, date id, SUM(inc) ii FROM Income GROUP BY point, date ) in1
FULL OUTER JOIN
(SELECT point op, date od, SUM(out) oo FROM Outcome GROUP BY point, date ) ou1 
ON op=ip AND od=id

也许这会成功吗?

答案 1 :(得分:1)

我不知道你想要达到什么目标,但在我看来,你需要的是FULL OUTER JOIN

SELECT 
    ISNULL(Income.point, OutCome.point) AS Point, ISNULL(Income.date, OutComeDate) AS [date], SUM(out) AS Expenses, SUM(inc) AS Income
FROM Income 
FULL OUTER JOIN Outcome 
    ON Income.point = Outcome.point AND Income.date = Outcome.date
GROUP BY Income.point, Income.date

答案 2 :(得分:0)

如果我正确理解您的问题,您可以使用CTE。

WITH CTE
AS
(
    SELECT [point]
    ,      [date]
    ,      [SumOut] = SUM([out])
    ,      [SumInc] = SUM([inc])
    FROM Income 
    GROUP BY [point], [date]
)

SELECT CTE.[Point]
,      CTE.[Date]
,      CTE.[SumOut]
,      CTE.[SumInc]
FROM CTE
LEFT JOIN Outcome ON CTE.[point] = Outcome.point AND CTE.[date] = Outcome.[date]

答案 3 :(得分:0)

假设您需要在2个单独的表中注册此数据(不是最优雅的方式),您可以使用子查询。 UNION可能不是这样做的,因为它不会将每个“点”的数据集放在一个记录中。

答案 4 :(得分:0)

SELECT A. * FROM (SELECT Income.point,Income.date,SUM(out),SUM(inc) 从     收入 LEFT JOIN     收入来自Income.point = Outcome.point            AND Income.date = Outcome.date 通过...分组     Income.point,Income.date)A

UNION

SELECT B. * FROM (SELECT Outcome.point,Outcome.date,SUM(out),SUM(inc) 从     结果 LEFT JOIN     收入ON Income.point = Outcome.point            AND Income.date = Outcome.date 通过...分组     Outcome.point,Outcome.date)B; <​​/ p>

答案 5 :(得分:0)

Select  ip,id,oo,ii from(
Select i.point as ip,i.date as id,sum(i.inc) as ii from Income as I group BY i.point, i.date) in1

Left join (
Select o.point as op, o.date as od,sum(o.out) as oo from outcome as O group by o.point, o.date) ou1
on ip=op and id=od

union

Select  op,od,oo,ii from(
Select o.point as op, o.date as od,sum(o.out) as oo from outcome as O group by o.point, o.date) ou2

Left join (
Select i.point as ip,i.date as id,sum(i.inc) as ii from Income as I group BY i.point, i.date) in2
on ip=op and id=od