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 |
|
Vaishu
Posting Yak Master
178 Posts |
Posted - 2008-01-11 : 06:31:39
|
HIThe below is the asp code pasted here to explain what I am trying to do. How to Sum the weight of the item group by itemcode which is passed as a parameter Do While Not objRS.EOF itmcode = objRS("SKU")sSQL = "SELECT code, numpacks, weight FROM tblproducts WHERE code = '" & itmcode & "'" Set rsNumpks = Conn.Execute(sSQL) If Not rsNumpks.EOF Then numpacks = CDbl(rsNumpks("Numpacks")) weigh = CDbl(rsNumpks("Weight")) End If Set rsNumpks = NothingLoop |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-11 : 06:41:38
|
| Try:-Do While Not objRS.EOF itmcode = objRS("SKU")sSQL = "SELECT p.code, p.numpacks, t.totalweight FROM tblproducts p INNER JOIN (SELECT code,SUM(weight) as 'totalweight' FROM tblproducts GROUP BY code)t ON t.code=p.code WHERE p.code = '" & itmcode & "'" Set rsNumpks = Conn.Execute(sSQL) If Not rsNumpks.EOF Then numpacks = CDbl(rsNumpks("Numpacks")) weigh = CDbl(rsNumpks("Weight")) End If Set rsNumpks = NothingLoop |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-01-11 : 07:09:02
|
quote: Originally posted by Vaishu HIThe below is the asp code pasted here to explain what I am trying to do. How to Sum the weight of the item group by itemcode which is passed as a parameter Do While Not objRS.EOF itmcode = objRS("SKU")sSQL = "SELECT code, numpacks, weight FROM tblproducts WHERE code = '" & itmcode & "'" Set rsNumpks = Conn.Execute(sSQL) If Not rsNumpks.EOF Then numpacks = CDbl(rsNumpks("Numpacks")) weigh = CDbl(rsNumpks("Weight")) End If Set rsNumpks = NothingLoop
Also you should avoid having concatenated sql. Instead make use of stored procedure with input parametersMadhivananFailing to plan is Planning to fail |
 |
|
|
Vaishu
Posting Yak Master
178 Posts |
Posted - 2008-01-11 : 10:08:10
|
Hi Thanks for the solution but I am getting below errorThe sum or average aggregate operation cannot take a nvarchar data type as an argumentweight is a nvarchar in tablequote: Originally posted by visakh16 Try:-Do While Not objRS.EOF itmcode = objRS("SKU")sSQL = "SELECT p.code, p.numpacks, t.totalweight FROM tblproducts p INNER JOIN (SELECT code,SUM(weight) as 'totalweight' FROM tblproducts GROUP BY code)t ON t.code=p.code WHERE p.code = '" & itmcode & "'" Set rsNumpks = Conn.Execute(sSQL) If Not rsNumpks.EOF Then numpacks = CDbl(rsNumpks("Numpacks")) weigh = CDbl(rsNumpks("Weight")) End If Set rsNumpks = NothingLoop
|
 |
|
|
Vaishu
Posting Yak Master
178 Posts |
Posted - 2008-01-11 : 10:18:57
|
| HiThank you and solved it like belowSELECT p.code, p.NumPacks, p.weight, t.totalweightFROM tblProducts p INNER JOIN (SELECT code, SUM(cast(weight AS float)) AS 'totalweight' FROM tblproducts GROUP BY code) t ON t.code = p.codeWHERE (p.code = 'DENE') |
 |
|
|
|
|
|
|
|