Oracle Not exists - oracle

Hi All I have written a query that returns some results based on completions of classes. I'm having trouble writing an exception report based on the same classes. I think I need to use a "NOT EXISTS" subquery but am not sure how.
Here is the completions query
select papf.employee_number
, papf.full_name
, haou.name
, oe.title
, oe.course_end_date completed_date
, oav.version_name
from per_all_people_f papf
, per_all_assignments_f paaf
, hr_all_organization_units haou
, ota_events oe
, ota_delegate_bookings odb
, ota_activity_versions oav
where papf.person_id = paaf.person_id
and paaf.organization_id = haou.organization_id
and odb.event_id = oe.event_id
and odb.booking_status_type_id in (1006, 1004)
and odb.delegate_person_id = paaf.person_id
and oe.activity_version_id in (3, 1001, 2, 8, 2001)
and oe.activity_version_id = oav.activity_version_id
and haou.organization_id = 4729
and trunc(sysdate) between papf.effective_start_date and papf.effective_end_date
and trunc(sysdate) between paaf.effective_start_date and paaf.effective_end_date
order by papf.employee_number;
I want to return results for papf.employee_number, papf.full_name,and oav.version_name for employees who have not completed these courses.
So oe.activity_version_id not exists(3, 1001, 2, 8, 2001)
I'm kind of confused as to where I should do this?
Thanks

Try:
select papf.employee_number
, papf.full_name
, haou.name
, oe.title
, oe.course_end_date completed_date
, oav.version_name
from per_all_people_f papf
, per_all_assignments_f paaf
, hr_all_organization_units haou
, ota_events oe
, ota_delegate_bookings odb
, ota_activity_versions oav
where papf.person_id = paaf.person_id
and paaf.organization_id = haou.organization_id
and odb.event_id = oe.event_id
and odb.booking_status_type_id in (1006, 1004)
and odb.delegate_person_id = paaf.person_id
/* and oe.activity_version_id in (3, 1001, 2, 8, 2001) */
and NOT EXISTS(
SELECT null FROM ota_events oe1
WHERE oe1.activity_version_id = oav.activity_version_id
AND odb.event_id = oe1.event_id
AND oe1.activity_version_id in (3, 1001, 2, 8, 2001)
)
and oe.activity_version_id = oav.activity_version_id
and haou.organization_id = 4729
and trunc(sysdate) between papf.effective_start_date and papf.effective_end_date
and trunc(sysdate) between paaf.effective_start_date and paaf.effective_end_date
order by papf.employee_number;

Based on what you've mentioned (and as user symcbean pointed out, to avoid contradicting the logic in the initial SQL), you may need a separate query as shown below to get your results. Try using NOT IN.
select papf.employee_number
, papf.full_name
, haou.name
, oe.title
, oe.course_end_date completed_date
, oav.version_name
from per_all_people_f papf
, per_all_assignments_f paaf
, hr_all_organization_units haou
, ota_events oe
, ota_delegate_bookings odb
, ota_activity_versions oav
where papf.person_id = paaf.person_id
and paaf.organization_id = haou.organization_id
and odb.event_id = oe.event_id
and odb.booking_status_type_id in (1006, 1004)
and odb.delegate_person_id = paaf.person_id
and oe.activity_version_id NOT IN (3, 1001, 2, 8, 2001)
and oe.activity_version_id = oav.activity_version_id
and haou.organization_id = 4729
and trunc(sysdate) between papf.effective_start_date and papf.effective_end_date
and trunc(sysdate) between paaf.effective_start_date and paaf.effective_end_date
order by papf.employee_number;
HTH

Related

Using Cumulative and Percent Rank functions in my query

