Need help in joining tables to get the accurate results - sql

I have 4 tables, the 1st table 'LD0P0K' is the main table i need to join with 2nd 'LD0P0K01' and 3rd 'LD0P0K04' with the 1st column value 'HUSHLNR' and get the 'PNR' from both tables and then join with last table 'LD0P0A' to get the values with 'PNR'.
I tried the below solution but its not giving the data from 3rd table and giving 6 records with 2 rows for each 2nd table.
Select HS.HUSHLNR, HS.FOMDAT,HS.TOMDAT,HSM.PNR,HSM.FAMHUVUD,HSM.MARK,HSM.AVFOMDAT,HSM.AVTOMDAT,P.KUNDNUMMER,P.FODDAT from LD0P0K HS
LEFT OUTER JOIN LD0P0K01 HSM on HS.HUSHLNR = HSM.HUSHLNR
LEFT OUTER JOIN LD0P0K04 CHM on HS.HUSHLNR = CHM.HUSHLNR
LEFT OUTER JOIN LD0P0A P on p.PNR= HSM.PNR AND p.PNR = CHM.PNR
Where HS.HUSHLNR='906'

You can simply use Union to combined the fields
Here is the link for Union to understand
SQL Union
and here is the code change it whatever you want
Select HS.HUSHLNR as HUSHLNR, HS.fromdate as FromDte,HSM.PNR as PNR from LD0P0K HS
inner JOIN LD0P0K01 HSM on HS.HUSHLNR = HSM.HUSHLNR
union
Select CHM.HUSHLNR as HUSHLNR, '' as FromDte,CHM.PNR as PNR from LD0P0K04 CHM
order by PNR
Output:

Related

Join tables when 3 column in first table that can point to same column in second table

I have the following DB structure:
And right now I can't make up a query to get
a creator data, admin data and tech data from item_contacts...
What kind of JOIN I need to use and how?
I think you want 3 joins on item_contacts - one for each column whose data you want to recover:
select
i.*,
cc.data as creator_data,
ca.data as admin_data,
ct.data as tech_data
from items i
inner join item_contacts cc on cc.contact_id = i.creator_id
inner join item_contacts ca on ca.contact_id = i.admin_id
inner join item_contacts ct on ct.contact_id = i.tech_id

WHERE clause and LEFT JOIN in SQL Server query

I have this query:
SELECT
EnrollmentID, MarketID
FROM
Contracts AS CO
LEFT JOIN
Customers AS C ON C.EnrollmentID = CO.BatchID AND MarketID = 'AB'
WHERE
C.EnrollmentID IS NULL
Here, I have a question that in this query is it possible that the query will verify data for MarketID = 'AB' in left join because as per WHERE condition?
I am getting result of EnrollmentIDs and MarketIDs are all NULL.
Note: The LEFT JOIN keyword returns all the rows from the left table (Contracts ), even if there are no matches in the right table (Customers ).
Now, if you want to select right table column and there are no matching data in the right table ,like.
SELECT CO.EnrollmentID, CO.MarketID ,C.Some_col
FROM Contracts AS CO
LEFT JOIN Customers AS C ON C.EnrollmentID = CO.BatchID
so, C.Some_col column will get all the null value for no matching rows in the right table.i think this is the reason why you are getting the null value for
MarketIDs and EnrollmentIDs.
hope, this help you.

I want to retrieve data from 4 tables in SQL Server

I want to retrieve data from 4 tables. Patient table has id as PK which is the foreign key in other three tables ett, phar and ssc. Where a patient lie in only one category. i.e patient id pt1 exists in either of the 3 tables. now I want to retrieve patient info along with its associated category.
My query is:
SELECT *
FROM Patient p
INNER JOIN ETT t
ON p.Patient_ID = t.Patient_ID || INNER JOIN Pharmacological ph
ON p.Patient_ID = ph.Patient_ID
I used OR clause because I want only 1 inner join executing at one time. but its not giving me results, any suggestions??
....Patient table has ID as PK which is the foreign key in other three
tables name: ett, phar and ssc where a patient lie in only one
category. Example, patient id pt1 exists in either of the 3 tables.
Based on your statement, you can join all the tables in table Patient using LEFT JOIN since a record can only exist on one table. The query below uses COALESCE which returns the first non-null value with int the list.
The only thing you need is to manually specify the column names that you want to be shown on the list as shown below.
SELECT a.*,
COALESCE(t.colA, p.ColA, s.ColA) ColA,
COALESCE(t.colB, p.ColB, s.ColB) ColB,
COALESCE(t.colN, p.ColN, s.ColN) ColN
FROM Patient a
LEFT JOIN ETT t
ON a.Patient_ID = t.Patient_ID
LEFT JOIN Phar p
ON a.Patient_ID = p.Patient_ID
LEFT JOIN SSC s
ON a.Patient_ID = s.Patient_ID
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
For or - do not ise ||, use "or"
You cannot join with or, you need re-format your query.

