JOIN multiple fields to one field - sql

dbname : table_name
table : abc
Remark1 Remark2 Remark3 Remark4 Remark5
1 2 3 4 5
table : xyz
Kod_type description
1 xxxx
2 yyyy
3 zzzz
4 aaaa
5 bbbb
how do i join Remark1,Remark2,Remark3,Remark4,Remark5 with kod_type?

Use AND Condition with On clause while joining two tables.
If you wants to match all remarks column should match to Kod_Type
SELECT abc.*,
xyz.*
FROM abc
INNER JOIN xyz
ON abc.Remark1 = xyz.Kod_Type
AND abc.Remark2 = abc.Remark1
AND abc.Remark3 = abc.Remark1
AND abc.Remark4 = abc.Remark1
AND abc.Remark5 = abc.Remark1
If you want the record in which any of the remarks column match to Kod_Type
SELECT abc.*,
xyz.*
FROM abc
INNER JOIN xyz
ON abc.Remark1 = xyz.Kod_Type
OR abc.Remark2 = xyz.Kod_Type
OR abc.Remark3 = xyz.Kod_Type
OR abc.Remark4 = xyz.Kod_Type
OR abc.Remark5 = xyz.Kod_Type

You do it the same way you will do normally -
SELECT ABC.*, XYZ.* FROM XYZ, ABC
WHERE
XYZ.KOD_TYPE=ABC.REMARK1
AND XYZ.KOD_TYPE=ABC.REMARK2
AND XYZ.KOD_TYPE=ABC.REMARK3
AND XYZ.KOD_TYPE=ABC.REMARK4
AND XYZ.KOD_TYPE=ABC.REMARK5
If you need query where any one remark matches -
SELECT ABC.*, XYZ.* FROM XYZ, ABC
WHERE
XYZ.KOD_TYPE=ABC.REMARK1
OR XYZ.KOD_TYPE=ABC.REMARK2
OR XYZ.KOD_TYPE=ABC.REMARK3
OR XYZ.KOD_TYPE=ABC.REMARK4
OR XYZ.KOD_TYPE=ABC.REMARK5

The question is not very clear, but I think something like this was intended.
SELECT COALESC(d1.description, '') as description1
, COALESC(d2.description, '') as description2
, COALESC(d3.description, '') as description3
, COALESC(d4.description, '') as description4
, COALESC(d5.description, '') as description5
FROM abc
LEFT JOIN xyz d1 ON d1.kod_type=abc.remark1
LEFT JOIN xyz d2 ON d2.kod_type=abc.remark2
LEFT JOIN xyz d3 ON d3.kod_type=abc.remark3
LEFT JOIN xyz d4 ON d4.kod_type=abc.remark4
LEFT JOIN xyz d5 ON d5.kod_type=abc.remark5
;

Related

SQL - join tables on multiple columns

I want to join few tables:
table1:
A B_key B_version C D
123 abc 1 ccc 11
123 abc 2 ddd 11
456 dfg 1 rrr 22
789 vvv 1 55
table2:
A E F
123 s 5
456 r
111 t 2
table3:
B_key B_version G
abc 1 aa
abc 1 bb
abc 2 aa
abc 2 cc
dfg 1 aa
so the result would look like this:
A B_key B_version C D E F G
123 abc 1 ccc 11 s 5 aa
123 abc 1 ccc 11 s 5 bb
123 abc 2 ddd 11 s 5 aa
123 abc 2 ddd 11 s 5 cc
456 dfg 1 rrr 22 r aa
789 vvv 1 55
Version can go as high as 8.
IF I don't have A, B_key or B_version - the line is useless. Otherwise I need to keep all the information I do have.
In reality I have many more columns.
I've tried:
SELECT table1.A, table 1.B_key, table 1.B_version, table 1.C, table 1.D,
table2.E, table2.F,
table3.G
FROM table1
LEFT JOIN table2
ON table1.A = table2.A
LEFT JOIN table3
ON table1.B_key = table3.B_key AND
table1.B_version = table3.B_version
and the same with FULL JOIN.
It ends up the same: for every B_key only the highest B_version is kept, while the others disappear.
How can I avoid loosing information?
You can use left joins among tables as below :
select t1.A, t1.B_key, t1.B_version, t1.C, t1.D, t2.E, t2.F, t3.G
from table1 t1
left join table2 t2 on t2.A = t1.A
left join table3 t3 on t3.B_key = t1.B_key and t3.B_version = t1.B_version
Demo
in order to bring also the rows for unmatched values for join conditions.
If I understand correctly, you want all the b_keys and b_versions from table1 and table3. Then you want to bring in the other data. That suggests left joins
select . . .
from ((select B_key, B_version
from table1
) union -- on purpose to remove duplicates
( select B_key, B_version
from table3
)
) bb left join
table1 t1
on t1.b_key = bb.b_key and
t1.b_version = bb.b_version left join
table2 t2
on t2.a = t1.a left join
table3 t3
on t1.b_key = bb.b_key and
t1.b_version = bb.b_version;

Merge data in one table 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.

how to get Unique data from one table and the other

