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
 General SQL Server Forums
 New to SQL Server Programming
 Calculation on an Aggregrate function

Author  Topic 

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2012-11-26 : 15:28:34
Hi All - I have the following table:
[CODE]
SKU PRICE
123456 $10
123456 $20
123456 $30
123457 $40
123457 $50
123457 $60
[/CODE]
My query to outputs a unique SKU based on the lowest price:

[CODE]
SELECT SKU, MIN(PRICE) FROM TABLE1
GROUP BY SKU
[/CODE]

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
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-26 : 15:33:44
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

121 Posts

Posted - 2012-11-26 : 15:54:59
THANKS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Go to Top of Page
   

- Advertisement -