I'm working on this query and would like to use the cumulative distribution or percent rank function to provide me with cumulative distribution and the percent rank for field 'leads distribution'. however, it appears that when the decile value is populated it doesn't give me the right number for that decile for example the LeadGrouping record 'FLJeffersonCollateral' should have a decile of 1 but is showing a decile of 2. Can you help what am I doing wrong? Below is the query.
Select c.LeadGrouping
, c.StateID
, c.County
, c.Media_Type
, Sum(c.Leads) as 'Leads'
, Sum(isnull(c.CCPEnroll,0)) as 'CCPEnroll'
, c.CloseRate
--, row_number() over(partition by c.Field1 order by c.CloseRate desc) as line_number
, sum(mx.MaxLeads) as 'MaxLeads' --ALTER TO MAX LEADS OVERALL --sum(mx.MaxLeads)
, CONVERT(DECIMAL(10,10), isnull(Sum(c.leads),0) / convert(numeric, isnull(sum(mx.MaxLeads),0))) as 'LeadDistribution' --CONFIRM TOTAL SUMS TO 100%
, PERCENT_RANK () over (order by Convert (Decimal(10,10), (isnull(Sum(c.leads),0) / convert(numeric, isnull(sum(mx.MaxLeads),0))))) as 'percent rank'
From (Select 'A' as 'Field1'
, b.LeadGrouping
, b.StateID
, b.County
, b.Media_Type
, Sum(b.Leads) as 'Leads'
, Sum(b.CCPEnroll) as 'CCPEnroll'
, CONVERT(DECIMAL(5,2), isnull(Sum(b.CCPEnroll),0) / convert(numeric, isnull(Sum(b.Leads),0))) as 'CloseRate'
From (Select a.LeadGrouping
, a.StateID
, a.County
, a.Media_Type
, Sum(a.Lead) as 'Leads'
, Sum(a.CCPEnroll) as 'CCPEnroll'
From (Select distinct al.LeadID
, al.Interaction_ID
, al.createddate
, al.appointmentdate
, al.C2CExp
, Case When al.EnrollMonth is not null Then DateDiff(day, al.createddate, al.AppDate)
Else DateDiff(day, al.createddate, GETDATE()) End as 'LeadTenureInDays'
, Count(distinct i.interaction_id) as 'LeadInteractionCount'
, l.lead_interactionsource__c
, al.MarketID
, left(al.state_county_key,2) as 'StateID'
, al.County
, al.Media_Type
, al.SegmentName
, al.Correctcampaign
, c.name as 'CampaignName'
, c.lead_program__c
, l.lead_disposition__c
, al.CCPEnroll
, al.PDPEnroll
, al.AppDate
, al.EnrollMonth
, al.writingagentid
, p.ProducerType
, al.Lead
, case When f.fipscode is null Then 'Not In CCP Footprint'
When f.FIPSCode is not null Then 'In CCP Footprint'
else 'ERROR' End as 'Footprint'
, CONCAT(left(al.state_county_key,2),al.county, al.Media_Type) as 'LeadGrouping'
From rknow.dbo.allleadstemp2 al
Left Join rknow.dbo.campaign_sfdc_datalake c on al.correctcampaign = c.id
Left Join rknow.dbo.lead_sfdc_datalake l on al.leadid = l.id
Left Join rknow.dbo.producerstructure_datalake p on al.writingagentid = p.producerid and al.createddate >= p.effectivestartdate and al.createddate <= DateAdd(day, 1, p.effectiveenddate)
Left Join [RKnow].[rpt].[interaction] i on al.leadid = i.lead_id
Inner Join rknow.dbo.fipsspc f on al.fips = f.fipscode --2022 CCP County Level Footprint
Where al.C2CExp >= DateAdd(day,1,getdate())
and al.lead_type = 'ccp' --HD 12/04/2020: Added criteria filter to exclude CrossSell leads
Group by al.LeadID
, al.Interaction_ID
, al.C2CExp
, al.createddate
, al.MarketID
, al.state_county_key
, al.County
, al.Media_Type
, al.SegmentName
, al.Correctcampaign
, al.appointmentdate
, c.name
, c.lead_program__c
, l.lead_disposition__c
, al.CCPEnroll
, al.PDPEnroll
, al.EnrollMonth
, al.writingagentid
, p.ProducerType
, l.lead_interactionsource__c
, al.AppDate
, f.fipscode
, al.Lead) a
Group by a.LeadGrouping
, a.Media_Type
, a.StateID
, a.County) b
Group by b.LeadGrouping
, b.StateID
, b.County
, b.Media_Type) c
Left Join (Select distinct 'A' as 'Field1', sum(mx.lead) as 'MaxLeads'
From(Select distinct al.LeadID
, al.Interaction_ID
, al.createddate
, al.appointmentdate
, al.C2CExp
, Case When al.EnrollMonth is not null Then DateDiff(day, al.createddate, al.AppDate)
Else DateDiff(day, al.createddate, GETDATE()) End as 'LeadTenureInDays'
, Count(distinct i.interaction_id) as 'LeadInteractionCount'
, l.lead_interactionsource__c
, al.MarketID
, left(al.state_county_key,2) as 'StateID'
, al.County
, al.Media_Type
, al.SegmentName
, al.Correctcampaign
, c.name as 'CampaignName'
, c.lead_program__c
, l.lead_disposition__c
, al.CCPEnroll
, al.PDPEnroll
, al.AppDate
, al.EnrollMonth
, al.writingagentid
, p.ProducerType
, al.Lead
, case When f.fipscode is null Then 'Not In CCP Footprint'
When f.FIPSCode is not null Then 'In CCP Footprint'
else 'ERROR' End as 'Footprint'
, CONCAT(left(al.state_county_key,2),al.county, al.Media_Type) as 'LeadGrouping'
From rknow.dbo.allleadstemp2 al
Left Join rknow.dbo.campaign_sfdc_datalake c on al.correctcampaign = c.id
Left Join rknow.dbo.lead_sfdc_datalake l on al.leadid = l.id
Left Join rknow.dbo.producerstructure_datalake p on al.writingagentid = p.producerid and al.createddate >= p.effectivestartdate and al.createddate <= DateAdd(day, 1, p.effectiveenddate)
Left Join [RKnow].[rpt].[interaction] i on al.leadid = i.lead_id
Inner Join rknow.dbo.fipsspc f on al.fips = f.fipscode --2022 CCP County Level Footprint
Where al.C2CExp >= DateAdd(day,1,getdate())
and al.lead_type = 'ccp' --HD 12/04/2020: Added criteria filter to exclude CrossSell leads
Group by al.LeadID
, al.Interaction_ID
, al.C2CExp
, al.createddate
, al.MarketID
, al.state_county_key
, al.County
, al.Media_Type
, al.SegmentName
, al.Correctcampaign
, al.appointmentdate
, c.name
, c.lead_program__c
, l.lead_disposition__c
, al.CCPEnroll
, al.PDPEnroll
, al.EnrollMonth
, al.writingagentid
, p.ProducerType
, l.lead_interactionsource__c
, al.AppDate
, f.fipscode
, al.Lead) mx) mx on mx.Field1 = c.Field1
Group by c.LeadGrouping
, c.StateID
, c.County
, c.Media_Type
, c.CloseRate
, c.Field1

ORA-01427 Single-Row Subquery Returns More than One Row - how to resolve?

