2010-03-12 7 views
2

나는 테이블 (사과)가 포함의 결합 합계 : 나는 사람의 수의 순서에 따라 고객의 순위를 할 방법에 대한 만드는 두 개의 열

cid date_am date_pm 
---------------------- 
1  1  1 
2  2  1 
3  1  3 
1  1  2 

내가 이전 (심하게) 질문을 (1) 그들이 가지고 있었다. 이것은 하나의 열 위대한 작품

SELECT cid, sum(date_pm) AS No_of_ones 
FROM apples 
WHERE date_am =1 
GROUP BY cid 
ORDER BY no_of_ones DESC 

하지만 내가 어떻게 두 컬럼의 합에 대해 동일 할 것이다 :이 솔루션은 (하나의 열을 기준으로)이었다. 예.

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) 

희망은 당신이 고마워요

답변

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 
+0

의도적으로 미안하지 – bsandrabr

+0

@bsandrabr을 돕기 위해 충분한 것을 명확하게 관리했습니다 : 당신은 되돌릴 수 downvote를 던진 후 사람이 대답을 편집 한 경우 upvote 화살표를 클릭하여 downvote. –

0
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 
+0

두 경우 모두에게 감사 드리며 둘 다 매우 완벽하게 감사했습니다. – bsandrabr

관련 문제