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

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;

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.

Is group by good for this use case?

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.

Write a SQL Query to find different users linked to same User ID

So I'm currently on a project where I'm working with multiple sources, and one of them is SAP data.
I need to return "duplicates" in essence and find all the different users, that are linked to the same SAP User ID. There are entries that are valid however, as the data describes access roles to the different SAP systems. So it is normal if the same user occurs more than once. But I need to find where there is a different name assigned to the same User ID.
This is what I currently have:
select *
from (
select *,
row_number() over (partition by FULL_NAME order by USER_ID) as row_number
from SAP_TABLE
) as rows order by USER_ID desc
Any help would be appreciated. Thanks!
You would partition by the user_id
select *
from (
select *,
count(distinct (full_name)) over (partition by user_id) as rnk
from SAP_TABLE
) as rows
where rnk>1
order by USER_ID desc
are you looking for this?
select count(distinct FULL_NAME),
USER_ID
from SAP_TABLE
group by USER_ID
having count(distinct FULL_NAME) > 1
You can use this piece of code to find all the user id's that occur more than once.
SELECT USER_ID, COUNT(*)
FROM SAP_TABLE
GROUP BY 1
HAVING COUNT(*) > 1;

Specific sql query with count of records

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;

Is it possible to get a function result with columns which are not in the group by (SQL)?

I am trying to get the last registration date of a course, but I want to know the id of thar record. As MAX is a function, I must use group by id, which I do not want, because the result is very different (From only one record to each record per id).
Which is the way to manage a query like this?:
SELECT id, MAX(registration_date) AS registration_date
FROM courses;
Because it gives an error and I must do this to avoid it:
SELECT id, MAX(registration_date) AS registration_date
FROM courses
GROUP BY id;
And I do not want the result of the last one.
You could use the rank() window function for that:
SELECT id
FROM (SELECT id, RANK() OVER (ORDER BY registration_date DESC) AS rk
FROM courses)
WHERE rk = 1
One method is to use a sub query like this:
select *
from [dbo].[Courses]
where registration_date =
(select max(registration_date)
from [dbo].[Courses])
but with only a date to match this may return more than one record.
If possible, include more fields in the where clause to narrow it down.