I've had this query running for over 2 years and just recently this began throwing the ORA-01427 single row sub-query returns multiple rows error. What's the best way to debug this when there are multiple sub-queries? Should I be adding a MAX statement to each subquery? I've tried switching some of the = operators to IN but that's not working and I'm not confident it would give me the correct results either.
Appreciate any insight or assistance this community can provide. I'm ripping my hair out working through this one!
SELECT
NVL(
(SELECT
'Y'
FROM
PER_ASSIGNMENT_SUPERVISORS_F
WHERE
MANAGER_ID = PAPF.PERSON_ID
AND MANAGER_TYPE ='LINE_MANAGER'
AND TRUNC(SYSDATE) BETWEEN EFFECTIVE_START_DATE AND EFFECTIVE_END_DATE
AND ROWNUM = 1),'N') MANAGER_FLAG,
PAAM.ASSIGNMENT_STATUS_TYPE,
PAAM.EMPLOYMENT_CATEGORY,
TO_CHAR(PPOS.DATE_START,'YYYY-MM-DD') AS HIRE_DATE,
PER_EXTRACT_UTILITY.GET_STANDARD_WORKING_HOURS(PAAM.ASSIGNMENT_ID,TRUNC(SYSDATE)) AS STANDARD_WORKING_HOURS,
TO_CHAR(PAAM.EFFECTIVE_START_DATE,'YYYY-MM-DD') AS EFFECTIVE_START_DATE,
(
SELECT
PJFVL.JOB_CODE
FROM
PER_ALL_ASSIGNMENTS_M PAAM1,
PER_JOBS_F_VL PJFVL
WHERE
PAAM1.PERSON_ID = PASF.MANAGER_ID
AND TRUNC(SYSDATE) BETWEEN PAAM1.EFFECTIVE_START_DATE AND PAAM1.EFFECTIVE_END_DATE
AND TRUNC(SYSDATE) BETWEEN PJFVL.EFFECTIVE_START_DATE AND PJFVL.EFFECTIVE_END_DATE
and PAAM1.ASSIGNMENT_STATUS_TYPE='ACTIVE'
AND PAAM1.ASSIGNMENT_TYPE = 'E'
AND PAAM1.effective_latest_change = 'Y'
AND PJFVL.JOB_ID = PAAM1.JOB_ID
)as Manager_job_code,
(
SELECT
HOIF.ORG_INFORMATION1
FROM
PER_ALL_ASSIGNMENTS_M PAAM2,
HR_ORGANIZATION_INFORMATION_F HOIF
WHERE
PAAM2.PERSON_ID = PASF.MANAGER_ID
AND HOIF.ORG_INFORMATION_CONTEXT = 'DEPT_DET'
and PAAM2.ASSIGNMENT_STATUS_TYPE='ACTIVE'
AND PAAM2.ASSIGNMENT_TYPE = 'E'
AND PAAM2.effective_latest_change = 'Y'
AND TRUNC(SYSDATE) BETWEEN PAAM2.EFFECTIVE_START_DATE AND PAAM2.EFFECTIVE_END_DATE
AND TRUNC(SYSDATE) BETWEEN HOIF.EFFECTIVE_START_DATE AND HOIF.EFFECTIVE_END_DATE
AND HOIF.ORGANIZATION_ID = PAAM2.ORGANIZATION_ID
) as Manager_dep_code,
(SELECT
COUNT(PPOS.PERIOD_OF_SERVICE_ID)
FROM
PER_PERIODS_OF_SERVICE PPOS
WHERE
1 = 1
AND PAAM.PERSON_ID = PPOS.PERSON_ID
) AS INACTIVE_WORKRELATIONSHIP,
PAPF.PERSON_NUMBER as SAMACCOUNTNAME,
(SELECT CSB.NAME FROM
CMP_SALARY CS,
CMP_SALARY_BASES CSB
WHERE
CS.ASSIGNMENT_ID = PAAM.ASSIGNMENT_ID
AND CS.SALARY_BASIS_ID = CSB.SALARY_BASIS_ID
and TRUNC(SYSDATE) BETWEEN CS.DATE_FROM AND CS.DATE_TO
) AS hourly_salary_Paid,
TO_CHAR(PP.DATE_OF_BIRTH,'YYYY-MM-DD') AS DOB,
PPNFV.LAST_NAME,
PPNFV.FIRST_NAME,
HLA.LOCATION_NAME as Location,
HLA.ADDRESS_LINE_1 AS LOC_ADDRESS_1,
HLA.ADDRESS_LINE_2 AS LOC_ADDRESS_2,
HLA.TOWN_OR_CITY AS City,
HLA.POSTAL_CODE AS ZIP_CODE,
HLA.REGION_2 AS STATE,
PPNFV.KNOWN_AS AS PREFERRED_NAME,
TRIM((PPNFV.KNOWN_AS||' '||PPNFV.LAST_NAME)) AS PREFERRED_NAME_LAST_NAME,
(SELECT
PPNFV.DISPLAY_NAME
FROM per_person_names_f_v PPNFV
WHERE 1 = 1
AND PPNFV.PERSON_ID = PASF.MANAGER_ID
AND PPNFV.NAME_TYPE='GLOBAL'
AND TRUNC(SYSDATE) BETWEEN PPNFV.EFFECTIVE_START_DATE AND PPNFV.EFFECTIVE_END_DATE
) AS MANAGER_NAME,
(SELECT
PAPF.PERSON_NUMBER
FROM PER_ALL_PEOPLE_F PAPF
WHERE PAPF.PERSON_ID = PASF.MANAGER_ID
AND TRUNC(SYSDATE) BETWEEN PAPF.EFFECTIVE_START_DATE AND PAPF.EFFECTIVE_END_DATE
) AS MANAGER_NUMBER,
HAOUFVL.NAME AS DEPARTMENT,
PAAM.ASSIGNMENT_NAME AS JOB_TILE,
PLE.NAME as Company,
PJLG.INFORMATION1 AS FLSA,
SUBSTR(PNI.NATIONAL_IDENTIFIER_NUMBER,-4) AS SSN_NUMBER,
PAAM.ASS_ATTRIBUTE1 as Officer_TITLE,
(
select
bu.name
from
hr_all_organization_units_f_vl bu
where 1 = 1
and paam.business_unit_id = bu.organization_id
and trunc(sysdate) between bu.effective_start_date and bu.effective_end_date
) as BUS_UNIT,
TO_CHAR (PPOS.ORIGINAL_DATE_OF_HIRE,'YYYY-MM-DD') AS ORIGINAL_DATE_OF_HIRE1,
(
CASE WHEN PPLF.SEX = 'F' THEN
'TRUE'
ELSE
'FALSE'
END
) AS GENDER,
(
CASE WHEN PJFFVL.JOB_FAMILY_NAME = 'Executive' THEN
'ELT'
ELSE
' '
END
) AS ELT_DESIGNATION,
HOIF.ORG_INFORMATION1 as DEPATMENT_CODE,
PJFV.JOB_CODE AS JOB_CODE,
PAF.ADDRESS_LINE_1 AS HOME_ADDRESS_LINE_1,
PAF.ADDRESS_LINE_2 AS HOME_ADDRESS_LINE_2,
PAF.TOWN_OR_CITY AS HOW_ADDRESS_CITY,
PAF.REGION_2 AS HOME_ADDRESS_STATE,
PAF.POSTAL_CODE AS HOME_ADRESS_ZIP_CODE,
PGFTL.NAME as Grade_level,
(SELECT
distinct (per_extract_utility.get_decoded_lookup('JOB_FUNCTION_CODE',PJF.JOB_FUNCTION_CODE))
FROM
PER_JOB_SECURED_LIST_V job WHERE TRUNC(SYSDATE) BETWEEN effective_start_date AND effective_end_date
) as JOB_FUNCTION,
pp.attribute1 as PER_NETWORKID,
(SELECT
PPNFV.attribute1
FROM PER_PERSONS PPNFV
WHERE 1 = 1
AND PPNFV.PERSON_ID = PASF.MANAGER_ID
) AS MANAGER_NETWORKID,
HOIF.ORG_INFORMATION2 AS REGION,
to_char(paam.ass_attribute_date1,'MM/DD/YYYY') as OfficerPromoDate
FROM
PER_ALL_ASSIGNMENTS_M PAAM,
PER_ASSIGNMENT_SUPERVISORS_F PASF,
PER_PERIODS_OF_SERVICE PPOS,
PER_ALL_PEOPLE_F PAPF,
PER_PERSONS PP,
per_person_names_f_v PPNFV,
HR_LOCATIONS_ALL HLA,
HR_ALL_ORGANIZATION_UNITS_F_VL HAOUFVL,
PER_JOB_LEG_F PJLG,
PER_NATIONAL_IDENTIFIERS PNI,
PER_PEOPLE_LEGISLATIVE_F PPLF,
PER_JOB_FAMILY_F_VL PJFFVL,
PER_JOBS_F_V PJFV,
HR_ORGANIZATION_INFORMATION_F HOIF,
PER_ADDRESSES_F PAF,
PER_PERSON_ADDR_USAGES_F PPAUF,
PER_GRADES_F_TL PGFTL,
PER_JOBS_F PJF,
PER_LEGAL_EMPLOYERS PLE,
HR_ALL_ORGANIZATION_UNITS_F HAOUF
WHERE
PAAM.ASSIGNMENT_STATUS_TYPE='ACTIVE'
AND PAAM.ASSIGNMENT_TYPE = 'E'
AND PAAM.effective_latest_change = 'Y'
AND PAAM.ASSIGNMENT_ID = PASF.ASSIGNMENT_ID(+)
AND PASF.MANAGER_TYPE (+) = 'LINE_MANAGER'
AND PNI.PERSON_ID(+) = PAAM.PERSON_ID
AND PAAM.PERSON_ID = PPLF.PERSON_ID(+)
and PJFV.JOB_FAMILY_ID= PJFFVL.JOB_FAMILY_ID(+)
AND PJFV.JOB_ID(+) = PAAM.JOB_ID
AND HOIF.ORGANIZATION_ID(+)= PAAM.ORGANIZATION_ID
AND HOIF.ORG_INFORMATION_CONTEXT(+) = 'DEPT_DETAILS'
AND PPAUF.PERSON_ID(+)= PAPF.PERSON_ID
AND PPAUF.ADDRESS_TYPE (+) = 'HOME'
AND PPAUF.ADDRESS_ID= PAF.ADDRESS_ID(+)
AND PGFTL.GRADE_ID(+)= PAAM.GRADE_ID
AND PJF.JOB_ID(+)= PAAM.JOB_ID
AND PJLG.INFORMATION_CATEGORY='HRX_US_JOBS'
and HAOUF.ORGANIZATION_ID(+)=PLE.ORGANIZATION_ID
and PAAM.LEGAL_ENTITY_ID=PLE.ORGANIZATION_ID
AND TRUNC(SYSDATE) BETWEEN PAAM.EFFECTIVE_START_DATE(+) AND PAAM.EFFECTIVE_END_DATE (+)
AND PAAM.PERIOD_OF_SERVICE_ID = PPOS.PERIOD_OF_SERVICE_ID
AND PAPF.PERSON_ID = PAAM.PERSON_ID
AND PP.PERSON_ID = PAAM.PERSON_ID
AND PPNFV.PERSON_ID = PAAM.PERSON_ID
AND HLA.LOCATION_ID(+) = PAAM.LOCATION_ID
AND PJLG.JOB_ID(+) = PAAM.JOB_ID
AND HAOUFVL.ORGANIZATION_ID(+) = PAAM.ORGANIZATION_ID
AND PPNFV.NAME_TYPE = 'GLOBAL'
AND TRUNC(SYSDATE) BETWEEN PPNFV.EFFECTIVE_START_DATE AND PPNFV.EFFECTIVE_END_DATE
AND TRUNC(SYSDATE) BETWEEN PAPF.EFFECTIVE_START_DATE AND PAPF.EFFECTIVE_END_DATE
AND TRUNC(SYSDATE) BETWEEN HAOUFVL.EFFECTIVE_START_DATE(+) AND HAOUFVL.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PPLF.EFFECTIVE_START_DATE(+) AND PPLF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PJFFVL.EFFECTIVE_START_DATE(+) AND PJFFVL.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PJFV.EFFECTIVE_START_DATE(+) AND PJFV.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN HOIF.EFFECTIVE_START_DATE(+) AND HOIF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PAF.EFFECTIVE_START_DATE(+) AND PAF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PPAUF.EFFECTIVE_START_DATE(+) AND PPAUF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PGFTL.EFFECTIVE_START_DATE(+) AND PGFTL.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PJF.EFFECTIVE_START_DATE(+) AND PJF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PLE.EFFECTIVE_START_DATE AND PLE.EFFECTIVE_END_DATE
AND TRUNC(SYSDATE) BETWEEN HAOUF.EFFECTIVE_START_DATE(+) AND HAOUF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN HLA.EFFECTIVE_START_DATE(+) AND HLA.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PJLG.EFFECTIVE_START_DATE(+) AND PJLG.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PASF.EFFECTIVE_START_DATE(+) AND PASF.EFFECTIVE_END_DATE(+)
ORDER BY PAPF.PERSON_NUMBER
Run the query in a SQL client that will generate a more detailed error message. For example, the below code is run in SQL*Plus, which correctly indicates that the problem is with the subquery on line 3.
SQL> select
2 (select 1 from dual) this_will_work,
3 (select 1 from dba_objects) this_will_fail,
4 (select 1 from dual) this_will_work
5 from dual;
(select 1 from dba_objects) this_will_fail,
*
ERROR at line 3:
ORA-01427: single-row subquery returns more than one row

