How to group using exists - sql

I have the following table
personid talent
1 swim
2 play
1 play
1 swim
2 play
3 swim
3 swim
2 play
So person 1 can both swim and play. Person 2 can only play. Person 3 can only swim.
I need to get the following result
personid talent
1 both
2 play
3 swim
How can I do this using exists ?
I tried
SELECT DISTINCT personid,
CASE WHEN (EXISTS(
SELECT * FROM mytable
-- I got stuck
PS : I have a long solution that works . . But i do not like it because it is long
SELECT DISTINCT dis2.personid , CASE WHEN talcount = 2 THEN 'both'
ELSE talent END AS talent
FROM
(
SELECT personid , COUNT(talent) talcount
FROM
(
SELECT DISTINCT personid , talent
FROM my_table
) AS dis
GROUP BY personid
) dis2
JOIN my_table dis3
ON dis2.personid = dis3.personid

Do you really need to use EXISTS?
SELECT
personid,
CASE
WHEN COUNT(DISTINCT talent) = 2 THEN 'both'
ELSE MIN (talent)
END
FROM talents
GROUP BY personid

You could use the WITH clause to achieve the same effect:
WITH
DISTINCT_TALENTS(PERSONID, TALENT) AS
(SELECT DISTINCT PERSONID, TALENT
FROM TALENTS)
SELECT DISTINCT PERSONID, TALENT
FROM
(SELECT A.PERSONID,
CASE WHEN TALENT_COUNT = 2 THEN 'BOTH' ELSE A.TALENT END
FROM
DISTINCT_TALENTS A
INNER JOIN
(SELECT PERSONID, COUNT(TALENT) TALENT_COUNT
FROM DISTINCT_TALENTS
GROUP BY PERSONID) B
ON A.PERSONID = B.PERSONID)
First you create a virtual DISTINCT_TABLES table:
+------------------+
| personid talent |
+------------------+
| 1 play |
| 1 swim |
| 2 play |
| 3 swim |
+------------------+
next you create a subquery b with the following
+------------------------+
| personid talent_count |
+------------------------+
| 1 2 |
| 2 1 |
| 3 1 |
+------------------------+
you join with original DISTINCT_TALENTS to obtain
+----------+--------+--------------+
| personid | talent | talent_count |
+----------+--------+--------------+
| 1 | both | 2 |
| 1 | both | 2 |
| 2 | play | 1 |
| 3 | swim | 1 |
+----------+--------+--------------+
you take the distinct personid, talent to obtain the final result.
A solution similar to using exists is:
SELECT DISTINCT PERSONID, TALENT
FROM
(
SELECT
B.PERSONID,
CASE
WHEN A.TALENT IS NULL THEN 'swim'
WHEN B.TALENT IS NULL THE 'play'
ELSE 'both'
END TALENT
FROM
TALENTS A
FULL OUTER JOIN
TALENTS B
ON A.PERSONID = B.PERSONID
AND A.TALENT='play'
AND B.TALENT='swim'
)
And finally, also with the EXISTS function used like a lookup function:
SELECT DISTINCT PERSONID, TALENT
FROM (
SELECT A.PERSONID,
CASE
WHEN A.TALENT = 'play' AND EXISTS (SELECT 1 FROM TALENTS B WHERE A.PERSONID = B.PERSONID AND B.TALENT = 'swim')
THEN 'both'
WHEN A.TALENT = 'swim' AND EXISTS (SELECT 1 FROM TALENTS B WHERE A.PERSONID = B.PERSONID AND B.TALENT = 'play')
THEN 'both'
ELSE
A.TALENT
END TALENT
FROM
TALENTS A)

Related

Need help for MS Access Select Request using 2 tables

For a "products reservation system", I have 2 tables :
"RD", for global reservations data (fieds: ID, CustomerID, Date, ...)
"RP", for reserved products data per reservation (fields: ID, RD_ID, ProductID, Status, ...). RD_ID fits with the ID in RD table (field for joining). Status field can have these values: O, C, S.
I need to extract (with 2 Select instructions) the list of reservations and the number of reservations for which all products have status 'O' .
Data example for RP:
ID | RD_ID | ProdID | Status
----------------------------
1 | 1 | 100 | O
2 | 1 | 101 | O
3 | 1 | 102 | O
4 | 2 | 105 | O
5 | 2 | 100 | S
6 | 3 | 101 | C
7 | 3 | 102 | O
In this example, Select statement should return only RD_ID 1
For the number of ID, the following request does not work because it also includes reservations with products having different status:
SELECT COUNT(rd.ID) FROM rd INNER JOIN rp ON rp.RD_ID = rd.ID WHERE rp.Status = 'O';
Could you help me for the right Select statement?
Thank you.
SELECT rd.ID, COUNT(rd.ID) CountOfRD, status
FROM rd INNER JOIN rp ON rp.RD_ID
GROUP BY rd.ID, status
Use not exists as follows:
Select t.* from your_table t
Where t.status = 'O'
And not exists (select 1 from your_table tt
Where t.rd_id = tt.rd_id
And t.status != tt.status)
You can also use group by and having as follows:
Select rd_id
From your_table t
Group by rd_id
Having sum(case when status <> 'O' then 1 end) > 0

Select all records of one table that contain two records in another with certain id

I have two tables of 1:m relation. Need to select which People records have both records in Actions table whit id 1 and 2
People
+----+------+--------------+
| id | name | phone_number |
+----+------+--------------+
| 1 | John | 111111111111 |
+----+------+--------------+
| 3 | Jane | 222222222222 |
+----+------+--------------+
| 4 | Jack | 333333333333 |
+----+------+--------------+
Action
+----+------+------------+
| id | PplId| ActionId |
+----+------+------------+
| 1 | 1 | 1 |
+----+------+------------+
| 2 | 1 | 2 |
+----+------+------------+
| 3 | 2 | 1 |
+----+------+------------+
| 4 | 4 | 2 |
+----+------+------------+
Output
+----+------+--------------+----------
|PplId| name | Phone |ActionId |
+-----+------+-------------+----+-----
| 1 | John | 111111111111| 1 |
+-----+------+-------------+----+-----
| 1 | John | 111111111111| 2 |
+-----+------+-------------+----+-----
Return records of People that have both Have Actionid 1 and Action id 2(Have records in Actions).
Window functions are one method. Assuming actions are not duplicated for a person:
select pa.*
from (select p.*, a.action, count(*) over (partition by p.id) as num_actions
from people p join
action a
on p.id = a.pplid
where a.action in (1, 2)
) pa
where num_actions = 2;
In my opinion, getting two rows with the action detail seems superfluous -- you already know the actions. If you only want the people, then exists comes to mind:
select p.*
from people p
where exists (select 1 from actions where a.pplid = p.id and a.action = 1) and
exists (select 1 from actions where a.pplid = p.id and a.action = 2);
With the right index (actions(pplid, action)), I would expect two exists to be faster than group by.
Try this below query using subquery and join
select a.Pplid, name, phone, actionid from (
select a.pplid as Pplid, name, phone_number as phone
from People P
join Action A on a.pplid= p.id
group by a.pplid, name, phone_number
having count(*)>1 )P
join Action A on a.Pplid= p.Pplid
Try something like this
IF OBJECT_ID('tempdb..#People') IS NOT NULL DROP TABLE #People
CREATE TABLE #People (id INT, name VARCHAR(255), phone_number VARCHAR(50))
INSERT #People
SELECT 1, 'John', '111111111111' UNION ALL
SELECT 3, 'Jane', '222222222222' UNION ALL
SELECT 4, 'Jack', '333333333333'
IF OBJECT_ID('tempdb..#Action') IS NOT NULL DROP TABLE #Action
CREATE TABLE #Action (id INT, PplId INT, ActionId INT)
INSERT #Action
SELECT 1, 1, 1 UNION ALL
SELECT 2, 1, 2 UNION ALL
SELECT 3, 2, 1 UNION ALL
SELECT 4, 4, 2
GO
SELECT p.ID AS PplId
, p.name
, p.phone_number AS Phone
, a.ActionId
FROM #People p
JOIN #Action a
ON p.ID = a.PplId
WHERE p.ID IN ( SELECT PplId
FROM #Action
WHERE ActionId IN (1, 2)
GROUP BY PplId
HAVING COUNT(*) = 2 )
AND a.ActionId IN (1, 2)
GO

Count how many times a value appears in tables SQL

Here's the situation:
So, in my database, a person is "responsible" for job X and "linked" to job Y. What I want is a query that returns: name of person, his ID and he number of jobs it's linked/responsible. So far I got this:
select id_job, count(id_job) number_jobs
from
(
select responsible.id
from responsible
union all
select linked.id
from linked
GROUP BY id
) id_job
GROUP BY id_job
And it returns a table with id in the first column and number of occurrences in the second. Now, what I can't do is associate the name of person to the table. When i put that in the "select" from beginning it gives me all the possible combinations... How can I solve this? Thanks in advance!
Example data and desirable output:
| Person |
id | name
1 | John
2 | Francis
3 | Chuck
4 | Anthony
| Responsible |
process_no | id
100 | 2
200 | 2
300 | 1
400 | 4
| Linked |
process_no | id
101 | 4
201 | 1
301 | 1
401 | 2
OUTPUT:
| OUTPUT |
id | name | number_jobs
1 | John | 3
2 | Francis | 3
3 | Chuck | 0
4 | Anthony | 2
Try this way
select prs.id, prs.name, count(*) from Person prs
join(select process_no, id
from Responsible res
Union all
select process_no, id
from Linked lin ) a on a.id=prs.id
group by prs.id, prs.name
I would recommend aggregating each of the tables by the person and then joining the results back to the person table:
select p.*, coalesce(r.cnt, 0) + coalesce(l.cnt, 0) as numjobs
from person p left join
(select id, count(*) as cnt
from responsible
group by id
) r
on r.id = p.id left join
(select id, count(*) as cnt
from linked
group by id
) l
on l.id = p.id;
select id, name, count(process_no) FROM (
select pr.id, pr.name, res.process_no from Person pr
LEFT JOIN Responsible res on pr.id = res.id
UNION
select pr.id, pr.name, lin.process_no from Person pr
LEFT JOIN Linked lin on pr.id = lin.id) src
group by id, name
order by id
Query ain't tested, give it a shot, but this is the way you want to go

SQL left join two tables independently

If I have these tables:
Thing
id | name
---+---------
1 | thing 1
2 | thing 2
3 | thing 3
Photos
id | thing_id | src
---+----------+---------
1 | 1 | thing-i1.jpg
2 | 1 | thing-i2.jpg
3 | 2 | thing2.jpg
Ratings
id | thing_id | rating
---+----------+---------
1 | 1 | 6
2 | 2 | 3
3 | 2 | 4
How can I join them to produce
id | name | rating | photo
---+---------+--------+--------
1 | thing 1 | 6 | NULL
1 | thing 1 | NULL | thing-i1.jpg
1 | thing 1 | NULL | thing-i2.jpg
2 | thing 2 | 3 | NULL
2 | thing 2 | 4 | NULL
2 | thing 2 | NULL | thing2.jpg
3 | thing 3 | NULL | NULL
Ie, left join on each table simultaneously, rather than left joining on one than the next?
This is the closest I can get:
SELECT Thing.*, Rating.rating, Photo.src
From Thing
Left Join Photo on Thing.id = Photo.thing_id
Left Join Rating on Thing.id = Rating.thing_id
You can get the results you want with a union, which seems the most obvious, since you return a field from either ranking or photo.
Your additional case (have none of either), is solved by making the joins left join instead of inner joins. You will get a duplicate record with NULL, NULL in ranking, photo. You can filter this out by moving the lot to a subquery and do select distinct on the main query, but the more obvious solution is to replace union all by union, which also filters out duplicates. Easier and more readable.
select
t.id,
t.name,
r.rating,
null as photo
from
Thing t
left join Rating r on r.thing_id = t.id
union
select
t.id,
t.name,
null,
p.src
from
Thing t
left join Photo p on p.thing_id = t.id
order by
id,
photo,
rating
Here's what I came up with:
SELECT
Thing.*,
rp.src,
rp.rating
FROM
Thing
LEFT JOIN (
(
SELECT
Photo.src,
Photo.thing_id AS ptid,
Rating.rating,
Rating.thing_id AS rtid
FROM
Photo
LEFT JOIN Rating
ON 1 = 0
)
UNION
(
SELECT
Photo.src,
Photo.thing_id AS ptid,
Rating.rating,
Rating.thing_id AS rtid
FROM
Rating
LEFT JOIN Photo
ON 1 = 0
)
) AS rp
ON Thing.id IN (rp.rtid, rp.ptid)
MySQL has no support for full outer joins so you have to hack around it using a UNION:
Here's the fiddle: http://sqlfiddle.com/#!2/d3d2f/13
SELECT *
FROM (
SELECT Thing.*,
Rating.rating,
NULL AS photo
FROM Thing
LEFT JOIN Rating ON Thing.id = Rating.thing_id
UNION ALL
SELECT Thing.*,
NULL,
Photo.src
FROM Thing
LEFT JOIN Photo ON Thing.id = Photo.thing_id
) s
ORDER BY id, photo, rating

Select from other table if value exist

I created a fiddle for this, at this link:
http://www.sqlfiddle.com/#!2/7e007
I could'nt find SQL compact / CE so it's in MySQL.
The tables looks like this
Records Clients
ID | NAME | AGE ID | NAME
------------------ ----------------
1 | John | 20 1 | John
2 | Steven | 30 2 | Daniel
3 | Abraham | 30 3 |
4 | Donald | 25 5 | Lisa
6 | | 35 6 | Michael
7 | | 42 7 |
I would like to select from both tables, and if the id is in both tables and both have names I would like the the name from "Clients" as the default. If the name in Records is blank, use the client name (if any) and if the Clients.Name is blank; use the records.Name.
From tables above, i would like this:
ID | NAME | AGE
------------------
1 | John | 20
2 | Daniel | 30
3 | Abraham | 30
4 | Donald | 25
5 | Lisa |
6 | Michael | 35
7 | | 42
How do i do this in SQL Compact?
EDIT:
Thanks to great answers below i've managed to come up with this query, which ALMOST works:
SELECT t.id, t.name, t.age FROM
(
(
SELECT r.id,
CASE WHEN r.name = NULL THEN c.name ELSE r.name END as name,
r.age
FROM Records r
LEFT JOIN Clients c ON c.id = r.id
)
UNION
(
SELECT c.id, c.name, null as age FROM Clients c where c.id NOT IN (select id from Records)
)
) as t ORDER BY t.id
This gives me this output:
ID | NAME | AGE
------------------
1 | John | 20
2 | Daniel | 30
3 | Abraham | 30
4 | Donald | 25
5 | Lisa |
6 | | 35
7 | | 42
"Michael" (should be on #6) is missing in this case. Why?!
select r.id,
IF(c.name != '',c.name,r.name) as name,
r.age
FROM Records r
LEFT JOIN Clients c ON c.id = r.id
GROUP BY c.id
Use above query.
EDITED:
SELECT t.id, t.name, t.age FROM
(
(
SELECT r.id,
CASE WHEN c.name <> '' THEN c.name ELSE r.name END as name,
r.age
FROM Records r
LEFT JOIN Clients c ON c.id = r.id
)
UNION
(
SELECT c.id, c.name, null as age FROM Clients c where c.id NOT IN (select id from Records)
)
) as t ORDER BY t.id
Use this query.
please try,hope this will work..
select c.id,
IF(NAME='',(select name from Records where id = c.id),'')
If(NAME=NULL,(select name from Records where id = c.id),NULL)
Else c.NAME
from client c;
cheers!!!
select case when a.id <> '' then a.id else b.id end as id ,
case when a.name <> '' then a.name else b.name end as name,a.age
from records a
full outer join clients b on a.Id = b.id
order by a.id
Use COALECSE to get the first not-null value:
select id, coalesce(clients.name, records.name) as correct_name, records.age
from records
join clients using (id);
EDIT: In case not existing names are stored as '' instead of NULL use:
select id, case when clients.name = '' then records.name else clients.name end as correct_name, records.age
from records
join clients using (id);
Of course you can also react on both '' and NULL by asking
when clients.name = '' or clients.name is null then
See http://www.sqlfiddle.com/#!2/7e007/36.