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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 help pls

Author  Topic 

Radhiga
Starting Member

35 Posts

Posted - 2006-04-18 : 16:11:24
SELECT department, SUM(sales) as "Total sales"
FROM order_details
GROUP BY department;


along with this how can i find the departname also.
i need something like this

select departmentnane,departmentid,sum(sales) from order_details
GROUP BY department;


tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-04-18 : 16:15:19
[code]
SELECT o.departmentname, t.department, t.TotalSales
FROM order_details o
INNER JOIN
(
SELECT department, SUM(sales) as TotalSales
FROM order_details
GROUP BY department
) t
ON o.department = t.department AND o.sales = t.TotalSales
[/code]

Tara Kizer
aka tduggan
Go to Top of Page

Radhiga
Starting Member

35 Posts

Posted - 2006-04-18 : 16:52:27
LOT OF THANKS FOR U. WORKED...THANKS SO MUCH
Go to Top of Page

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_details
GROUP BY departmentname,departmentid



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 Kizer
aka tduggan
Go to Top of Page

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 Kizer
aka tduggan



Tara, is it because of inefficiency ? or any other ?

Srinika
Go to Top of Page

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 Kizer
aka tduggan
Go to Top of Page

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 Kizer
aka tduggan


Well. I have to keep this in my mind

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -