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 |
|
Radhiga
Starting Member
35 Posts |
Posted - 2006-04-18 : 16:11:24
|
| SELECT department, SUM(sales) as "Total sales"FROM order_detailsGROUP BY department;along with this how can i find the departname also.i need something like this select departmentnane,departmentid,sum(sales) from order_detailsGROUP BY department; |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-04-18 : 16:15:19
|
| [code]SELECT o.departmentname, t.department, t.TotalSalesFROM order_details oINNER JOIN ( SELECT department, SUM(sales) as TotalSales FROM order_details GROUP BY department) tON o.department = t.department AND o.sales = t.TotalSales[/code]Tara Kizeraka tduggan |
 |
|
|
Radhiga
Starting Member
35 Posts |
Posted - 2006-04-18 : 16:52:27
|
| LOT OF THANKS FOR U. WORKED...THANKS SO MUCH |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-04-19 : 03:20:55
|
| Cant you use this if there is unique departmentname for each departmentid?select departmentname,departmentid,sum(sales) as Total_sales from order_detailsGROUP BY departmentname,departmentidMadhivananFailing to plan is Planning to fail |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-04-19 : 11:19:21
|
| I don't like throwing other columns into the GROUP BY when they aren't needed, even if it still works.Tara Kizeraka tduggan |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-04-19 : 11:27:53
|
quote: Originally posted by tkizer I don't like throwing other columns into the GROUP BY when they aren't needed, even if it still works.Tara Kizeraka tduggan
Tara, is it because of inefficiency ? or any other ?Srinika |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-04-19 : 12:08:43
|
| No. It's a preference. It makes it more readable to put only the things that are needed into the GROUP BY. Everything else that you need should be done via the derived table.Tara Kizeraka tduggan |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-04-20 : 01:37:38
|
quote: Originally posted by tkizer No. It's a preference. It makes it more readable to put only the things that are needed into the GROUP BY. Everything else that you need should be done via the derived table.Tara Kizeraka tduggan
Well. I have to keep this in my mind MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|