Merge data in one table SQL - sql

I have 2 tables A, B
Table A:
ID Value1 Value2
------------------------
1000 10 25
1001 4 12
1002 2 6
1003 1 8
Table B:
ID Value3 Value4
------------------------
1000 51 12
1003 3 10
What I need is to show the below result:
ID Value1 Value2 Value3 Value4
-----------------------------------------
1000 10 25 51 12
1001 4 12 NULL NULL
1002 2 6 NULL NULL
1003 1 8 3 10
The query I've tried:
SELECT I.AgentId AS AGENT
,COUNT(I.Indice) AS [Number of Inbound calls]
,AVG(I.WrapupDuration) AS [AVG Wrapup IN]
,COUNT(O.Indice) AS [Number of Out calls]
,AVG(O.WrapupDuration) AS [AVG Wrapup Out]
FROM [HN_Ondata].[dbo].[vwInboundCalls] I
LEFT JOIN [HN_Ondata].[dbo].vwOutboundCalls O
ON I.AgentId = O.AgentId
GROUP BY I.AgentId

You just need a LEFT JOIN and a query like below, you don't need another table:
SELECT *
FROM TABLE_A A
LEFT JOIN TABLE_B B ON A.ID = B.ID

You have to use LEFT JOIN to achieve your goal.
SELCT * FROM TABELA LEFT JOIN TABLEB ON TABLEA.ID = TABLEB.ID

You should use left join
SELECT
*
FROM
TABLEA T1
LEFT JOIN
TABLEB T2
ON
T1.ID = T2.ID
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.

Related

How to join only latest date values from another table and prevent duplication

I'm trying to lookup a unique value from table b and get it into table a.
Table b stores multiple values that are changing by date.
I would like to join but only getting the values with the latest date from table b.
Table a
Unique ID
1
2
Table b
Date Unique ID Price
01/01/2019 1 100
01/02/2019 1 101
01/03/2019 1 102
01/01/2019 2 90
01/02/2019 2 91
01/03/2019 2 92
Expected result
Unique ID Price Date
1 102 01/03/2019
2 92 01/03/2019
Appreciate your help!
Have a sub-query that returns each UniqueID together with its max date. IN that result.
select * from tablename
where (UniqueID, date) in (select UniqueID, max(date)
from tablename
group by UniqueID)
You want correlated subquery :
select b.*
from tableb b
where b.date = (select max(b1.date) from tableb b1 where b1.UniqueID = b.UniqueID);
If you want to go with JOIN then you can do JOIN with subquery :
select a.UniqueID , b.Price, b.Date
from tablea a inner join
tableb b
on b.UniqueID = a.UniqueID
where b.date = (select max(b1.date) from tableb b1 where b1.UniqueID = a.UniqueID);
A correlated subquery?
select b.*
from b
where b.date = (select max(b2.date) from b b2 where b2.unique_id = b.unique_id);

How to get Oracle to return unique results in a one to many relationship tables with a left join

I have a three tables
Table 1
Id Department
1 A
2 B
3 C
4 D
Table 2
Id DepartId Name
1 1 ABC
2 1 DEF
3 1 ASD
4 2 FGH
5 2 HJK
6 3 ZXC
Table 3
Id Depart Area
1 A pp
2 B
3 C nn
4 D oo
I need the result
Id Depart Name Area
1 A ABC pp
2 B FGH Null
3 C ZXC nn
4 D NULL oo
I need one matching entry from table 2 and table 3 to corresponding entry in the table 1
Do a left join to also get t1 rows without any reference in the t2 table. GROUP BY to get only 1 row per Department.
select t1.id, t1.Department, min(t2.Name)
from t1
left join t2 on t1.id = t2.DepartId
group by t1.id, t1.Department
I think I would do this with a correlated subquery:
select t1.*,
(select t2.name
from t2
where t1.id = t2.DepartId and rownum = 1
) as t2name
from t1;
This saves the overhead of an aggregation. An index on t2(DepartId, name) is optimal for this query.
by the way not the answer to your specific question but if instead of just one you want all the names you can use listagg
SELECT t1.id,
department,
LISTAGG (name, ',') WITHIN GROUP (ORDER BY name) names
FROM t1, t2
WHERE t1.id = t2.departId(+)
GROUP BY t1.id, department
ORDER BY 1
ID Department Names
1 A ABC,ASD,DEF
2 B FGH, HJK
3 C ZXC
4 D

How to get the top row in join which is ordered by another column

I have an inner join on 2 tables.
Consider
Table1
sid name
1 abc
2 xyz
Table2
sdid sid detailname
1 1 a
2 1 b
3 2 x
So my query looks like below
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.sid=t2.sid
the result i get is
sid name sdid sid detailname
1 abc 1 1 a
1 abc 2 1 b
2 xyz 3 2 x
I want to modify this query to get the highest 'sdid' from table 2
my end result should look like
sid name sdid sid detailname
1 abc 2 1 b
2 xyz 3 2 x
Include one more subquery in the join to get the max sdid for each sid from table2.
SELECT t1.sid,t1.name,t2.sdid,t2.sid,t2.detailname
FROM table1 t1
INNER JOIN table2 t2 ON t1.sid=t2.sid
INNER JOIN (select max(sdid) as maxsdid, sid from table2 group by sid) t21
ON t21.sid=t2.sid and t21.sdid = t2.sdid

