Inner Join returning wrong number of results - sql-server

My query is returning 54 rows when it should return 59. I feel like I only need the Inner join because I wouldn't want to include NULL values in my results. I've also tried doing Left Outer Joins to filter out some of the non matching values but it returns too many results (62). I'm using DISTINCT so Duplicates shouldn't be the issue.
The query I'm trying to Create:
"List all ingredient classes and the titles of any recipes that use at least one
ingredient of that class, if any. Eliminate all duplicates. (2 columns, 59 rows)"
My Code:
select DISTINCT Ingredients.IngredientClassID, Recipes.RecipeTitle
from Ingredients
Inner join Recipe_Ingredients on Ingredients.IngredientID =
Recipe_Ingredients.IngredientID
Inner join Recipes on Recipe_Ingredients.RecipeID = Recipes.RecipeID;
Link to a DB Fiddle I setup
Diagram of Database:

I think 54 rows is the correct output. My query gave the same number of rows.
FROM Ingredient_Classes ic
LEFT JOIN Ingredients i ON i.IngredientClassID = ic.IngredientClassID
LEFT JOIN Recipe_Ingredients ri ON ri.ingredientId = i.ingredientId
JOIN recipes r ON r.recipeid = ri.recipeId
WHERE i.ingredientId IS NOT NULL
GROUP BY ic.IngredientClassDescription, r.recipetitle
ORDER BY recipetitle;
You can select distinct(recipe.title) and see that all recipes are present in the output. So I don't think we missed anything.

Related

Duplicates when using inner join in t-sql

I know i am missing something ,my issue is, I have two tables with identical values except a filter and trying to join these temp tables in a SP but i am getting duplicate values.
Below is the sample code
SELECT DISTINCT
B.SUBSCRIBER_TAX_ID, B.MEMBER_FIRST_NAME, B.MEMBER_LAST_NAME,
B.BENEFIT_PLAN_NAME AS MEDICAL_PLAN, B.MEMBER_EFF_DATE AS MED_EFF_DATE, B.MEMBER_TERMINATION_DATE AS MED_END_DATE,
P.BENEFIT_PLAN_NAME AS PHARM_PLAN_NAME, P.MEMBER_EFF_DATE AS PHARM_EFF_DATE, P.MEMBER_TERMINATION_DATE AS PHARM_ENDdATE
FROM #BH_MED B
INNER JOIN #BH_PHARM P ON B.MEMBER_HCC_ID = P.MEMBER_HCC_ID
order by b.BENEFIT_PLAN_NAME,P.BENEFIT_PLAN_NAME
I want results as
!I want distinct abc,def in column 3 and column 6
Use group by
SELECT DISTINCT
B.SUBSCRIBER_TAX_ID, B.MEMBER_FIRST_NAME, B.MEMBER_LAST_NAME,
B.BENEFIT_PLAN_NAME AS MEDICAL_PLAN, B.MEMBER_EFF_DATE AS MED_EFF_DATE, B.MEMBER_TERMINATION_DATE AS MED_END_DATE,
P.BENEFIT_PLAN_NAME AS PHARM_PLAN_NAME, P.MEMBER_EFF_DATE AS PHARM_EFF_DATE, P.MEMBER_TERMINATION_DATE AS PHARM_ENDdATE
FROM #BH_MED B
INNER JOIN #BH_PHARM P ON B.MEMBER_HCC_ID = P.MEMBER_HCC_ID
GROUP BY B.SUBSCRIBER_TAX_ID, B.MEMBER_FIRST_NAME, B.MEMBER_LAST_NAME,B.BENEFIT_PLAN_NAME,B.MEMBER_EFF_DATE,B.MEMBER_TERMINATION_DATE,P.BENEFIT_PLAN_NAME,P.MEMBER_EFF_DATE,P.MEMBER_TERMINATION_DATE
order by b.BENEFIT_PLAN_NAME,P.BENEFIT_PLAN_NAME

MS SQL Table Joins - Multiple Tables

