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 |
nizguy
Starting Member
37 Posts |
Posted - 2012-07-24 : 13:38:30
|
Hello all,I wrote this query and it return 2 rows.How can i write this query return one row and the sum of the rp.Pallet_total in the middle columnThanksSELECT r.RQuoteID, rp.Pallet_total, sub_amt + fuelSurcharge as 'RegQteAmt'FROM RQuote r inner join RQuote_PItem rp on r.RQuoteID = rp.RQuoteIdWHERE r.RQuoteId = 1777336Result=======1777336 1088.5800 1260.00001777336 173.2100 1260.0000This is the output that I want==============================1777336 1261.79 1260.00 |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-24 : 13:42:42
|
select RQuoteID, Pallet_total = sum(Pallet_total), RegQteAmtfrrom(SELECT r.RQuoteID, rp.Pallet_total, sub_amt + fuelSurcharge as RegQteAmtFROM RQuote rinner join RQuote_PItem rpon r.RQuoteID = rp.RQuoteIdWHERE r.RQuoteId = 1777336) agroup by RQuoteID, RegQteAmt==========================================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. |
|
|
nizguy
Starting Member
37 Posts |
Posted - 2012-07-24 : 14:12:36
|
Perfect.... Thank youI have other questions.. Let say if I want return more columns, do i have to group by all these columns? If i want to display 20 columns then i have to group by 20 columns? does it slow down the process? Any alternate way to do this?Thanksselect RQuoteID, Pallet_total = sum(Pallet_total), RegQteAmt, name, address1, city, state, zipfrom(SELECT r.RQuoteID, rp.Pallet_total, sub_amt + fuelSurcharge as RegQteAmt ,name, address1, city, state, zipFROM RQuote rinner join RQuote_PItem rpon r.RQuoteID = rp.RQuoteIdWHERE r.RQuoteId = 1777336) agroup by RQuoteID, RegQteAmt, name, address1, city, state, zip |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-24 : 14:24:01
|
quote: Originally posted by nizguy Perfect.... Thank youI have other questions.. Let say if I want return more columns, do i have to group by all these columns? If i want to display 20 columns then i have to group by 20 columns? does it slow down the process? Any alternate way to do this?Thanksselect RQuoteID, Pallet_total = sum(Pallet_total), RegQteAmt, name, address1, city, state, zipfrom(SELECT r.RQuoteID, rp.Pallet_total, sub_amt + fuelSurcharge as RegQteAmt ,name, address1, city, state, zipFROM RQuote rinner join RQuote_PItem rpon r.RQuoteID = rp.RQuoteIdWHERE r.RQuoteId = 1777336) agroup by RQuoteID, RegQteAmt, name, address1, city, state, zip
you cant do it inlineyou need to do it via joinSELECT t.*,u.PalletTotal,u.RegQteAmtFROM(SELECT r.RQuoteID, name, address1, city, state, zipFROM RQuote rinner join RQuote_PItem rpon r.RQuoteID = rp.RQuoteIdWHERE r.RQuoteId = 1777336)tINNER JOIN(SELECT r.RQuoteID, SUM(rp.Pallet_total) AS PalletTotal, SUM(sub_amt + fuelSurcharge) as RegQteAmtFROM RQuote rinner join RQuote_PItem rpon r.RQuoteID = rp.RQuoteIdWHERE r.RQuoteId = 1777336GROUP BY r.RQuoteID)uON u.RQuoteID = t.RQuoteID ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
nizguy
Starting Member
37 Posts |
Posted - 2012-07-24 : 15:17:28
|
thanksI love this forum. Help me a lot |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-24 : 15:46:50
|
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|