Syntax Error: Token Join at Position 760 when importing SQL into Discoverer 11g - oracle

I wrote the query (below) in PL/SQL Developer for a client and would like to import it into Oracle Discoverer Desktop 11g so the report is self-serve. The query works perfectly in PL/SQL but when I go to import it into Discoverer, I get the following errors: "Error: Syntax error: token JOIN at position 760. Error: Parse stopped due to fatal errors."
Can anyone tell me what these errors mean and how to resolve the issue?
Thanks!
Query:
Select eid.person_code, eid.last_name plnownr_lname, eid.first_name plnownr_fname, eid.birth_date plnownr_DOB, des.status_code, dun.unit_code, ect.last_name BenRecip_LNAME, ect.first_name BenRecip_fname, ect.birth_date BenRecip_DOB,decode(ect.relation,'00','Unknown','01','Spouse','02','Common-Law Spouse','03','Partner', '05','Daughter','06','Son','07','Step-Daughter','08','Step-Son','09', 'Granddaughter','11', 'Father','12','Mother', '20','Sister','21','Brother','24','Aunt','28','Nephew','29','Ex-spouse','31','Friend','43','Trust','50','User Defined','52','User Defined', '53','User Defined','54','User Defined',ect.relation) relation, bpn.description, bpn.plan_code, bend.premium_start_date, bend.premium_end_date
From p2k_hr_identities eid
Join p2k_hr_contacts ect on ect.eid_id = eid.id
Join p2k_be_benefit_recipients bbr on bbr.ect_id = ect.id
Join p2k_be_enrollments ben on ben.id = bbr.ben_id
Join p2k_be_plan_definitions bpn on bpn.id = ben.bpn_id
Join p2k_hr_employments eem on eem.eid_id = eid.id
and eem.termination_date is null
Join p2k_hr_assignments eas on eas.eem_id = eem.id
Join p2k_hr_assignment_details easd on easd.eas_id = eas.id
and easd.expiry = to_date('3999-12-31', 'yyyy-mm-dd')
Join p2k_cm_units dun on dun.id = easd.dun_id
Join p2k_be_enrollment_details bend on bend.ben_id = ben.id
and bend.premium_end_date = to_date('3999-12-31','yyyy-mm-dd')
and bend.premium_start_date != to_date('0001-01-01','yyyy-mm-dd')
Join p2k_cm_employment_statuses des on des.id = easd.des_id
and des.status_code not in ('TERMINATED', 'RETIRED')
Union
Select eid.person_code, eid.last_name plnownr_lname, eid.first_name eplnownr_fname, eid.birth_date plnownr_dob, des.status_code, dun.unit_code, eid.last_name BenRecip_lname, eid.first_name BenRecip_fname, eid.birth_date BenRecip_dob, 'Employee-NoRelation', bpn.description, bpn.plan_code, bend.premium_start_date, bend.premium_end_date
From p2k_hr_identities eid
Join p2k_hr_employments eem on eem.eid_id = eid.id
and eem.termination_date is null
Join p2k_be_enrollments ben on ben.eem_id = eem.id
Join p2k_be_plan_definitions bpn on bpn.id = ben.bpn_id
Join p2k_hr_assignments eas on eas.eem_id = eem.id
Join p2k_hr_assignment_details easd on easd.eas_id = eas.id
and easd.expiry = to_date('3999-12-31', 'yyyy-mm-dd')
Join p2k_cm_units dun on dun.id = easd.dun_id
Join p2k_be_enrollment_details bend on bend.ben_id = ben.id
and bend.premium_end_date = to_date('3999-12-31','yyyy-mm-dd')
and bend.premium_start_date != to_date('0001-01-01','yyyy-mm-dd')
Join p2k_cm_employment_statuses des on des.id = easd.des_id
and des.status_code not in ('TERMINATED', 'RETIRED')
Order by 2,3,5,6

Related

column ambiguously defined even though all the selected columns are prefixed, while creating view