I am new to MS SQL and am having trouble joining 4 tables within a query.
I am trying to join Orders, Order Lines, Client, and Picked tables to create a query to show quantity ordered and picked for a client. If I comment out the last inner join for Picked, I get the correct results. When I include the inner join for Picked the query returns results but data that should be in the Picked fields is NULL. One order line can have 1 or more Picked lines.
SELECT W_Warehouse, OH.OrderID, OH.RequiredDate, C.Client, OL.LineNbr, OL.QtyOrd, P.QtyPick
FROM Order
INNER JOIN Warehouse on Order.OH_WHS = Warehouse.W_PK
INNER JOIN Client on Order.O_Client = Client.C_PK
INNER JOIN OrderLine on Order.O_PK = OrderLine.OL_PK
INNER JOIN Picked on OrderLine.O_PK = Picked.P_PK
WHERE C.CLIENT = 'WENDYS'
Without knowing the data in the tables it is difficult to answer precisely.
But as you say you have 1+ rows in the Picked table, you probably want to do aggregation with GROUP BY and SUM()
Maybe this is what you're looking for:
SELECT
W.W_Warehouse,
OH.OrderID,
OH.RequiredDate,
C.Client,
OL.LineNbr,
OL.QtyOrd,
P.QtyPick
FROM
Order OH
INNER JOIN Warehouse W on OH.OH_WHS = W.W_PK
INNER JOIN Client C on OH.O_Client = C.C_PK
INNER JOIN OrderLine OL on OH.O_PK = OL.OL_PK
CROSS APPLY (
select sum(QtyPick) as QtyPick
from Picked P
where OL.O_PK = P.P_PK
) P
WHERE
C.CLIENT = 'WENDYS'
It calculates the sum of QtyPick separately so it doesn't increase the number of lines in the result.

How to join one select with another when the first one not always returns a value for specific row?