Business phone number and mobile phone number are two fields getting same data why?

(select pp.PHONE_NUMBER from per_phones pp where pp.person_id = papf.person_id
and pp.phone_type= 'W1' and rownum<2
AND TRUNC(SYSDATE) BETWEEN pp.DATE_FROM(+) AND nvl(pp.DATE_TO(+),trunc(sysdate))) Per_BUS_PH,
(select pp.PHONE_NUMBER from per_phones pp where pp.person_id = papf.person_id
and pp.phone_type= 'WM' and rownum<2
AND TRUNC(SYSDATE) BETWEEN pp.DATE_FROM(+) AND nvl(pp.DATE_TO(+),trunc(sysdate))) Per_MBL_BUS,
(select pp.PHONE_NUMBER from per_phones pp where pp.person_id = papf.person_id
and pp.phone_type= 'HM' and rownum<2
AND TRUNC(SYSDATE) BETWEEN pp.DATE_FROM(+) AND nvl(pp.DATE_TO(+),trunc(sysdate))) Per_MBL_HME ,
(select pp.PHONE_NUMBER from per_phones pp where pp.person_id = papf.person_id
and pp.phone_type= 'H1' and rownum<2
AND TRUNC(SYSDATE) BETWEEN pp.DATE_FROM(+) AND nvl(pp.DATE_TO(+),trunc(sysdate))) Per_HME_PH from
per_persons pp,
per_all_people_f papf;
Here why I am getting business phone number and mobile business number same.?
It should be different and how can i get different phone number for both field which added I added in Oracle BI report..?

