Get intersecting rows based on a column value - sql

I have a table in SQL Server.This is how data is stored currently in table:
I want to get all common menu_id when multiple role_id are provided.
For eg: I want menu_id 1 when role_id provided are 1 and 3 as menu_id 1 is common for all role_id specified. I have no clue what query should I use. Can anyone help me?
Thanks in advance!

You seem to want menu_ids that have a list of role_id values. This should do what you want:
select menu_id
from t
where role_id in (1, 3)
group by menu_id
having count(*) = 2; -- number of values in IN list
Here is a db<>fiddle.

use group by with having
select menu_id from tablename
group by menu_id
having count(distinct role_id)>1

Related

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.

How can I make selection based on conditions on SQL?

There is a table based on ID an those ID's status keys:
The table
I need to write query that will bring higher status key of the same ID. For example; query will bring only the row with status key number 9 for ID number 123. But it will bring the row with status key number 2 for ID number 156.
Hope I managed to explain myself clearly. Please help me with this query.
Use max() aggregation
select id, max(status_key)
from tablename
group by id
You didn't tag your backend, this would work with many backends and older versions of many backends (assuming you have other columns too in your table - otherwise do only group by):
select myTable.*
from myTable
inner join
(select id, max(statusKey) as statusKey
from myTable
group by id) tmp on myTable.id = tmp.id and myTable.statusKey = tmp.statusKey;

selecting a distinct primary key and getting a count of values for them

I have a table that holds values like this,
menu_id inner_text
1 Inner 1
1 Inner 2
2 Inner 1
2 Inner 2
2 Inner 3
3 Inner 1
I want to be able to select the menu_id distinctly and get a count of values that are present beside menu_id, for example
menu_id count
1 2
2 3
3 1
I tried a few queries myself but not able to get the result I needed.
What do i do?
SELECT menu_id, COUNT(*)
FROM table
GROUP BY menu_id
ORDER BY menu_id
select menu_id,count(*)
from table
group by menu_id
please try this
SELECT MENU_ID,COUNT(*) AS COUNT FROM TABLE
GROUP BY MENU_ID
Regards
Ashutosh Arya
Depending on your RDBMS, query might look like:
SELECT
menu_id,
COUNT(1)
FROM MyTable
GROUP BY(menu_id)
Try this:
SELECT menu_id,COUNT(*) as count
FROM tablename
GROUP BY menu_id;

SQL SELECT Statement max id

I have a table called apps with id, type, and group_id.
The table has multiple group_id with same number.
I need to get the max id for each group_id number where type equals 1.
My sql skills are not so good.
Any help is greatly appreciated
select group_id, max(id) as max_id
from apps
where type = 1
group by group_id

Find Specific Rows

I'm trying to build a rather specific query to find a set of user_ids based on topics they have registered to.
Unfortunately it's not possible to refactor the tables so I have to go with what I've got.
Single table with user_id and registration_id
I need to find all user_ids that have a registration_id of (4 OR 5) AND NOT 1
Each row is a single user_id/registration_id combination.
My SQL skills aren't the best, so I'm really scratching my brain. Any help would be greatly appreciated.
SELECT *
FROM (
SELECT DISTINCT user_id
FROM registrations
) ro
WHERE user_id IN
(
SELECT user_id
FROM registrations ri
WHERE ri.registration_id IN (4, 5)
)
AND user_id NOT IN
(
SELECT user_id
FROM registrations ri
WHERE ri.registration_id = 1
)
Most probably, user_id, registration_id is a PRIMARY KEY in your table. If it's not, then create a composite index on (user_id, registration_id) for this to work fast.
Possibly not the best way to do it (my SQL skills aren't the best either), but should do the job:
SELECT user_id
FROM table AS t
WHERE registration_id IN (4, 5)
AND NOT EXISTS (SELECT user_id
FROM table
WHERE user_id = t.user_id
AND registration_id = 1);
Another way with eliminating duplicates of user_id:
SELECT user_id
FROM registrations
WHERE registration_id IN (4, 5)
except
SELECT user_id
FROM registrations
WHERE registration_id =1
One use of a table:
select user_id from registrations
where registration_id <=5
group by user_id
having MIN(registration_id)>1 and MAX(registration_id)>= 4