Check condition within table possibly self-join? - sql

I have a users table with many columns however I am trying to use the below 2 columns to fetch the users do not have expired = 1
The query should return only user 2. Any help is much appreciated.
user_id expired
1 1
1 0
2 0
2 0
3 1

I know you have an answer, but if you are just looking for a list of single user_id's this might work a little more efficiently:
select user_id
from users
GROUP BY user_id
having MAX(expired) = 0

You can do this using not in like below :
select * from users where user_id
not in(select distinct user_id from users where expired = 1);
See SQL Here

Related

How to create a new table that only keeps rows with more than 5 data records under the same id in Bigquery

I have a table like this:
Id
Date
Steps
Distance
1
2016-06-01
1000
1
There are over 1000 records and 50 Ids in this table, most ids have about 20 records, and some ids only have 1, or 2 records which I think are useless.
I want to create a table that excludes those ids with less than 5 records.
I wrote this code to find the ids that I want to exclude:
SELECT
Id,
COUNT(Id) AS num_id
FROM `table`
GROUP BY
Id
ORDER BY
num_id
Since there are only two ids I need to exclude, I use WHERE clause:
CREATE TABLE `` AS
SELECT
*
FROM ``
WHERE
Id <> 2320127002
AND Id <> 7007744171
Although I can get the result I want, I think there are better ways to solve this kind of problem. For example, if there are over 20 ids with less than 5 records in this table, what shall I do? Thank you.
Consider this:
CREATE TABLE `filtered_table` AS
SELECT *
FROM `table`
WHERE TRUE QUALIFY COUNT(*) OVER (PARTITION BY Id) >= 5
Note: You can remove WHERE TRUE if it runs successfully without it.

SQL SELECT WHERE IN another SELECT with GROUP_CONCAT

Good Day,
I have 3 Tables - Ticket, Ticket Batch (Multiple Ticket Rows To One Batch) and Ticket Staff (Multiple Staff Rows To One Ticket) and wish to ultimately UPDATE the ticket_batch table with the COUNT of all staff working on tickets per ticket batch.
The tables with applicable columns look as follows
ticket:
| ticket_number | recon_number |
ticket_batch:
| recon_number |
ticket_staff:
| ticket_number |
So I have written the following SQL query to essentially first if I do get the COUNT:
SELECT COUNT(*)
FROM ticket_staf
WHERE ticket_staff.ticket_number IN (SELECT GROUP_CONCAT(ticket.ticket_number) FROM ticket WHERE ticket.recon_number = 1);
Which the query just keeps running, but when I execute the queries separately:
SELECT GROUP_CONCAT(ticket.ticket_number)
FROM ticket
WHERE ticket.recon_number = 1;
I get 5 ticket numbers within split seconds and if I paste that string in the other portion of the query:
SELECT COUNT(*)
FROM ticket_staff
WHERE ticket_staff.ticket_number IN (1451,1453,1968,4457,4458);
It returns the correct COUNT.
So ultimately I guess can I not write queries with GROUP_CONCATS into another SELECT WHERE IN? And how should I structure my query?
Thanks for reading :)
I prefer Inner join as follows:
SELECT COUNT(distinct ts.*)
FROM ticket_staff ts
LEFT JOIN ticket t
ON ts.ticket_number = t.ticket_number
WHERE t.recon_number = 1;
GROUP_CONCAT() doesn't look right. I suspect you are confusing a list of values for IN with a string. They are not the same thing.
In general, I would recommend EXISTS over IN anyway:
SELECT COUNT(*)
FROM ticket_staff ts
WHERE EXISTS (SELECT 1
FROM ticket t
WHERE ts.ticket_number = t.ticket_number AND
t.recon_number = 1
);
For this query, you want an index on ticket(ticket_number, recon_number). However, I am guessing that ticket(ticket_number) is the primary key, which is enough of an index by itself.

Select Count from One to Many Table

I am using MS SQL.
I have a access group/permission one to many table setup for my security system. Users are assigned a Group. Each group can have a number of various permissions. I need a select statement that checks (returns a count) if a group has a specific permission.
The data I will have for the select statement is the GroupID of the User and the permissionName they are requesting access to.
Logically what I need is:
Get the ID of the permissionName. pID = SELECT permissionID FROM Permission_List WHERE permissionName = 'News'
Check Group_Permission Table to see if GroupID of the User intersects with permissionID equaling pID
If the count is 0 the group doesn't have access. If the count is 1 the group has access.(I'll handle this in PHP)
TABLE: Group_List
groupID groupName groupDesc
1 Standard Normal Access Level.
2 Limited Limited Access Level.
3 Medium Medium Access Level.
TABLE: Permission_List
permissionID permissionName permissionProtect permissionDesc
1 News 0 News section access.
2 Forums 0 Forums section access.
3 Contacts 0 Contact section access.
TABLE: Group_Permission
groupID permissionID
1 1
1 2
1 3
2 1
3 1
3 2
You should be able to JOIN the tables together and use the COUNT aggregate. For this example, I've assumed you are supplying a group id and a permission name.
SELECT COUNT(*)
FROM Group_Permission GP
JOIN Permission_List PL ON GP.permissionId = PL.permissionId
WHERE PL.permissionName = 'News'
AND GP.groupId = #someGroupId
If count is greater than 0, then the permission exists for that group.

SQL count query, using where clause from 2 different tables

I am new to SQL. I need to run a one-time query at a few different sites to get a count. The query needs to give me a count of all records based on a where clause. But I'm having trouble figuring out the syntax.
Here's what I tried:
SELECT COUNT(KEYS.IDXKEYID) FROM KEYS, KEYFLAGS
WHERE IDXLEVELID = 1
AND KEYFLAGS.BKEYSEVERMADE = -1
Which gave me a crazy number.
Basically, IDXKEYID is a primary key, and exists in both the KEYS and KEYFLAGS table. I want a count of all IDXKEYID records in the database that meet the above WHERE clause critera. I just want 1 simple result in 1 column/row.
COUNT
-----
12346
Thanks in advance!
SELECT COUNT(DISTINCT KEYS.IDXKEYID) -- count each key only once
FROM KEYS, KEYFLAGS
WHERE KEYS.IDXLEVELID = 1
AND KEYFLAGS.BKEYSEVERMADE = -1
AND KEYS.IDXKEYID = KEYFLAGS.IDXKEYID -- you're missing this link
Or you can write it using EXISTS
SELECT COUNT(1) -- count each key only once
FROM KEYS
WHERE KEYS.IDXLEVELID = 1
AND EXISTS (
SELECT *
FROM KEYFLAGS
WHERE KEYS.IDXKEYID = KEYFLAGS.IDXKEYID -- correlate
AND KEYFLAGS.BKEYSEVERMADE = -1)

Can I write this query using the criteria API or am I stuck with HQL?

I have the following query which I would like to write using the criteria api of NH.
select status, count(1) from (select distinct Status, post_id from post_statistics) tbl group by status
each post_id can exist multiple times in post_statistics
e.g.
id post_id status
1 1 open
1 1 edit
1 1 open
1 2 open
so the query should return the following results:
status count
open 2
edit 1
thx in advance.
mappings, classes?
if i'm thinking right, with a proper mapping this would effectively be something like
(pseudo-HQL)
select stat.Name, count(stat.Posts) from status stat