I want to add a subquery with a date parameter to return a calculated value but my query is not working

I have a query that fetches data from oracle inventory and purchasing. Now I want to add a subquery from po_line_locations_all with a date parameter to return a calculated value as if the value lies in the subquery show it else display zero, But in my case the query shows nothing if subquery returns null.
I want to show the result of the main query even if subquery returns null.
SELECT distinct msib.segment1 Item_Code,
MSIB.inventory_item_id,
MSIB.organization_id ORG_ID,
msib.description Item_Description,
msib.primary_unit_of_measure UOM,
ph.attribute1 Item_Type,
SUM(plla.quantity) - SUM(plla.quantity_received) On_Order,
ph.currency_code currency,
max(pl.unit_price) FOB_Value_in_FCY,
max(ph.rate_date),
max(ph.rate) forex_rate,
(
SELECT SUM (moq.transaction_quantity)
FROM mtl_system_items_b msi, mtl_onhand_quantities moq
WHERE moq.organization_id(+) = msi.organization_id
AND moq.inventory_item_id(+) = msi.inventory_item_id
and moq.ORGANIZATION_ID =MSIB.organization_id
and msi.inventory_item_id = MSIB.inventory_item_id
GROUP BY msi.segment1, msi.organization_id
) CURR_STOCK,
(
SELECT NVL(ABS(sum(mtmt.transaction_quantity)),0) from MTL_MATERIAL_TRANSACTIONS mtmt
WHERE 1=1
AND mtmt.inventory_item_id = MSIB.inventory_item_id --4018
AND mtmt.organization_id = MSIB.organization_id--499
and mtmt.TRANSACTION_ACTION_ID NOT IN (24, 30)
AND to_date(mtmt.transaction_date) >= to_date(&Roll_back_date,'DD-MON-RRRR')
)RB_TRANSACTIONS,
(
select ABS(SUM(mmt.transaction_quantity))
from MTL_MATERIAL_TRANSACTIONS mmt
where mmt.TRANSACTION_ACTION_ID NOT IN (24, 30)
and (mmt.ORGANIZATION_ID = MSIB.organization_id --499--579
)
and (mmt.INVENTORY_ITEM_ID = MSIB.inventory_item_id --4128 --4165
and mmt.TRANSACTION_TYPE_ID in (33, 52)
)
and (mmt.transaction_date between
to_date(add_months(&CONS_f_DATE, -12),'DD-MON-YYYY')
AND to_date(&CONS_f_DATE, 'DD-MON-YYYY')
)
AND (mmt.parent_transaction_id IS NULL)
) annual_Consumption,
(
select ABS(SUM(mmt.transaction_quantity) / 4)
FROM MTL_MATERIAL_TRANSACTIONS mmt
WHERE mmt.TRANSACTION_ACTION_ID NOT IN (24, 30)
and (mmt.ORGANIZATION_ID = MSIB.organization_id --499--579
)
and (mmt.INVENTORY_ITEM_ID = MSIB.inventory_item_id --4128 --4165
AND mmt.TRANSACTION_TYPE_ID in (33, 52)
)
and (mmt.transaction_date between
to_date(add_months(&CONS_f_DATE, -12),
'DD-MON-YYYY') AND
to_date(&CONS_f_DATE, 'DD-MON-YYYY'))
AND (mmt.parent_transaction_id IS NULL)
) months_Consumption,
(
select ABS((SUM(mmt.transaction_quantity) / 4) / 3)
FROM MTL_MATERIAL_TRANSACTIONS mmt
WHERE mmt.TRANSACTION_ACTION_ID NOT IN (24, 30)
and (mmt.ORGANIZATION_ID = MSIB.organization_id --499--579
)
and (mmt.INVENTORY_ITEM_ID = MSIB.inventory_item_id --4128 --4165
AND mmt.TRANSACTION_TYPE_ID in (33, 52))
and (mmt.transaction_date between
to_date(add_months(&CONS_f_DATE, -12),
'DD-MON-YYYY') AND
to_date(&CONS_f_DATE, 'DD-MON-YYYY'))
AND (mmt.parent_transaction_id IS NULL)
) monthly_Average,
(
select MATERIAL_COST
FROM CST_ITEM_COST_TYPE_V vw
WHERE vw.organization_id = MSIB.organization_id
AND - 1 = -1
and (vw.INVENTORY_ITEM_ID = MSIB.inventory_item_id)
) Unit_Cost, --new
sum(quan.t_quantity) - sum(r_quantity) finala
FROM mtl_system_items_b MSIB,
PO_HEADERS_ALL ph,
Po_Lines_All pl,
PO_LINE_LOCATIONS_ALL PLLA,
-------------------SUBQUERY---------------------------------------
(select nvl(sum(subplla.quantity),0) t_quantity, nvl(sum(subplla.quantity_received),0) r_quantity ,subpl.item_id
from po_headers_all subph,
po_lines_all subpl,
po_line_locations_all subplla
where subph.po_header_id = subpl.po_header_id
and subplla.po_header_id = subph.po_header_id
and subpl.po_line_id = subplla.po_line_id
and subplla.org_id = subpl.org_id
and to_date(subplla.creation_date) >= to_date(&Roll_back_date,'DD-MON-RRRR')
group by subph.attribute1, subph.currency_code, subpl.item_id
) quan
-------------------SUBQUERY---------------------------------------
WHERE 1=1
and ph.po_header_id = pl.po_header_id
and msib.inventory_item_id (+) = pl.item_id
and pl.item_id (+) = quan.item_id
and plla.po_header_id = ph.po_header_id
and pl.po_line_id = plla.po_line_id
and plla.org_id = pl.org_id
and msib.organization_id in
(select haou.organization_id
from hr_organization_information hoi,
hr_all_organization_units haou
where haou.organization_id = hoi.organization_id
and hoi.org_information1 = 'INV'
and hoi.org_information2 = 'Y'
and haou.name like '%HEIS%')
and MSIB.Inventory_Item_Id=NVL(&ITEM,MSIB.Inventory_Item_Id)
and MSIB.organization_id = nvl(&P_ORGI, MSIB.organization_id)
AND to_date(plla.creation_date) BETWEEN
to_date(add_months(&Roll_back_date, -12),'DD-MON-YYYY') AND
to_date(&Roll_back_date,'DD-MON-YYYY')
GROUP BY msib.segment1,
MSIB.inventory_item_id,
msib.description,
MSIB.organization_id,
msib.primary_unit_of_measure,
ph.attribute1,
ph.currency_code
My guess is that your problem is simply using old-syntax joins instead of something that has been around for a really long time.
SELECT DISTINCT msib.segment1 Item_Code,
MSIB.inventory_item_id,
MSIB.organization_id ORG_ID,
msib.description Item_Description,
msib.primary_unit_of_measure UOM,
ph.attribute1 Item_Type,
SUM(plla.quantity) - SUM(plla.quantity_received) On_Order,
ph.currency_code currency,
max(pl.unit_price) FOB_Value_in_FCY,
max(ph.rate_date),
max(ph.rate) forex_rate,
(
SELECT SUM(moq.transaction_quantity)
FROM mtl_system_items_b msi
RIGHT JOIN mtl_onhand_quantities moq ON moq.organization_id = msi.organization_id
AND moq.inventory_item_id = msi.inventory_item_id
WHERE moq.ORGANIZATION_ID = MSIB.organization_id
AND msi.inventory_item_id = MSIB.inventory_item_id
GROUP BY msi.segment1,
msi.organization_id
) CURR_STOCK,
(
SELECT NVL(ABS(sum(mtmt.transaction_quantity)), 0)
FROM MTL_MATERIAL_TRANSACTIONS mtmt
WHERE 1 = 1
AND mtmt.inventory_item_id = MSIB.inventory_item_id --4018
AND mtmt.organization_id = MSIB.organization_id --499
AND mtmt.TRANSACTION_ACTION_ID NOT IN (24,30)
AND to_date(mtmt.transaction_date) >= to_date(&Roll_back_date, 'DD-MON-RRRR')
) RB_TRANSACTIONS,
mmt.annual_Consumption annual_Consumption,
mmt.annual_Consumption / 4 months_Consumption,
mmt.annual_Consumption / 12 monthly_Average,
(
SELECT MATERIAL_COST
FROM CST_ITEM_COST_TYPE_V vw
WHERE vw.organization_id = MSIB.organization_id
AND vw.INVENTORY_ITEM_ID = MSIB.inventory_item_id
) Unit_Cost, --new
sum(quan.t_quantity) - sum(r_quantity) finala
FROM mtl_system_items_b MSIB
LEFT JOIN PO_HEADERS_ALL ph ON msib.inventory_item_id = pl.item_id
INNER JOIN Po_Lines_All pl ON ph.po_header_id = pl.po_header_id
INNER JOIN PO_LINE_LOCATIONS_ALL PLLA ON plla.po_header_id = ph.po_header_id AND pl.po_line_id = plla.po_line_id AND plla.org_id = pl.org_id
LEFT JOIN
-------------------SUBQUERY---------------------------------------
(
SELECT nvl(sum(subplla.quantity), 0) t_quantity,
nvl(sum(subplla.quantity_received), 0) r_quantity,
subpl.item_id
FROM po_headers_all subph
INNER JOIN po_lines_all subpl ON subph.po_header_id = subpl.po_header_id
INNER JOIN po_line_locations_all subplla ON subplla.po_header_id = subph.po_header_id
AND subpl.po_line_id = subplla.po_line_id
AND subplla.org_id = subpl.org_id
WHERE to_date(subplla.creation_date) >= to_date(&Roll_back_date, 'DD-MON-RRRR')
GROUP BY subph.attribute1,
subph.currency_code,
subpl.item_id
) quan ON pl.item_id = quan.item_id
-------------------SUBQUERY---------------------------------------
LEFT JOIN (
SELECT mmt.ORGANIZATION_ID,
mmt.INVENTORY_ITEM_ID,
ABS(SUM(mmt.transaction_quantity)) AS annual_Consumption
FROM MTL_MATERIAL_TRANSACTIONS mmt
WHERE mmt.TRANSACTION_ACTION_ID NOT IN (24,30)
AND mmt.TRANSACTION_TYPE_ID IN (33,52)
AND mmt.transaction_date BETWEEN to_date(add_months(&CONS_f_DATE, - 12), 'DD-MON-YYYY')
AND to_date(&CONS_f_DATE, 'DD-MON-YYYY')
AND mmt.parent_transaction_id IS NULL
) mmt ON mmt.ORGANIZATION_ID = MSIB.organization_id --499--579
AND mmt.INVENTORY_ITEM_ID = MSIB.inventory_item_id --4128 --4165
WHERE msib.organization_id IN (
SELECT haou.organization_id
FROM hr_organization_information hoi
JOIN hr_all_organization_units haou ON haou.organization_id = hoi.organization_id
WHERE hoi.org_information1 = 'INV'
AND hoi.org_information2 = 'Y'
AND haou.name LIKE '%HEIS%'
)
AND MSIB.Inventory_Item_Id = NVL(&ITEM, MSIB.Inventory_Item_Id)
AND MSIB.organization_id = nvl(&P_ORGI, MSIB.organization_id)
AND to_date(plla.creation_date) BETWEEN to_date(add_months(&Roll_back_date, - 12), 'DD-MON-YYYY')
AND to_date(&Roll_back_date, 'DD-MON-YYYY')
GROUP BY msib.segment1,
MSIB.inventory_item_id,
msib.description,
MSIB.organization_id,
msib.primary_unit_of_measure,
ph.attribute1,
ph.currency_code;
Here is the query in its simplest form, The main query is working good without the subquery. But when subquery returns null (i.e. no output in the date range) the whole query returns nothing. I want it to display results regardless the subquery results. My guess is something is wrong with the JOIN –
SELECT distinct msib.segment1 Item_Code,
MSIB.inventory_item_id,
MSIB.organization_id ORG_ID,
msib.description Item_Description,
msib.primary_unit_of_measure UOM,
ph.attribute1 Item_Type,
ph.currency_code currency,
max(pl.unit_price) FOB_Value_in_FCY,
max(ph.rate_date),
max(ph.rate) forex_rate,
sum(quan.t_quantity) - sum(r_quantity) finala
FROM mtl_system_items_b MSIB,
PO_HEADERS_ALL ph,
Po_Lines_All pl,
PO_LINE_LOCATIONS_ALL PLLA,
------------SUBQUERY-------------------------------
(select nvl(sum(subplla.quantity),0) t_quantity, nvl(sum(subplla.quantity_received),0) r_quantity ,subpl.item_id
from po_headers_all subph,
po_lines_all subpl,
po_line_locations_all subplla
where subph.po_header_id = subpl.po_header_id
and subplla.po_header_id = subph.po_header_id
and subpl.po_line_id = subplla.po_line_id
and subplla.org_id = subpl.org_id
and to_date(subplla.creation_date) >= to_date(&Roll_back_date,'DD-MON-RRRR')
group by subph.attribute1, subph.currency_code, subpl.item_id
) quan
------------SUBQUERY-------------------------------
WHERE 1=1
and ph.po_header_id = pl.po_header_id
and msib.inventory_item_id (+) = pl.item_id
-----------------joining subquery-------------------
and pl.item_id (+) = quan.item_id
-----------------joining subquery-------------------
and plla.po_header_id = ph.po_header_id
and pl.po_line_id = plla.po_line_id
and plla.org_id = pl.org_id
and msib.organization_id in
(select haou.organization_id
from hr_organization_information hoi,
hr_all_organization_units haou
where haou.organization_id = hoi.organization_id
and hoi.org_information1 = 'INV'
and hoi.org_information2 = 'Y'
and haou.name like '%HEIS%')
and MSIB.Inventory_Item_Id=NVL(&ITEM,MSIB.Inventory_Item_Id)
and MSIB.organization_id = nvl(&P_ORGI, MSIB.organization_id)
AND to_date(plla.creation_date) BETWEEN
to_date(add_months(&Roll_back_date, -12),'DD-MON-YYYY') AND
to_date(&Roll_back_date,'DD-MON-YYYY')
GROUP BY msib.segment1,
MSIB.inventory_item_id,
msib.description,
MSIB.organization_id,
msib.primary_unit_of_measure,
ph.attribute1,
ph.currency_code

