473,378 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Sum QTY within 2 tables

Dear Experts,

I have 2 tables as below and I want to sum the QTY for table MWOHED and also MWOPTR.
Then the result would be show as 'Result'.

Please help me to define the coding, as my coding will duplicate the sum qty for table mwohed.

Thanks in advanced.
[HTML]Table MWOHED
Sch# MO# Color# QTY
104988 3311403 Black 10
104988 3311404 Black 10
104988 3311405 Black 18
104988 3311406 Black 28
104988 3311407 Black 42
104988 3311408 Black 40
104988 3311409 Black 48
104988 3311410 Black 26[/HTML]

[HTML]Table MWOPTR
Workcenter MO# Report date QTY
11000 3311403 20070523 10
11000 3311404 20070523 10
11000 3311405 20070522 18
11000 3311406 20070522 18
11000 3311406 20070523 10
11000 3311407 20070522 2
11000 3311407 20070523 10
11000 3311407 20070523 20
11000 3311407 20070524 5
11000 3311407 20070602 2
11000 3311407 20070608 3
11000 3311408 20070523 10
11000 3311408 20070523 20
11000 3311408 20070524 5
11000 3311408 20070602 2
11000 3311408 20070608 3
11000 3311409 20070522 8
11000 3311409 20070523 10
11000 3311409 20070523 20
11000 3311409 20070524 5
11000 3311409 20070602 2
11000 3311409 20070608 3
11000 3311410 20070522 26
41000 3311403 20070523 9
41000 3311404 20070523 9
41000 3311405 20070522 18
41000 3311406 20070522 18
41000 3311406 20070523 9
41000 3311407 20070522 2
41000 3311407 20070523 9
41000 3311407 20070523 20
41000 3311407 20070524 5
41000 3311407 20070602 2
41000 3311407 20070608 3
41000 3311408 20070523 10
41000 3311408 20070523 20
41000 3311408 20070524 5
41000 3311408 20070602 2
41000 3311408 20070608 3[/HTML]
[HTML] Result
Sch# Color# Ttl_Order Qty Ttl_Cut Qty Ttl_Sew Qty
104988 Black 222 222 144
[/HTML]
Sep 21 '07 #1
5 2301
amitpatel66
2,367 Expert 2GB
Could you please post your Query for my reference??
Sep 21 '07 #2
pbmods
5,821 Expert 4TB
Heya, Erosche.

Something like this, perhaps?
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `MWOHED`.`Sch#`,
  3.         `MWOHED`.`Color#`,
  4.         SUM(`table`.`something`)
  5.             AS `Ttl_Order_Qty`,
  6.         etc.
  7.     FROM
  8.     (
  9.             `MWOHED`
  10.         LEFT JOIN
  11.             `MWOPTR`
  12.                 USING
  13.                     (`MO#`)
  14.     )
  15.     GROUP BY
  16.         `MWOHED`.`Sch#`,
  17.         `MWOHED`.`Color#`
  18.  
The idea is to use the SUM() function to specify which columns you want summed (from the names of your table fields, I was unable to determine exactly which fields go here).

