注意を考慮したうえでの計算で求める総積
こんな感じ?
WITH Input(id, n) AS ( SELECT 1, 1 UNION ALL SELECT 1, 2 UNION ALL SELECT 1, 3 UNION ALL SELECT 1, 0 UNION ALL SELECT 2, 1 UNION ALL SELECT 2, -2 UNION ALL SELECT 2, -3 UNION ALL SELECT 3, 1 UNION ALL SELECT 3, -2 UNION ALL SELECT 3, 3 ) SELECT id , CASE WHEN SUM(CASE n WHEN 0 THEN 1 ELSE 0 END) >= 1 THEN 0 ELSE CASE SUM(CASE WHEN n < 0 THEN 1 ELSE 0 END) % 2 WHEN 1 THEN -EXP(SUM(LOG(NULLIF(ABS(n), 0)))) ELSE EXP(SUM(LOG(NULLIF(ABS(n), 0)))) END END AS n FROM Input GROUP BY id ORDER BY id
結果は、
id | n |
---|---|
1 | 0 |
2 | 6 |
3 | -6 |
こう。