Specific sql query with count of records - sql

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;

Related

How to find AVG of Count in SQL

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.

BigQuery GROUP BY ... HAVING asks for other columns to be grouped

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;

SQL - Select Query to get group by records whose sum(data) > 24

I need to select a UserID from the table whose sum of Data greater than 24.
I can able to select group and sum the records using
SELECT SUM(DATA),UserID FROM TableName GROUP BY UserID
But how can I select only the records for which SUM(DATA)>24
I have tried
SELECT SUM(DATA),UserID FROM #tempTimesheetValue where SUM(DATA)>24 GROUP BY UserID
But its not working.
Thanks in advance for suggestion..,
you can do this by below query:
select UserID, DATA from (
SELECT SUM(DATA) as DATA, UserID FROM #tempTimesheetValue GROUP BY UserID
) A where DATA > 24
The question might as well have the correct answer, which is;
SELECT SUM(DATA), UserID
FROM #tempTimesheetValue
GROUP BY UserID
HAVING SUM(DATA) > 24;
A subquery could be used, but it is unnecessary complication.

How to group by in sql with condition

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;

Select statement with count

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)