| Author |
Topic |
|
bismarkcount
Starting Member
15 Posts |
Posted - 2007-11-05 : 16:55:26
|
| Hello!!i have two tablesscanner_input: id | quantity | status-----------------------------------prod1 | 6 | OKprod2 | 7 | OKprod3 | 3 | Pprod4 | 3 | OKprod5 | 3 | OKprod7 | 3 | OKandsap_input:id | quantity | status-----------------------------------prod1 | 6 | OKprod2 | 7 | OKprod3 | 3 | Pprod4 | 2 | OKprod6 | 3 | OKprod8 | 3 | OKi need a query to join both tables and compare the quantities of an item in table SCANNER_INPUT with its corresponding item in SAP_INPUT, so the result would be something like this:sap_inputid(scanner)| qty(scan)| id(sap) | qty(sap) | difference------------------------------------------------------------prod1 | 6 | prod1 | 6 | 0prod2 | 7 | prod2 | 7 | 0prod3 | 3 | prod3 | 3 | 0prod4 | 3 | prod4 | 2 | 1prod5 | 3 | prod5 | | 3prod6 | | prod6 | 3 | -3prod7 | 3 | prod7 | | 3prod8 | | prod8 | 3 | -3I found i can use a full outer join... but i dont know how to use it.any ideas??thnx |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-11-05 : 16:59:12
|
| what have you tried so far? post your scripts and any error msgs..Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
Van
Constraint Violating Yak Guru
462 Posts |
Posted - 2007-11-05 : 17:03:45
|
| Just out of curiosity which particular CIS class is this for? |
 |
|
|
bismarkcount
Starting Member
15 Posts |
Posted - 2007-11-05 : 17:08:10
|
| cant figure out how to use the full outer join with both tables and a calculation.but i think that i can also use something like:select scanner_input.id, scanner_input.quantity, sap_input.id, sap_input.quantity, select(scanner_input.quantity - sap_input.quantity)from scanner_input, sap_input??my target query is kinda more complex, but i simplified the concept to make it easier and faster to read for the bloggers. but that is the general idea.any suggestions??thnx |
 |
|
|
bismarkcount
Starting Member
15 Posts |
Posted - 2007-11-05 : 17:10:31
|
| this is not for school. this is for my work. to check if the scanned items in the shipment fit the SAP output quantity |
 |
|
|
Van
Constraint Violating Yak Guru
462 Posts |
Posted - 2007-11-05 : 17:14:15
|
| Something like this:select sci.id, sci.quantity, sai.id, sai.quantity, sci.quantity - sai.quantity as 'Difference'from scanner_input sci join sap_input sai on sci.id = sai.id |
 |
|
|
bismarkcount
Starting Member
15 Posts |
Posted - 2007-11-05 : 17:27:49
|
| and how could i put a condition on that query? for example:where sap_input.id = xxx? thnx in advance |
 |
|
|
Van
Constraint Violating Yak Guru
462 Posts |
Posted - 2007-11-05 : 17:29:02
|
| Put this at the bottomwhere sai.id = xxxselect sci.id, sci.quantity, sai.id, sai.quantity, sci.quantity - sai.quantity as 'Difference'from scanner_input sci join sap_input sai on sci.id = sai.idwhere sai.id = xxx |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-06 : 02:29:29
|
[code]DECLARE @Scanner TABLE (ID VARCHAR(5), Quantity INT, [Status] VARCHAR(2))INSERT @ScannerSELECT 'prod1', 6, 'OK' UNION ALLSELECT 'prod2', 7, 'OK' UNION ALLSELECT 'prod3', 3, 'P' UNION ALLSELECT 'prod4', 3, 'OK' UNION ALLSELECT 'prod5', 3, 'OK' UNION ALLSELECT 'prod7', 3, 'OK'DECLARE @Sap TABLE (ID VARCHAR(5), Quantity INT, [Status] VARCHAR(2))INSERT @SapSELECT 'prod1', 6, 'OK' UNION ALLSELECT 'prod2', 7, 'OK' UNION ALLSELECT 'prod3', 3, 'P' UNION ALLSELECT 'prod4', 2, 'OK' UNION ALLSELECT 'prod6', 3, 'OK' UNION ALLSELECT 'prod8', 3, 'OK'SELECT ID AS [id(scanner)], SUM(Scanner) AS Scanner, SUM(Sap) AS Sap, SUM(Scanner) - SUM(Sap) AS [Difference]FROM ( SELECT ID, Quantity AS Scanner, 0 AS Sap FROM @Scanner UNION ALL SELECT ID, 0, Quantity FROM @Sap ) AS dGROUP BY ID[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Van
Constraint Violating Yak Guru
462 Posts |
Posted - 2007-11-06 : 10:55:05
|
| I don't see where the OP is asking about summing things or getting any totals. I only see where the OP is asking to put columns from 2 tables side by side where they match on ID and then take the difference between 2 columns (qty). |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-06 : 11:02:32
|
SUM is my trick to get UNION ALL to work.Zero plus a value returns the value. Nothing more. Look closer to the code and you will see how it works.Also, using sum make the code somewhat "future safe", when adding more complexity to the tables. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Van
Constraint Violating Yak Guru
462 Posts |
Posted - 2007-11-06 : 11:27:04
|
| I see. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-06 : 11:27:30
|
quote: Originally posted by Van I only see where the OP is asking to put columns from 2 tables side by side where they match on ID and then take the difference between 2 columns (qty).
And you forgot the FULL JOIN functionalityselect coalesce(sci.id, sai.id), coalesce(sci.quantity, 0), coalesce(sai.id, sci.id), coalesce(sai.quantity, 0), coalesce(sci.quantity, 0) - coalesce(sai.quantity, 0) as 'Difference'from scanner_input as scifull join sap_input as sai on sai.id = sci.id E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Van
Constraint Violating Yak Guru
462 Posts |
Posted - 2007-11-06 : 11:28:54
|
| Yep, that I did. But I was just trying to give the OP something to go on really. |
 |
|
|
bismarkcount
Starting Member
15 Posts |
Posted - 2007-11-06 : 11:48:35
|
| yep, full outer join worked fine. thnx a lot van and peso |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
|