SQL Select only rows where foreign key has an attribute - sql

I have this table:
| id | Reader id | Book id | Taken date | Return date |
And, for example, 3 rows
id Reader_id Book_id Taken_date Return_date
1 1 1 1999-01-08 NULL
2 2 2 2015-03-09 2015-04-10
3 1 3 2013-01-01 2014-01-01
I need to get the id's of the readers who have returned books, so all the rows with that id in this table need to have Return_date != NULL. In the case above the query is supposed to return only the second row and not return the last row, because the reader from the last row has not returned a book. How to achieve that?

First identify the Reader_id who has to return books
SELECT Reader_id
FROM yourtable
WHERE Return_date IS NULL
Then select the readers from which is not present in above query result
Use NOT IN
SELECT *
FROM yourtable
WHERE Reader_id NOT IN (SELECT Reader_id
FROM yourtable
WHERE Return_date IS NULL)
Or use Not Exists which can handle NULL values from Sub-Query
SELECT *
FROM yourtable a
WHERE NOT EXISTS (SELECT 1
FROM yourtable b
WHERE b.Return_date IS NULL
AND a.Reader_id = b.Reader_id)

You can do this with a left join :
SELECT * FROM YourTable t
LEFT OUTER JOIN YourTable s
ON(t.reader_id = s.reader_id
and s.id <> t.id
and s.Return_date is null)
WHERE s.id is null
AND t.Return_date is not null

Try this...
select
reader_id
from
tablename
where id not in (SELECT id
FROM tablename
WHERE Return_date IS NULL)

Related

SQL Query: Filter duplicate entries by getting the entries with the maximum id number then join another table's data with it

I am trying to get all id, std_name from table1, and all id, score from table2 where std_id of table2 matches the id of table1 and deleted_at should be null for all entries of table1 and table2. But table2 can have duplicate std_ids, in that case, I only want the entries with the maximum id number from table2.
Sample table1:
id
std_name
deleted_at
1
jhon
null
2
sam
null
3
joe
null
Sample table2:
id
std_id
score
deleted_at
1
1
10
null
2
2
20
null
3
1
30
null
So far I have tried using this query:
const query = knex.select([
't1.id as t1id',
't1.std_name as name',
't2.score as score'
])
.from('table1 as t1')
.leftJoin('table2 as t2', function () {
this.on('t2.std_id', '=', 't1.id')
})
.joinRaw('left join (select MAX(id) as id, std_id from table2 group by std_id) as kst on kst.std_id = t2.std_id');
query.where({'t1.deleted_at': null}).orderBy('t1.id')
Results generated for the above query:
id
name
score
1
jhon
30
2
sam
20
But this only returns the maximum id entry of the duplicate entries from table2 and omits the entries of table1, but I also want the ids from table1 which are not included in the std_id of table2.
My desired output:
id
name
score
1
jhon
30
2
sam
20
3
joe
null
You can use window functions. In SQL this looks like:
select t1.*, t2.score
from table1 t1 left join
(select t2.*,
row_number() over (partition by t2.std_id order by t2.id desc) as seqnum
from table2 t2
) t2
on t2.std_id = t1.id and t2.seqnum = 1;

Find next row with specific value in a given row

The table I have now looks something like this. Each row has a time value (on which the table is sorted in ascending order), and two values which can be replicated across rows:
Key TimeCall R_ID S_ID
-------------------------------------------
1 100 40 A
2 101 50 B
3 102 40 C
4 103 50 D
5 104 60 A
6 105 40 B
I would like to return something like this, wherein for each row, a JOIN is applied such that the S_ID and Time_Call of the next row that shares that row's R_ID is displayed (or is NULL if that row is the last instance of a given R_ID). Example:
Key TimeCall R_ID S_ID NextTimeCall NextS_ID
----------------------------------------------------------------------
1 100 40 A 102 C
2 101 50 B 103 D
3 102 40 C 105 B
4 103 50 D NULL NULL
5 104 60 A NULL NULL
6 105 40 B NULL NULL
Any advice on how to do this would be much appreciated. Right now I'm joining the table on itself and staggering the key on which I'm joining, but I know this won't work for the instance that I've outlined above:
SELECT TOP 10 Table.*, Table2.TimeCall AS NextTimeCall, Table2.S_ID AS NextS_ID
FROM tempdb..#Table AS Table
INNER JOIN tempdb..#Table AS Table2
ON Table.TimeCall + 1 = Table2.TimeCall
So if anyone could show me how to do this such that it can call rows that aren't just consecutive, much obliged!
Use LEAD() function:
SELECT *
, LEAD(TimeCall) OVER (PARTITiON BY R_ID ORDER BY [Key]) AS NextTimeCall
, LEAD(S_ID) OVER (PARTITiON BY R_ID ORDER BY [Key]) AS NextS_ID
FROM Table2
ORDER BY [Key]
SQLFiddle DEMO
This is only test example I had close by ... but i think it could help you out, just adapt it to your case, it uses Lag and Lead ... and it's for SQL Server
if object_id('tempdb..#Test') IS NOT NULL drop table #Test
create table #Test (id int, value int)
insert into #Test (id, value)
values
(1, 1),
(1, 2),
(1, 3)
select id,
value,
lag(value, 1, 0) over (order by id) as [PreviusValue],
lead(Value, 1, 0) over (order by id) as [NextValue]
from #Test
Results are
id value PreviusValue NextValue
1 1 0 2
1 2 1 3
1 3 2 0
Use an OUTER APPLY to select the top 1 value that has the same R_ID as the first Query and has a higher Key field
Just change the TableName to the actual name of your table in both parts of the query
SELECT a.*, b.TimeCall as NextTimeCall, b.S_ID as NextS_ID FROM
(
SELECT * FROM TableName as a
) as a
OUTER APPLY
(
SELECT TOP 1 FROM TableName as b
WHERE a.R_ID = b.R_ID
AND a.Key > B.Key
ORDER BY Key ASC
) as b
Hope this helps! :)
For older versions, here is one trick using Outer Apply
SELECT a.*,
nexttimecall,
nexts_id
FROM table1 a
OUTER apply (SELECT TOP 1 timecall,s_id
FROM table1 b
WHERE a.r_id = b.r_id
AND a.[key] < b.[key]
ORDER BY [key] ASC) oa (nexttimecall, nexts_id)
LIVE DEMO
Note : It is better to avoid reserved keywords(Key) as column/table names.

