假设我们有下表:
index value 1 55 2 66 3 77 1 88 3 99
当index = 1或index = 3时,如何求值(值),在一个select语句中,当index = 2时,avg(value)是什么?
答案 0 :(得分:3)
轻松:
SELECT index, CASE WHEN index IN (1, 3) THEN SUM(value) ELSE AVG(value) END
FROM yourTable
GROUP BY index
答案 1 :(得分:1)
SELECT sum(value) as agg,
'S' as aggregateType
FROM DaTAble
WHERE index IN (1, 3)
UNION ALL
SELECT avg(value) as agg,
'A' as aggregateType
FROM DaTAble
WHERE index = 2