Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6623277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:30:50+00:00 2026-05-25T21:30:50+00:00

I need to compare the final balances of two tables from different clients grouped

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:30:51+00:00Added an answer on May 25, 2026 at 9:30 pm

    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]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to compare the final balances of two tables from different clients grouped
I need compare two date. One is from databese, second is from now. var
I need to compare particular content of 2 SQL tables located in different servers:
I need to compare dozens of fields in two objects (instances of the same
I have two xml config files, I need compare ONLY the structure of the
I need to compare two columns in a sql table. The data in one
I have Oracle files that I need to compare to CVS files, but the
I need to compare two Unix timestamps and I'm having trouble with the math.
I need to compare two dictionary values if the types stored are equal, this
I need to compare a key object from a div to a string. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.