大家好,我在查询方面需要一些帮助。我有一个由许多列组成的表,它们从“过期”,“有风险”,“不适用”等数据中得到相同的结果。
我如何将不同列中的“风险”示例计算为一个。
例如:
---------------------------------------------
| 1_status1 | 1_status2 | 1_status3 |
| NA | at-risk | at-risk |
答案 0 :(得分:0)
如果这不是您正在寻找的内容,我会更新此内容(您的要求在您正在寻找的内容方面相当模糊)。但您可以为所有列执行Union All
并在结果上运行Count
聚合:
Select Status, Count(*) Count
From
(
Select `1_status1` As Status
From Table
Union All
Select `1_status2` As Status
From Table
Union All
Select `1_status3` As Status
From Table
) As A
Group by Status
答案 1 :(得分:0)
假设您想要计算有风险的次数'在所有行和列中都会出现,执行此操作的一种方法是使用一组union all语句来旋转表(在其轴上转动它),然后执行常规计数查询。
此查询:
select value, count(*) as "count"
from
(
select '1_status1' as type, "1_status1" as value from t
union all
select '1_status2' as type, "1_status2" as value from t
union all
select '1_status3' as type, "1_status3" as value from t
) a
--where value = 'at-risk' -- uncomment to limit the count to 'at-risk'
group by value
根据你的数据,会给出这个结果:
value count
at-risk 2
NA 1