CREATE OR REPLACE VIEW V_DU_NBO_CAMPAIGN
AS
SELECT PMM_UPSELL_CAMPAIGN.STATUS, PMM_UPSELL_CAMPAIGN.TEST_EPOCH, DU_NBO_CAMPAIGN.*, OFFER_MSG_OFFERMESSAGE.TEXT AS OFFER_MSG_OFFERMESSAGE, NULL AS AWARD_MSG, REMINDER_MSG_REMINDERMESSAGE.TEXT AS REMINDER_MSG_REMINDERMESSAGE, PMM_PROGRAM.BASE_TIMEZONE, PMM_UPSELL_CAMPAIGN.CAMPAIGN_TEMPLATE, PMM_UPSELL_CAMPAIGN.TASKS_SUBMITTED, PMM_UPSELL_CAMPAIGN.TASKS_EVALUATED, PMM_UPSELL_CAMPAIGN.TASKS_COMPLETED, PMM_UPSELL_CAMPAIGN.EMITS, PMM_UPSELL_CAMPAIGN.LAST_EVALUATION, PMM_UPSELL_CAMPAIGN.LAST_PREEVALUATION, PMM_UPSELL_CAMPAIGN.TARGET_LIST_JOB_STATUS, PMM_UPSELL_CAMPAIGN.TARGET_LIST_TYPE, PMM_UPSELL_CAMPAIGN.TARGET_LIST_ID, PMM_UPSELL_CAMPAIGN.PENDING_TIME, PMM_UPSELL_CAMPAIGN.STARTING_TIME, PMM_UPSELL_CAMPAIGN.OFFERING_TIME, PMM_UPSELL_CAMPAIGN.OPEN_TIME, PMM_UPSELL_CAMPAIGN.SUSPENDED_TIME, PMM_UPSELL_CAMPAIGN.COMPLETE_TIME, PMM_UPSELL_CAMPAIGN.FAILED_TIME, PMM_UPSELL_CAMPAIGN.KILLED_TIME, PMM_UPSELL_CAMPAIGN.LAST_UPDATE_TIME
FROM
DU_NBO_CAMPAIGN
LEFT JOIN
PMM_UPSELL_CAMPAIGN
ON PMM_UPSELL_CAMPAIGN.CAMPAIGNID = DU_NBO_CAMPAIGN.CAMPAIGNID
LEFT JOIN
PMM_PROGRAM
ON PMM_PROGRAM.PROGRAM_ID = DU_NBO_CAMPAIGN.PROGRAM_ID
LEFT JOIN
DU_NBO_CAMPAIGN_MSG OFFER_MSG_OFFERMESSAGE
ON DU_NBO_CAMPAIGN.PROGRAM_ID = OFFER_MSG_OFFERMESSAGE.PROGRAM_ID
AND DU_NBO_CAMPAIGN.CAMPAIGNID = OFFER_MSG_OFFERMESSAGE.CAMPAIGNID
AND PMM_PROGRAM.DEFAULT_LANGUAGE = OFFER_MSG_OFFERMESSAGE."LANGUAGE"
AND OFFER_MSG_OFFERMESSAGE.MESSAGEID = 'NBO_SMS_OFFER'
LEFT JOIN
DU_NBO_CAMPAIGN_MSG OFFER_MSG_OFFERMESSAGE
ON DU_NBO_CAMPAIGN.PROGRAM_ID = OFFER_MSG_OFFERMESSAGE.PROGRAM_ID
AND DU_NBO_CAMPAIGN.CAMPAIGNID = OFFER_MSG_OFFERMESSAGE.CAMPAIGNID
AND PMM_PROGRAM.DEFAULT_LANGUAGE = OFFER_MSG_OFFERMESSAGE."LANGUAGE"
AND OFFER_MSG_OFFERMESSAGE.MESSAGEID = 'NBO_SMS_AWARD'
LEFT JOIN
DU_NBO_CAMPAIGN_MSG REMINDER_MSG_REMINDERMESSAGE
ON DU_NBO_CAMPAIGN.PROGRAM_ID = REMINDER_MSG_REMINDERMESSAGE.PROGRAM_ID
AND DU_NBO_CAMPAIGN.CAMPAIGNID = REMINDER_MSG_REMINDERMESSAGE.CAMPAIGNID
AND PMM_PROGRAM.DEFAULT_LANGUAGE = REMINDER_MSG_REMINDERMESSAGE."LANGUAGE"
AND REMINDER_MSG_REMINDERMESSAGE.MESSAGEID = 'NBO_SMS_REMINDER'
ORDER BY DU_NBO_CAMPAIGN.SCHEDULED_DAY DESC, DU_NBO_CAMPAIGN.SCHEDULED_HOUR DESC, DU_NBO_CAMPAIGN.NAME ASC;
I get this error when running above in Sql Developer:-
Error at Command Line:1 Column:0
Error report:
SQL Error: ORA-00918: column ambiguously defined
00918. 00000 - "column ambiguously defined"