I have a complex query to retrieve some results:
EDITED QUERY (added the UNION ALL):
SELECT t.*
FROM (
SELECT
dbo.Intervencao.INT_Processo, analista,
ETS.ETS_Sigla, ATC.ATC_Sigla, PAT.PAT_Sigla, dbo.Assunto.SNT_Peso,
CASE
WHEN ETS.ETS_Sigla = 'PE' AND (PAT.PAT_Sigla = 'LIB' OR PAT.PAT_Sigla = 'LBR') THEN (0.3*SNT_Peso)
WHEN ETS.ETS_Sigla = 'CD' THEN (0.3*SNT_Peso)*0.3
ELSE SNT_Peso
END AS PESOAREA,
CASE
WHEN a.max_TEA_FimTarefa IS NULL THEN a.max_TEA_InicioTarefa
ELSE a.max_TEA_FimTarefa
END AS DATA_INICIO_TERMINO,
ROW_NUMBER() OVER (PARTITION BY ATC.ATC_Sigla, a.SRV_Id ORDER BY TEA_FimTarefa DESC) AS seqnum
FROM dbo.Tarefa AS t
INNER JOIN (
SELECT
MAX(dbo.TarefaEtapaAreaTecnica.TEA_InicioTarefa) AS max_TEA_InicioTarefa,
MAX (dbo.TarefaEtapaAreaTecnica.TEA_FimTarefa) AS max_TEA_FimTarefa,
dbo.Pessoa.PFJ_Descri AS analista, dbo.AreaTecnica.ATC_Id, dbo.Tarefa.SRV_Id
FROM dbo.TarefaEtapaAreaTecnica
LEFT JOIN dbo.Tarefa ON dbo.TarefaEtapaAreaTecnica.TRF_Id = dbo.Tarefa.TRF_Id
LEFT JOIN dbo.AreaTecnica ON dbo.TarefaEtapaAreaTecnica.ATC_Id = dbo.AreaTecnica.ATC_Id
LEFT JOIN dbo.ServicoAreaTecnica ON dbo.TarefaEtapaAreaTecnica.ATC_Id = dbo.ServicoAreaTecnica.ATC_Id
AND dbo.Tarefa.SRV_Id = dbo.ServicoAreaTecnica.SRV_Id
INNER JOIN dbo.Pessoa ON dbo.Pessoa.PFJ_Id = dbo.ServicoAreaTecnica.PFJ_Id_Analista
GROUP BY dbo.AreaTecnica.ATC_Id, dbo.Tarefa.SRV_Id, dbo.Pessoa.PFJ_Descri
) AS a ON t.SRV_Id = a.SRV_Id
INNER JOIN dbo.TarefaEtapaAreaTecnica AS TarefaEtapaAreaTecnica_1 ON
t.TRF_Id = TarefaEtapaAreaTecnica_1.TRF_Id
AND a.ATC_Id = TarefaEtapaAreaTecnica_1.ATC_Id
AND a.max_TEA_InicioTarefa = TarefaEtapaAreaTecnica_1.TEA_InicioTarefa
LEFT JOIN AreaTecnica ATC ON TarefaEtapaAreaTecnica_1.ATC_Id = ATC.ATC_Id
LEFT JOIN Etapa ETS ON TarefaEtapaAreaTecnica_1.ETS_Id = ETS.ETS_Id
LEFT JOIN ParecerTipo PAT ON TarefaEtapaAreaTecnica_1.PAT_Id = PAT.PAT_Id
LEFT OUTER JOIN dbo.Servico ON a.SRV_Id = dbo.Servico.SRV_Id
INNER JOIN dbo.Intervencao ON dbo.Servico.INT_Id = dbo.Intervencao.INT_Id
LEFT JOIN dbo.Assunto ON dbo.Servico.SNT_Id = dbo.Assunto.SNT_Id
) t
The result is following:
It works good, the problem is that I was asked that if when a row is not present on this query, it must contain values from another table (ServicoAreaTecnica), so I got this query for the other table based on crucial information of the first query. So if I UNION ALL I get this:
Query1 +
UNION ALL
SELECT INN.INT_Processo,
PES.PFJ_Descri,
NULL, --ETS.ETS_Sigla,
ART.ATC_Sigla,
NULL ,--PAT.PAT_Sigla,
ASS.SNT_Peso,
NULL, --PESOAREA
NULL, --DATA_INICIO_TERMINO
NULL --seqnum
FROM dbo.ServicoAreaTecnica AS SAT
INNER JOIN dbo.AreaTecnica AS ART ON ART.ATC_Id = SAT.ATC_Id
INNER JOIN dbo.Servico AS SER ON SER.SRV_Id = SAT.SRV_Id
INNER JOIN dbo.Assunto AS ASS ON ASS.SNT_Id = SER.SNT_Id
INNER JOIN dbo.Intervencao AS INN ON INN.INT_Id = SER.INT_Id
INNER JOIN dbo.Pessoa AS PES ON PES.PFJ_Id = SAT.PFJ_Id_Analista
The result is following:
So what I want to do is to remove row number 1 because row number 2 exists on the first query, I think I got it explained better this time. The result should be only row number 1, row number 2 would appear only if query 1 doesn't retrieve a row for that particular INN.INT_Processo.
Thanks!
Ok, there are two ways to reduce your record set. Given that you've already written the code to produce the table with the extra rows, it might be easiest to just add code to reduce that:
Select * from
(Select *
, Row_Number() over
(partition by IntProcesso, Analista order by ISNULL(seqnum, 0) desc) as RN
from MyResults) a
where RN = 1
This will assign row_number 1 to any rows that came from your first query, or to any rows from the second query that do not have matches in the first query, then filter out extra rows.
You could also use outer joins with isnull or coalesce, as others have suggested. Something like this:
Select ISNULL(a.IntProcesso, b.IntProcesso) as IntProcesso
, ISNULL(a.Analista, b.Analista) as Analista
, ISNULL(a.ETSsigla, b.ETSsigla) as ETSsigla
[repeat for the rest of your columns]
from Table1 a
full outer join Table2 b
on a.IntProcesso = b.IntProcesso and a.Analista = b.Analista
Your code is hard to read, because of the lengthy names of everything (and to be honest, the fact that they're in a language I don't speak also makes it a lot harder).
But how about: replacing your INNER JOINs with LEFT JOINs, adding more LEFT JOINs to draw in the alternative tables, and introducing ISNULL clauses for each variable you want in the results?
If you do something like ... Query1 Right Join Query2 On ... that should get only the rows in Query2 that don't appear in Query 1.

