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 |
|
longinos
Starting Member
11 Posts |
Posted - 2008-08-24 : 01:58:24
|
| products:prodID, qtyIn, QtyOunsupplierOrderDetails:prodID, qtycustomerOrderDetails:prodID, qtyHow can I update Products set qtyIn = sum(supplierOrderDetails.qty), qtyOut = sum(customerOrderDetails.qty)I know very little sql and I got stock at this |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2008-08-24 : 04:49:59
|
| Update Products SET qtyIn = SuppTotalQty, qtyOut = CustTotalQtyfrom products inner join (select sum(qty) as SuppTotalQty, prodID from supplierOrderDetails group by prodID) SuppTot on products.prodID = SuppTot.ProdIDinner join (select sum(qty) as CustTotalQty, prodID from customerOrderDetails group by prodID) CustTot on products.prodID = CustTot.ProdID--Gail ShawSQL Server MVP |
 |
|
|
longinos
Starting Member
11 Posts |
Posted - 2008-08-24 : 12:01:52
|
| Thanks Gail Shaw I really like your Code, but it did not do what I was trying to do, maybe because I didn't explain it well, I have allready found a solution which is very amateur, but it works, I wish someone can do it with fewer lines, and a simpler wayHere it is:create table qtyi(qtyiID int not null identity(1,1),prodID int, qty int,)insert into qtyi(prodID, qty)select distinct SOrderDetails.prodID, SUM(sorderdetails.qty) from SOrderDetailsinner join Product on SOrderDetails.prodID = Product.prodID group by SOrderDetails.prodIDcreate table qtyo(qtyoID int not null identity(1,1), prodID int, qty int)insert into qtyo(prodID, qty)select distinct COrderDetails.prodID, SUM(COrderDetails.qty) from COrderDetails inner join Product on COrderDetails.prodID = Product.prodID group by COrderDetails.prodIDupdate Product set QI = qtyi.qty from qtyi inner join Product on qtyi.prodID = Product.prodIDupdate Product set QO = qtyo.qty from qtyo inner join Product on qtyo.prodID = Product.prodIDdrop table qtyidrop table qtyo |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-24 : 13:20:27
|
quote: Originally posted by longinos Thanks Gail Shaw I really like your Code, but it did not do what I was trying to do, maybe because I didn't explain it well, I have allready found a solution which is very amateur, but it works, I wish someone can do it with fewer lines, and a simpler wayHere it is:create table qtyi(qtyiID int not null identity(1,1),prodID int, qty int,)insert into qtyi(prodID, qty)select distinct SOrderDetails.prodID, SUM(sorderdetails.qty) from SOrderDetailsinner join Product on SOrderDetails.prodID = Product.prodID group by SOrderDetails.prodIDcreate table qtyo(qtyoID int not null identity(1,1), prodID int, qty int)insert into qtyo(prodID, qty)select distinct COrderDetails.prodID, SUM(COrderDetails.qty) from COrderDetails inner join Product on COrderDetails.prodID = Product.prodID group by COrderDetails.prodIDupdate Product set QI = qtyi.qty from qtyi inner join Product on qtyi.prodID = Product.prodIDupdate Product set QO = qtyo.qty from qtyo inner join Product on qtyo.prodID = Product.prodIDdrop table qtyidrop table qtyo
WHy are you taking distinct while you're grouping by prodid. it already returns only one record per prodid so distinct is not required.Also i cant understand how your solution differs from that provided by GilaMonster.Can you explain why the provided solution didnt work with some data example? |
 |
|
|
longinos
Starting Member
11 Posts |
Posted - 2008-08-24 : 17:07:41
|
quote: Originally posted by visakh16
quote: Originally posted by longinos Thanks Gail Shaw I really like your Code, but it did not do what I was trying to do, maybe because I didn't explain it well, I have allready found a solution which is very amateur, but it works, I wish someone can do it with fewer lines, and a simpler wayHere it is:create table qtyi(qtyiID int not null identity(1,1),prodID int, qty int,)insert into qtyi(prodID, qty)select distinct SOrderDetails.prodID, SUM(sorderdetails.qty) from SOrderDetailsinner join Product on SOrderDetails.prodID = Product.prodID group by SOrderDetails.prodIDcreate table qtyo(qtyoID int not null identity(1,1), prodID int, qty int)insert into qtyo(prodID, qty)select distinct COrderDetails.prodID, SUM(COrderDetails.qty) from COrderDetails inner join Product on COrderDetails.prodID = Product.prodID group by COrderDetails.prodIDupdate Product set QI = qtyi.qty from qtyi inner join Product on qtyi.prodID = Product.prodIDupdate Product set QO = qtyo.qty from qtyo inner join Product on qtyo.prodID = Product.prodIDdrop table qtyidrop table qtyo
WHy are you taking distinct while you're grouping by prodid. it already returns only one record per prodid so distinct is not required.Also i cant understand how your solution differs from that provided by GilaMonster.Can you explain why the provided solution didnt work with some data example?
Sorry about the data example, I had problems using the sql publishing wizard, but I will tell you the diffrences that I found.In the code of Gail Shaw it would only affect 18 rows, I gess because of the inner join, It didn't update all the rows that I wanted and the code that I wrote it would affect first update 50 rows, and the second update would affect 18 rows, Updating all the rows that I wanted |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-25 : 00:11:28
|
ok.perhaps this was what you wanted:-Update Products SET qtyIn = coalesce(SuppTotalQty,qtyIn), qtyOut = coalesce(CustTotalQty,qtyOut)from products left outer join (select sum(qty) as SuppTotalQty, prodID from supplierOrderDetails group by prodID) SuppTot on products.prodID = SuppTot.ProdIDleft outer join (select sum(qty) as CustTotalQty, prodID from customerOrderDetails group by prodID) CustTot on products.prodID = CustTot.ProdID but without any data sample, its not easy to provide an accurate soln which is why Gila just made an attempt guessing that its soln you expected. Thats why we always suggest you to post some sample data. |
 |
|
|
|
|
|
|
|