如何在公共表表达式中按两个数据字段进行分组

发布于 2024-12-08 23:11:10 字数 1538 浏览 0 评论 0原文

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

我的查询执行了这么多操作,但现在我还需要将其按月分组。我当前的查询对所有日期进行了总计。数据实际上需要按 ID 和月份排序,以便他们知道每个 ID 的月末余额。有没有办法用我目前所拥有的来做到这一点?

提前致谢。

这是我到目前为止所拥有的。

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

    INSERT [Table1]
    VALUES  (1, 200, 1001, '9/07/2011'),
            (1, -20, 1002, '9/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]

我尝试了这个,但得到了错误的答案。

    WITH [cteTable1Sum] AS 
         ( SELECT [ID], SUM([cost]) 
         AS [cost] FROM [Table1] 
         GROUP BY [ID], [TheDate] )

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.

I have the query that does this much, but now I need to get it to group by month as well. My current query totals it for all dates. The data actually needs to be sorted by ID and by month so they know the ending monthly balance for every ID. Is there a way to do this with what I have so far?

Thanks in advance.

This is what I have so far.

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

    INSERT [Table1]
    VALUES  (1, 200, 1001, '9/07/2011'),
            (1, -20, 1002, '9/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]

I tried this and I get the wrong answer.

    WITH [cteTable1Sum] AS 
         ( SELECT [ID], SUM([cost]) 
         AS [cost] FROM [Table1] 
         GROUP BY [ID], [TheDate] )

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

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

发布评论

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

评论(1

仅一夜美梦 2024-12-15 23:11:10

为此,您可能应该使用 MONTH()YEAR() 函数:

;WITH [cteTable1Sum] AS
    (
        SELECT [ID],
              MONTH(TheDate)  as Mo,
              YEAR(TheDate) as yr,
              SUM([cost]) AS [cost]
        FROM [Table1]
        GROUP BY [ID], MONTH(TheDate), YEAR(TheDate)
    )       

这将为您提供每个 ID 的每月细分。然后将您的 JOIN 更改为:

INNER JOIN [cteTable1Sum] cte
         ON [Table1].[ID] = cte.[ID]
         AND MONTH(Table1.TheDate) = cte.Mo
         AND YEAR(Table1.TheDate) = cte.Yr

You should probably use the MONTH() and YEAR() functions for this:

;WITH [cteTable1Sum] AS
    (
        SELECT [ID],
              MONTH(TheDate)  as Mo,
              YEAR(TheDate) as yr,
              SUM([cost]) AS [cost]
        FROM [Table1]
        GROUP BY [ID], MONTH(TheDate), YEAR(TheDate)
    )       

This will give you the monthly breakdown of each month for each ID. Then change your JOIN to:

INNER JOIN [cteTable1Sum] cte
         ON [Table1].[ID] = cte.[ID]
         AND MONTH(Table1.TheDate) = cte.Mo
         AND YEAR(Table1.TheDate) = cte.Yr
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文