我有这个查询:
SELECT agent, prezent, pobs, pobsextins
FROM `raport`
WHERE agent="agent1" and (pobs="da" || prezent="da" || pobsextins="da")
它返回:
agent prezent pobs pobsextins
agent1 NU NU DA
agent1 NU NU DA
agent1 NU DA DA
agent1 DA NU DA
我必须编写什么查询来自动计算所有行中的“ DA”并将其按代理分组?
我尝试过:
SELECT agent, prezent, pobs, pobsextins, COUNT(prezent), COUNT(pobs), COUNT(pobsextins)
FROM `raport`
WHERE agent="agent1" and (pobs="da" || prezent="da" || pobsextins="da")
但是我发现它只计算该列存在多少次。
如果我尝试... COUNT(prezent) WHERE prezent="da", COUNT(pobs) WHERE...
给出错误
答案 0 :(得分:1)
您可以对个案和分组依据
select agent
,sum(case when prezent = 'DA' then 1 else 0 end ) +
sum(case when pobs = 'DA' then 1 else 0 end ) +
sum(case when pobsextins = 'DA' then 1 else 0 end ) total
from my_table
group by agent
答案 1 :(得分:0)
您可以使用并集和分组依据。 希望它对您有用。
select agent,count(*) from
(select agent from `raport` where pobs="da"
union all
select agent from `raport` where prezent="da"
union all
select agent from `raport` where pobsextins="da") myReport
group by agent;