Re-writing a view that uses condition join [OR]

I have this view that is performing really bad, and the problem is its build on joins that use or conditions
I have tried re-writing this for the past 2 days, using CTE and UNION but the result is incorrect
Maybe you guys can help, just few ideas how to do it, because I am completely lost.
CREATE VIEW [dbo].[V_Amazon_Listings]
AS
SELECT
DISTINCT
ISNULL(a.MerchantId, b.MerchantId) AS MerchantId
, ISNULL(a.SKU, b.[seller-sku]) AS SKU
, ISNULL(a.Quantity, 0) AS NONFBAQTY
, ISNULL(b.[Quantity Available], 0) AS FBAQTY
, ISNULL(a.[ASIN], b.[ASIN]) AS [ASIN]
, CASE
WHEN salePrices.SKU IS NOT NULL THEN ISNULL(CONVERT(DECIMAL(18, 2), (salePrices.SalePrice * vamm.USDConversionRate)), 9999)
ELSE ISNULL(CONVERT(DECIMAL(18, 2), (a.Price * vamm.USDConversionRate)), 9999)
END AS Price
, CASE ISNULL(a.Quantity, 0)
WHEN 0 THEN ISNULL(b.[Quantity Available], 0)
ELSE ISNULL(a.Quantity, 0)
END AS Quantity
--a.ReportRequestId,
, CASE WHEN ISNULL(a.Quantity, 0) = 0 AND ISNULL(b.[Quantity Available], 0) > 0 THEN 1
ELSE 0
END AS IsFBAed
, CASE WHEN c.[seller-sku] IS NULL THEN 0
ELSE 1
END AS IsRestricted--,
--a.ReportRequestId RegReportRequestID, b.ReportRequestId FBAReportRequestID, c.ReportRequestID CancReportRequestID
FROM
(
SELECT
ID
, MerchantId
, SKU
, [ASIN]
, Price
, CASE Quantity
WHEN '' THEN 0
ELSE Quantity
END AS Quantity
, ReportRequestId
FROM dbo.Amazon_Listings_Raw WITH (NOLOCK)
WHERE RIGHT(SKU,4) <> '__ON'
AND ReportRequestId IN
(
SELECT
MAX(ReportRequestId)
FROM v_Amazon_Listings_Raw WITH (NOLOCK)
GROUP BY MerchantId
)
) AS a
FULL JOIN
(
SELECT
b.ID
, b.MerchantId
, b.[seller-sku]
, b.[fulfillment-channel-sku]
, b.[ASIN]
, b.[condition-type]
, b.[Warehouse-Condition-code]
, b.[Quantity Available]
, b.ReportRequestId
FROM dbo.[Amazon_FBA_Listings_Raw] AS b WITH (NOLOCK)
WHERE
ReportRequestId IN
(
SELECT
MAX(ReportRequestId)
FROM [v_Amazon_FBA_Listings_Raw] WITH (NOLOCK)
GROUP BY MerchantId
)
AND [Warehouse-Condition-code] = 'Sellable'
) AS b
ON a.MerchantId = b.MerchantId
AND a.SKU = b.[seller-sku]
LEFT JOIN
(
SELECT
ID
, MerchantId
, [item-name]
, [item-description]
, [seller-sku]
, Price
, Quantity
, [image-url]
, [item-is-marketplace]
, [product-id-type]
, [zshop-shipping-fee]
, [item-note]
, [item-condition]
, [zshop-category1]
, [zshop-browse-path]
, [zshop-storefront-feature]
, asin1
, asin2
, asin3
, [will-ship-internationally]
, [expedited-shipping]
, [zshop-boldface]
, [product-id]
, ReportRequestId
FROM dbo.[Amazon_Cancelled_Listings_Raw] WITH (NOLOCK)
WHERE RIGHT([seller-sku],4) <> '__ON'
AND ReportRequestId IN
(
SELECT MAX(ReportRequestId)
FROM [v_Amazon_Cancelled_Listings_Raw] WITH (NOLOCK)
GROUP BY MerchantId
)
) AS c
ON (a.MerchantId = c.MerchantId OR b.MerchantId = c.MerchantId)
AND (b.[seller-sku] = c.[seller-sku] OR a.SKU = c.[seller-sku])
INNER JOIN V_Amazon_Marketplace_Merchants vamm
ON (a.MerchantId = vamm.MerchantID OR b.MerchantId = vamm.MerchantId)
LEFT JOIN AmazonOurPrices salePrices WITH (NOLOCK)
ON vamm.MerchantId = salePrices.MerchantId
AND a.SKU = salePrices.SKU
AND salePrices.SalePrice <> salePrices.RegularPrice
This is what I have come up with, its runs really fast 20 seconds,
but the data is off, not sure what I am missing or doing wrong.
any help would be glady appreciated
CREATE VIEW AMZ_NOTUSED_12_22_2017
as
WITH A ( MerchantID,Sku,Asin,Price,Quantity, [Seller-Sku],SalePrice,USDConversionRate,salePricesSKU,[Quantity Available])
AS
(
SELECT a.MerchantId,a.SKU,a.ASIN,a.Price,a.Quantity, c.[seller-sku],salePrices.SalePrice,vamm.USDConversionRate,salePrices.SKU AS salePricesSKU,'' [Quantity Available]
FROM
(
SELECT
ID
, MerchantId
, SKU
, [ASIN]
, Price
, CASE Quantity
WHEN '' THEN 0
ELSE Quantity
END AS Quantity
, ReportRequestId
FROM dbo.Amazon_Listings_Raw WITH (NOLOCK)
WHERE RIGHT(SKU,4) <> '__ON'
AND ReportRequestId IN
(
SELECT
MAX(ReportRequestId)
FROM v_Amazon_Listings_Raw WITH (NOLOCK)
GROUP BY MerchantId
)
) AS a
LEFT JOIN
(
SELECT
ID
, MerchantId
, [item-name]
, [item-description]
, [seller-sku]
, Price
, Quantity
, [image-url]
, [item-is-marketplace]
, [product-id-type]
, [zshop-shipping-fee]
, [item-note]
, [item-condition]
, [zshop-category1]
, [zshop-browse-path]
, [zshop-storefront-feature]
, asin1
, asin2
, asin3
, [will-ship-internationally]
, [expedited-shipping]
, [zshop-boldface]
, [product-id]
, ReportRequestId
FROM dbo.[Amazon_Cancelled_Listings_Raw] WITH (NOLOCK)
WHERE RIGHT([seller-sku],4) <> '__ON'
AND ReportRequestId IN
(
SELECT MAX(ReportRequestId)
FROM [v_Amazon_Cancelled_Listings_Raw] WITH (NOLOCK)
GROUP BY MerchantId
)
) AS c
ON a.MerchantId = c.MerchantId and a.SKU = c.[seller-sku]
INNER JOIN V_Amazon_Marketplace_Merchants vamm
ON a.MerchantId = vamm.MerchantID
LEFT JOIN AmazonOurPrices salePrices WITH (NOLOCK)
ON vamm.MerchantId = salePrices.MerchantId
AND a.SKU = salePrices.SKU
AND salePrices.SalePrice <> salePrices.RegularPrice
)
,
B AS
(
SELECT b.MerchantId,
b.[Seller-SKU] AS Sku,
b.ASIN,
c.Price,
c.quantity ,
c.[seller-sku],
salePrices.SalePrice,
vamm.USDConversionRate,
salePrices.SKU AS salePricesSKU,
b.[Quantity Available]
FROM
(
SELECT
b.ID
, b.MerchantId
, b.[seller-sku]
, b.[fulfillment-channel-sku]
, b.[ASIN]
, b.[condition-type]
, b.[Warehouse-Condition-code]
, b.[Quantity Available]
, b.ReportRequestId
FROM dbo.[Amazon_FBA_Listings_Raw] AS b WITH (NOLOCK)
WHERE
ReportRequestId IN
(
SELECT
MAX(ReportRequestId)
FROM [v_Amazon_FBA_Listings_Raw] WITH (NOLOCK)
GROUP BY MerchantId
)
AND [Warehouse-Condition-code] = 'Sellable'
) AS b
LEFT JOIN
(
SELECT
ID
, MerchantId
, [item-name]
, [item-description]
, [seller-sku]
, Price
, Quantity
, [image-url]
, [item-is-marketplace]
, [product-id-type]
, [zshop-shipping-fee]
, [item-note]
, [item-condition]
, [zshop-category1]
, [zshop-browse-path]
, [zshop-storefront-feature]
, asin1
, asin2
, asin3
, [will-ship-internationally]
, [expedited-shipping]
, [zshop-boldface]
, [product-id]
, ReportRequestId
FROM dbo.[Amazon_Cancelled_Listings_Raw] WITH (NOLOCK)
WHERE RIGHT([seller-sku],4) <> '__ON'
AND ReportRequestId IN
(
SELECT MAX(ReportRequestId)
FROM [v_Amazon_Cancelled_Listings_Raw] WITH (NOLOCK)
GROUP BY MerchantId
)
) AS c
ON
b.[seller-sku] = c.[seller-sku] and b.MerchantId = c.MerchantId
INNER JOIN V_Amazon_Marketplace_Merchants vamm
ON b.MerchantId = vamm.MerchantId
LEFT JOIN AmazonOurPrices salePrices WITH(NOLOCK)
ON vamm.MerchantId = salePrices.MerchantId
AND b.[Seller-SKU] = salePrices.SKU
AND salePrices.SalePrice <> salePrices.RegularPrice
)
SELECT
DISTINCT
a.MerchantId AS MerchantId
, a.SKU
, a.Quantity AS NONFBAQTY
, ISNULL(a.[Quantity Available],0) AS FBAQTY
, a.[ASIN] AS [ASIN]
, CASE
WHEN salePricesSKU IS NOT NULL THEN ISNULL(CONVERT(DECIMAL(18, 2), (SalePrice * USDConversionRate)), 9999)
ELSE ISNULL(CONVERT(DECIMAL(18, 2), (a.Price * USDConversionRate)), 9999)
END AS Price
, CASE ISNULL(a.Quantity, 0)
WHEN 0 THEN ISNULL(a.[Quantity Available], 0)
ELSE ISNULL(a.Quantity, 0)
END AS Quantity
, CASE WHEN ISNULL(a.Quantity, 0) = 0 AND ISNULL(a.[Quantity Available], 0) > 0 THEN 1
ELSE 0
END AS IsFBAed
, CASE WHEN a.[seller-sku] IS NULL THEN 0
ELSE 1
END AS IsRestricted
FROM
(
SELECT * FROM a
UNION
SELECT * FRom B
) a

Resources