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
Related
Is it possible to do case statements and to left outer joins like I am doing below? I put comments on what I am trying to achieve.
SELECT
eventGame.Id,
eventGame.[Type] AS GameType
FROM GrassrootsHoops.EventGame eventGame
LEFT OUTER JOIN (SELECT
divisionGameTeamResult.Id,
divisionGameTeamResult.GameId,
COALESCE(divisionPool.Name, divisionTeamPoolPoolBracket.Name) AS DivisionPoolName
FROM GrassrootsHoops.DivisionGameTeamResult divisionGameTeamResult
LEFT OUTER JOIN
-- CASE eventGame.[Type] = 1 THEN
GrassrootsHoops.DivisionTeamPoolGame divisionTeamPoolGame
ON divisionTeamPoolGame.Id = divisionGameTeamResult.Id
LEFT OUTER JOIN GrassrootsHoopS.DivisionTeamPool divisionTeamPool
ON divisionTeamPool.Id = divisionTeamPoolGame.DivisionTeamPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionPool divisionPool
ON divisionPool.Id = divisionTeamPool.DivisionPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionPoolSettings divisionPoolSettings
ON divisionPool.Id = divisionPoolSettings.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionTeam divisionTeam
ON divisionTeam.Id = divisionTeamPool.DivisionTeamId
LEFT OUTER JOIN GrassrootsHoops.Team team
ON team.Id = divisionTeam.Id
LEFT OUTER JOIN
-- CASE eventGame.[Type] = 2 THEN
GrassrootsHoops.DivisionBracketParticipant divisionBracketGameParticipant
ON divisionBracketGameParticipant.DivisionGameTeamResultId = divisionGameTeamResult.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantPart divisionBracketParticipantPart
ON divisionBracketParticipantPart.Id = divisionBracketGameParticipant.DivisionBracketParticipantPartId
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantTeam divisionBracketGameParticipantTeam
ON divisionBracketGameParticipantTeam.Id = divisionBracketGameParticipant.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionTeam divisionTeamBracket
ON divisionTeamBracket.Id = divisionBracketGameParticipantTeam.DivisionTeamId
LEFT OUTER JOIN GrassrootsHoops.Team teamBracket
ON teamBracket.Id = divisionTeamBracket.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantBracket divisionBracketParticipantBracket
ON divisionBracketParticipantBracket.Id = divisionBracketGameParticipant.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionBracket divisionBracketParticipantBracketBracket
ON divisionBracketParticipantBracketBracket.Id = divisionBracketParticipantBracket.DivisionBracketId
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantPool divisionBracketGameParticipantPool
ON divisionBracketGameParticipantPool.Id = divisionBracketGameParticipant.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionPool divisionPoolBracket
ON divisionPoolBracket.Id = divisionBracketGameParticipantPool.DivisionPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantTeamPool divisionBracketGameParticipantTeamPool
ON divisionBracketGameParticipantTeamPool.Id = divisionBracketGameParticipant.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionTeamPool divisionTeamPoolBracket
ON divisionTeamPoolBracket.Id = divisionBracketGameParticipantTeamPool.DivisionTeamPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionPool divisionTeamPoolPoolBracket
ON divisionTeamPoolPoolBracket.Id = divisionTeamPoolBracket.DivisionPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionTeam divisionTeamPoolTeamBracket
ON divisionTeamPoolTeamBracket.Id = divisionTeamPoolBracket.DivisionTeamId
LEFT OUTER JOIN GrassrootsHoops.Team teamPoolTeamBracket
ON teamPoolTeamBracket.Id = divisionTeamPoolTeamBracket.Id
WHERE divisionGameTeamResult.HomeTeam = 1) homeTeam
ON homeTeam.GameId = eventGame.GameId
You can't do case statements with outer joins. I think you can do what you want, though by using a UNION. Here's an example using a simpler schema where we are doing two separate joins based on the type of game.
SELECT
Game.Id,
PoolResult.Score
FROM
Game
JOIN PoolResult
ON PoolResult.GameId = Game.Id
WHERE
Game.GameType = 1
UNION
SELECT
Game.Id,
BracketResult.Score
FROM
Game
JOIN BracketResult
ON BracketResult.GameId = Game.Id
WHERE
Game.GameType = 2
(Here's a working version at SQL Fiddle)
For your query, you just want to write two completely separate queries one for each type of game. Once you have that working for each separate query, just stick a UNION in between them and the results will be concatenated together. Here's your query modified to use a UNION. I don't have an easy way to check it for errors, so there's probably some minor errors
SELECT
divisionGameTeamResult.Id,
eventGame.[Type] AS GameType
FROM
GrassrootsHoops.EventGame eventGame
JOIN GrassrootsHoops.DivisionGameTeamResult divisionGameTeamResult
ON (divisionGameTeamResult.GameId = eventGame.GameId
JOIN GrassrootsHoops.DivisionTeamPoolGame divisionTeamPoolGame
ON divisionTeamPoolGame.Id = divisionGameTeamResult.Id
JOIN GrassrootsHoopS.DivisionTeamPool divisionTeamPool
ON divisionTeamPool.Id = divisionTeamPoolGame.DivisionTeamPoolId
JOIN GrassrootsHoops.DivisionPool divisionPool
ON divisionPool.Id = divisionTeamPool.DivisionPoolId
JOIN GrassrootsHoops.DivisionPoolSettings divisionPoolSettings
ON divisionPool.Id = divisionPoolSettings.Id
JOIN GrassrootsHoops.DivisionTeam divisionTeam
ON divisionTeam.Id = divisionTeamPool.DivisionTeamId
JOIN GrassrootsHoops.Team team
ON team.Id = divisionTeam.Id
WHERE
divisionGameTeamResult.HomeTeam = 1
AND
eventGame.[Type] = 1
UNION
SELECT
divisionGameTeamResult.Id,
eventGame.[Type] AS GameType
FROM
GrassrootsHoops.EventGame eventGame
JOIN GrassrootsHoops.DivisionGameTeamResult divisionGameTeamResult
ON (divisionGameTeamResult.GameId = eventGame.GameId)
JOIN GrassrootsHoops.DivisionBracketParticipant divisionBracketGameParticipant
ON divisionBracketGameParticipant.DivisionGameTeamResultId = divisionGameTeamResult.Id
JOIN GrassrootsHoops.DivisionBracketParticipantPart divisionBracketParticipantPart
ON divisionBracketParticipantPart.Id = divisionBracketGameParticipant.DivisionBracketParticipantPartId
JOIN GrassrootsHoops.DivisionBracketParticipantTeam divisionBracketGameParticipantTeam
ON divisionBracketGameParticipantTeam.Id = divisionBracketGameParticipant.Id
JOIN GrassrootsHoops.DivisionTeam divisionTeamBracket
ON divisionTeamBracket.Id = divisionBracketGameParticipantTeam.DivisionTeamId
JOIN GrassrootsHoops.Team teamBracket
ON teamBracket.Id = divisionTeamBracket.Id
JOIN GrassrootsHoops.DivisionBracketParticipantBracket divisionBracketParticipantBracket
ON divisionBracketParticipantBracket.Id = divisionBracketGameParticipant.Id
JOIN GrassrootsHoops.DivisionBracket divisionBracketParticipantBracketBracket
ON divisionBracketParticipantBracketBracket.Id = divisionBracketParticipantBracket.DivisionBracketId
JOIN GrassrootsHoops.DivisionBracketParticipantPool divisionBracketGameParticipantPool
ON divisionBracketGameParticipantPool.Id = divisionBracketGameParticipant.Id
JOIN GrassrootsHoops.DivisionPool divisionPoolBracket
ON divisionPoolBracket.Id = divisionBracketGameParticipantPool.DivisionPoolId
JOIN GrassrootsHoops.DivisionBracketParticipantTeamPool divisionBracketGameParticipantTeamPool
ON divisionBracketGameParticipantTeamPool.Id = divisionBracketGameParticipant.Id
JOIN GrassrootsHoops.DivisionTeamPool divisionTeamPoolBracket
ON divisionTeamPoolBracket.Id = divisionBracketGameParticipantTeamPool.DivisionTeamPoolId
JOIN GrassrootsHoops.DivisionPool divisionTeamPoolPoolBracket
ON divisionTeamPoolPoolBracket.Id = divisionTeamPoolBracket.DivisionPoolId
JOIN GrassrootsHoops.DivisionTeam divisionTeamPoolTeamBracket
ON divisionTeamPoolTeamBracket.Id = divisionTeamPoolBracket.DivisionTeamId
JOIN GrassrootsHoops.Team teamPoolTeamBracket
ON teamPoolTeamBracket.Id = divisionTeamPoolTeamBracket.Id
WHERE
divisionGameTeamResult.HomeTeam = 1
AND
eventGame.[Type] = 1
i have the above SQL query is not compiling somewhere i lost a truck if you please help me locate the error.
SELECT *
FROM tblWarehouse AS W INNER JOIN (((tblTransactionsSC AS T
LEFT JOIN tblCustomer AS C ON T.tracstID = C.cstID)
INNER JOIN (tblTransactionsSubSC AS TS
LEFT JOIN tblWarehouseItem AS WI
ON TS.trswhiID = WI.whiID)
ON T.traID = TS.trstraID)
ON W.wrhID = T.trawrhID)
LEFT JOIN tblTransactionsSC ON tblStockItemAssignment.siaID = tblTransactionsSC.trasiaID
Your JOIN ON clauses are going haywire; try placing them properly like
SELECT w.*
FROM tblWarehouse AS W
INNER JOIN tblTransactionsSC AS T ON W.wrhID = T.trawrhID
LEFT JOIN tblCustomer AS C ON T.tracstID = C.cstID
INNER JOIN tblTransactionsSubSC AS TS ON T.traID = TS.trstraID
LEFT JOIN tblWarehouseItem AS WI ON TS.trswhiID = WI.whiID
LEFT JOIN tblTransactionsSC ON tblStockItemAssignment.siaID = tblTransactionsSC.trasiaID
I use a system that doesn't allow me access to the SQL database directly and I have to use their in built reporting tool which is just awful. I'm running the below code and its telling me there is a syntax error near ) but I can't see myself how this is incorrect, any ideas?
SELECT ST.Staff_ID,
COUNT (CASE WHEN Referral_Date BETWEEN #StartDate AND #EndDate ) AS 'Referrals',
COUNT (CASE WHEN Att_Start_Dttm BETWEEN #StartDate AND #EndDate ) AS 'Appointments'
FROM tbldiaryappointment DA
left outer join tblDiaryAppointmentClinicianInvitee CI on DA.Diary_Appointment_ID = CI.Diary_Appointment_ID
left outer join tblstaff ST on CI.Clinician_Invitee_Staff_ID = ST.STaff_ID
left outer join tbllocation LOC on LOC.Location_ID = DA.Scheduled_Location_ID
left outer join tblpatient PAT on PAT.Patient_ID = DA.Patient_ID
left outer join tblAttendanceTypeValues ATV on ATV.Attendance_Type_ID = DA.Attendance_Type_ID
left outer join tblDiaryAppointmentScheduledEvent SE on SE.Diary_Appointment_ID = DA.Diary_Appointment_ID
left outer join tblEventValues EVE on EVE.Event_ID = SE.Event_ID
left outer join tblProfessionalGroupValues PGV on ST.Professional_Group_ID = PGV.Professional_Group_ID
left outer join tblStaffJobRoleCodeValues JOB on JOB.Staff_Job_Role_Code_ID = ST.Job_Role_Code_ID
left outer join tblReferral REF on REF.Patient_ID = DA.Patient_ID
Syntax for Searched CASE expression is:
CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
You're missing the part after Boolean_expression.
Also, your statement is missing GROUP BY clause.
SELECT ST.Staff_ID,
COUNT (CASE WHEN Referral_Date BETWEEN #StartDate AND #EndDate THEN 1 END) AS 'Referrals',
COUNT (CASE WHEN Att_Start_Dttm BETWEEN #StartDate AND #EndDate THEN 1 END ) AS 'Appointments'
FROM tbldiaryappointment DA
left outer join tblDiaryAppointmentClinicianInvitee CI on DA.Diary_Appointment_ID = CI.Diary_Appointment_ID
left outer join tblstaff ST on CI.Clinician_Invitee_Staff_ID = ST.STaff_ID
left outer join tbllocation LOC on LOC.Location_ID = DA.Scheduled_Location_ID
left outer join tblpatient PAT on PAT.Patient_ID = DA.Patient_ID
left outer join tblAttendanceTypeValues ATV on ATV.Attendance_Type_ID = DA.Attendance_Type_ID
left outer join tblDiaryAppointmentScheduledEvent SE on SE.Diary_Appointment_ID = DA.Diary_Appointment_ID
left outer join tblEventValues EVE on EVE.Event_ID = SE.Event_ID
left outer join tblProfessionalGroupValues PGV on ST.Professional_Group_ID = PGV.Professional_Group_ID
left outer join tblStaffJobRoleCodeValues JOB on JOB.Staff_Job_Role_Code_ID = ST.Job_Role_Code_ID
left outer join tblReferral REF on REF.Patient_ID = DA.Patient_ID
GROUP BY ST.Staff_ID
I've been struggling with this for some time now. It's something to do with how I'm joining the MMP_PPRVU I believe. Essentially, I'm trying to join CLARITY_TDL_TRAN --> CLARITY_EAP1 --> MMP_PPRVU. As well as CLARITY_TDL_TRAN --> MMP_STAGING_UNITS --> MMP_PPRVU.
All left outer joins. I've tried multiple different approaches but cannot come up with a solution that works. Any help is greatly appreciated.
SELECT........
FROM
CLARITY.dbo.CLARITY_TDL_TRAN as CLARITY_TDL_TRAN
left outer join
dbo.MMP_STAGING_UNITS as MMP_STAGING_UNITS on MMP_Staging_Units.eTDL_ID = CLARITY_TDL_TRAN.TDL_ID and CLARITY_TDL_TRAN.DETAIL_TYPE in (1, 10)
left outer join
CLARITY.dbo.CLARITY_EAP as CLARITY_EAP on CLARITY_EAP.PROC_ID = CLARITY_TDL_TRAN.PROC_ID
left outer join
CLARITY.dbo.CLARITY_EAP AS CLARITY_EAP1 on CLARITY_EAP1.PROC_ID = CLARITY_TDL_TRAN.PROC_ID --look into this alias
left outer join
CLARITY.dbo.CLARITY_POS as CLARITY_POS on CLARITY_TDL_TRAN.POS_ID = CLARITY_POS.POS_ID
left outer join
CLARITY.dbo.CLARITY_DEP as CLARITY_DEP on CLARITY_TDL_TRAN.DEPT_ID = CLARITY_DEP.DEPARTMENT_ID
left outer join
CLARITY.dbo.CLARITY_LOC as CLARITY_LOC on CLARITY_TDL_TRAN.LOC_ID = CLARITY_LOC.LOC_ID
left outer join
dbo.mmp_PatientsTestaYes as mmp_PatientsTestaYes on CLARITY_TDL_TRAN.INT_PAT_ID = mmp_PatientsTestaYes.PAT_ID
left outer join
CLARITY.dbo.CLARITY_SA as CLARITY_SA on CLARITY_TDL_TRAN.SERV_AREA_ID = CLARITY_SA.SERV_AREA_ID
left outer join
CLARITY.dbo.CLARITY_SER as CLARITY_SER on CLARITY_TDL_TRAN.PERFORMING_PROV_ID = CLARITY_SER.PROV_ID
left outer join
CLARITY.dbo.CLARITY_EAF_TIN as CLARITY_EAF_TIN ON CLARITY_LOC.LOC_ID = CLARITY_EAF_TIN.FACILITY_ID
left outer join
dbo.MMP_PPRVU as MMP_PPRVU on MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
left outer join
dbo.MMP_revctrs$ as MMP_revctrs$ ON CLARITY_EAP.PROC_CODE = MMP_revctrs$.CPT
left outer join
MMP_PPRVU ON CLARITY_EAP_1.PROC_CODE = MMP_PPRVU.HCPCS
WHERE (CLARITY_TDL_TRAN.DETAIL_TYPE IN (1, 10)) AND (mmp_PatientsTestaYes.PAT_ID IS NULL) AND (CLARITY_EAF_TIN.LINE = 1)
When you give a table an alias in your FROM clause, it's superflous to name the alias the same as the table name.
LEFT OUTER JOIN dbo.MMP_PPRVU AS MMP_PPRVU
ON MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
Furthermore, you join in the same table a few JOIN's down and it's name is the same. One of these two tables will need to have an alias that is different from the table name. Perhaps:
LEFT OUTER JOIN dbo.MMP_PPRVU as MMP_PPRVU_1
ON MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
So it doesn't conflict with:
LEFT OUTER JOIN MMP_PPRVU
ON CLARITY_EAP_1.PROC_CODE = MMP_PPRVU.HCPCS
I find it helpful to use slightly different indentation when writing my FROM clause so that these things stand out a bit more and it's easier to parse.
FROM CLARITY.dbo.CLARITY_TDL_TRAN AS CLARITY_TDL_TRAN
LEFT OUTER JOIN dbo.MMP_STAGING_UNITS AS MMP_STAGING_UNITS
ON MMP_Staging_Units.eTDL_ID = CLARITY_TDL_TRAN.TDL_ID
AND CLARITY_TDL_TRAN.DETAIL_TYPE IN (
1,
10
)
LEFT OUTER JOIN CLARITY.dbo.CLARITY_EAP AS CLARITY_EAP
ON CLARITY_EAP.PROC_ID = CLARITY_TDL_TRAN.PROC_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_EAP AS CLARITY_EAP1
ON CLARITY_EAP1.PROC_ID = CLARITY_TDL_TRAN.PROC_ID --look into this alias
LEFT OUTER JOIN CLARITY.dbo.CLARITY_POS AS CLARITY_POS
ON CLARITY_TDL_TRAN.POS_ID = CLARITY_POS.POS_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_DEP AS CLARITY_DEP
ON CLARITY_TDL_TRAN.DEPT_ID = CLARITY_DEP.DEPARTMENT_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_LOC AS CLARITY_LOC
ON CLARITY_TDL_TRAN.LOC_ID = CLARITY_LOC.LOC_ID
LEFT OUTER JOIN dbo.mmp_PatientsTestaYes AS mmp_PatientsTestaYes
ON CLARITY_TDL_TRAN.INT_PAT_ID = mmp_PatientsTestaYes.PAT_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_SA AS CLARITY_SA
ON CLARITY_TDL_TRAN.SERV_AREA_ID = CLARITY_SA.SERV_AREA_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_SER AS CLARITY_SER
ON CLARITY_TDL_TRAN.PERFORMING_PROV_ID = CLARITY_SER.PROV_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_EAF_TIN AS CLARITY_EAF_TIN
ON CLARITY_LOC.LOC_ID = CLARITY_EAF_TIN.FACILITY_ID
LEFT OUTER JOIN dbo.MMP_PPRVU AS MMP_PPRVU_1
ON MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
LEFT OUTER JOIN dbo.MMP_revctrs$ AS MMP_revctrs$
ON CLARITY_EAP.PROC_CODE = MMP_revctrs$.CPT
LEFT OUTER JOIN MMP_PPRVU
ON CLARITY_EAP_1.PROC_CODE = MMP_PPRVU.HCPCS
I like to use indentation to help keep joins understandable.
You can see below that you give the second join against CLARITY_EAP the alias of CLARITY_EAP1 but then when you try to join against it you use the alias CLARITY_EAP_1 which isn't defined and is likely throwing an error.
SELECT........
FROM CLARITY.dbo.CLARITY_TDL_TRAN as CLARITY_TDL_TRAN
left outer join dbo.MMP_STAGING_UNITS as MMP_STAGING_UNITS on MMP_Staging_Units.eTDL_ID = CLARITY_TDL_TRAN.TDL_ID and CLARITY_TDL_TRAN.DETAIL_TYPE in (1, 10)
left outer join dbo.MMP_PPRVU as MMP_PPRVU1 on MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
left outer join CLARITY.dbo.CLARITY_EAP as CLARITY_EAP on CLARITY_EAP.PROC_ID = CLARITY_TDL_TRAN.PROC_ID
left outer join dbo.MMP_revctrs$ as MMP_revctrs$ ON CLARITY_EAP.PROC_CODE = MMP_revctrs$.CPT
left outer join CLARITY.dbo.CLARITY_EAP AS CLARITY_EAP1 on CLARITY_EAP1.PROC_ID = CLARITY_TDL_TRAN.PROC_ID --look into this alias
left outer join MMP_PPRVU as MMP_PPRVU2 ON CLARITY_EAP1.PROC_CODE = MMP_PPRVU2.HCPCS
left outer join CLARITY.dbo.CLARITY_POS as CLARITY_POS on CLARITY_TDL_TRAN.POS_ID = CLARITY_POS.POS_ID
left outer join CLARITY.dbo.CLARITY_DEP as CLARITY_DEP on CLARITY_TDL_TRAN.DEPT_ID = CLARITY_DEP.DEPARTMENT_ID
left outer join CLARITY.dbo.CLARITY_LOC as CLARITY_LOC on CLARITY_TDL_TRAN.LOC_ID = CLARITY_LOC.LOC_ID
left outer join CLARITY.dbo.CLARITY_EAF_TIN as CLARITY_EAF_TIN ON CLARITY_LOC.LOC_ID = CLARITY_EAF_TIN.FACILITY_ID
left outer join dbo.mmp_PatientsTestaYes as mmp_PatientsTestaYes on CLARITY_TDL_TRAN.INT_PAT_ID = mmp_PatientsTestaYes.PAT_ID
left outer join CLARITY.dbo.CLARITY_SA as CLARITY_SA on CLARITY_TDL_TRAN.SERV_AREA_ID = CLARITY_SA.SERV_AREA_ID
left outer join CLARITY.dbo.CLARITY_SER as CLARITY_SER on CLARITY_TDL_TRAN.PERFORMING_PROV_ID = CLARITY_SER.PROV_ID
WHERE
CLARITY_TDL_TRAN.DETAIL_TYPE IN (1, 10) AND
mmp_PatientsTestaYes.PAT_ID IS NULL AND
CLARITY_EAF_TIN.LINE = 1
I'm getting this error: Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause.
I searched on internet, but i don't know how to apply the correction in my case. I found different types of query only.
I'm trying to get a Count result in a field (line 5), but when I add the Count query i'm getting this error.
I'm using SQL SERVER 2008 R2.
When I remove the Count from SELECT and GROUP BY the query run correctly.
SELECT TF.COD_FORNECEDOR AS 'Cód. Fornec.',
TF.NOM_FANTASIA AS 'Fornecedor',
DM.COD_CONTRATO_RELACIONADO AS 'CONTRATO',
UA2.NOM_USUARIO AS 'NOM_USUARIO_COMPRADOR',
(SELECT COUNT(COD_CONTRATO_RELACIONADO) FROM TB_DEMANDA D INNER JOIN TB_PROCESSO P ON D.COD_CONTRATO_RELACIONADO = P.NUM_CONTRATO WHERE D.COD_CONTRATO_RELACIONADO = DM.COD_CONTRATO_RELACIONADO) AS 'NUM_ADITIVO',
0 AS 'Qtd. Aditivo',
SUM(DM.VAL_TOTAL_ORCADO) AS 'Valor Inicial',
SUM(TP.VAL_CONTRATADO) AS 'Valor Final',
((SUM(TP.VAL_CONTRATADO) / SUM(DM.VAL_TOTAL_ORCADO)) -1) * 100 AS 'Var. %'
FROM TB_FORNECEDOR TF
LEFT JOIN TB_DEMANDA DM ON DM.COD_FORNECEDOR = TF.COD_FORNECEDOR
LEFT JOIN TB_PROCESSO TP ON DM.COD_DEMANDA = TP.COD_DEMANDA
LEFT JOIN TB_PROCESSO_FORNECEDOR PF ON PF.COD_PROCESSO = TP.COD_PROCESSO
LEFT JOIN USUARIO UA ON UA.NUM_USUARIO = DM.NUM_USUARIO_COMPRADOR
LEFT JOIN USUARIO UA2 ON UA2.NUM_USUARIO = DM.NUM_USUARIO_COMPRADOR
LEFT JOIN TB_STATUS_DEMANDA_DATA SD ON SD.COD_DEMANDA = DM.COD_DEMANDA
LEFT JOIN TB_BASELINE BA ON BA.COD_PROCESSO = TP.COD_PROCESSO
LEFT JOIN TB_BASELINE_TAREFAS BT ON BT.COD_BASELINE = BA.COD_BASELINE AND BT.COD_PROCESSO = BA.COD_PROCESSO
LEFT JOIN TB_STATUS_PROCESSO SP ON SP.COD_STATUS = SD.COD_STATUS
LEFT JOIN TB_MEDIO_STATUS MS ON MS.COD_MEDIO_STATUS = SP.COD_MEDIO_STATUS
left JOIN #TB_PROCESSO TT ON TT.COD_PROCESSO = TP.COD_PROCESSO
GROUP BY
TF.COD_FORNECEDOR,
TF.NOM_FANTASIA,
DM.COD_CONTRATO_RELACIONADO,
UA2.NOM_USUARIO,
(SELECT COUNT(COD_CONTRATO_RELACIONADO) FROM TB_DEMANDA D INNER JOIN TB_PROCESSO P ON D.COD_CONTRATO_RELACIONADO = P.NUM_CONTRATO WHERE D.COD_CONTRATO_RELACIONADO = DM.COD_CONTRATO_RELACIONADO)
ORDER BY TF.NOM_FANTASIA
Try this:
SELECT TF.COD_FORNECEDOR AS 'Cód. Fornec.',
TF.NOM_FANTASIA AS 'Fornecedor',
DM.COD_CONTRATO_RELACIONADO AS 'CONTRATO',
UA2.NOM_USUARIO AS 'NOM_USUARIO_COMPRADOR',
sq.cnt AS 'NUM_ADITIVO',
0 AS 'Qtd. Aditivo',
SUM(DM.VAL_TOTAL_ORCADO) AS 'Valor Inicial',
SUM(TP.VAL_CONTRATADO) AS 'Valor Final',
((SUM(TP.VAL_CONTRATADO) / SUM(DM.VAL_TOTAL_ORCADO)) -1) * 100 AS 'Var. %'
FROM TB_FORNECEDOR TF
LEFT JOIN TB_DEMANDA DM ON DM.COD_FORNECEDOR = TF.COD_FORNECEDOR
LEFT JOIN TB_PROCESSO TP ON DM.COD_DEMANDA = TP.COD_DEMANDA
LEFT JOIN TB_PROCESSO_FORNECEDOR PF ON PF.COD_PROCESSO = TP.COD_PROCESSO
LEFT JOIN USUARIO UA ON UA.NUM_USUARIO = DM.NUM_USUARIO_COMPRADOR
LEFT JOIN USUARIO UA2 ON UA2.NUM_USUARIO = DM.NUM_USUARIO_COMPRADOR
LEFT JOIN TB_STATUS_DEMANDA_DATA SD ON SD.COD_DEMANDA = DM.COD_DEMANDA
LEFT JOIN TB_BASELINE BA ON BA.COD_PROCESSO = TP.COD_PROCESSO
LEFT JOIN TB_BASELINE_TAREFAS BT ON BT.COD_BASELINE = BA.COD_BASELINE AND BT.COD_PROCESSO = BA.COD_PROCESSO
LEFT JOIN TB_STATUS_PROCESSO SP ON SP.COD_STATUS = SD.COD_STATUS
LEFT JOIN TB_MEDIO_STATUS MS ON MS.COD_MEDIO_STATUS = SP.COD_MEDIO_STATUS
LEFT JOIN #TB_PROCESSO TT ON TT.COD_PROCESSO = TP.COD_PROCESSO
LEFT JOIN (
SELECT D.COD_CONTRATO_RELACIONADO, COUNT(COD_CONTRATO_RELACIONADO) AS cnt
FROM TB_DEMANDA D
INNER JOIN TB_PROCESSO P ON D.COD_CONTRATO_RELACIONADO = P.NUM_CONTRATO
GROUP BY D.COD_CONTRATO_RELACIONADO
) sq ON sq.COD_CONTRATO_RELACIONADO = DM.COD_CONTRATO_RELACIONADO
GROUP BY
TF.COD_FORNECEDOR,
TF.NOM_FANTASIA,
DM.COD_CONTRATO_RELACIONADO,
UA2.NOM_USUARIO,
sq.cnt
ORDER BY TF.NOM_FANTASIA
I integrated the num_aditivo subquery in a LEFT JOIN.