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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:11:57+00:00 2026-06-14T19:11:57+00:00

I have the following two tables: Table X Product Type Sales 1 a 1

  • 0

I have the following two tables:

Table X

Product  Type   Sales
1   a   1
1   b   1
1   c   0
1   a   2

Table Y

Product Inventory
1   10

I would like my query to return:

Product type    sales   inventory
1          a    3       10
1          b    1       0
1          c    0       0

Issue is using aggregate functions and not over counting the inventory. Example:

select X.product, X.type, sum(X.sales), sum( case when X.sales > 0 then Y.inventory else 0 end) 
from x,y
where x.product = y.product 
group by x.product,x.type
  • 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-14T19:11:59+00:00Added an answer on June 14, 2026 at 7:11 pm

    If you are basing the final result on the sales, this should give you the result:

    select distinct x1.product,
      x1.type,
      x2.totalsales,
      case when x1.sales > 0 then y.inventory else 0 end
    from x x1
    left join
    (
      select product,
          type,
          sum(sales) as TotalSales
      from x
      group by Product, Type
    ) x2
      on x1.product = x2.product
      and x1.type = x2.type
    left join y
      On Y.Product = x1.Product
    

    See SQL Fiddle with Demo

    I am unsure why you have the inventory for b listed as zero in your sample result considering it has a total sales greater than zero.

    Edit #1:

    Based on your comments, I would suggest looking at using row_number():

    select product, 
      type,
      totalsales,
      case when rn = 1 then inventory else 0 end inventory
    from
    (
      select x.product,
        x.type,
        sum(sales) TotalSales,
        row_number() over(partition by x.product order by x.type) rn,
        y.inventory
      from x 
      inner join y
        on x.product = y.product
      group by x.product, x.type, y.inventory
    ) src
    

    See SQL Fiddle with Demo

    Or if you cannot group by inventory, then join outside of the subquery:

    select src.product, 
      src.type,
      src.totalsales,
      case when rn = 1 then inventory else 0 end inventory
    from
    (
      select x.product,
        x.type,
        sum(sales) TotalSales,
        row_number() over(partition by x.product order by x.type) rn
      from x 
      group by x.product, x.type
    ) src
    inner join y
      on src.product = y.product
    

    See SQL Fiddle with Demo

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

Sidebar

Related Questions

I have the following two tables in my mysql database. Table Name: groups id
I have the following two tables, affiliates and referrers. affiliates Table id loginid 3
I have two tables with the following (simplified) structures: table "Factors" which holds data
So I have two tables. Table 1 is named 'appointment' with the following fields:
I have two tables with the following schema: CREATE TABLE sales_data ( sales_time date
I have two tables Product having following structure: ProductID,ProductName, IsSaleTypeA, IsSaleTypeB, IsSaleTypeC 1, AAA,
I have two tables, categories and products. I am using CodeIgniter. Categories CREATE TABLE
I have the following two tables and data:- CREATE TABLE customers ([id] int, [name]
I have two tables, table A : Customer_ID Product Date Of Sale Pay Meth
I have two tables: one holding products and one holding related inventory moves. Product

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.