The name 'devicetypes' is not in scope on the right side of 'equals'. Consider swapping the expressions on either side of 'equals'

I am trying to get data out of my db but i am getting the above mentioned error on this line. Please HELP!!!!
join specvalue in db.Types on devicespecifications.DeviceTypeFKID equals devicetypes.DeviceTypeID
I have Tried switching the equals but it doesn't work. Please Help
List<DeviceDetails> devicedetails = (
from devices in db.Device
join devicespecifications in db.DeviceSpecifications on devices.DeviceID equals devicespecifications.DeviceFKID
join devicetypes in db.Types on devices.DeviceTypeFKID equals devicetypes.DeviceTypeID
join specvalue in db.Types on devicespecifications.DeviceTypeFKID equals devicetypes.DeviceTypeID // This Line is giving me the above mentioned error
join devicehistories in db.DeviceHistory on devices.DeviceID equals devicehistories.DeviceFKID
join locations in db.Locations on devices.LocationFKID equals locations.LocationID
join ips in db.IP on devices.DeviceID equals ips.DeviceFKID
where devices.DeviceID == id
select new DeviceDetails()
{
DeviceID = devices.DeviceID,
DeviceName = devices.DeviceName,
EntryDate = devices.EntryDate,
AssignDate = devices.AssignDate,
DeviceStatus = devices.DeviceStatus.ToString(),
MACAddress = devices.MACAddress,
DateRepaired= devicehistories.DateRepaired,
Remarks= devicehistories.Remarks,
SpecificationType = devicespecifications.DeviceTypeFKID,
devicetypes.DeviceTypeID,
SpecificationValue = devicespecifications.SpecificationValue,
FamilyIP = ips.FamilyIP,
ChildIP = ips.ChildIP,
LocationTypeValue = locations.LocationTypeValue,
DeviceTypeValue = devicetypes.DeviceTypeValue
}).ToList<DeviceDetails>();
return devicedetails;
}
In the mentioned row:
join specvalue in db.Types on devicespecifications.DeviceTypeFKID equals devicetypes.DeviceTypeID
you use devicetypes name again but you should use specvalue in this line.

Query Optimization - Suggestions?

