Sql 使两列相加

Sql 使两列相加,sql,sum,Sql,Sum,我有一张桌子(苹果),里面有: cid date_am date_pm ---------------------- 1 1 1 2 2 1 3 1 3 1 1 2 早些时候我问了一个问题(很糟糕),我将如何按照客户数量(1)对客户进行排名。解决方案是(基于一列): 这对一列非常有效,但我如何对两列的总和进行同样的处理呢。 即 希望我能把这一点讲清楚,让你帮个忙-谢谢 select cid, sum

我有一张桌子(苹果),里面有:

cid  date_am date_pm 
----------------------
1      1       1
2      2       1
3      1       3  
1      1       2
早些时候我问了一个问题(很糟糕),我将如何按照客户数量(1)对客户进行排名。解决方案是(基于一列):

这对一列非常有效,但我如何对两列的总和进行同样的处理呢。 即

希望我能把这一点讲清楚,让你帮个忙-谢谢

select cid, sum(case when date_pm = 1 then 1 else 0 end) + sum(case when date_am = 1 then 1 else 0 end)
from apples
group by cid


多亏了这两个案例,你们给出的答案非常有效thanks@bsandrabr:如果某人在您投下向下一票后编辑了他们的答案,您可以通过单击向上一票箭头来反转向下一票。
SELECT cid, sum( date_pm ) AS No_of_ones
FROM apples
WHERE date_am =1
add to
SELECT cid, sum( date_am ) AS No_of_ones
FROM apples
WHERE date_pm =1
GROUP by cid
ORDER by no_of_ones(added)
select cid, sum(case when date_pm = 1 then 1 else 0 end) + sum(case when date_am = 1 then 1 else 0 end)
from apples
group by cid
select cid, sum(addone) as total from 
  (select cid, 1 as addone
   from apples
    where date_pm = 1 group by cid
    union
    select cid, 1 as addone
    from apples
    where date_am = 1 group by cid) 
group by cid order by total DESC
select cid, sum(case when date_am=1 then 1 else 0 end) 
            + sum(case when date_pm=1 then 1 else 0 end) as total 
from apples 
group by CID 
order by total DESC