SQL JOIN STRUGGLE - sql

SELECT Count(TRK.REACTIE_ID),
S.UITGEVER,
S.NAAM,
S.BESCHRIJVING,
S.AFBEELDING_BESTANDSPAD,
S.SUBTITLE,
C.NAAM
FROM CATEGORIEEN_KOPPELTABEL CK
LEFT OUTER JOIN CATEGORIE C
ON C.CATEGORIE_ID = CK.CATEGORIE_ID
LEFT OUTER JOIN TV_SHOW S
ON S.TV_SHOW_ID = CK.TV_SHOW_ID
AND S.NAAM = 'South Park'
FULL OUTER JOIN TVSHOW_REACTIES_KOPPELTABEL TRK
ON TRK.TV_SHOW_ID = S.TV_SHOW_ID
GROUP BY TRK.TV_SHOW_ID,
S.UITGEVER,
S.NAAM,
S.BESCHRIJVING,
S.AFBEELDING_BESTANDSPAD,
S.SUBTITLE,
C.NAAM;
I can't figure out why I can't get only the first row back.
Now I get three rows back with null values and I want only to get the first row. Please help!

rows without nulls use the following FROM statement and keep the rest of query the same.:
FROM CATEGORIEEN_KOPPELTABEL CK
JOIN CATEGORIE C ON C.CATEGORIE_ID = CK.CATEGORIE_ID
JOIN TV_SHOW S ON S.TV_SHOW_ID = CK.TV_SHOW_ID AND S.NAAM = 'South Park'
JOIN TVSHOW_REACTIES_KOPPELTABEL TRK ON TRK.TV_SHOW_ID = S.TV_SHOW_ID

Related

ORA-00918: column ambiguously defined on Left Outer Join

I have the below query, and for some reason the Left Outer Join to PO_HEADERS_ALL POH is causing the error ORA-00918: column ambiguously defined on Left Outer Join. I don't have any columns selected from this table that would be causing this error from what I see. If I comment out this join then the query validates successfully. Appreciate any assistance on this, thanks!
SELECT
ESIB.ITEM_NUMBER,
ESIT.LONG_DESCRIPTION,
IOP.ORGANIZATION_CODE,
IOQD.SUBINVENTORY_CODE,
SUM(
IOQD.PRIMARY_TRANSACTION_QUANTITY
) ONHAND_QTY,
ESIB.PRIMARY_UOM_CODE ONHAND_UOM,
UOM.UOM_CODE TO_UOM,
INTRACONV.CONVERSION_RATE
, SEGMENT1||'-'||SEGMENT2||'-'||SEGMENT3||'-'||SEGMENT4||'-'||SEGMENT5 LOCATOR
,hp.party_name MFG
,etp.tp_item_number MFG_PART_NUM
FROM
EGP_SYSTEM_ITEMS_B ESIB
LEFT OUTER JOIN EGP_SYSTEM_ITEMS_TL ESIT ON ESIB.INVENTORY_ITEM_ID = ESIT.INVENTORY_ITEM_ID
AND ESIB.ORGANIZATION_ID = ESIT.ORGANIZATION_ID --Ended
INNER JOIN INV_ONHAND_QUANTITIES_DETAIL IOQD ON ESIB.INVENTORY_ITEM_ID = IOQD.INVENTORY_ITEM_ID
AND ESIB.ORGANIZATION_ID = IOQD.ORGANIZATION_ID
LEFT OUTER JOIN inv_item_locations LOC ON inventory_location_id= IOQD.locator_id
LEFT OUTER JOIN egp_item_relationships_b eir ON eir.inventory_item_id = ESIB.inventory_item_id and eir.item_relationship_type = 'MFG_PART_NUM'
LEFT OUTER JOIN egp_trading_partner_items etp ON etp.tp_item_id = eir.tp_item_id
and etp.tp_type = 'MANUFACTURER'
LEFT OUTER JOIN hz_parties hp ON hp.party_id = etp.trading_partner_id
LEFT OUTER JOIN PO_LINES_ALL POL ON POL.ITEM_ID = ESIB.INVENTORY_ITEM_ID
--ERROR IS OCCURING WITH THE FOLLOWING JOIN:
LEFT OUTER JOIN PO_HEADERS_ALL POH ON POH.PO_HEADER_ID = POL.PO_HEADER_ID
INNER JOIN INV_UNITS_OF_MEASURE_B UOM ON UOM.BASE_UOM_FLAG = 'Y'
LEFT OUTER JOIN INV_UOM_CONVERSIONS INTRACONV ON --INTRACONV.UOM_CODE = IOQD.TRANSACTION_UOM_CODE
INTRACONV.UOM_CODE = ESIB.PRIMARY_UOM_CODE
AND UOM.UOM_CLASS = INTRACONV.UOM_CLASS
AND INTRACONV.INVENTORY_ITEM_ID = IOQD.INVENTORY_ITEM_ID
--AND INTRACONV.UOM_CODE = UOM.UOM_CODE
INNER JOIN INV_ORG_PARAMETERS IOP ON ESIB.ORGANIZATION_ID = IOP.ORGANIZATION_ID
WHERE
(
INTRACONV.UOM_CODE = IOQD.TRANSACTION_UOM_CODE
OR ESIB.PRIMARY_UOM_CODE = UOM.UOM_CODE
)
GROUP BY
ESIB.ITEM_NUMBER,
ESIT.LONG_DESCRIPTION,
IOP.ORGANIZATION_CODE,
IOQD.SUBINVENTORY_CODE,
ESIB.PRIMARY_UOM_CODE,
UOM.UOM_CODE,
INTRACONV.CONVERSION_RATE
, SEGMENT1||'-'||SEGMENT2||'-'||SEGMENT3||'-'||SEGMENT4||'-'||SEGMENT5
,hp.party_name
,etp.tp_item_number