I am hoping someone can help me understand this XML Showplan for a long running query. This query comes from a Crystal Report developed by the software vendor that is very slow running, and I'm trying to convert it to an SSRS report while speeding the query up.
Here is a link to the Execution Plan - File hosted on File Dropper.
And here is the actual query itself:
Select Distinct
CD.desclong [Center],
CD.CenterType,
Sub.Description [SubCenter],
Org.Description [OrgCenter],
CO.CodeID [CenterC],
Sub.CodeID [SubCenterC],
Org.CodeID [OrgCenterC],
Reg.Description [Region],
Reg.CodeID [RegionC],
Rec.CodeID [RecID],
Rec.Description [RecName],
Rec.NickName [RecNickName],
Acct.AccountID [AcctCharCode],
DM.DriveID,
DM.ExternalID [DriveExtID],
DM.FromDateTime [DriveDate],
dbo.getShiftProductProjectionList(DSD.shiftid) [ProductProjections],
dbo.getShiftProcedureProjectionList(DSD.shiftid) [ProcedureProjections],
dbo.getShiftMobileList(DSD.shiftid, '1,2,3') [MobileSetups],
LD.LocationName [LocationName],
Case DM.OwnerType
When 1 Then FSM.DisplayName
When 0 Then Acct.InternalName
End [OwnerName], Acct.ExternalID [AcctExtID], SM.ExternalID [SiteExtID],
Stat.StatusText [DriveStatus],
DSD.ShiftID,
DSD.ShiftStart,
DSD.ShiftEnd,
DT.EarlyShiftStart,
DT.LateShiftEnd,
Changes.*,
dbo.getOriginalProcedureProjection(Changes.dsprocshiftid, Changes.dsprocprocedureid) [OriginalProjection],
(Select Top 1 DescMed From Production.[dbo].QuickCodes where CodeValue = 'ACTEXTIDTAG') [ActExtIDTag],
(Select Top 1 DescMed From Production.[dbo].QuickCodes where CodeValue = 'SITEEXTIDTAG') [SiteExtIDTag],
(Select Top 1 DescMed From Production.[dbo].QuickCodes where CodeValue = 'DRVEXTIDTAG') [DrvExtIDTag],
Case When (Changes.TableName = 'DriveShiftProcedureDetail') Then (Select Top 1 projectionid From driveshiftproceduredetail where uniquekey = Changes.owneruniquekey) else null end as ProjectionID
From
Production.[dbo].rpt_DriveMaster DM
Join Production.[dbo].rpt_DriveShiftDetail DSD on DSD.DriveID = DM.DriveID
Join Production.[dbo].CriticalDriveChanges Changes on (Changes.SourceDriveID = DM.DriveID and Changes.SourceShiftID = DSD.ShiftID and
Production.[dbo].returnDateTime(year(Changes.ChangeWhen),month(Changes.ChangeWhen),day(Changes.ChangeWhen)) between '01/04/2015' and '01/04/2015')
And
(
(
(Changes.TableName='DriveShiftProcedureDetail' and Changes.ColumnName='projection')
or
(Changes.TableName='DriveShiftDetail' and (Changes.ColumnName='ShiftStart' or Changes.ColumnName='ShiftEnd'))
or
(Changes.TableName='DriveShiftDetail' and Changes.ColumnName='StaffRequested')
or
(Changes.TableName='DriveShiftDetail' and Changes.changetype='Delete')
or
(Changes.TableName='DriveMaster' and Changes.ColumnName='StatusID' and Changes.NewValue='5')
or
(Changes.TableName='DriveMaster' and Changes.ColumnName='FromDateTime')
or
(Changes.TableName='DriveShiftRoleTimeDetail' and Changes.ColumnName='Lead')
or
(Changes.TableName='DriveShiftRoleTimeDetail' and Changes.ColumnName='TravelTo')
or
(Changes.TableName='DriveShiftRoleTimeDetail' and Changes.ColumnName='Setup')
or
(Changes.TableName='DriveShiftRoleTimeDetail' and Changes.ColumnName='Breakdown')
or
(Changes.TableName='DriveShiftRoleTimeDetail' and Changes.ColumnName='TravelFrom')
or
(Changes.TableName='DriveShiftRoleTimeDetail' and Changes.ColumnName='WrapUp')
)
and
(Changes.ChangeWho<>'RMADMIN')
)
Join Production.[dbo].rpt_DriveStatusDef Stat on Stat.StatusID = DM.StatusID
Join Production.[dbo].DriveTimes DT on DT.DriveID = DM.DriveID
Left Outer Join Production.[dbo].rpt_CenterDetail CD on CD.CenterID = DM.centerid
Left Outer Join Production.[dbo].IDViewRegion Reg on CD.Region = Reg.CodeID
Left Outer Join Production.[dbo].IDViewOrgCenter Org on CD.OrgCenter = Org.CodeID
Left Outer Join Production.[dbo].IDViewOrgSubCenter Sub on CD.OrgSubcenter = Sub.CodeID
Left Outer Join Production.[dbo].IDViewCollectionOp CO on cd.centerid = CO.CodeID
Left Outer Join Production.[dbo].IDViewRecruiter Rec on Rec.CodeID = DM.RecruiterID
Left Outer Join Production.[dbo].rpt_Accounts Acct on Acct.AccountID = DM.AccountID
Left Outer Join Production.[dbo].rpt_FixedSiteScheduleMaster FSM on FSM.DrawID = DM.DrawID
Left Outer Join Production.[dbo].rpt_LocationDetail LD on LD.LocationID = DM.LocationID
Join Production.[dbo].rpt_SiteMaster SM on SM.SiteID = DM.SiteID
Where
(dbo.returnDateTime(year(DM.Fromdatetime),month(DM.Fromdatetime),day(DM.Fromdatetime)) between '01/04/2015' and '01/04/2015')
or (dbo.returnDateTime(year(Changes.previousdrivedate),month(Changes.previousdrivedate),day(Changes.previousdrivedate)) between '01/04/2015' and '01/04/2015')
Here is the actual execution plan using Set Statistics XML On
Execution Plan
Thanks!
I removed the functions for returnDateTime and used the following instead:
-- In the FROM statement
Join Hemasphere_Dev.[dbo].CriticalDriveChanges Changes on (Changes.SourceDriveID = DM.DriveID And Changes.SourceShiftID = DSD.ShiftID And Changes.ChangeWhen Between '01/01/2015' And '01/15/2015')
--Hemasphere_Dev.[dbo].returnDateTime(year(Changes.ChangeWhen),month(Changes.ChangeWhen),day(Changes.ChangeWhen)) between '01/01/2015' And '01/15/2015')
-- In the WHERE statement
DM.FromDateTime Between '01/01/2015' And '01/15/2015'
Or Changes.PreviousDriveDate Between '01/01/2015' and '01/15/2015'
--(dbo.returnDateTime(year(DM.FromDateTime),month(DM.FromDateTime),day(DM.FromDateTime)) Between '01/01/2015' And '01/15/2015')
--Or (dbo.returnDateTime(year(Changes.previousdrivedate),month(Changes.previousdrivedate),day(Changes.previousdrivedate)) Between '01/01/2015' And '01/15/2015')
The query actually ran slower this way.