Inner Join and Left Join in same sql query

I'm trying to perform inner join and then left join in same sql query.
But the left join is not working. It is not showing the null values
I've two tables EVENT_INVITATIONS and USERINFO which has 2 records when joined.
so, the join query goes like this :
select * from [UandMePROD].[dbo].EVENT_INVITATIONS EI
join [UandMePROD].[dbo].USERINFO UI on EI.USER_ID = UI.USER_ID and EI.EVENT_ID=11033
It gives out 2 records.
So, I'm performing a left join with another table CLIENT_CONTACTS which has only 1 matching record in it.
So, actually it should show the null value to the unmatched record. but it is not showing the second record. It is showing only 1 record which is matched(join)
My failed sql query :
select * from [UandMePROD].[dbo].EVENT_INVITATIONS EI
join [UandMePROD].[dbo].USERINFO UI on EI.USER_ID = UI.USER_ID
left join CLIENT_CONTACTS CC on UI.MOBILENO=CC.MOBILE_NUMBER
where cc.CLIENT_ID=20111 and EI.EVENT_ID=11033
can you please tell me where I'm doing mistake?
I need the 2 records.
Since you are doing a left join, cc.CLIENT_ID is null for all the values which only exist in CLIENT_CONTACTS and your where clause Where cc.CLIENT_ID = 20111
converts your LEFT JOIN into INNER JOIN. Adding this filter in ON clause solves the issue.
select *
from [UandMePROD].[dbo].EVENT_INVITATIONS EI
inner join [UandMePROD].[dbo].USERINFO UI on EI.[USER_ID] = UI.[USER_ID]
left join CLIENT_CONTACTS CC on UI.MOBILENO = CC.MOBILE_NUMBER
and cc.CLIENT_ID = 20111
where EI.EVENT_ID=11033
You should not specify EI.EVENT_ID on WHERE clause. Those limit your results after the join. You should specify EI.EVENT_ID in an ON clause.
select * from [UandMePROD].[dbo].EVENT_INVITATIONS EI join [UandMePROD].[dbo].USERINFO UI on EI.USER_ID = UI.USER_ID AND (EI.EVENT_ID=11033 or EI.EVENT_ID is null) left join CLIENT_CONTACTS CC on UI.MOBILENO=CC.MOBILE_NUMBER where cc.CLIENT_ID=20111
Providing some suggestions,since it is not possible to say what is happening with out table data
Left join should show null values from output of first inner join eventhough there are no mobile number matches,so try removing where condition and see if you are getting any result

Linq Left outer join Query

How to convert this sql query into Linq query?
select *
from setupUOMs as su
LEFT OUTER JOIN scmSKUUoMs as ssu
on su.UoMID != ssu.UoMID
where ssu.SKUID = 446 and su.UMTypeID = 5
Following is the linq query.
from c in setupUOMs
join o in scmSKUUoMs
on c.UOMID equals o.UoMID into sr
from x in sr.DefaultIfEmpty()
where x.SKUID == 446
select x
In above query I've so far only done to extract the join number but what I want is to select the non equal records of left table but I'm able to show joined record. and it returns the records of only left table while my result is based on both columns. In where clause I can access x.SKUID which is from left table but can't access x.UMTypesID which is form right table (Means no column from right table is returned on which I can make condition).
Take a look at this LEFT JOIN LINQTOSQL Example.
http://www.devcurry.com/2011/01/linq-left-join-example-in-c.html
Maybe it will help.
A join with a non-equals condition is not a join but a Cartesian product excluding the inner joined records. There may be valid reasons to make a query as you did, but there's always a risk of blowing up the result set in terms of number of records.
So let's assume the conditions ensure a result that makes sense, then you can do:
from su in setupUOMs
from ssu in scmSKUUoMs // from .. from is a Cartesian product
where su.UOMID != ssu.UoMID
&& ssu.SKUID == 446
&& su.UMTypeID == 5
select new { su.Prop1, ssu.Prop2, ... }

Resources