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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:29:36+00:00 2026-06-17T22:29:36+00:00

I have the following tables: Material material_ID | material_Name A | Sugar B |

  • 0

I have the following tables:

Material
material_ID | material_Name
A           | Sugar
B           | Flour
C           | Egg Yolk
D           | Water
E           | Tea Powder

Product
product_ID  | product_Name | material_ID    
1           | Cake         | A              
1           | Cake         | B              
1           | Cake         | C
2           | Tea          | D
2           | Tea          | E
2           | Tea          | A              

ScaleData
record_ID   | product_ID     | material_ID  | Weight
1001        | 1              | A            | 30
1002        | 1              | B            | 11
1003        | 1              | C            | 25
1004        | 1              | A            | 31
1005        | 1              | B            | 25
1006        | 2              | D            | 15
1007        | 2              | E            | 20
1008        | 2              | E            | 21

As you can see: product no 1 is cake and it require material: A, B, and C in order to form a complete product. Product no 2 is tea and it require material: D, E, and A to form a complete product.

From the weighing table (ScaleData) we can see that there is 1 cake and 0 tea. 

record_ID: 1001, 1002, 1003 creates one complete cake.
record_ID: 1004, 1005 are remaining incomplete cake ingredients.
record_ID: 1006, 1007, 1008 are remaining incomplete tea ingredients.

Question:
A. How can i create the following result table based on data from above tables:

Result
product_ID  | product_Name  | Qty
1           | Cake          | 1
2           | Tea           | 0


B. How can i display the remaining ingredients like below?

Remaining
record_ID   | product_ID   | material_ID  | Weight
1004        | 1            | A            | 31
1005        | 1            | B            | 25
1006        | 2            | D            | 15
1007        | 2            | E            | 20
1008        | 2            | E            | 21

EDIT: I am currently able to solve this with a mix of .NET Code + SQL Query. What I am looking for is pure SQL Query solution. My current method is as follows:

1. Execute: "SELECT DISTINCT product_ID FROM Product" to get a list of Product_ID.

2. Iterate through each product_ID and retrieve list of material from each product.
   Something like: "SELECT material_ID FROM Product WHERE product_ID=1"

3. Scan ScaleData table for each material and get the total row counts
   Something like:
   TotalA = "SELECT COUNT(*) FROM ScaleData WHERE product_ID=1 AND material_ID='A'
   TotalB = "SELECT COUNT(*) FROM ScaleData WHERE product_ID=1 AND material_ID='B'
   TotalC = "SELECT COUNT(*) FROM ScaleData WHERE product_ID=1 AND material_ID='C'

4. Compare variables: TotalA, TotalB, TotalC and get the lowest value.
   Lets say TotalA = 2 ; TotalB = 2 ; TotalC = 1
   LowestCount = TotalC = 1

5. Then we can tell total Qty for product 1 is 1 ( based on lowest count ).
   Remaining for Material A = 2 - 1 = 1
   Remaining for Material B = 2 - 1 = 1

That is my current solution and I know it is not very efficient. I prefer pure SQL solution and I hope some SQL Gurus are willing to help me..

  • 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-17T22:29:37+00:00Added an answer on June 17, 2026 at 10:29 pm

    This would do the first part. It could probably be cleaned up, but what it basically does is to take the ingredient there is least of per product, and that’ll be the number of products you can make.

    SELECT product_id,product_name, min(c) Qty
    FROM
    (
      SELECT p.product_id, p.product_name, p.material_id, count(sd.product_id) c
      FROM product p
      LEFT JOIN scaledata sd
        ON p.product_id=sd.product_id
       AND p.material_id = sd.material_id
      GROUP BY product_id, p.product_name, p.material_id
    ) a
    GROUP BY product_id
    

    I made an SQLfiddle to test with for anyone with time to write the second part 😉

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

Sidebar

Related Questions

I have following three tables 1. Product Master (pm) Product_Code Product_Name Product_Category (Books, Stationary,
I have following tables questions -> id, question_data, user_id users -> id, fname, lname
Suppose that we have following tables create table Employee( 2 EMPNO NUMBER(3), 3 ENAME
I have the following tables: users +----------+----------+----------+ | id | name | dob |
I have the following tables in my database: Products ID_PRODUCT PRODUCTNAME PRICE Customers ID_CUSTOMER
I have the following tables. I want to run a query but I think
I have the following tables. I wanted it so when I make more boxes
I have the following tables in SQL Server 2005 ReceiptPoint: ID (PK), Name GasIndexLocation:
i have the following tables in sql server: photoalbumsTable: album_ID album_caption albumtagmap id album_id
I have the following tables, with these keys in my database: bookings session_id sessions

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.