在视图的结果集中返回具有非聚合列的聚合列的最佳方法是什么

发布于 2024-12-07 14:26:35 字数 951 浏览 4 评论 0原文

我需要比较按 ID 分组的不同客户的两个表的最终余额。这些表具有相同的 ID,但一个表有多个 ID 条目,而另一个表则没有。我需要将表中的行与多个条目相加,因此我只有一个最终数字可以进行计算。

表 1:

ID,    cost,    traceNumber,   TheDate
1,     200,     1001,          10/07/2011
1,     -20,     1002,          10/08/2011
1,     130,     1003,          10/10/2011
2,     300,     1005,          10/10/2011

表 2:

ID,    cost
1,     200
2,     300

ID 1 的结果为 310,与表 2 的 200 相比,相差 110

查询将如下所示。

SELECT DISTINCT
    Table1.ID,
    Table1.TheDate ,
    Table1.traceNumber,
    Table1.[cost] AS Frost_Balance, 
    SUM(Table1.[cost]) AS SUM_Frost_Balance, 
    Table2.[cost] AS Ternean_Balance, 
    SUM(Table1.[cost]) - Table2.[cost] AS Ending_Balance, 
FROM  
    Table1 
    INNER JOIN Table2 ON Table1.ID =Table2.CustomerID 
GROUP BY 
    dbo.Frost.ID

查询必须在结果集中显示多个列,因为它将是用于报告。我尝试按结果集中的所有列进行分组,但这给了我错误的结果。还有另一种方法来计算需要求和的列吗?

I need to compare the final balances of two tables from different clients grouped by ID. The tables have the same ID's But one has multiple ID entries and the other does not. I need to sum row from the table with multiple entries so I have just one final number to do calculations with.

Table1:

ID,    cost,    traceNumber,   TheDate
1,     200,     1001,          10/07/2011
1,     -20,     1002,          10/08/2011
1,     130,     1003,          10/10/2011
2,     300,     1005,          10/10/2011

Table2:

ID,    cost
1,     200
2,     300

Result for ID 1, would be 310 compared to table2 of 200 with a difference of 110

The Query will look something like this.

SELECT DISTINCT
    Table1.ID,
    Table1.TheDate ,
    Table1.traceNumber,
    Table1.[cost] AS Frost_Balance, 
    SUM(Table1.[cost]) AS SUM_Frost_Balance, 
    Table2.[cost] AS Ternean_Balance, 
    SUM(Table1.[cost]) - Table2.[cost] AS Ending_Balance, 
FROM  
    Table1 
    INNER JOIN Table2 ON Table1.ID =Table2.CustomerID 
GROUP BY 
    dbo.Frost.ID

The query has to display multiple columns in the result set because it is going to be used to report on. I tried grouping by all columns in the result set but that gave me wrong results. Is there another way to compute the column that needs to be summed up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

强者自强 2024-12-14 14:26:35

您可以使用“窗口聚合函数”,如下所示:

select 
    table1.*,
    sum(table1.cost) over (partition by table1.id) sum_frost_balance,
    table2.cost,
    sum(table1.cost) over (partition by table1.id) - table2.cost ending_balance
from table1
    join table2 on table1.id = table2.id

You can use a "Window Aggregate Function" like this:

select 
    table1.*,
    sum(table1.cost) over (partition by table1.id) sum_frost_balance,
    table2.cost,
    sum(table1.cost) over (partition by table1.id) - table2.cost ending_balance
from table1
    join table2 on table1.id = table2.id
骄傲 2024-12-14 14:26:35

这就是你想做的吗?

-- Data setup
CREATE TABLE [Table1]
(
    [ID] INT,
    [cost] INT,
    [traceNumber] INT,
    [TheDate] DATE
)

INSERT [Table1]
VALUES  (1, 200, 1001, '10/07/2011'),
        (1, -20, 1002, '10/08/2011'),
        (1, 130, 1003, '10/10/2011'),
        (2, 300, 1005, '10/10/2011')

CREATE TABLE [Table2]
(
    [ID] INT,
    [cost] INT
)

INSERT [Table2]
VALUES  (1, 200),
        (2, 300)

-- Query
;WITH [cteTable1Sum] AS
(
    SELECT [ID], SUM([cost]) AS [cost]
    FROM [Table1]
    GROUP BY [ID]
)       
SELECT  [Table1].[ID], 
        [Table1].[TheDate], 
        [Table1].[traceNumber],
        [Table1].[cost] AS [Frost_Balance],
        cte.[cost] AS [SUM_Frost_Balance],
        [Table2].[cost] AS [Ternean_Balance],
        cte.[cost] - [Table2].[cost] AS [Ending_Balance]
FROM [Table1]
INNER JOIN [Table2]
    ON [Table1].[ID] = [Table2].[ID]
INNER JOIN [cteTable1Sum] cte
    ON [Table1].[ID] = cte.[ID]

Is this what you're trying to do?

-- Data setup
CREATE TABLE [Table1]
(
    [ID] INT,
    [cost] INT,
    [traceNumber] INT,
    [TheDate] DATE
)

INSERT [Table1]
VALUES  (1, 200, 1001, '10/07/2011'),
        (1, -20, 1002, '10/08/2011'),
        (1, 130, 1003, '10/10/2011'),
        (2, 300, 1005, '10/10/2011')

CREATE TABLE [Table2]
(
    [ID] INT,
    [cost] INT
)

INSERT [Table2]
VALUES  (1, 200),
        (2, 300)

-- Query
;WITH [cteTable1Sum] AS
(
    SELECT [ID], SUM([cost]) AS [cost]
    FROM [Table1]
    GROUP BY [ID]
)       
SELECT  [Table1].[ID], 
        [Table1].[TheDate], 
        [Table1].[traceNumber],
        [Table1].[cost] AS [Frost_Balance],
        cte.[cost] AS [SUM_Frost_Balance],
        [Table2].[cost] AS [Ternean_Balance],
        cte.[cost] - [Table2].[cost] AS [Ending_Balance]
FROM [Table1]
INNER JOIN [Table2]
    ON [Table1].[ID] = [Table2].[ID]
INNER JOIN [cteTable1Sum] cte
    ON [Table1].[ID] = cte.[ID]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文