- Published on
Aggregate Product in SQL Server
- Authors

- Name
- Jeevan Wijerathna
- @iamjeevanvj
Problem
Calculate
PRODUCTSUMAVGExample
Calculate Product(Multiplication) of
[Value][CategoryId]DECLARE @Temp_Table ASTABLE([Value]float,[CategoryId]int)INSERT INTO @Temp_Table (Id, Value, CategoryId)VALUES (1, 1),(2, 1),(3, 1),(1, 2),(2, 2),(3, 3),(3, 3)
Expected Result Set
| CategoryId | |
|---|---|
| 1 | 6(1*2*3) |
| 2 | 2(1*2) |
| 3 | 9(3*3) |
Solution
SELECT CategoryId,ROUND(Exp(Sum(IIf([Value]=0,0,Log([Value]))))*IIf(Min([Value])=0,0,1),2)FROM @Temp_TableGROUP BY CategoryId
Result
| CategoryId | |
|---|---|
| 1 | 6 |
| 2 | 2 |
| 3 | 9 |
Happy Coding :)