Subquery returned more than 1 value in sql server

SELECT * FROM TableC
INNER JOIN TableB ON TableB.mid=TableC.mid
INNER JOIN TableA ON TableA.userid=(
SELECT distinct userid
FROM TableB)
Subquery returned more than 1 value.
Medical_Master
MedicalID MedicalName
1(pk) abc
2 xyx
3 pqr
Child_Medical_Master
ChildMID MedicalID Station Name
1(pk) 1(fk) bnb mfk
2 1 def rwr
3 2 re wrw
Medical_Visit
VTID PMID RFMID age
1(pk) 2(fk) 1 34
2 2 3 45
3 3 1 45
4 1 2 44
5 2 2 76
Medical_Study
UID VTID ChildMID SMID Date time
1(pk) 1(fk) 1 1 kk jdj
2 2 3 2 kdf lfl
6 3 2 3 rgr rtr
Doctor_Master
RFMID Doctorname
1(pk) mr.john
2 mr.jack
3 mr.jim
PAtient_Master
PMID Firstname LastNAme
1(pk) df ere
2 rwe rwer
3 rwr fwr
Study_Master
SMID MedicalID Description Duration
1(pk) 1(fk) fdf efe
2 1 ddf dfdf
3 2 df ef
I want these columns from tables how should be my correct query?
UID,PMID,FIRSTNAME,LASTNAME,AGE,MEDICALNAME,DESCRIPTION,STATION,DATE,DoctorName
Assuming you don't want to do a normal join and there is a purpose to the subquery over a normal join:
You either need to limit what is coming into the subquery like this:
select * from TableC
inner join TableB on TableB.mid=TableC.mid
inner join TableA on TableA.userid=(select distinct userid from TableB where userid=3)
or change your main query like this:
select * from TableC
inner join TableB on TableB.mid=TableC.mid
inner join TableA on TableA.userid in (select distinct userid from TableB)
Okay, got the code and made a sqlfiddle for you to see it working.
select
medical_study.uid,
patient_master.PMID,
patient_master.firstname,
patient_master.surname,
medical_visit.age,
medical_master.medicalName,
study_master.descripto,
child_medical_master.station,
medical_study.dater,
doctor_master.doctorname
from
medical_master
join child_medical_master
on medical_master.medicalID=child_medical_master.medicalID
join medical_study
on child_medical_master.childMID=medical_study.childMID
join medical_visit
on medical_study.VTID=medical_visit.VTID
join doctor_master
on medical_visit.RFMID=doctor_master.RFMID
join patient_master
on medical_visit.PMID=patient_master.PMID
join study_master
on medical_master.medicalID=study_master.medicalID
try this:
select * from TableC
inner join TableB on TableB.mid=TableC.mid
inner join TableA on TableA.userid=TableB.userid

SQL Select Statement issue - returning rows conditionally on a 2nd table

I could really use some help with the following SQL Select statement scenario:
I need to select all rows from a table conditionally depending on whether a userID has already entered data into a second table with the same ID.
Example:
Select all rows from TABLE A for idNumber where idNumber not in
TABLE B
but for each idNumber that IS in TABLE B, still return row unless a
specific userID is in that row in TABLE B.
TABLE A
========
idNumber|type|Date
1 A 01/01/01
2 A 01/01/01
3 B 01/01/01
4 B 01/01/01
5 B 01/01/01
TABLE B
========
idNumber|type|userID
1 A 0000
3 B 0000
4 B 1111
userID to exclude records for = 1111
SQL Query should return:
idNumber|type|Date
1 A 01/01/01
2 A 01/01/01
3 B 01/01/01
5 B 01/01/01
Apologies for the long winded post but i hope it makes sense.
Many thanks in advance,
ukjezza.!!
Select idNumber, type, Date
From TableA
Where Not Exists (
Select 1
From TableB
Where TableB.idNumber = TableA.idNumber
And TableB.userID = 1111
)
Another choice:
Select TableA.idNumber, TableA.type, TableA.Date
From TableA
Left Join TableB
On TableB.idNumber = TableA.idNumber
And TableB.userId = 1111
Where TableB.idNumber Is Null
Looks like a LEFT JOIN and COALESCE could take care of it:
SELECT a.*
FROM TableA as a
LEFT JOIN TableB as b
ON a.idNumber = b.idNumber
WHERE COALESCE(b.userID, -1) != 1111
select A.*
from TableA as A
left outer join TableB as B
on A.idNumber = B.idNumber
where B.idNumber is null or
B.userID <> '1111'