Then you use the GROUP BY clause to tell MySQL which fields have to be unique in order to denote a unique entity. In other words, in the example above, each row must have a unique `Sch#` and `Color#` in order to get its own row in the result set; otherwise, it will be grouped (and its values SUM()'ed) into the row that has the same `Sch#` and `Color#`.
Sep 21 '07 #3
Could you please post your Query for my reference??
Hi...

I'm using winsql for my coding. And below is my query :
Expand|Select|Wrap|Line Numbers
  1. select sch#, color, sum(mwohed.ttl_order), sum(mwoptr.ttl_cut)
  2. from mwohed, mwoptr
  3. where sch# = '104988'
  4. and mwohed.MO# = mwoptr.MO#
  5. and workcenter like '11%'  --FOr Total Cut qty
  6. group by sch#, color
  7.  
With above query, I will get duplicate total order qty because of the MO# is repeated at table MWOPTR when I need to get total cut qty (workcenter 11000).
Then I also don't know how to get the total sew qty (workcenter 41xxxx) together in 1 query.

Could you help ?

Thanks a lot.... ^_^
Sep 25 '07 #4
Heya, Erosche.

Something like this, perhaps?
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `MWOHED`.`Sch#`,
  3.         `MWOHED`.`Color#`,
  4.         SUM(`table`.`something`)
  5.             AS `Ttl_Order_Qty`,
  6.         etc.
  7.     FROM
  8.     (
  9.             `MWOHED`
  10.         LEFT JOIN
  11.             `MWOPTR`
  12.                 USING
  13.                     (`MO#`)
  14.     )
  15.     GROUP BY
  16.         `MWOHED`.`Sch#`,
  17.         `MWOHED`.`Color#`
  18.  
The idea is to use the SUM() function to specify which columns you want summed (from the names of your table fields, I was unable to determine exactly which fields go here).

Then you use the GROUP BY clause to tell MySQL which fields have to be unique in order to denote a unique entity. In other words, in the example above, each row must have a unique `Sch#` and `Color#` in order to get its own row in the result set; otherwise, it will be grouped (and its values SUM()'ed) into the row that has the same `Sch#` and `Color#`.

Hey Moderator....

Thanks a lot for the query, but it still can't serve my request....
There's 3 column that I need to sum, which is Total order qty, Total cut qty (which is from table MWOPTR, field workcenter 11000), & Total sew qty (which is from table MWOPTR, field workcenter 41000).
Understand the usage of SUM() and group by, but I just can't think of correct query to combine those data into 1 correct query.

Please help....

Thanks...
Sep 25 '07 #5
as i understand, u want all the details from mwohead and the sum of qty from mwoptr:
Expand|Select|Wrap|Line Numbers
  1. select mwohed.*, sum(mwoptr.qty)
  2.  
  3. from mwohed as mwohed
  4.  
  5. left join mwoptr as mwoptr on mwohed."MO#"=mwoptr."MO#"
  6.  
  7. group by
  8. "Sch#", "MO#", color, mwohed.qty
  9.  
Sep 13 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Dave | last post by:
I have a system that basically stores a database within a database (I'm sure lots have you have done this before in some form or another). At the end of the day, I'm storing the actual data...
4
by: Ryan Ternier | last post by:
I'm having an issue with an inner loop. Here's the basic Code: For each......{ //Do code here //start other loop For each....{
4
by: Girish | last post by:
Im trying to create a grid within a grid programmatically. Ive been successful in doing this but I need the embedded grid to fire its ItemDataBound event so I can handle it. The event does not seem...
2
by: Geoff Jones | last post by:
Hi Is it possible to create a new table within an access database from within VB? That is, I have an access database which has several tables. I would like, from VB.NET, to create a new table...
4
by: Mark D Powell | last post by:
We are running SQL Server 2000 SP3. We have linked servers in use that we use to access Oracle tables. Recently the claim has been made that you can access Oracle tables from within SQL Server...
1
by: daniellee2006 | last post by:
I am creating a basic website to store people profiles and within this website i have a page that creates a table dependent on the number of records in mysql written in PHP within these tables...
3
by: Aaron | last post by:
Hi, It is possible to establish relationships between tables within a dataset and this allows some useful features. For example, given a row from a table you can use GetParentRow,...
1
by: rshivaraman | last post by:
Hi All : A couple of tables have been identified to be deleted. My job is to find if it is at all used. On searching the web, i found a proc to search for a string within all databases in a...
1
by: Birky | last post by:
I am truly stuck and need some help on how to put this form together; any help or suggestions you can provide would be greatly appreciated. (Sorry this one is so long but I believe it is necessary to...
3
by: Harlequin | last post by:
I must start this posing by making the point that I am NOT a VB programmer and I'm something of a Newbie to MS Access. I can program in a number of languages (Java, Javascript, PERL,PHP and TCL) but...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.