SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Many aggregates in one query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sauce1979
Starting Member

37 Posts

Posted - 05/08/2012 :  20:45:21  Show Profile  Reply with Quote
I have the following tables:


Products
(Product_id,Product_Name,Product_Type,Cost_Price)

Sales
(SalesOrder_id, Product_id,Quantity, Sell_Price, Sale_Date)

I want to produce a query that provides most profitable products, per year,week,month,day and overall.

I can do these queries individually for example the top 5 profitable products per month



SELECT TOP 5 P.Product_Name,DATEPART(MM,S.SaleDate) AS MoNTH(S.Sell_Price P.Cost_Price) * S.Quantity AS Profit
FROM Products P INNER JOIN Sales S
ON S.Product_id = P.Product_id
GROUP BY P.Product_Name,DATEPART(MM,S.SaleDate)



Is there a way to produce an output with profitable products per week, year etc in one query?

khtan
In (Som, Ni, Yak)

Singapore
16745 Posts

Posted - 05/08/2012 :  21:04:06  Show Profile  Reply with Quote
What is the expected result like ?


KH
Time is always against us


Edited by - khtan on 05/08/2012 21:04:57
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000