Full outer join a table to another table that's being left joined to the main table

So i've got hte below code that works for the most part but when I'm trying to bring in the last table i cant get the expected results.
SELECT
CASE
WHEN CCO.Account_Manager__c IS NULL THEN Opp.OwnerId ELSE CCO.Account_Manager__c
END AS AccountManager,
CCO.Account_Associate__c as ClientOfficeAccountAssociate,
Opp.OwnerId as AccountOwnerId,
CCO.Account_Manager__c as ClientOfficeAM,
ACC.Name as AccountName,
OPP.Forum_Monthly_Recurring_Revenue_MRR__c,
PAYG.Revenue
LEFT JOIN `round-cacao-234512.salesforce.Opportunity` as OPP
ON OPP.Id = Split.OpportunityId AND OPP.OwnerId = Split.SplitOwnerId
LEFT JOIN `round-cacao-234512.salesforce.Account` as ACC
on ACC.Id = OPP.AccountId
LEFT JOIN `round-cacao-234512.salesforce.User` as USER
ON USER.Id = OPP.OwnerId
LEFT JOIN `round-cacao-234512.salesforce_df.Company_Client_Office__c` as CCO
ON CCO.Opportunity__c = OPP.Id
LEFT JOIN `round-cacao-234512.salesforce.Client_Office_Intranet__c` as COI
ON COI.Id = CCO.Client_Office_Intranet__c
LEFT JOIN `round-cacao-234512.salesforce.RecordType` as RT
ON RT.Id = OPP.RecordTypeId
LEFT JOIN `commercial-analysis.materialised.2022AMDashForumRRQuotas` as Quota
on Quota.AccountID = CCO.Account_Manager__c OR Quota.AccountID = Opp.OwnerId
LEFT JOIN `commercial-analysis.2022_AM_Territory.FPAYG_Quotas` as PAYGQuota
on PAYGQuota.ClientOffice = COI.Name
Full Outer JOIN `round-cacao-234512.materialised.2022AMDashPAYGRevenue` PAYGRev
on PAYGRev.ClientOffice = COI.Name
So my last table round-cacao-234512.materialised.2022AMDashPAYGRevenue will be joined on clientoffice but will also have client offices that don't appear in COI, so I want those to come in with nulls on all the values that don't match up.
Hope that makes sense

INNER JOIN from another INNER JOIN