How do I not select a row from sets with null and not null in SQL?

I have tried to find this on SO.
I have a table,
id | col2 | col3
---- ---- ----
5 smith (null)
5 smith 100
12 Jackson 356
12 Jackson 400
8 Johnson (null)
9 bob 1200
In this scenario I only want the rows from a set where the same id only has one non-null. In other words, I don't want smith, I don't want Johnson. I only want Jackson and bob.
I have tried,
select * from table
where is not null a
nd not exists (select * from table where is null)
I can't get it to work.
Your statement and desired results don't quite match, but this will give you every ID that does not have a NULL value in column 3:
SELECT * FROM table
WHERE id NOT IN
(SELECT ID FROM table WHERE col3 IS NULL)
If you want records with just one non-null (which you state but your expected result doesn't match, use
SELECT * FROM table
WHERE id NOT IN
(SELECT ID
FROM table
WHERE col3 IS NOT NULL
GROUP BY ID
HAVING COUNT(id) = 1
)
You can use NOT EXISTS but include a WHERE to reference each id:
select *
from yourtable t
where col3 is not null
and not exists (select id
from yourtable d
where d.col3 is null
and t.id = d.id);
See Demo

SQL Server - Select Specific Rows

I have a table with data:
id a_no accountname
1 NULL ac1
1 234 ac2
1 567 ac3
2 NULL ac4
I want to select row with the following criteria:
if same id exist in more than 1 row, then remove the row with a_no=NULL
if id exist in 1 row only, keep that row unchanged
So that the resulting rows should be
id a_no accountname
1 234 ac2
1 567 ac3
2 NULL ac4
How to write the T-SQL query? Thanks.
select * from yourtable
where a_no is not null
union all
select * from yourtable
where id not in
(select id from your table where a_no is not null)
Based on Martinjn query I'd go for:
select * from t
where id not in (
select id from t
where a_no is not null)
or a_no is not null
DELETE FROM yourtable
WHERE id IN (SELECT id FROM yourtable
WHERE NOT a_no IS NULL)
AND a_no IS NULL
Edit: Much simpler
Edit II: Misread your question, you only want to select the rows, not delete them from your table. If so, you can use the following:
SELECT * FROM yourtable
WHERE a_no IS NOT NULL
OR id NOT IN (SELECT id FROM yourtable
WHERE a_no IS NOT NULL)

SELECT only records which must fill two conditions

I have this table:
id type otherid
1 4 1234
2 5 1234
3 4 4321
As you can see there are 3 records, 2 of them belongs to otherid "1234" and got type of 4 and 5.
Last record belongs to otherid of "4321" and has only a type of 4.
I need to select all otherid that got only the type 4 and not the type5.
Example: after this select on that table the query shuould return only the record 3
Thanks
add1:
Please consider the TYPE can be any number from 1 up to 20.
I only need otherid that got type 4 but not type 5 ( except than that they can have any other type )
add2:
using mysql 5.1
This is kind of a workaround
SELECT * FROM (
SELECT GROUP_CONCAT('|',type,'|') type,other_id FROM table GROUP BY otherid
) t WHERE type LIKE '%|4|%' AND type NOT LIKE '%|5|%'
You could use a not exists subquery:
select distinct otherid
from YourTable as yt1
where yt1.type = 4
and not exists
(
select *
from YourTable as yt2
where yt1.otherid = yt2.otherid
and yt1.type <> yt2.type -- use this line for any difference
and yt2.type = 5 -- or this line to just exclude 5
)
Another way is by using a left join from where you exclude rows that have both type 4 and 5:
select a.*
from table1 a
left join table1 b on b.otherid = a.otherid and b.type = 5
where a.type = 4 and b.id is null