开发者

my sql sum the count values

Select Count(Case When an_user_plan.campaign_pid != 0 Then 1 End) As Email,
  Count(Case When an_user_plan.sms_pid != 0 Then 1 End) As Sms,
  Count(Case When an_user_plan.survey_pid != 0 Then 1 End) As Survey,
  sum(Email,Sms,Survey)as total
From an_user_plan 

This sum(Email,Sms,Survey)as total开发者_如何学Go have error how to sum the three values I want total as addition of three count vlaues how to do Please help me


SELECT SUM(campaign_pid != 0 +
           sms_pid != 0 +
           survey_pid != 0) AS total, ...


Try this:

Select Count(Case When an_user_plan.campaign_pid != 0 Then 1 End) +
  Count(Case When an_user_plan.sms_pid != 0 Then 1 End) +
  Count(Case When an_user_plan.survey_pid != 0 Then 1 End) as total
From an_user_plan 


While you have no GROUP BY clause, you could use variables -

SELECT @c1:=Count(...), @c2:=Count(...), @c1 + @c2 FROM table

Otherwise you can use a subquery -

SELECT c1, c2, c1 + c2 FROM (
  SELECT Count(...) c1, Count(...) c2 FROM table) t
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