|
Execute the following T-SQL example scripts in Microsoft SQL Server Management Studio Query Editor for minimum calculations.
USE AdventureWorks2008;
GO
-- SQL minimum of 2 values
-- SQL UDF - scalar-valued function - user-defined function
CREATE FUNCTION fnMinValue
(@ColumnA MONEY,
@ColumnB MONEY)
RETURNS MONEY
AS
BEGIN
RETURN (0.5 * ((@ColumnA + @ColumnB) - abs(@ColumnA - @ColumnB)))
END
GO
-- Test UDF
SELECT dbo.fnMinValue ( 1000.0, 1001.0)
GO
-- 1000.00
SELECT dbo.fnMinValue ( 1002.0, 1001.0)
GO
-- 1001.00
SELECT dbo.fnMinValue ( 1003.0, 1003.0)
GO
-- 1003.00 ------------
-- Minimum values by group
SELECT Color, MinListPrice=MIN(ListPrice)
FROM AdventureWorks2008.Production.Product
GROUP BY Color
/* Color MinListPrice
NULL 0.00
Black 0.00
Blue 34.99
Grey 125.00
Multi 8.99
Red 34.99
Silver 0.00
Silver/Black 40.49
White 8.99
Yellow 53.99 */
------------
Related articles:
What's the best way to select the minimum value from multiple columns?
MIN (Transact-SQL)
|