I have a database with 3 tables :
PM_FLX_ENTE
PM_ST
PM_ANO_SP
and I would like to get PM_ANO_SP.L_ANO_SP for certain values of PM_ANO_L.L_ANO which contains a code number with '/'.
SELECT COUNT(RF_INTRN),TRIM(pm_st.c_st),TRIM(pm_ano_l.L_ANO)
FROM PM_FLX_ENTE
INNER JOIN PM_ST ON PM_FLX_ENTE.C_ST = PM_ST.C_ST
INNER JOIN PM_ANO_L ON PM_FLX_ENTE.C_ANO = pm_ano_l.c_ano
WHERE pm_flx_ente.C_ANO <> '0000'
AND pm_ano_l.c_lang = 'FR'
group by TRIM(pm_st.c_st), TRIM(pm_ano_l.L_ANO)
ORDER BY COUNT(RF_INTRN) DESC
Can you help me please
Are you seeing an error of some sort?
Please note, that when you do multiple inner joins, the previous join conditions have to be true for the other joins to work.
I.E
INNER JOIN PM_ST ON PM_FLX_ENTE.C_ST = PM_ST.C_ST
Has to evaluate true in order for the below to work
INNER JOIN PM_ANO_L ON PM_FLX_ENTE.C_ANO = pm_ano_l.c_ano
To solve this, you may want to change your INNER JOINs to LEFT JOINs
SELECT TRIM(pm_st.c_st),TRIM(pm_ano_l.L_ANO),COUNT(RF_INTRN)
FROM PM_FLX_ENTE
LEFT JOIN PM_ST ON PM_FLX_ENTE.C_ST = PM_ST.C_ST
LEFT JOIN PM_ANO_L ON PM_FLX_ENTE.C_ANO = pm_ano_l.c_ano
LEFT JOIN PM_ANO_SP ON PM_ANO_L.L_ANO = pm_ano_sp.l_ano_sp
WHERE pm_flx_ente.C_ANO <> '0000'
AND C_LANG = 'FR'
and pm_ano_l.L_ANO like '%/%'
GROUP BY pm_st.c_st,pm_ano_l.L_ANO

Left Join not working

I am a SQL Server beginner and so I have been using MS Access to create queries and then have been amending them for SQL Server.
I have created a query which includes a left join, which works perfectly in Access but when I copy the SQL code across to SQL Server it does not show the null values and I cannot work out why.
I want to show all of the attribute values whether they include a value or not. Can anyone help? The code is as follows:
SELECT DISTINCT
tbLease.LeaseTitle
, tbAttributeValue.AttributeTemplateDefinitionLinkID
, tbAttributeValue.Value
FROM
((((tbBusinessUnit
LEFT JOIN
tbAttributeValue ON tbBusinessUnit.BusinessUnitID = tbAttributeValue.ParentID)
INNER JOIN
tbBuildingLinkBusinessUnit ON tbBusinessUnit.BusinessUnitID = tbBuildingLinkBusinessUnit.BusinessUnitID)
INNER JOIN
tbBuilding ON tbBuildingLinkBusinessUnit.BuildingID = tbBuilding.BuildingID)
INNER JOIN
(tbUnitLocation
INNER JOIN
tbUnit ON tbUnitLocation.UnitID = tbUnit.UnitID)
ON tbBuilding.BuildingID = tbUnitLocation.LocationID)
INNER JOIN
tbLease ON tbUnit.UnitID = tbLease.UnitID
WHERE
(((tbAttributeValue.AttributeTemplateDefinitionLinkID) = 30
Or (tbAttributeValue.AttributeTemplateDefinitionLinkID) = 31
Or (tbAttributeValue.AttributeTemplateDefinitionLinkID) = 32));
Hard to say what you want without some examples but you might want this:
WHERE coalesce(tbAttributeValue.AttributeTemplateDefinitionLinkID,30) in (30,31,32);
This will give you items where AttributeTemplateDefinitionLinkID is null
(that is it did not join on the left join) OR is one of those 3 values.
Right now if you don't join with the left join it will not display that row because of the where condition, so your left join is the same as an inner join.
The NULL values you would normally see with a left join are getting filtered out by your WHERE clause.
Any time you add filters to a WHERE clause that apply to an outer joined table, it will effectively make it the same as an inner join unless you include NULL values as an option in your where clause as well.
SELECT DISTINCT
tbLease.LeaseTitle,
tbAttributeValue.AttributeTemplateDefinitionLinkID,
tbAttributeValue.Value
FROM
tbBusinessUnit
LEFT JOIN tbAttributeValue ON tbBusinessUnit.BusinessUnitID = tbAttributeValue.ParentID
INNER JOIN tbBuildingLinkBusinessUnit ON tbBusinessUnit.BusinessUnitID = tbBuildingLinkBusinessUnit.BusinessUnitID
INNER JOIN tbBuilding ON tbBuildingLinkBusinessUnit.BuildingID = tbBuilding.BuildingID
INNER JOIN tbUnitLocation ON tbBuilding.BuildingID = tbUnitLocation.LocationID
INNER JOIN tbUnit ON tbUnitLocation.UnitID = tbUnit.UnitID
INNER JOIN tbLease ON tbUnit.UnitID = tbLease.UnitID
WHERE
tbAttributeValue.AttributeTemplateDefinitionLinkID in (30, 31, 32)
or tbAttributeValue.AttributeTemplateDefinitionLinkID is null
Move the right table filters from where clause to ON condition when you are using Left Outer Join else Left join will be implicitly converted to INNER JOIN. Try this
SELECT DISTINCT tblease.leasetitle,
tbattributevalue.attributetemplatedefinitionlinkid,
tbattributevalue.value
FROM tbbusinessunit
LEFT JOIN tbattributevalue
ON tbbusinessunit.businessunitid = tbattributevalue.parentid
AND ( tbattributevalue.attributetemplatedefinitionlinkid IN
( 30, 31, 32 )
OR tbattributevalue.attributetemplatedefinitionlinkid IS
NULL )
INNER JOIN tbbuildinglinkbusinessunit
ON tbbusinessunit.businessunitid =
tbbuildinglinkbusinessunit.businessunitid
INNER JOIN tbbuilding
ON tbbuildinglinkbusinessunit.buildingid = tbbuilding.buildingid
INNER JOIN tbunitlocation
ON tbbuilding.buildingid = tbunitlocation.locationid
INNER JOIN tbunit
ON tbunitlocation.unitid = tbunit.unitid
INNER JOIN tblease
ON tbunit.unitid = tblease.unitid

