I have a table named Patient in which I have columns like
ID Disease1 Disease2 Disease3
----------
1 4 3 2
----------
2 2 5
----------
3 6
----------
4 1
These are mapping values which I got from table Disease, in which disease names are placed like
1 hypertension
2 niddm
3 allergy
4 cough
5 floo
6 vv
etc
Now I want sql query to select
ID Disease1 Disease2 Disease3
----------
1 cough allergy niddm
----------
2 niddm floo
----------
3 vv
----------
4 HT
Please keep in mind that I have table mapped with 4,5 tables and I want original values in place of ids from all of them.
You need to join table Disease thrice on table Patient since there are three column from Patient that are dependent on Disease
SELECT a.ID,
b.Disease AS Disease1,
c.Disease AS Disease2,
d.Disease AS Disease3
FROM Patient a
LEFT JOIN Disease b
ON a.Disease1 = b.ID
LEFT JOIN Disease c
ON a.Disease2 = c.ID
LEFT JOIN Disease d
ON a.Disease3 = d.ID
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
Related
I have three tables :
field:
f_id
f_start
f_end
1
10
20
2
15
25
3
5
10
person :
p_id
p_name
1
Roger
2
John
3
Alicia
affect :
id
fk_field
fk_person
1
2
1
2
1
2
3
3
3
And I would like to select the dates and the names associated to. Like this
p_name
f_start
f_end
Roger
15
25
John
10
20
Alicia
5
10
I'm new to SQL and I don't know if i have to use JOIN or not... Thanks
You must join all 3 tables on their related columns:
SELECT p.p_name, f.f_start, f.f_end
FROM person p
INNER JOIN affect a ON a.fk_person = p.p_id
INNER JOIN field f ON f.f_id = a.fk_field;
Depending on your requirement you may need LEFT instead of INNER joins, but for this sample data the INNER joins will do.
I have two SQL tables
TABLE A
id | user | embedding
-----------------------
1 Ram [.12,.56]
2 Shyam [.23,.24]
3 Ghanshyam [.23,.39]
4 Balram [.34,.39]
TABLE B
--------------------
id | users
--------------------
1 [Ram,Shyam]
2 [Ram,Ghanshyam]
3 [Ram, Balram]
And I want to have a query that will return essentially table B but with the users replaced by their embeddings.
Desired output
-----------------------------
id | users
-----------------------------
1 [[.12,.56],[.23.,.24]]
2 [[.12,.56],[.23,.39]]
3 [[.12,.56], [.34,.39]]
How can I do this?
how about using unnest and array_agg:
select b.id , array_agg(embedding)
from TableB b
cross join unnest(b.users) c(user)
join TableA a
on c.users = a.user
group by b.Id
I would like to know what's the logic for multiple joins (for example below)
SELECT * FROM B returns 100 rows
SELECT B.* FROM B LEFT JOIN C ON B.ID = C.ID returns 120 rows
As I know using left join will returns any matching data from the left table which is B if data are found for both table. But how come when using left join, it returns more data than table B itself?
What am I do wrong or misunderstood here? Any guidance are very appreciated. Thanks in advance.
Let be table B:
id
----
1
2
3
Let be table C
id name
------------
1 John
2 Mary
2 Anne
3 Stef
Any id from b is matched with ids from c, then id=2 will be matched twice. So a left join on id will return 4 rows even if base table B has 3 rows.
Now look at a more evil example:
Table B
id
----
1
2
2
3
4
table C
id name
------------
1 John
2 Mary
2 Anne
3 Stef
Every id from b is matched with ids from c, then first id=2 will be matched twice and second id=2 will be matched twice so the result of
select b.id, c.name
from b left join c on (b.id = c.id)
will be
id name
------------
1 John
2 Mary
2 Mary
2 Anne
2 Anne
3 Stef
4 (null)
The id=4 is not matched but appears in the result because is a left join.
Look at the following example :
B = {1,2}
C = {(1,a),(1,b),(1,c),(1,d),(1,e)}
The result of B left join C will be :
1 | a
1 | b
1 | c
1 | d
1 | e
2 | null
The number of rows in the result is definitely larger than rows in B (2).
In general the number of rows in result of B left join C is bounded by B.size + C.size and not only by B.size as you think...
As per your query it do the join to B Table with C and B table is Left Table so it will display all the records of Left table in our case it is B and related from other Table in our Case it is C.
Let's say I have two tables. A students table and an observations table. If the students table looks like:
Id Student Grade
1 Alex 3
2 Barney 3
3 Cara 4
4 Diana 4
And the observations table looks like:
Id Student_Id Observation_Type
1 1 A
2 1 B
3 3 A
4 2 A
5 4 B
6 3 A
7 2 B
8 4 B
9 1 A
Basically, the result I'd like from the query would be the following:
Student Grade Observation_A_Count
Alex 3 2
Barney 3 1
Cara 4 2
Diana 4 0
In other words, I'd like to gather data for each student from the students table and for each student count the number of A observations from the observations table and tack that onto the other information. How do I go about doing this?
This is a simple join and aggregate:
select
a.Student,
a.Grade,
count(b.Id) as Observation_A_Count
from
Student a left join
Observations b on a.Id = b.Student_Id
group by
a.Student,
a.Grade
order by
1
Or, you can use a correlated subquery:
select
a.Student,
a.Grade,
(select count(*) from observations x where x.Student_Id = a.Id) as Observation_A_Count
from
Student a
order by
a.Student
You can join the table with a specific condition, by doing this you can have a field for Observation_B_Count and Observation_C_Count, etc.
SELECT Student.Student, Student.Grade, COUNT(Observation_Type.*) AS Observation_A_Count
FROM Student
LEFT JOIN Observations ON Observations.Student_ID = Student.Student_ID AND Observations.Observation_Type = 'A'
GROUP BY Student.Student, Student.Grade
Hi I am having a problems using Group By and joins between 3 tables.
I have a project table with various fields and a projectcode fields. I then have an invoice table and an hours table and each can have multiple rows per project. Both of these table have project code also.
The two SUM values are not calculating correctly and I am realy struggling to see where the issue is.
Here the sql I am using:
SELECT dbo.project.projectcode,
dbo.project.client,
dbo.project.project,
dbo.project.budget,
dbo.project.budget * 80 AS value,
SUM(dbo.harvest.hours) AS hourslogged,
SUM(dbo.salesforce.value) AS invoiced
FROM dbo.salesforce
RIGHT OUTER JOIN dbo.project
ON dbo.salesforce.projectcode = dbo.project.projectcode
LEFT OUTER JOIN dbo.harvest
ON dbo.project.projectcode = dbo.harvest.projectcode
GROUP BY dbo.project.projectcode,
dbo.salesforce.projectcode,
dbo.harvest.projectcode,
dbo.project.project,
dbo.project.client,
dbo.project.budget
Any help or tips on this would be much appreciated!
Whenever each of the two tables, dbo.salesforce and dbo.harvest, have more than 1 match for every projectcode, a mini-Cartesian product happens. Here's a simple illustration. Suppose there are tables A and B, like this:
Table A:
AID AVALUE
--- -------
1 ValueA1
2 ValueA2
Table B:
BID BVALUE AID
--- ------- ---
1 ValueB1 1
2 ValueB2 1
3 ValueB3 2
Now if we performed this join:
SELECT * FROM A JOIN B ON A.AID = B.AID
the result would be:
AID AVALUE BID BVALUE AID
--- ------- --- ------- ---
1 ValueA1 1 ValueB1 1
1 ValueA1 2 ValueB2 1
2 ValueA2 3 ValueB3 2
Enter table C:
CID CVALUE AID
--- ------- ---
1 ValueC1 1
2 ValueC2 1
3 ValueC3 1
And the join now is this:
SELECT * FROM A JOIN B ON A.AID = B.AID JOIN C ON A.AID = C.AID
What would be the result? Here:
AID AVALUE BID BVALUE AID CID CVALUE AID
--- ------- --- ------- --- --- ------- ---
1 ValueA1 1 ValueB1 1 1 ValueC1 1
1 ValueA1 1 ValueB1 1 2 ValueC2 1
1 ValueA1 1 ValueB1 1 3 ValueC3 1
1 ValueA1 2 ValueB2 1 1 ValueC3 1
1 ValueA1 2 ValueB2 1 2 ValueC3 1
1 ValueA1 2 ValueB2 1 3 ValueC3 1
As you can see, every match from B is repeated three times, for how many matches C has got. And, similarly, every match from C is repeated twice, because that is how many matches there are in B. The 'luckiest', of course, is the row from A, because it is repeated 2 × 3 = 6 times. That is a Cartesian join for you. And that's just what happens in your case too.
Not sure whether it is considered typical, but in such cases I would often group each table separately by the joining expression(s), then join the result sets. Your query would then look like this:
SELECT
p.projectcode,
p.client,
p.project,
p.budget,
p.budget * 80 AS value,
h.hourslogged,
s.invoiced
FROM dbo.project p
LEFT JOIN (
SELECT
projectcode,
SUM(dbo.salesforce.value) AS invoiced
FROM dbo.salesforce
GROUP BY projectcode
) s ON p.projectcode = s.projectcode
LEFT JOIN (
SELECT
projectcode,
SUM(dbo.harvest.hours) AS hourslogged
FROM dbo.harvest
GROUP BY projectcode
) h ON p.projectcode = h.projectcode
I'd suggest to avoid mixing right and left outer join.
Your central table is Project, so use it first.
SELECT dbo.project.projectcode,
dbo.project.client,
dbo.project.project,
dbo.project.budget,
dbo.project.budget * 80 AS value,
SUM(dbo.harvest.hours) AS hourslogged,
SUM(dbo.salesforce.value) AS invoiced
FROM dbo.project
LEFT OUTER JOIN dbo.salesforce
ON dbo.salesforce.projectcode = dbo.project.projectcode
LEFT OUTER JOIN dbo.harvest
ON dbo.project.projectcode = dbo.harvest.projectcode
GROUP BY dbo.project.projectcode,
dbo.project.project,
dbo.project.client,
dbo.project.budget
But the error come from the GROUP BY. You don't have to group by the two tables on which you are doing the aggregate, else your aggregate will not be good !