I'm doing an inner join on a table like this:
SELECT *
FROM patient p
INNER JOIN
vaccine v
ON
p.vaccine_id = v.id
The condition f.vac_1 = mv.id might not been satisfied in the case where a person have not been vaccinated. In such case, I don't want to ignore the row, but instead of displaying the vaccine name (which is the purpose of the inner join) to display an emtpy string.
How can this be done ?
Example
Table vaccinne
id
name
1
Moderna
2
Comirnaty
3
Janssen
Table patient
id
name
vaccine_id
1
john
1
2
kermit
2
3
jessica
I'm looking for a query to produce:
id
name
vaccine_id
1
john
Moderna
2
kermit
Comirnaty
3
jessica
If I understand correctly, you want a left join starting with foo:
SELECT *
FROM foo f LEFT JOIN
vac v
ON f.vac_1 = mv.id
Related
I have two tables, A and B.
A
ID age
1 24
2 25
45 22
B
ID school
34 school1
1 school2
I want to select IDs that are in B but not in A.
I wrote
Select distinct bb.school
From B as bb
Left outer join A as aa
On bb.ID=aa.ID
inner join C as cc
On bb.school=cc.school
This code returns exactly the same number of rows that I would have with an inner join instead of left outer join.
Am I doing something wrong?
Try using not in;
Select * From A Where ID Not In ( Select ID From B )
I want to get the value of one column if it exists and leave it empty if there is no value in that column
Table 1 (Student):
id Name dateofbirth
1 John 16-09-2015
2 Mark 25-08-2016
3 Matt 20-08-2017
4 Peter 16-08-2014
Table 2 (Relationship):
id StudentID NextOfKin Relationship Active
1 1 David Mother Y
2 2 Frank Father N
3 3 Jacob Mother Y
4 3 Park Mother N
SELECT a.Name, a.dateofbirth, b.NextOfKin
FROM dbo.Student a
LEFT OUTER JOIN dbo.Relationship b
ON a.id = b.StudentID
WHERE b.Relationship = 'Mother'
AND b.Active = 'Y'
I want to do something like if b.NextOfKin has a value enter it, else leave it blank. Please can you advice
In the above case, Peter does not have a nextOf kin, and Mark does not have a mother. I still want his name to appear but if no mother it should be blank
You can coalesce() function and put all other conditions in ON Clause instead of Where Clause
SELECT a.Name, a.dateofbirth, coalesce(b.NextOfKin,'') as NextOfKin
FROM dbo.Student a
LEFT OUTER JOIN dbo.Relationship b
ON a.id = b.StudentID
and b.Relationship = 'Mother'
AND b.Active = 'Y'
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.
I need help constructing an SQL statement with Sybase to collapse rows linked by three columns, ID, Name and DEPT. I have one table TABLE1:
ID NAME DEPT CAT
1 ghi P
1 CV G
2 abc P
2 IT G
2 HC G
3 def P
3 jkl P
3 ENT G
3 MC G
The CAT column means category. The P column means person. The G column means group.
ID can be person(NAME) as well as group(DEPT).
If ID(1) is associated with a person(NAME) as well as a group(DEPT) then I want to update the person(NAME) row to have DEPT name as well and then delete the group(DEPT) row. If ID(2) is associated with one person(NAME) and more than one DEPT(group) then I want to have two rows for that NAME. If ID(3) contains two different NAME and two different DEPT then I want separate rows for each NAME and DEPT as shown below:
Below is the expected output I want:
ID NAME DEPT CAT
1 ghi CV P
2 abc IT P
2 abc HC P
3 def ENT P
3 def MC P
3 jkl ENT P
3 jkl MC P
I would like an SQL statement that returns the above rows from TABLE1. Is it possible?
This might work for you.
SELECT t1.ID, t1.NAME, t2.DEPT, t1.CAT
FROM TABLE1 AS t1
INNER JOIN TABLE1 AS t2 ON (t1.ID = t2.ID)
WHERE t2.DEPT IS NOT NULL AND t1.NAME IS NOT NULL
I have two table with name LeftTable and RightTable as below
LeftTable
obsid Name Value
1 Ronak A
2 Aakash B
3 Pankti C
4 Sanket D
RightTable
obsid Name Value
1 Jhone F
2 Isabella M
3 Jonathan L
4 javafx p
Now when I Left join in some condition the output will be as below
ResultTable
obsid Name Value obsid Name Value
-------------------------------------------------------------------------
1 Ronak A 1 Jhone F
1 Ronak A 2 Isabella M
1 Ronak A 3 Jonathan L
1 Ronak A 4 javafx p
2 Aakash B 2 Isabella M
2 Aakash B 3 Jonathan L
2 Aakash B 4 javafx p
3 Pankti C 3 Jonathan L
3 Pankti C 4 javafx p
4 Sanket D 4 javafx p
This Left Join is help me to achieve One-To-Many Relationship in my application.
Left join condition is dynamic in my application. so its just an example I shown here. Suppose Left Join condition in above exampple is LeftTable.Obsid <= RightTable.Obsid
Now I want to achieve One-To-One relationship.
One-To-One relationship means when any of righttable record linked once with any lefttable record then that righttble record can not be linked with any other lefttable record.
For above example Output for one to one is as below
obsid Name Value obsid Name Value
1 Ronak A 1 Jhone F
2 Aakash B 2 Isabella M
3 Pankti C 3 Jonathan L
4 Sanket D 4 javafx p
I have achieve this functionality two way:
Using cursor
fetching one by one record using select query
Database tables are dynamic in my application. so while processing on millions of record takes too much time as fetching one by one record and processing on it takes time.
Is there any way to achieve One-To-One relation in single select query??
I want to avoid fetching one by one record and I am using Mysql database
Please help me.
You can achieve the required output which is shown in the example by just using inner join.
select * from LeftTable L inner join RightTable R
on L.obsid=R.obsid
But I if there are duplicate obsid in the left table you can use the below query
select A.obsid,A.Name,A.Value, R.obsid,R.Name,R.Value from(
select *,row_number() over (partition by obsid order by obsid ) as row_num
from LeftTable)A
inner join RightTable R
on A.obsid=R.obsid
where A.row_num=1
Please let me know this is what you expect