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

The Archive Base Latest Questions

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

I have 2 tables TABLE A INV AMT DISC 1001 1500 150 1002 3000

  • 0

I have 2 tables

TABLE A

INV    AMT   DISC
1001  1500    150
1002  3000    300

TABLE B

INV  DESC        AMT
1001 CHARGES     100
1001 FREIGHT      30
1001 INSURANCE    20
1002 CHARGES     215
1002 FREIGHT      32
1002 INSURANCE    25

For Table B, I used the SQL here to convert row to column format:

SELECT t.inv,
         MAX(CASE WHEN t.description = 'CHARGES' THEN t.amount ELSE NULL END) AS charges,
         MAX(CASE WHEN t.description = 'FREIGHT' THEN t.amount ELSE NULL END) AS freight,
         MAX(CASE WHEN t.description = 'INSURANCE' THEN t.amount ELSE NULL END) AS insurance
    FROM TABLE B
GROUP BY t.inv
ORDER BY t.inv

How can I combine the data into this format using SQL:

INV     AMT   DISC    CHARGES FREIGHT INSURANCE
1001   1500    150      100      30       20
1002   3000    300      215      32       25

Thanks.

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

    You need to join between Table A and you query against Table B

    declare @TableA as table (inv int, amount int, disc int)
    declare @TableB as table (inv int, description varchar(50), amount int)
    
    insert into @TableA values (1001, 1500, 105)
    insert into @TableA values (1002, 3000, 300)
    
    insert into @TableB values (1001, 'CHARGES', 100)
    insert into @TableB values (1001, 'FREIGHT', 30)
    insert into @TableB values (1001, 'INSURANCE', 20)
    insert into @TableB values (1002, 'CHARGES', 215)
    insert into @TableB values (1002, 'FREIGHT', 32)
    insert into @TableB values (1002, 'INSURANCE', 25)
    
    select
        A.inv,
        A.amount,
        A.disc,
        B.charges,
        B.freight,
        B.insurance
    from @TableA as A
        inner join (
                    SELECT t.inv,
                           MAX(CASE WHEN t.description = 'CHARGES' THEN t.amount ELSE NULL END) AS charges,
                           MAX(CASE WHEN t.description = 'FREIGHT' THEN t.amount ELSE NULL END) AS freight,
                           MAX(CASE WHEN t.description = 'INSURANCE' THEN t.amount ELSE NULL END) AS insurance
                    FROM @TableB as t
                    GROUP BY t.inv) as B
            on A.inv = B.inv
    where
        B.charges = 100 and
        A.inv = 1001
    

    Output is

    Output from query

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 tables AP and INV where both have the columns [PROJECT] and
I have two tables: table a ida valuea 1 a 2 b 3 c
I have 2 tables Table A NameID FirstName MiddleName LastName Addr1 Addr2 Phn1 Phn2
I have two Tables: Table 1: Questions : QuestionId NUMERIC Title TEXT Test Data
I have two tables: Table GL TRANS_NBR BASE_AMOUNT CTRL GRP 120211282 -7200 77 120211282
I have two tables: one table (okay, it's a view), vComputer, lists many computers
I have these tables Table 1 tbl1_site [facilityId] [name] Table 2 tbl2_applicant [pvid] [facilityId]
I have 2 tables Table 1: DB1, DB2, DB3, DB4, DB5, Some other identifiers
I have 2 tables Table NAME (id, name, phone, city, state, rid) Table NAMES2
I have two tables: Table A ID ABC_ID VAL Table B ID ABC_ID VAL

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.