If I want to display Order_Number data1 data2 data3 (most current by date changed OtherData1, OtherData2, OtherData3) date_changed the problem is I wasn't just one line, I was getting multiple lines for each order number.
What I would love to get is
1, a, f,q, cc,ccc,abc, 12/2/2014, bob
3, c, b,h, aa,aaa,abc, 12/2/2014, bob
Thanks!
I was working with
SELECT
t.Order_Number,
cr.data1, cr.data2, cr.data3,
t.OtherData1, t.OtherData2, t.OtherData3,
x.date_changed, cr.name
FROM
(SELECT
Order_Number,
Max(date_changed) as date_changed
FROM
table2
GROUP BY
Order_Number) x
JOIN
table2 t ON x.date_changed = t.date_changed
LEFT JOIN
table1 cr ON x.Order_Number = cr.Order_Number
WHERE cr.name = 'bob'
Here are example tables.
Table1:
Order_Number data1 data2 data3 name
1 a f q bob
2 b g g john
3 c b h bob
4 d s j john
Table2:
Order_Number date_changed OtherData1 OtherData2 OtherData3
1 11/30/2014 aa aaa abc
1 12/1/2014 bb bbb def
1 12/2/2014 cc ccc abc
3 12/1/2014 dd aaa def
2 11/30/2014 dd bbb abc
2 12/1/2014 ss ccc def
3 12/2/2014 aa aaa abc
4 11/26/2014 fc wer wsd
Your Join to config_log (Table2) needs to include the entire composite key if you want to retrieve unique rows.
JOIN
conf_log t ON x.date_changed = t.date_changed
And x.Order_Number = t.Order_number
I think you need to have 2 sub querys:
SELECT
Data.Order_Number,
Data.data1, Data.data2, Data.data3,
Data.OtherData1, Data.OtherData2, Data.OtherData3,
Data.date_changed, Data.name
FROM
(SELECT
Order_Number,
Max(date_changed) as date_changed
FROM
table2
GROUP BY
Order_Number) x
JOIN (SELECT
t.Order_Number,
cr.data1, cr.data2, cr.data3,
t.OtherData1, t.OtherData2, t.OtherData3,
t.date_changed, cr.name
FROM table2 t
JOIN table1 cr
ON t.Order_Number = cr.Order_Number) AS Data
ON x.date_changed = data.date_changed
AND x.Order_Number = data.Order_number
WHERE cr.name = 'bob'
The fact that you had cr.name in the where clause means the LEFT JOIN had the same affect as just JOIN.

TSQL - create columns based on field values and duplicate rows

in tsql how can I turn this sample table:
ID FIELDNO ROWNO VALUE
ABC 2 1 Cat1Val1
ABC 2 2 Cat1Val2
ABC 2 3 Cat1Val3
ABC 3 1 Cat2Val1
ABC 3 2 Cat2Val2
ABC 5 1 Cat3Val1
to a table that will create three columns based on the fieldno and duplicate the rows so that it lists all possible variations of whatever fieldno has the highest rowNo?
So fieldno 2 will become CAT1, 3 -> CAT2 and 5 -> CAT3
Expected result:
ID CAT1 CAT2 CAT3
ABC Cat1Val1 Cat2Val1 Cat3Val1
ABC Cat1Val1 Cat2Val2 Cat3Val1
ABC Cat1Val2 Cat2Val1 Cat3Val1
ABC Cat1Val2 Cat2Val2 Cat3Val1
ABC Cat1Val3 Cat2Val1 Cat3Val1
ABC Cat1Val3 Cat2Val2 Cat3Val1
I could then use this as a base to join with other tables.
Here's a fiddle with more data.
I've tried to create some CASE WHEN clauses but I think this is not going to work.
Hope you can show me a way how this can be solved.
Thank you.
This seems a bit unorthodox, but this should do it for you if I understood the problem correctly:
SELECT d1.id, d1.value Cat1, d2.value Cat2, d3.value Cat3
FROM Docs d1
INNER JOIN Docs d2 ON d2.id = d1.id AND d2.rowNo = d1.rowNo AND d2.fieldNo = 3
INNER JOIN Docs d3 ON d3.id = d2.id AND d3.rowNo = d1.rowNo AND d3.fieldNo = 5
AND d1.fieldNo = 2
This solution of course expects values will exist for each column.
Revised answer...
If the third join and only the third join (Docs.fieldNo = 5) is optional, you can do something like this:
SELECT
d2.id,
d2.value Cat1,
d3.value Cat2,
d5.value Cat3
FROM
(SELECT 2 fieldNo2, 3 fieldNo3, 5 fieldNo5) f
INNER JOIN Docs d2 ON d2.fieldNo = f.fieldNo2
INNER JOIN Docs d3 ON d3.fieldNo = f.fieldNo3 and d3.rowNo = d2.rowNo and d3.id = d2.id
LEFT JOIN Docs d5 ON d5.fieldNo = f.fieldNo5 and d5.rowNo = d2.rowNo and d5.id = d2.id
I've revised the rest of the query so that hopefully what it's doing is a little clearer.
Here are some answers on joins which you may find helpful: What is the difference between "INNER JOIN" and "OUTER JOIN"?
I have tried this in an access database:
replace myvalue with value (value is a reserved word in access)
I called the table test so replace it with your tablename.
SELECT test.id, test.myValue AS cat1, test_1.[myValue] AS cat2, test_2.myValue AS cat3
FROM test, test AS test_1, test AS test_2
WHERE (((test.myValue) Like "cat1*")
AND ((test_1.[myValue]) Like "cat2*")
AND ((test_2.myValue) Like "cat3*"))
ORDER BY test.myValue, test_1.[myValue], test_2.myValue;

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