SQL - Join issue

I have the following query:
SELECT rt.ID, rt.Name, rt.Rate, rt.Colour, vtb.ID AS 'vtbID', vtb.Value, rt.StdID
FROM Rates AS rt
LEFT OUTER JOIN VehicleTypeCostsBreakdown AS vtb ON rt.ID = vtb.RateID
LEFT OUTER JOIN VehicleTypeCostsDepots AS vtd ON vtd.ID = vtb.VehicleTypeDepotID AND vtd.DepotID = #DepotID AND vtd.VehicleTypeID = #VehicleTypeID
Basically, I want to select all 'rates' from Rates table, but if any references to a rate exists in the 'vtd' table, which has parameters that match #DepotID and #VehicleTypeID, I want to bring back the Value for that. If it doesn't have any referenced, I want it the 'vtb.Value' selection to be blank.
With the SQL above, it seems to always return a value for 'vtb.Value' value, even if the parameters are null. Am I missing something?
Try it this way. Basically, you'll LEFT JOIN to the derived table formed by the INNER JOIN between VehicleTypeCostsBreakdown and VehicleTypeCostsDepots. The INNER JOIN will only match when all of your conditions are true.
SELECT rt.ID, rt.Name, rt.Rate, rt.Colour, vtb.ID AS 'vtbID', vtb.Value, rt.StdID
FROM Rates AS rt
LEFT OUTER JOIN VehicleTypeCostsBreakdown AS vtb
INNER JOIN VehicleTypeCostsDepots AS vtd
ON vtd.ID = vtb.VehicleTypeDepotID
AND vtd.DepotID = #DepotID
AND vtd.VehicleTypeID = #VehicleTypeID
ON rt.ID = vtb.RateID
Try:
SELECT rt.ID, rt.Name, rt.Rate, rt.Colour, vtb.ID AS 'vtbID', vtb.Value, rt.StdID
FROM Rates AS rt
LEFT OUTER JOIN (SELECT b.ID, b.Value, b.RateID
FROM VehicleTypeCostsBreakdown AS b
JOIN VehicleTypeCostsDepots AS d
ON d.ID = b.VehicleTypeDepotID AND
d.DepotID = #DepotID AND
d.VehicleTypeID = #VehicleTypeID)
AS vtb ON rt.ID = vtb.RateID
Try this:
SELECT rt.ID, rt.Name, rt.Rate, rt.Colour, vtb.ID AS 'vtbID', vtb.Value, rt.StdID
FROM Rates AS rt
LEFT JOIN VehicleTypeCostsBreakdown AS vtb ON rt.ID = vtb.RateID
LEFT JOIN VehicleTypeCostsDepots AS vtd ON vtd.ID = vtb.VehicleTypeDepotID
WHERE vtd.ID IS NULL OR (vtd.DepotID = #DepotID AND vtd.VehicleTypeID = #VehicleTypeID)
You don't need to specify that the LEFT JOIN is an OUTER JOIN and you shouldn't put conditions in the ON section of a JOIN, that's what WHERE is for.