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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:56:47+00:00 2026-06-10T18:56:47+00:00

I have two tables as below: Invoice InvId | Amount | Name ———————– 1

  • 0

I have two tables as below:

Invoice

InvId | Amount  | Name
-----------------------
1     | 50      | John
2     | 30      | Mike
3     | 20      | John

Detail

MetalType| Weight | InvId
-------------------------
Gold     | 2      | 2
Silver   | 4      | 3
Silver   | 3      | 3
Gold     | 5      | 1

I would like to have the following output, but my query will only provide the total for silver and gold for John. How can I build a query that will also include the total invoice amount for John.

Total Invoice Amount For John = 70
Total Silver Weight = 7
total Gold Weith = 5

SELECT
SUM(IFF(D.MetalType=”Gold”, D.Weight, 0)) AS TotGold,
SUM((IFF(D.MetalType=”Silver”, D.Weight, 0)) AS TotSilver
FROM Invoice I INNER JOIN Detail D ON I.InvId = D.InvId WHERE I.Name = “John”   
  • 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-10T18:56:48+00:00Added an answer on June 10, 2026 at 6:56 pm

    Try this:

    For Sql-Server:

    SELECT 
    SUM(TotalAmount) AS TotalAmount,
    SUM(TotGold) AS TotGold,
    SUM(TotSilver) AS TotSilver
    FROM(
    SELECT
    SUM (I.Amount) OVER (Partition by D.Invid) AS TotalAmount,
    SUM(CASE WHEN D.MetalType='Gold' THEN D.Weight ELSE 0 END) AS TotGold,
    SUM(CASE WHEN D.MetalType='Silver' THEN D.Weight ELSE 0 END) AS TotSilver
    FROM Invoice I INNER JOIN Detail D ON I.InvId = D.InvId
    
    WHERE I.Name = 'John'
    GROUP BY D.InvId, I.Amount) n
    

    Here is an SQL Fiddle – now it kills the duplicate detail and counts it only once.

    EDITED for Access:

    SELECT
      n.Name,
      MAX(TotalAmount),
      SUM(TotGold) AS TotGold,
      SUM(TotSilver) AS TotSilver
    FROM(
      SELECT
       I.Name,
       SUM(CASE WHEN D.MetalType='Gold' THEN D.Weight ELSE 0 END) AS TotGold,
       SUM(CASE WHEN D.MetalType='Silver' THEN D.Weight ELSE 0 END) AS TotSilver
      FROM Invoice I 
      INNER JOIN Detail D ON I.InvId = D.InvId
      GROUP BY I.Name, D.InvId, I.Amount) n
    INNER JOIN (
       SELECT
         I.Name, SUM (I.Amount) AS TotalAmount
        FROM Invoice I
        GROUP BY I.Name) m ON m.Name = n.Name
    GROUP BY n.Name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have two Tables tblSiteVisits and tblConformedList and Colums as Below tblSiteVisits Name |
I have two tables described below. What I need is a single query that
I'm using SQL Server 2008. Let's say I have two hypothetical tables like below:
I have two tables like the ones below. I need to find what exchangeRate
I have two tables names book_list and book_category . Schema is like below: book_list
I have created two tables & inserted values as shown below . Table 1
I have 10 tables in my database(MySQL). two of them is given below tbl_state
I have two tables as defined below: EMAILS ID SUBJECT BODY 1 Test1 blah
I have two tables like the below one in my DB. In the first
so I have two tables. They are pictured below. I have a master table

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.