Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Calculation on an Aggregrate function
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

funk.phenomena
Posting Yak Master

Canada
121 Posts

Posted - 11/26/2012 :  15:28:34  Show Profile  Reply with Quote
Hi All - I have the following table:

SKU     PRICE
123456 $10
123456 $20
123456 $30
123457 $40
123457 $50
123457 $60

My query to outputs a unique SKU based on the lowest price:


SELECT SKU, MIN(PRICE) FROM TABLE1
GROUP BY SKU


However, I need to perform a calculation on that aggregrate function. If a price is more than $10, it will deduct $10 off it. SQL does not allow calculation on the MIN(PRICE) function. How can I achieve this?
Thanks!


sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/26/2012 :  15:33:44  Show Profile  Reply with Quote
Do the calculation inside the aggregate function
SELECT
	SKU,
	MIN(CASE WHEN PRICE > 10 THEN PRICE-10 ELSE PRICE END) AS AdjustedPrice
FROM
	Table1
GROUP BY
	SKU;

Sunita.
Go to Top of Page

funk.phenomena
Posting Yak Master

Canada
121 Posts

Posted - 11/26/2012 :  15:54:59  Show Profile  Reply with Quote
THANKS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Go to Top of Page
  Previous Topic Topic Next 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.08 seconds. Powered By: Snitz Forums 2000