I am trying to find users that have a count of purchase_id>x (10 in this example's case). How to condition the group by? what I did in the exam[ple below doesn't work
count(purchases.id), user_id from purchases
group by if(count(purchases.id)>10)
This is what did work (need to add a condition here)
select count(purchases.id), user_id from purchases
group by user_id
Thanks in advance
I think you want the having clause:
select count(*), user_id
from purchases
group by user_id
having count(*) > 10;
Related
This is what I have
select avg(visit_count) from ( SELECT count(user_id) as visit_count from table )group by user_id;
But I get the below error
ERROR 1248 (42000): Every derived table must have its own alias
if I add alias
then I get avg for only one user_id
What I want is the avg of visit_count for all user ids
SEE the picture for reference
Example 3,2.5,1.5
It means that your subquery needs to have an alias.
Like this:
select avg(visit_count) from (
select count(user_id) as visit_count from table
group by user_id) a
Your subquery is missing an alias. I think this is the version you want:
SELECT AVG(visit_count)
FROM
(
SELECT COUNT(user_id) AS visit_count
FROM yourTable
GROUP BY user_id
) t;
Note that GROUP BY belongs inside the subquery, as you want to find counts for all users.
I have a table that has user_id and role_id. I want to group user_id: 1 which also can multiple roles and In the end, I want to show count. Like for this user_id, there are 7 roles.
How can I achieve this in raw SQL query?
SELECT user_id, count(role_id) c
FROM atable
GROUP BY user_id
It seems that you are looking for typical group by:
select user_id,
count(role_id)
from MyTable
group by user_id;
here we group all records within MyTable by their user_id and then count all non null roled_id within each group. Depending on how role_id should be count you may want to put
count(all role_id)
to count null role_id as well as not null ones
count(distinct role_id)
to count distinct role_id in each group.
Running something like this:
SELECT user_id, username FROM `table` GROUP BY user_id HAVING COUNT(user_id) = 1
But BQ console complains that username is neither grouped nor aggregated. I'm looking at this post that explains how to remove rows that appears more than once. I'm assuming this error message is because there's no primary key or uniques in BQ? How can I get around this? I just want to eliminate repeated rows by user_id.
I just want to eliminate repeated rows by user_id.
below should do
SELECT user_id, ANY_VALUE(username) as username
FROM `table`
GROUP BY user_id
If you want one row per user_id, you can just use an aggregation function such as:
SELECT user_id, MAX(username) as username
FROM `table`
GROUP BY user_id
HAVING COUNT(user_id) = 1;
However, I might suggest using QUALIFY instead:
select t.*
from table t
where 1=1
qualify count(*) over (partition by user_id) = 1;
I want to count the number of ABC group using id.
SELECT group, count(id) as total FROM `user` WHERE group=`ABC`;
What's wrong?
Many thanks.
Include the columns in the select list in group by clause when using aggregate functions.
SELECT group, count(id) as total FROM user
WHERE group=`ABC`
GROUP BY group
Else simply get the count with out using other columns in the select statement.
SELECT count(id) as total FROM user
WHERE group=`ABC`
Try this:
SELECT group, count(id) as total FROM `user`
group by group having group like 'ABC';
If you want to get COUNT of users, who has the "group" field = "ABC"
SELECT count(id) as total FROM user WHERE group='ABC';
Also, it's better to avoid using SQL keywords in column names (GROUP is an SQL keyword)
I have simple table.
I need to build a SQL query and in result get the count of record where user re-played same game. So in this case we will have 3 in result..
You want a count group by
select user_id, count(*)
from your_table
group by user_id ;
You'll want to group by user_id and ensure the game count is greater than 1.
Assuming you're using SQL Server, please try something like:
SELECT user_id, count(*) as GamesPlayed
FROM table_name
GROUP BY user_id
HAVING count(*) > 1;