Inner join Giving columns Multiple values - sql-server

I have Three Tables In Database
Table : Group
Id, Name
Table : doctor
Id ,DId, DoctorName,
Table : Ratio
Id , DId, UpLimit , downLimit.
When i inner join them , I am Getting duplicate Values to Uplimit and downlimit,,
Actually Group is related to doctors, One group id can have multple doctors,, so when i save some data with Group and doctor, it is saving to all the records with same data, uplimit downlimit varies with different doctors , but when i inner join dem its showing same to all doctors,, how to skip uplimit and downlimit how to write the Query..
Select A.Group , B.doctor , C.Uplimit, D.downlimit from Group A
inner join Doctor B
on A.id = B.id
inner join C ratio
on A.id = c.id
issue is When i separately check it with doctor id in doctor table it is showing only one record, when i inner join them its showing same data to all doctors to particular
how to join them??

Please use the DISTINCT clause after SELECT. This will filter the duplicates.
Select DISTINCT A.[Group] , B.doctor , C.Uplimit, D.downlimit
FROM Group A
inner join Doctor B
on A.id = B.id
inner join Ratio C
on B.id = C.id
It is good to avoid using the keywords as table/column names.

You Table Structure must be like
Table : Group
Id, Name
Table : doctor
Id ,DId(foreign key for group table), DoctorName
Table : Ratio
Id(Foreign Key for Doctor Table), UpLimit , downLimit.
Ratio Table is either missing a foreign key or a primary key, assuming Id of Ratio table is foreign key
Select A.Group , B.doctor , C.Uplimit, D.downlimit from Group A
inner join Doctor B
on A.id = B.Did
inner join ratio C
on B.id = c.id
This should give what you needed

Related

Joining 3 Tables into one result set

