Reputation: 4166
I need to count taggers efforts, by count how many tags added by each tagger, with maximum tag for each article = 5 tags.
ex:
table structure article_tags
id user_id article_id tag_id
1 1 1 1
2 1 1 2
3 1 2 1
4 2 3 1
5 2 3 2
Is there advanced use of Group by with limit, or something like this? thx,
update:
I need to return how many tags added to articles, with max 5 for a single article, (so If someone added 30 tags for 30 articles = 30
Upvotes: 2
Views: 134
Reputation: 1269803
Is this what you want?
select user_id, least(count(*), 5)
from article_tags
group by user_id;
EDIT:
I think you want two levels of aggregation:
select article_id, sum(cnt) as restricted_tag_count
from (select user_id, article_id, least(count(*), 5) as cnt
from article_tags
group by user_id, article_id
) ua
group by article_id;
Upvotes: 7
Reputation: 4166
Solution based on 'Gordon Linoff'
SQL query:
select t.user_id,sum(t.total_tags) total
from
(
select user_id, least(count(*), 5) total_tags
from article_tags
group by user_id,article_id
) t
group by t.user_id
thx,
Upvotes: 0
Reputation: 13509
You can try something like this:-
SELECT user_id, CASE WHEN COUNT(tag_id) <= 5 THEN COUNT(tag_id)
ELSE 5 END AS TOTAL_TAGS
FROM YOUR_TABLE
GROUP BY user_id
Hope this helps.
Upvotes: 0
Reputation: 2998
SELECT USER_ID,ARTICAL_ID,if(count(*)>5,5,count(*)) as tags
from article_tags group by USER_ID,ARTICAL_ID;
Upvotes: 0