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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:17:03+00:00 2026-05-26T09:17:03+00:00

I have these SQL tables Supplier SUPPLIERID | SUPPLIERNAME 1 | sup1 2 |

  • 0

I have these SQL tables

Supplier

 SUPPLIERID | SUPPLIERNAME
 1          | sup1
 2          | sup2
 3          | sup3

Sold

ITEMID | SOLD | SUPPLIER ID | DATE
pen    | 10   | 1           | 2011-10-21
pen    | 5    | 1           | 2011-10-22
pen    | 5    | 1           | 2011-10-23
pen    | 20   | 3           | 2011-10-24

Bought

 ITEMID | BOUGHT | SUPPLIER ID | DATE
 pen    | 20     | 1           | 2011-9-21
 pen    | 5      | 3           | 2011-9-24

I’m currently using this script but it doesn’t seem to work as the SUM() of sold and bought is tripled. I suspect the reason is because there are 3 rows in sold but only 2 in bought. Any idea how to fix this?

   SELECT suppliername, SUM(sold) as sold, SUM(bought) AS bought
     FROM sold s
LEFT JOIN supplier sp
       ON s.supplierid = sp.supplierid
LEFT JOIN bought b
       ON sp.supplierid = b.supplierid
 GROUP BY suppliername
  • 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-26T09:17:03+00:00Added an answer on May 26, 2026 at 9:17 am
    SELECT suppliername, Sold, Bought
    FROM Supplier sp
    LEFT JOIN (SELECT SupplierId, SUM(Sold) Sold
               FROM sold
               GROUP BY SupplierId) s
    ON sp.SupplierId = s.SupplierId
    LEFT JOIN (SELECT SupplierId, SUM(bought) bought
               FROM bought
               GROUP BY SupplierId) b
    ON sp.SupplierId = b.SupplierId
    

    Basically, what I’m doing is calculating the amount sold and the amount bought for every supplierId separately (in the LEFT JOINs), so I always have the total amount sold and total amount bought. The querys that I’m using in the LEFT JOINs are called derived tables, since they are acting like a table on the general query. Hope this helps you get a more clear idea of what I did.

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

Sidebar

Related Questions

Let's say, we have these SQL tables: [Articles] bill int (pkey) arti int (pkey)
I'm working with SQL Server 2008. Let's say I have these three tables: Product
I have these tables (in SQL-Server 2008 R2): Table1: Id Guid 1 {530D8FE1-7541-43CC-9F92-1AA776490155} 2
I have 3 sql tables SourceKeys, Channels, and ChannelTypes. These tables are queried and
Let's say I have these sql tables: a) book b) customer c) borrow_history (book_id,
Possible Duplicate: SQL query multiple tables I have these 2 tables: ASSOCIATION_TABLE: [id, key,
I have an SQL Server DB with a table with these fields: A bit
We have two tables in a SQL Server 2005 database, A and B.There is
We have a few tables with persisted computed columns in SQL Server. Is there
We are upgrading/converting several old Access databases to MS-SQL. Many of these databases have

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.