How can I make this big nasty query more efficient?

This is for a PostgreSQL database. I have a view where I am joining a table to itself 15 times to create what we call levels, from there I am doing a COALESCE function on the 'levels'- then a little manipulation on that field, as well. I am also pulling what is a description field for each of the 15 levels. This is where my query became sluggishly slow. I am joining the SETHEADERT table to the multiple levels to get the description field for each level. As you can see I only have 3 description fields and it is currently taking very long to run. When I had 2 it took a little bit but wasn't bad. I hope this makes sense. My code is below. Any help on how to make this more efficient is greatly appreciated.
SELECT
subset_cls,
prctr1,
CASE
WHEN prctr1 LIKE 'PC%' THEN split_part( overlay(prctr1 placing '00000' from 1 for 2 ),'.',1)
ELSE prctr1 end as pctrl2,
LVL01,
desc01,
LVL02,
desc02
FROM
( SELECT
SRC.SAP_SETNODE.SUBSET_CLS AS SUBSET_CLS,
SRC.SAP_SETHEADERT.DESCRIPTION AS desc01,
DESC_02.DESCRIPTION AS desc02,
DESC_03.DESCRIPTION AS desc03,
SRC.SAP_SETNODE.SET_NAME AS LVL01,
SRC.SAP_SETNODE.SUBSET_NAME AS LVL02,
SETNODE_1.SUBSET_NAME AS LVL03,
SETNODE_2.SUBSET_NAME AS LVL04,
SETNODE_3.SUBSET_NAME AS LVL05,
SETNODE_4.SUBSET_NAME AS LVL06,
SETNODE_5.SUBSET_NAME AS LVL07,
SETNODE_6.SUBSET_NAME AS LVL08,
SETNODE_7.SUBSET_NAME AS LVL09,
SETNODE_8.SUBSET_NAME AS LVL10,
SETNODE_9.SUBSET_NAME AS LVL11,
SETNODE_10.SUBSET_NAME AS LVL12,
SETNODE_11.SUBSET_NAME AS LVL13,
SETNODE_12.SUBSET_NAME AS LVL14,
SETNODE_13.SUBSET_NAME AS LVL15,
COALESCE(
SETNODE_13.SUBSET_NAME,
SETNODE_12.SUBSET_NAME,
SETNODE_11.SUBSET_NAME,
SETNODE_10.SUBSET_NAME,
SETNODE_9.SUBSET_NAME,
SETNODE_8.SUBSET_NAME,
SETNODE_7.SUBSET_NAME,
SETNODE_6.SUBSET_NAME,
SETNODE_5.SUBSET_NAME,
SETNODE_4.SUBSET_NAME,
SETNODE_3.SUBSET_NAME,
SETNODE_2.SUBSET_NAME,
SETNODE_1.SUBSET_NAME,
SRC.SAP_SETNODE.SUBSET_NAME,
SRC.SAP_SETNODE.SET_NAME)
AS prctr1
FROM SRC.SAP_SETNODE
LEFT JOIN SRC.SAP_SETHEADERT ON SRC.SAP_SETHEADERT.SET_NAME = SRC.SAP_SETNODE.SET_NAME
LEFT JOIN SRC.SAP_SETNODE AS SETNODE_1 ON SRC.SAP_SETNODE.SUBSET_NAME = SETNODE_1.SET_NAME
AND SRC.SAP_SETNODE.SUBSET_CLS = SETNODE_1.SUBSET_CLS
LEFT JOIN SRC.SAP_SETHEADERT as DESC_02 ON DESC_02.SET_NAME = SETNODE_1.SET_NAME
LEFT JOIN SRC.SAP_SETNODE AS SETNODE_2 ON SETNODE_1.SUBSET_NAME = SETNODE_2.SET_NAME
AND SETNODE_1.SUBSET_CLS = SETNODE_2.SUBSET_CLS
LEFT JOIN SRC.SAP_SETHEADERT as DESC_03 ON DESC_03.SET_NAME = SETNODE_2.SET_NAME
LEFT JOIN SRC.SAP_SETNODE AS SETNODE_3 ON SETNODE_2.SUBSET_NAME = SETNODE_3.SET_NAME
AND SETNODE_2.SUBSET_CLS = SETNODE_3.SUBSET_CLS
LEFT JOIN SRC.SAP_SETNODE AS SETNODE_4 ON SETNODE_3.SUBSET_NAME = SETNODE_4.SET_NAME
AND SETNODE_3.SUBSET_CLS = SETNODE_4.SUBSET_CLS
LEFT JOIN SRC.SAP_SETNODE AS SETNODE_5 ON SETNODE_4.SUBSET_NAME = SETNODE_5.SET_NAME
AND SETNODE_4.SUBSET_CLS = SETNODE_5.SUBSET_CLS
LEFT JOIN SRC.SAP_SETNODE AS SETNODE_6 ON SETNODE_5.SUBSET_NAME = SETNODE_6.SET_NAME
AND SETNODE_5.SUBSET_CLS = SETNODE_6.SUBSET_CLS
LEFT JOIN SRC.SAP_SETNODE AS SETNODE_7 ON SETNODE_6.SUBSET_NAME = SETNODE_7.SET_NAME
AND SETNODE_6.SUBSET_CLS = SETNODE_7.SUBSET_CLS
LEFT JOIN SRC.SAP_SETNODE AS SETNODE_8 ON SETNODE_7.SUBSET_NAME = SETNODE_8.SET_NAME
AND SETNODE_7.SUBSET_CLS = SETNODE_8.SUBSET_CLS
LEFT JOIN SRC.SAP_SETNODE AS SETNODE_9 ON SETNODE_8.SUBSET_NAME = SETNODE_9.SET_NAME
AND SETNODE_8.SUBSET_CLS = SETNODE_9.SUBSET_CLS
LEFT JOIN SRC.SAP_SETNODE AS SETNODE_10 ON SETNODE_9.SUBSET_NAME = SETNODE_10.SET_NAME
AND SETNODE_9.SUBSET_CLS = SETNODE_10.SUBSET_CLS
LEFT JOIN SRC.SAP_SETNODE AS SETNODE_11 ON SETNODE_10.SUBSET_NAME = SETNODE_11.SET_NAME
AND SETNODE_10.SUBSET_CLS = SETNODE_11.SUBSET_CLS
LEFT JOIN SRC.SAP_SETNODE AS SETNODE_12 ON SETNODE_11.SUBSET_NAME = SETNODE_12.SET_NAME
AND SETNODE_11.SUBSET_CLS = SETNODE_12.SUBSET_CLS
LEFT JOIN SRC.SAP_SETNODE AS SETNODE_13 ON SETNODE_12.SUBSET_NAME = SETNODE_13.SET_NAME
AND SETNODE_12.SUBSET_CLS = SETNODE_13.SUBSET_CLS
GROUP BY SRC.SAP_SETNODE.SUBSET_CLS, SRC.SAP_SETHEADERT.DESCRIPTION, DESC_02.DESCRIPTION,
DESC_03.DESCRIPTION, SRC.SAP_SETNODE.SET_NAME,
SRC.SAP_SETNODE.SUBSET_NAME, SETNODE_1.SUBSET_NAME, SETNODE_2.SUBSET_NAME,
SETNODE_3.SUBSET_NAME, SETNODE_4.SUBSET_NAME, SETNODE_5.SUBSET_NAME,
SETNODE_6.SUBSET_NAME, SETNODE_7.SUBSET_NAME, SETNODE_8.SUBSET_NAME,
SETNODE_9.SUBSET_NAME, SETNODE_10.SUBSET_NAME, SETNODE_11.SUBSET_NAME,
SETNODE_12.SUBSET_NAME, SETNODE_13.SUBSET_NAME
HAVING SRC.SAP_SETNODE.SUBSET_CLS='0101' AND SRC.SAP_SETNODE.SET_NAME='SISW.'
||get_fy_part('YEAR', clock_timestamp())
ORDER BY SRC.SAP_SETNODE.SET_NAME, SRC.SAP_SETNODE.SUBSET_NAME, SETNODE_1.SUBSET_NAME,
SETNODE_2.SUBSET_NAME, SETNODE_3.SUBSET_NAME, SETNODE_4.SUBSET_NAME, SETNODE_5.SUBSET_NAME,
SETNODE_6.SUBSET_NAME, SETNODE_7.SUBSET_NAME
) foo
If you move the filtering to the where clause there will be less rows to be joined. Change this:
group by ...
HAVING
SRC.SAP_SETNODE.SUBSET_CLS = '0101' AND
SRC.SAP_SETNODE.SET_NAME = 'SISW.' || get_fy_part('YEAR', clock_timestamp())
to
where
SRC.SAP_SETNODE.SUBSET_CLS = '0101' AND
SRC.SAP_SETNODE.SET_NAME = 'SISW.' || get_fy_part('YEAR', clock_timestamp())
group by ...

