选择做出最积极贡献的用户

时间:2013-09-18 14:09:48

标签: mysql sql

我以为我有这个,但很明显我没有。从下表中,我试图展示那些做出最积极贡献(文章)的用户,然后是那些没有做出贡献的用户。表格很简单,artc_id是文章ID,artc_status是显示文章是否获得批准的状态。 0已获批准,1未获批准,然后是撰写文章的用户。

我想要达到的结果如下:

Total Contributions Positive    Contributing User
4                   4           2
3                   2           1
1                   1           4
3                   0           3

表格

"id"    "artc_id"   "artc_status"   "artc_user" "artc_country"
"1"     "1"         "0"             "1"         "US"
"2"     "2"         "0"             "1"         "US"
"3"     "3"         "1"             "1"         "US"
"4"     "4"         "0"             "2"         "US"
"5"     "5"         "0"             "2"         "US"
"6"     "6"         "0"             "2"         "US"
"7"     "7"         "0"             "2"         "US"
"8"     "8"         "1"             "3"         "US"
"9"     "9"         "1"             "3"         "US"
"10"    "10"        "1"             "3"         "US"
"11"    "11"        "0"             "4"         "US"

我提出的Sql

select count(artc_status) as stats , artc_user from contributions where artc_status = 0 group by artc_user order by  stats desc;

我没有太多运气得到像我上面发布的结果。你能帮忙吗?这完全超出了我的范围。

2 个答案:

答案 0 :(得分:1)

select 
     count(artc_status) as stats , 
     count(case when artc_status=1 then 1 end) Positive,      
     artc_user[Contributing User] 
from 
    contributions 
group by 
    artc_user 
order by  stats desc;

答案 1 :(得分:0)

我认为您只需要条件聚合来获取两个摘要列:

select count(*) as TotalContributions, count(artc_status = 0) as PositiveContributions, artc_user
from contributions
group by artc_user
order by PositiveContributions desc;