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 |
|
Deon Smit
Starting Member
47 Posts |
Posted - 2008-06-04 : 02:08:58
|
| I have got 2 tables. SKU and SKUXLOC. SKUXLOC is the details for SKU. Locations where stuff is putt. Here is my script.Select * from SKU CJoin (Select SKU from SKUXLOC where LOCATIONTYPE = 'CASE' and QTY > 0) AOn C.SKU = A.SKUJoin (Select SKU from SKUXLOC where LOCATIONTYPE = 'PICK' and QTY = 0) BOn a.sku = b.skuTable SKU isSKU | Supp | -----------------1234 | Supp1 | 456 | Supp2 | 789 | Supp3 |Table SKUXLOC isSKU | SKUXLOC | QTY |----------------------------------1234 | PICK | 1 | 1234 | Case | 0 | 1234 | CASE | 55 | 1234 | PICK | 0 | 1234 | CHER | 71 | 456 | CASE | 88 | 456 | PICK | 0 |456 | PICK | 0 |456 | CASE | 7 |456 | PICKTO | 9 |789 | PICK | 10 |789 | CASE | 5 |My Query gives me SKU 1234 and 456. Because it has a line with PICK and QTY = 0 and a line with CASE and QTY > 0 Per SKU. I need the SUM op the qty Of CASE and PICK first to be calculated. And then my conditions should be met. In that case only SKU 456 should return. Cause Total PICK = 0 And Total Case = 95. Not 1234 Cause PICK = 1 and Case= 55. So only one condition is met.Can any one help me. Regards. |
|
|
metaraja
Starting Member
3 Posts |
Posted - 2008-06-04 : 02:38:51
|
| can try like thisSelect * from SKU CJoin (Select SKU, sum(QTY) as qty from SKUXLOC where LOCATIONTYPE = 'CASE' group by SKU ,LOCATIONTYPE ) AOn (C.SKU = A.SKU and a.QTY > 0 )Join (Select SKU,sum(QTY) as qty from SKUXLOC where LOCATIONTYPE = 'PICK' group by SKU ,LOCATIONTYPE ) BOn ( a.sku = b.sku and b.QTY = 0 )Raja |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-04 : 02:51:10
|
quote: Originally posted by Deon Smit I have got 2 tables. SKU and SKUXLOC. SKUXLOC is the details for SKU. Locations where stuff is putt. Here is my script.Select * from SKU CJoin (Select SKU from SKUXLOC where LOCATIONTYPE = 'CASE' and QTY > 0) AOn C.SKU = A.SKUJoin (Select SKU from SKUXLOC where LOCATIONTYPE = 'PICK' and QTY = 0) BOn a.sku = b.skuTable SKU isSKU | Supp | -----------------1234 | Supp1 | 456 | Supp2 | 789 | Supp3 |Table SKUXLOC isSKU | SKUXLOC | QTY |----------------------------------1234 | PICK | 1 | 1234 | Case | 0 | 1234 | CASE | 55 | 1234 | PICK | 0 | 1234 | CHER | 71 | 456 | CASE | 88 | 456 | PICK | 0 |456 | PICK | 0 |456 | CASE | 7 |456 | PICKTO | 9 |789 | PICK | 10 |789 | CASE | 5 |My Query gives me SKU 1234 and 456. Because it has a line with PICK and QTY = 0 and a line with CASE and QTY > 0 Per SKU. I need the SUM op the qty Of CASE and PICK first to be calculated. And then my conditions should be met. In that case only SKU 456 should return. Cause Total PICK = 0 And Total Case = 95. Not 1234 Cause PICK = 1 and Case= 55. So only one condition is met.Can any one help me. Regards.
SELECT SKU.*FROM SKU mainINNER JOIN(SELECT SKU, SUM(CASE WHEN SKUXLOC='PICK' THEN QTY ELSE 0 END) AS Pick,SUM(CASE WHEN SKUXLOC='CASE' THEN QTY ELSE 0 END) AS CaseFROM SKUXLOCGROUP BY SKU)tmpON tmp.SKU=main.SKUWHERE tmp.Pick=0 AND tmp.Case>0 |
 |
|
|
Deon Smit
Starting Member
47 Posts |
Posted - 2008-06-04 : 03:50:55
|
quote: Originally posted by metaraja can try like thisSelect * from SKU CJoin (Select SKU, sum(QTY) as qty from SKUXLOC where LOCATIONTYPE = 'CASE' group by SKU ,LOCATIONTYPE ) AOn (C.SKU = A.SKU and a.QTY > 0 )Join (Select SKU,sum(QTY) as qty from SKUXLOC where LOCATIONTYPE = 'PICK' group by SKU ,LOCATIONTYPE ) BOn ( a.sku = b.sku and b.QTY = 0 )Raja
This works 100%. Thanks alot. |
 |
|
|
|
|
|