I am trying to join only select columns from 3 different tables and haven't been successful.
Table 1, Patient, has the following columns I need:
ExternalID, UserDefinedXML, ServiceSiteUid
Table 2, PDI, has the following columns I need:
Patient ID, FirstName, LastName, State
Table 3, ListServiceSite has the following columns I need:
ServiceSiteUid, Name
I need to join Patient and PDI based on the common ID columns ExternalID and PatientID, and then join Patient and ListServiceSite by ServiceSiteUid.
Here's what I have that has been unsuccessful:
SELECT
*
FROM
(SELECT
ExternalID, UserDefinedXml, ServiceSiteUid
FROM
Patient) A
INNER JOIN
(SELECT
[Patient ID], FirstName, LastName, State
FROM
PatDemogImport) B ON A.ExternalID = B.[Patient ID]
WHERE
UserDefinedXml IS NOT NULL;
I am very new to SQL so please have patience! Thank you in advance.
This should do it
SELECT *
FROM Patient as p
JOIN PatDemogImport as pdi
ON p.ExternalID = pdi.[Patient ID]
JOIN ListServiceSite as lss
ON lss.ServiceSiteUid = p.ServiceSiteUid
WHERE p.UserDefinedXml IS NOT NULL;
SELECT * FROM PATIENT
JOIN
PDI on PATIENT.ExternalID = PDI.ExternalID AND PATIENT.PatientID = PDI.PatientID
JOIN
ListServiceSite on ListServiceSite.ServiceSiteUid = PATIENT.ServiceSiteUid
where
UserDefinedXml IS NOT NULL;
That should give you what you're looking for. There's no need for subselects in this instance.
Essentially what you are doing is joining the tables based on the relationship between columns. So in the above we join PDI and PATIENT based on their 2 shared columns and ListServiceSite and PATIENT on ServiceSiteUid.
Please note that when you do a SELECT *, you will get duplicate columns in the result (since you're basically getting all the columns across all those tables) so I'd recommend specifying what you select. i.e. SELECT PATIENT.ExternalID, ....
Finally, I'd recommend reading up on the different kind of joins. The above is just a regular join but there are several others like OUTER JOIN INNER JOIN RIGHT JOIN LEFT JOIN and they all have differences.
SELECT Patient.*
FROM Patient INNER JOIN PatDemogImport as pdi ON
Patient.ExternalID=pdi.Patientid
Inner join ListServiceSite on
Patient.ServiceSiteUid=ListServiceSite.ServiceSiteUid
WHERE Patient.UserDefinedXml IS NOT NULL
Isn't it just as simple as this? You want an inner join and you just need to specify the columns you need. This should give you exactly the answer you're looking for. These other answers have the same idea, but they assume that the tables contain only the columns listed and no other columns. PatientID should be the same as ExternalID so no need to specify that column and C.ServiceSiteUid should be the same as in Patient.
SELECT A.ExternalID, A.UserDefinedXML, A.ServiceSiteUid,
B.FirstName, B.LastName, B.State,
C.Name
FROM Patient A
INNER JOIN PatDemogImport B ON B.PatientID = A.ExternalID
INNER JOIN ListServiceSite C ON C.ServiceSiteUid = A.ServiceSiteUid
WHERE A.UserDefinedXML IS NOT NULL

T-SQL: Get all, but join on 'not null'

So I'm trying to get all items from a table, and then add some data if there is a joined member, otherwise keep the data empty (null)
What I have is this:
SELECT
t.id AS _id,
m.name AS name
FROM
tableT AS t
INNER JOIN
tableM AS m ON t.m_id = m.id
The tables look like this:
tableT:
id m.id
----------------
1 NULL
2 1
3 NULL
tableM:
id name
----------------
1 'Bob'
The desired result should be this:
id name
------------------
1 NULL
2 'Bob'
3 NULL
How can I achieve this, as far as I can see inner join won't join on NULL values since they can't be matched
You're looking for a LEFT JOIN
SELECT
t.id AS _id,
m.name AS name
FROM tableT AS t
LEFT JOIN
tableM AS m
ON t.m_id = m.id
The definition of an INNER JOIN will only return data where there is a match in both tables, if one table has missing data then that row will not be returned.
A LEFT JOIN will get all data from the first table and only matching records in the second table, allowing for NULL values.
A little further reading if it's useful;
http://www.w3schools.com/sql/sql_join_left.asp
use LEFT JOIN:
SELECT
t.id AS _id,
m.name AS name
FROM tableT AS t
LEFT OUTER JOIN
tableM AS m
ON t.m_id = m.id
Your requirement is Left Join:
SELECT
t.id AS _id,
m.name AS name
FROM tableT AS t
LEFT OUTER JOIN
tableM AS m
ON t.m_id = m.id
Please check below link for more information regarding different type of joins.
https://msdn.microsoft.com/en-us/library/zt8wzxy4.aspx

Select all the data from another table even if corresponding value from another table is NULL

I have this query:
SELECT city.CITY_NAME,
SUM(case when c.CUSTOMER_ID=o.CUSTOMER_ID and o.ORDER_ID=od.ORDER_ID
then od.TOTAL_AMT_PER_ITEM
else 0 end) AS TOTAL_AMT_PER_ITEM
FROM [ORDER] o
INNER JOIN ORDER_DETAILS od
ON o.ORDER_ID = od.ORDER_ID
INNER JOIN CUSTOMER c
ON o.CUSTOMER_ID = c.CUSTOMER_ID
INNER JOIN CUSTOMER_ADDRESS ca
ON ca.CUSTOMER_ID = c.CUSTOMER_ID
INNER JOIN CITY city
ON ca.CITY_ID = city.CITY_ID
GROUP BY city.CITY_NAME
I am a beginner in SQL SERVER. This query displays only the CITY_NAME that has a corresponding TOTAL_AMT_PER_ITEM value. What I need is to display all the CITY_NAMEs in the database even if their corresponding value is NULL. What is the work around for this? Can someone help me out? Thanks!
I change the order of the joins maybe that help.
You start with CITY because is the source for your GROUP BY and try to see if have any CUSTOMER_ADDRESS.
I guess if is a new store you can have 0 customers.
Then INNER JOIN because direction cant exist alone, they belong to a customer
Then LEFT JOIN because again a CUSTOMER may or may not have [ORDERS].
Then INNER JOIN because every [ORDERS] have [ORDER DETAILS]
Finally you SUM(od.TOTAL_AMT_PER_ITEM) from the last JOIN table, this can get some NULL's so you need include COALESCE
SELECT city.CITY_NAME,
COALESCE(SUM(od.TOTAL_AMT_PER_ITEM) , 0) as TOTAL_AMT_PER_ITEM
FROM [CITY]
LEFT JOIN [CUSTOMER_ADDRESS] ca
ON ca.CITY_ID = [CITY].CITY_ID
INNER JOIN CUSTOMER c
ON ca.CUSTOMER_ID = c.CUSTOMER_ID
LEFT JOIN [ORDER] o
ON o.CUSTOMER_ID = c.CUSTOMER_ID
INNER JOIN ORDER_DETAILS od
ON o.ORDER_ID = od.ORDER_ID
GROUP BY [CITY].CITY_NAME
btw you should change the name of the table [Order] to [Orders] because Order is a reserved word and can cause problems.
In general I rather use the plural name for tables because is an entity saving multiple of one type
CITIES instead of CITY
CUSTOMERS intead of CUSTOMER
ORDER_DETAILS is already plural, so try to keep consistence.
SELECT
city.CITY_NAME,
SUM(od.TOTAL_AMT_PER_ITEM) AS TOTAL_AMT_PER_ITEM
FROM
CUSTOMER c
INNER JOIN
CUSTOMER_ADDRESS ca
ON ca.CUSTOMER_ID = c.CUSTOMER_ID
INNER JOIN
CITY city
ON ca.CITY_ID = city.CITY_ID
LEFT JOIN
[ORDER] o
ON o.CUSTOMER_ID = c.CUSTOMER_ID
LEFT JOIN
ORDER_DETAILS
ON o.ORDER_ID = od.ORDER_ID
GROUP BY city.CITY_NAME

SQL Join Multiple columns in one table with one column in another table

I got two tables as below
table 1 :Which got multiple columns which refers to a value in the second table.
tbale2:Lookup table where it got a row for every possible value for the columns in the above table
What I want to do is lookup the values in columns ItemID,ORDIG,CatID,MediaID in table 1 from ValueID in table2 and return ValueName
so at the end my result should look like
ItemID OrgID CatID MediaID
i859049 Singapore Full 0001edf
You will need to join to the lookup table once for each value you need, but should likely need to use a LEFT instead of INNER join since the values may be null.
SELECT
T1.ItemId,
Items.ValueName AS ItemName,
T1.OrgID,
Orgs.ValueName AS OrgName,
T1.CatID,
Cats.ValueName AS CatName,
T1.MediaID,
Media.ValueName AS MediaName
FROM Table1 T1
LEFT OUTER JOIN Table2 Items
ON T1.ItemId = Items.ValueID
LEFT OUTER JOIN Table2 Orgs
ON T1.OrgId = Orgs.ValueID
LEFT OUTER JOIN Table2 Cats
ON T1.CatId = Cats.ValueID
LEFT OUTER JOIN Table2 Media
ON T1.MediaId = Media.ValueID

SQL UNION INNER JOIN

Im trying to select from 2 tables that have the same columns, but both tables have an inner join -
select e.ID,
c.FullName,
car.VIN,
car.Registration,
e.Telephone,
e.Mobile,
e.Email,
e.EstimateTotal,
e.LastUpdated,
e.LastUpdateBy from (select id from Estimates UNION ALL select id from PrivateEstimates) e
inner join Customers c on c.ID = e.CustomerID
inner join Cars car on car.ID = e.CarID
where e.Status = 0
The trouble is, it can't find e.CustomerID, e.CarID or e.Status on the inner join? Any ideas?
Your subquery
select id from Estimates
union all
select id from PrivateEstimates
returns only a single id column. Include necessary columns in the subquery if you want to use those columns in JOIN statements

Resources