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.
Related
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
I have these tables and values:
Person Account
------------------ -----------------------
ID | CREATED_BY ID | TYPE | DATA
------------------ -----------------------
1 | 1 | T1 | USEFUL DATA
2 | 2 | T2 |
3 | 3 | T3 |
4 | 4 | T2 |
Person_account_link
--------------------------
ID | PERSON_ID | ACCOUNT_ID
--------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 3
4 | 3 | 4
I want to select all persons with T1 account type and get the data column, for the others persons they should be in the result without any account information.
(I note that person 1 has two accounts : account_id_1 and account_id_2 but only one row must be displayed (priority for T1 type if exist otherwise null)
The result should be :
Table1
-----------------------------------------------------
PERSON_ID | ACCOUNT_ID | ACCOUNT_TYPE | ACCOUNT_DATA
-----------------------------------------------------
1 | 1 | T1 | USEFUL DATA
2 | NULL | NULL | NULL
3 | NULL | NULL | NULL
4 | NULL | NULL | NULL
You can do conditional aggregation :
SELECT p.id,
MAX(CASE WHEN a.type = 'T1' THEN a.id END) AS ACCOUNT_ID,
MAX(CASE WHEN a.type = 'T1' THEN 'T1' END) AS ACCOUNT_TYPE,
MAX(CASE WHEN a.type = 'T1' THEN a.data END) AS ACCOUNT_DATA
FROM person p LEFT JOIN
Person_account_link pl
ON p.id = pl.person_id LEFT JOIN
account a
ON pl.account_id = a.id
GROUP BY p.id;
You would need an outer join, starting with Person and then to the other two tables. I would also aggregate with group by and min to tackle the situation where a person would have two or more T1 accounts. In that case one of the data is taken (the min of them):
select p.id person_id,
min(a.id) account_id,
min(a.type) account_type,
min(a.data) account_data
from Person p
left join Person_account_link pa on p.id = pa.person_id
left join Account a on pa.account_id = a.id and a.type = 'T1'
group by p.id
In Postgres, I like to use the FILTER keyword. In addition, the Person table is not needed if you only want persons with an account. If you want all persons:
SELECT p.id,
MAX(a.id) FILTER (a.type = 'T1') as account_id,
MAX(a.type) FILTER (a.type = 'T1') as account_type,
MAX(a.data) FILTER (a.type = 'T1') as account_data
FROM Person p LEFT JOIN
Person_account_link pl
ON pl.person_id = p.id LEFT JOIN
account a
ON pl.account_id = a.id
GROUP BY p.id;
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
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)
I have the following query:
select bb.Name, COUNT(*) as Num from BOutcome bo
JOIN BOffers bb ON bo.ID = bb.BOutcomeID
WHERE bo.EventID = 123 AND bo.OfferTypeID = 321 AND bb.NumA > bb.NumB
GROUP BY bb.Name
The table looks like:
Name | Num A | Num B
A | 10 | 3
B | 2 | 3
C | 10 | 3
A | 9 | 3
B | 2 | 3
C | 9 | 3
The expected output should be:
Name | Count
A | 2
B | 0
C | 2
Because when name is A and C then Num A is bigger to times than Num B and when Name is B, in both records Num A is lower than Num B.
My current output is:
Name | Count
A | 2
C | 2
Because B's output is 0, i am not getting it back in my query.
What is wrong with my query? how should I get it back?
Here is my guess. I think this is a much simpler approach than all of the left/right join hoops people have been spinning their wheels on. Since the output of the query relies only on columns in the left table, there is no need for an explicit join at all:
SELECT
bb.Name,
[Count] = SUM(CASE WHEN bb.NumA > bb.NumB THEN 1 ELSE 0 END)
-- just FYI, the above could also be written as:
-- [Count] = COUNT(CASE WHEN bb.NumA > bb.NumB THEN 1 END)
FROM dbo.BOffers AS bb
WHERE EXISTS
(
SELECT 1 FROM dbo.BOutcome
WHERE ID = bb.BOutcomeID
AND EventID = 123
AND OfferTypeID = 321
)
GROUP BY bb.Name;
Of course, we're not really sure that both Name and NumA/NumB are in the left table, since the OP talks about two tables but only shows one table in the sample data. My guess is based on the query he says is "working" but missing rows because of the explicit join.
Another wild guess. Feel free to downvote:
SELECT ba.Name, COUNT(bb.BOutcomeID) as Num
FROM
( SELECT DISTINCT ba.Name
FROM
BOutcome AS b
JOIN
BOffers AS ba
ON ba.BOutcomeID = b.ID
WHERE b.EventID = 123
AND b.OfferTypeID = 321
) AS ba
LEFT JOIN
BOffers AS bb
ON AND bb.Name = ba.Name
AND bb.NumA > bb.NumB
GROUP BY ba.Name ;