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

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

在这种情况下,练习如下:

假设每个网点(点)的钱的收入(inc)和支出(out)一天登记任意次,得到一个包含字段的结果集:网点、日期、费用、收入。

请注意,单个记录必须对应于每个日期的每个出口。

使用收入和结果表。

4

6 回答 6

1

试试这个代码

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

也许这会成功?

于 2013-08-15T19:14:06.003 回答
0

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

于 2013-08-15T19:09:22.073 回答
0

我看不出你想要达到什么目标,但在我看来你需要的是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
于 2013-08-15T19:14:58.400 回答
-1

如果我正确理解您的问题,您可以使用 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]
于 2013-08-15T19:08:32.820 回答
-1

SELECT A.* FROM (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 , 收入. 日期) A

联盟

SELECT B.* FROM (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 , 结果. 日期) B;

于 2013-08-15T19:21:28.883 回答
-1
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
于 2015-01-21T07:58:46.707 回答