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 8454691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:05:35+00:00 2026-06-10T12:05:35+00:00

I have two tables: TABLE_A TABLE_B Fields: Trans Amend Trans Amend data: 100 0

  • 0

I have two tables:

           TABLE_A                 TABLE_B
Fields:    Trans   Amend           Trans   Amend 
data:      100       0             100      0
           100       1
           110       0         
                                   120      0
                                   120      1
           130       0             130      0 
                                   130      1
           140       0             140      0
           150       0             150      0
           150       1             150      1
           150       2             

What I want is a table (view) that will combine (union) these to tables but will only show the highest Amend for each Trans

Looking for this as the answer:

Fields:    Trans   Amend   
data:      100       1
           110       0
           120       1
           130       1
           140       0 
           150       2   

Then to make it harder, I would like to know if there is a way I can tell from which table the data is coming from. Table A always wins when Record A and Record B are equal
Looking for this as the answer:

Fields:    Trans   Amend    WhichTBL
data:      100       1      Table_A
           110       0      Table_A
           120       1      Table_B
           130       1      Table_B
           140       0      Table_A
           150       2      Table_A

I know a UNION can’t be done to get this result.

  • 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-06-10T12:05:36+00:00Added an answer on June 10, 2026 at 12:05 pm

    would this work?

    SELECT 
        trans, MAX(max_amend) as max_max_amend
    FROM
        (SELECT
            'a' AS src, trans, MAX(amend) AS max_amend
        FROM
            table_a
        GROUP BY
            trans
    
        UNION ALL
    
        SELECT
            'b' AS src, trans, MAX(amend) AS max_amend
        FROM
            table_b
        GROUP BY
            trans) m
    GROUP BY
        trans
    

    Lucero’s point below is correct, the min(src) would be on the global set, not the related max()

    I think you’d have to combine the source and table values into one column you can max. In your example, adding 1 to the value is all you need to distinguish the sources, like:

    SELECT trans, Max(amend) AS MaxOfamend, 1+[amend] AS isa, 0 AS isb
    FROM TableA
    GROUP BY trans
    

    but you could add 100, or multiply by a big value, or whatever works with your data. The idea is to combine the two pieces of information, the amend value and the source, into one column.

    Then, after the information is combined, you get the max of that value, then strip off the source flag by uncombining them (subtracting 1, dividing by 100, whatever)


    OK, here’s what I got:

    CREATE VIEW [dbo].[viewA]    AS
    SELECT trans, MAX(amend + .20) AS srcIsA, 0 AS srcIsb
    FROM  dbo.tableA
    GROUP BY trans
    
    CREATE VIEW [dbo].[viewB]    AS
    SELECT trans, 0 AS srcIsA, MAX(amend + .10) AS srcIsB
    FROM  dbo.tableB
    GROUP BY trans
    
    CREATE VIEW [dbo].[viewU]    AS
    SELECT * from viewA
    union all
    select *
    FROM  viewb
    
    CREATE VIEW [dbo].[viewv]    AS
    SELECT trans, srcIsA, srcIsb, srcIsA + srcIsb AS total
    FROM  dbo.viewU
    
    CREATE VIEW [dbo].[vieww]    AS
    SELECT trans, MAX(total) AS max_total
    FROM  dbo.viewv
    GROUP BY trans
    
    CREATE VIEW [dbo].[viewx] AS
    SELECT trans, 
    max_total, 
    CAST(max_total AS int) AS maxval, 
    CASE WHEN (max_total - CAST(max_total AS int)) = .1 THEN 'a' ELSE 'b' END AS src
    FROM  dbo.vieww
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two similar tables table_a and table_b table_a is my current data and
I have two tables: Table_A name1 create_date Table_B name2 keep_time name1 is something like
I have two tables, table_a and table_b: table_a: city -------- tokoyo london table_b: zipcode
Say I have two tables called A (fields: id, phase, name) and B(fields: id,
i have two tables table1 fields fid,fname,fage a ,abc ,20 b ,bcv ,21 c
I have two MySQL tables a and b with fields x and y. Table
I have two tables tbl_a and tbl_b and their fields are tbl_a ----- a_id
I have two tables: TableA and TableB. Both have dates and rates fields. I
I have two MySql tables as shown below with the data shown: CREATE TABLE
I have two tables that I'm querying from. In one table, there are fields

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.