LINQ EF Join with added CROSS JOIN

This linq to ef syntax produces the sql syntax shown below. How can I get it to produce without the CROSS JOIN? The cross join is giving me a ton of extra records.
vehicleList = (from _vehicle in shireyContext.Vehicles
join _statusDescription in shireyContext.StatusDescriptions
on _vehicle.Status equals _statusDescription.StatusId
join _newOptions2 in shireyContext.VehicleOption_New
on _vehicle.StockNo equals _newOptions2.StockNo
where _vehicle.NewOrUsed == NewOrUsed && _vehicle.Model == Model && _newOptions2.Color != null
from _newOptions in shireyContext.VehicleOption_New
select new VehicleDomainEntity
{
StockNo = _vehicle.StockNo,
Year = _vehicle.VehicleYear,
Make = _vehicle.Make,
Model = _vehicle.Model,
Description = _newOptions2.Description,
ExteriorColor = _vehicle.ExteriorColor,
InteriorColor = _vehicle.InteriorColor,
InternetPrice = _vehicle.CodedCost,
ListPrice = _vehicle.ListPrice,
Status = _statusDescription.StatusDescriptionText,
NewOrUsed = _vehicle.NewOrUsed,
Mileage = _vehicle.Mileage,
VIN = _vehicle.VIN
}).ToList();
produces this sql:
SELECT
Extent2.StatusId AS StatusId,
Extent1.StockNo AS StockNo,
Extent1.VehicleYear AS VehicleYear,
Extent1.Make AS Make,
Extent1.Model AS Model,
Extent3.Description AS Description,
Extent1.ExteriorColor AS ExteriorColor,
Extent1.InteriorColor AS InteriorColor,
Extent1.CodedCost AS CodedCost,
Extent1.ListPrice AS ListPrice,
Extent2.StatusDescriptionText AS StatusDescriptionText,
Extent1.NewOrUsed AS NewOrUsed,
Extent1.Mileage AS Mileage,
Extent1.VIN AS VIN
FROM dbo.Vehicles AS Extent1
INNER JOIN dbo.StatusDescription AS Extent2 ON Extent1.Status = Extent2.StatusId
INNER JOIN dbo.VehicleOption_New AS Extent3 ON Extent1.StockNo = Extent3.StockNo
CROSS JOIN dbo.VehicleOption_New AS Extent4
WHERE (Extent1.NewOrUsed = 'N') AND (Extent1.Model = 'cts' AND (Extent3.Color IS NOT NULL))
I think you want this
from _vehicle in shireyContext.Vehicles
join _statusDescription in shireyContext.StatusDescriptions
on _vehicle.Status equals _statusDescription.StatusId
join _newOptions2 in shireyContext.VehicleOption_New into VehicleNew
on _vehicle.StockNo equals _newOptions2.StockNo
where _vehicle.NewOrUsed == NewOrUsed && _vehicle.Model == Model && _newOptions2.Color != null
from _newOptions in VehicleNew
The two lines that are changed are:
join _newOptions2 in shireyContext.VehicleOption_New into VehicleNew
and
from _newOptions in VehicleNew

Resources