Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
netagra
Starting Member
3 Posts |
Posted - 2010-12-07 : 07:25:12
|
Hello to all,it's nice to find you.I have two tables one with production data the other is scrap data. The tables look pretty much identical, though i cannot make my query work.Production table is like thisPrDate, PrUser, PrMachine, PrDept, PrItemCode, PrQuantityScrap table is like thisScDate, ScUser, ScMachine, ScDept, ScItemCode, ScQuantityWhat i need is to produce one table with a specific date that has both production and scrap quantities per user, item code and machineWhat i have done is thisSELECT PrUser, PrMachine, PrItemCode, PrDept, SUM(PrQuantity), Sum(ScQuantity)FROM Production LEFT JOIN Scrap ON PrUser = ScUser AND PrMachine = ScMachine AND PrItemCode = ScItemCode AND PrDept = ScDept WHERE (PrDate Between '2010-01-11 06:00' And '2010-10-11 06:00') AND PrDept = '611.4' AND Q9USER = '01225E'GROUP BY PrUser, PrMachine, PrItemCode, PrDeptORDER BY PrUser, PrMachineBut it doesn't work. It produces crazy amounts on sums! If i run two different queries each for the specific table i get my answers. The problem is i cannot join them in one table result.What's wrong?Thanks for your time! |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-12-07 : 07:43:23
|
select PrUser, PrMachine, PrItemCode, PrDept, PrQuantity, ScQuantityfrom(select PrUser, PrMachine, PrItemCode, PrDept, PrQuantity = SUM(PrQuantity)from Production where PrDate Between '2010-01-11 06:00' And '2010-10-11 06:00'AND PrDept = '611.4'AND Q9USER = '01225E'GROUP BY PrUser, PrMachine, PrItemCode, PrDept) prleft join(select ScUser, ScMachine, ScItemCode, ScDept, ScQuantity = SUM(ScQuantity)from Scrap where PrDate Between '2010-01-11 06:00' And '2010-10-11 06:00'AND PrDept = '611.4'AND Q9USER = '01225E'GROUP BY ScUser, ScMachine, ScItemCode, ScDept) scon PrUser = ScUser AND PrMachine = ScMachine AND PrItemCode = ScItemCode AND PrDept = ScDeptORDER BY PrUser, PrMachine==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
netagra
Starting Member
3 Posts |
Posted - 2010-12-07 : 08:11:36
|
Thanks for your reply.It doesn't like the PrQuantity = SUM(PrQuantity)ScQuantity = SUM(ScQuantity)or something else?This is what i getServer: Msg 170, Level 15, State 1, Line 4Line 4: Incorrect syntax near '='.Server: Msg 170, Level 15, State 1, Line 13Line 13: Incorrect syntax near '='.And sorry Q9User is PrUser ! |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-12-07 : 08:31:54
|
Looks ok - is this sql server?tryselect PrUser, PrMachine, PrItemCode, PrDept, SUM(PrQuantity) as PrQuantity==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
netagra
Starting Member
3 Posts |
Posted - 2010-12-07 : 09:40:07
|
Thanks again!It worked perfectly.I made some modifications and it looks like thisselect PrUser, PrMachine, PrItemCode, PrDept, PrQuantity, ScQuantityfrom(select PrUser, PrMachine, PrItemCode, PrDept, PrQuantity = SUM(PrQuantity)from Productionwhere PrDate Between '2010-01-11 06:00' And '2010-10-11 06:00'AND PrDept = '611.4'AND PrUser = '01225E'GROUP BY PrUser, PrMachine, PrItemCode, PrDept) prleft join(select ScUser, ScMachine, ScItemCode, ScDept, ScQuantity = SUM(ScQuantity)from Scrapwhere ScDate Between '2010-01-11 06:00' And '2010-10-11 06:00'AND ScDept = '611.4'AND ScUser = '01225E'GROUP BY ScUser, ScMachine, ScItemCode, ScDept) scon PrUser = ScUser AND PrMachine = ScMachine AND PrItemCode = ScItemCode AND PrDept = ScDeptORDER BY PrUser, PrMachine, PrItem, PrStepIt combines these two queries in one and makes the results i need.What is this technique called in order to work more with ?Thank you so much. |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-12-07 : 09:43:50
|
pr and sc are called derived tables.You could also do the same thing with common table expressions which is sometomes more useful.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
|
|
|
|
|