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

  • Home
  • SEARCH
  • 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 689187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:18:23+00:00 2026-05-14T02:18:23+00:00

I have a large database and am putting together a report of the data.

  • 0

I have a large database and am putting together a report of the data. I have aggregated and summed the data from many tables to get two tables that look like the following.

id | code | value          id | code | value
13 |  AA  | 0.5            13 |  AC  | 2.0
13 |  AB  | 1.0            14 |  AB  | 1.5
14 |  AA  | 2.0            13 |  AA  | 0.5
15 |  AB  | 0.5            15 |  AB  | 3.0
15 |  AD  | 1.5            15 |  AA  | 1.0

I need to get a list of id’s, with the code (sumed from both tables) with the largest value.

13 |  AC
14 |  AA
15 |  AB

There are 4-6 thousand records and it is not possible to change the original tables. I’m not too worried about performance as I only need to run it a few times a year.

edit:
Let me see if I can explain a bit more clearly, imagine the id is the customer id, the code is who they ordered from and the value is how much they spent there.

I need a list of the all the customer id’s and the store that customer spent the most money at (and if they spent the same at two different stores, put a value such as ‘ZZ’ in for the store name).

  • 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-14T02:18:24+00:00Added an answer on May 14, 2026 at 2:18 am

    try this:

    DECLARE @Table1 table (id int, code char(2), value decimal(5,1))
    INSERT @Table1 VALUES (13 ,  'AA'  , 0.5)
    INSERT @Table1 VALUES (13 ,  'AB'  , 1.0)
    INSERT @Table1 VALUES (14 ,  'AA'  , 2.0)
    INSERT @Table1 VALUES (15 ,  'AB'  , 0.5)
    INSERT @Table1 VALUES (15 ,  'AD'  , 1.5)
    
    DECLARE @Table2 table (id int, code char(2), value decimal(5,1))
    INSERT @Table2 VALUES (13 ,  'AC'  , 2.0)
    INSERT @Table2 VALUES (14 ,  'AB'  , 1.5)
    INSERT @Table2 VALUES (13 ,  'AA'  , 0.5)
    INSERT @Table2 VALUES (15 ,  'AB'  , 3.0)
    INSERT @Table2 VALUES (15 ,  'AA'  , 1.0)
    
    SELECT
         dt.id, MAX(dt.code) AS code, sum(dt.value) as value
        from (select id, code, value
                  from @Table1
                  UNION ALL
                  select
                      id, code, value
                      from @Table2
             ) dt
            group by dt.id
            order by id
    

    OUTPUT:

    id          code value
    ----------- ---- ---------------------------------------
    13          AC   4.0
    14          AB   3.5
    15          AD   6.0
    
    (3 row(s) affected)
    

    I’m not sure what you are after? this is the MAX code per id summing the value. if this is not what you are after please specify i nthe question more clearly

    EDIT after OP’s edit, using same tables as code from above:

    ;WITH AllTAbles AS
    (select 
         id, code, value
         from @Table1
     UNION ALL
     select
         id, code, value
         from @Table2
    )
    , MaxValues AS
    (SELECT
         dt.id, MAX(dt.value) as MaxValue, SUM(dt.value) AS SumValue
         from AllTAbles dt
         group by dt.id
    )
    , StoreCount AS
    (SELECT
         a.id,a.Code, COUNT(*) AS StoreCount
         FROM AllTAbles            a
             INNER JOIN MaxValues  m ON a.id=m.id AND a.value=m.MaxValue
         GROUP BY a.id,a.Code
    )
    SELECT
        s.id
            ,CASE
                 WHEN s.StoreCount=1 THEN s.Code
                 ELSE 'ZZ'
             END AS code
            ,m.SumValue
        FROM StoreCount           s
            INNER JOIN MaxValues  m ON s.id=m.id
        ORDER BY s.id
    

    OUTPUT:

    id          code SumValue
    ----------- ---- ----------
    13          AC   4.0
    14          AA   3.5
    15          AB   6.0
    
    (3 row(s) affected)
    

    OP doesn’t say the version of SQL Server, so here is a pre SQL Server 2005 version that does not use CTEs, has same output as the CTE version above:

    SELECT
        s.id
            ,CASE
                 WHEN s.StoreCount=1 THEN s.Code
                 ELSE 'ZZ'
             END AS code
            ,s.SumValue
        FROM (SELECT           
                  a.id,a.Code, COUNT(*) AS StoreCount, m.SumValue
                  FROM (select 
                            id, code, value
                            from @Table1
                        UNION ALL
                        select
                            id, code, value
                            from @Table2
                       ) a
                      INNER JOIN (SELECT
                                      dt.id, MAX(dt.value) as MaxValue, SUM(dt.value) AS SumValue
                                      from (select 
                                                id, code, value
                                                from @Table1
                                            UNION ALL
                                            select
                                                id, code, value
                                                from @Table2
                                           ) dt
                                      group by dt.id
                                 ) m ON a.id=m.id AND a.value=m.MaxValue
                  GROUP BY a.id,a.Code,m.SumValue
             ) s
        ORDER BY s.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large database with two tables: stat and total. The example of
I have a large database with over 150 tables that I've recently been handed.
I have a large database that were used to archive tables before implementing structural
I have a large database of users (~200,000) that I'm transferring from a ASP.NET
I have a large database of normalized order data that is becoming very slow
We have a large database with hundreds of tables that have lookup text stored
I have a large database that contains many urls, there are many domains repeating
I have a large database (90GB data, 70GB indexes) that's been slowly growing for
I have one large database table of request data, much like Apache request logs,
We have a large database with a lot of data in it. I found

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.