2017-08-08 7 views
1

ファイルを閲覧した回数とユーザごとにダウンロードされた回数をカウントするクエリを作成しようとしています。ユーザごとのmysqlクエリカウントと異なるカラム値に基づくファイル名

Dataset 
Userid | callaction | description 
1  | Viewed  | abc.pdf 
2  | Viewed  | abc.pdf 
2  | Viewed  | xyz.pdf 
1  | Viewed  | abc.pdf 
1  | Downloaded | abc.pdf 
1  | Downloaded | abc.pdf 
1  | Downloaded | abc.pdf 
2  | Downloaded | xyz.pdf 
1  | Downloaded | xyz.pdf 

私のクエリは次のとおりです。私が探していた結果が間違っている

select userid, description, 
count(description) as 'Number of views', 
count(description) as 'Number of Downloads' 
from tablename 
where callaction = 'VIEWFILE' OR callaction = 'DOWNLOAD' 
group by userid, description; 

しかし、これは私の閲覧とダウンロードが同じテーブルを与えている。

Userid | callaction | description | Number of views | Number of Downloads 
1  | Viewed  | abc.pdf  | 2    | 3 
1  | Viewed  | xyz.pdf  | 0    | 1 
2  | Viewed  | abc.pdf  | 1    | 0 
2  | Viewed  | xyz.pdf  | 1    | 1 

答えて

4
select userid, description, 
     sum(callaction = 'VIEWFILE') as 'Number of views', 
     sum(callaction = 'DOWNLOAD') as 'Number of Downloads' 
from tablename 
group by userid, description; 
+0

ありがとうございましたctly – stephenad

関連する問題