您可以通过 CTE 的 LEFT 连接来实现,它将所有可能的结果(0、1、2、3)返回到表和聚合中。
如果ValueX列的数据类型为TEXT:
WITH cte(total) AS (VALUES (0), (1), (2), (3))
SELECT c.total, COUNT(t.number) counter
FROM cte c LEFT JOIN tablename t
ON c.total = (t.ValueA = 'TRUE') + (t.ValueB = 'TRUE') + (t.ValueC = 'TRUE')
GROUP BY total
如果你想要 1 行中的结果:
SELECT SUM(total = 0) total_0,
SUM(total = 1) total_1,
SUM(total = 2) total_2,
SUM(total = 3) total_3
FROM (
SELECT (ValueA = 'TRUE') + (ValueB = 'TRUE') + (ValueC = 'TRUE') total
FROM tablename
)
请参阅demo。
如果ValueX列的数据类型为BOOLEAN(或INTEGER):
WITH cte(total) AS (VALUES (0), (1), (2), (3))
SELECT c.total, COUNT(t.number) counter
FROM cte c LEFT JOIN tablename t
ON c.total = t.ValueA + t.ValueB + t.ValueC
GROUP BY total
如果你想要 1 行中的结果:
SELECT SUM(total = 0) total_0,
SUM(total = 1) total_1,
SUM(total = 2) total_2,
SUM(total = 3) total_3
FROM (
SELECT ValueA + ValueB + ValueC total
FROM tablename
)
请参阅demo。