Oracle/plsq Inner join multiple column to table one column

I have problems joining 2 table let say I have a column on the 1st table name nationalitycode(this is number) and it fetches the description(it could be american, chinese etc) depending on the first table nationalitycode. So I tried inner join the table so the first inner join is good and working perfectly.
here is the code:
SELECT person.firstnm, person.middlenm, person.lastnm, refcd.description
FROM person
INNER JOIN refcd
ON person.natcd = refcd.id;
However when I try to join the 2nd column from the 1st table let say the column name is gencd(gendercode its a foreign key) but when I updated the query to inner join 2 column from one table getting the value from one column in the 2nd table I get this error.
"Column ambigously defined" I get this error and I understand this my question is is there anyway to achieve my desired output? I want to join 2 table, specifically joining 2 column on table 1 to 1 column in table 2.
Here is my updated query:
SELECT person.firstnm, person.middlenm, person.lastnm, refcd.description
FROM person
INNER JOIN refcd
ON person.natcd = refcd.id
INNER JOIN refcd
ON person.gencd = refcd.id;
Help is really appreciated.
Your query should be
SELECT person.firstnm, person.middlenm, person.lastnm, refcd.description
FROM person INNER JOIN refcd ON person.natcd = refcd.id
INNER JOIN refcd R2 ON person.gencd = R2.id;

Access SQL: Union select, show both fields

I have this SQL:
SELECT Ph.Account, Ph.Ct FROM Ph
UNION SELECT Rx.Account, Rx.Ct FROM Rx;
Which works fine, but the Ph.Ct and Rx.Ct fields may not always be the same. So I wanted to display both of them, but the query is only showing 1 "Ct" field and not both.
How can I have it show both?
Here's ph:
12685 3
29568 1
38771 2
Here's rx:
10657 1
12685 2
68781 2
79874 1
What's what I want to come out from the query:
Account ph.ct rx.ct
10657 1
12685 3 2
29568 1
38771 2
68781 2
79874 1
A UNION gets the correct data set (about 800 results), but not the right fields. Any JOINs I've tried do not give the right data set (only about 300 results).
What you need is a full outer join. For each value of Account appearing in either table, this will give the corresponding values of Ct for each table if the given value of Account appears and null otherwise.
select Account,Ph.Ct as ph_ct,Rx.Ct as rx_ct
from Ph full outer join Rx on (Ph.Account=Rx.Account);
Edit: Since Access apparently doesn't support full outer joins (for some god awful reason), you can achieve the same effect with the union of a left join with a right join:
select Ph.Account, Ph.Ct as ph_ct, Rx.Ct as rx_ct
from Ph left join Rx on (Ph.Account=Rx.Account)
union
select Rx.Account, Ph.Ct as ph_ct,Rx. Ct as rx_ct
from Ph right join Rx on (Ph.Account=Rx.Account);
which is also equivalent to (the probably faster):
select Ph.Account, Ph.Ct as ph_ct, Rx.Ct as rx_ct
from Ph left join Rx on (Ph.Account=Rx.Account)
where (Rx.Account IS NULL)
union all
select Rx.Account, Ph.Ct as ph_ct, Rx.Ct as rx_ct
from Ph right join Rx on (Ph.Account=Rx.Account);
You cannot have a full outer join in MS Access, so:
SELECT m.Account, Ph.Ct, Rx.Ct FROM
((SELECT Ph.Account FROM Ph
UNION SELECT Rx.Account FROM Rx) As m
LEFT JOIN Ph ON m.Account = Ph.Account)
LEFT JOIN Rx ON m.Account = Rx.Account
I don't think you want a union. You want a join. If you join the fields on Account, you can show the Rx.Ct and Ph.Ct as well.
In a union, it lists all the rows from one table, stacked on top of another table, excluding duplicates. Each row is from a different table.
In a join, each row is a combination of two tables, which means the columns that are not joined can hold different values.
Here you go:
SELECT Ph.Account, Ph.Ct, Rx.Ct FROM Ph INNER JOIN Rx ON Ph.Account=Rx.Account;
Just for future references about UNION Operator:
The UNION operator is used to combine the result-set of two or more SELECT statements.
Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.
Note: The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL.
For more info: UNION Operator
You want a join, not a union:
select
coalesce(Ph.Account, Rx.Account) as Account
Ph.Ct,
Rx.Ct
from
Ph
full outer join Rx on
Ph.Account = Rx.Account
You can change inner to the type of join you want. Read more about the different types of joins here.
unions are used when you want to take one set of results and append them to the rowset of another result. joins are used when you want to take one set of results and append them to the columnset of another result.