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
 Transact-SQL (2000)
 Using aggregate functions on a sub-query

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-12-07 : 08:12:18
chris writes "I have 2 tables, Orders, and OrderDetail.

Orders contains e-commerce orders placed online and the OrderDetail table contains rows of each item purchased in that order transaction.

I need to get the total sales by day for a certain time period and then compute the total for the entire time period, the min for the time period, the max and the average sales by day for the entire time period.

The following query correctly returns the totals by day. I can't figure out how to get the sum, min, max, and average for the values returned by this query.

Can you help?

SELECT sum(od.ext_price) 'DailySales'
FROM tJW_OrderDetail od, tJW_Orders o
WHERE o.order_id=od.order_id AND o.order_date>='11/01/2005' AND o.order_date<='11/30/2005'
GROUP BY o.order_date"

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2005-12-07 : 08:29:56
[code]SELECT sum(od.ext_price) as [DailySales, min(od.ext_price) as [Min], max(od.ext_price) as [Max], avg(od.ext_price) as [Avg]
FROM tJW_OrderDetail od, tJW_Orders o
WHERE o.order_id=od.order_id AND o.order_date>='11/01/2005' AND o.order_date<='11/30/2005'
GROUP BY o.order_date[/code]

-----------------
[KH]
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-12-07 : 08:32:26
and use universal date format yyyymmdd to avoid conflict with local settings

Madhivanan

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

- Advertisement -