with ces as
(select account_num,
imsi,
previous_month,
current_month,
status
from (
select account_num,
imsi,
to_char(u.previous_month,'yyyymmdd')previous_month,
to_char(u.current_month,'yyyymmdd')current_month,
ls.status
from ( select account_num
,imsi
,case when usage_date < add_months(to_date('20151110','yyyymmdd'),-1) then usage_date
else null
end previous_month
,case when usage_date >= add_months(to_date('20151110','yyyymmdd'),-1) then usage_date
else null
end current_month
from genevabatchuser.loadusagedata
where event_source = 'NUMEREXROMHYB'
and account_num = 'ACC000142'
and imsi in ('310640100000003','310640100000004','310640100000338','310640100000331')
)u full outer join GENEVABATCHUSER.TMOLOADIMSILIST ls using (account_num,imsi)
)
)
select * from ces
where imsi in ('310640100000003','310640100000004','310640100000338','310640100000331')
Im seeing the output like below:
ACCOUNT_NUM IMSI PREVIOUS_MONTH CURRENT_MONTH
ACC000142 310640100000003 10/8/2015 5:18:29 AM
ACC000142 310640100000003 11/3/2015 11:25:33 PM
ACC000142 310640100000003 11/3/2015 5:18:29 AM
ACC000142 310640100000003 10/8/2015 11:25:33 PM
My Expected output is:
ACCOUNT_NUM IMSI PREVIOUS_MONTH CURRENT_MONTH
ACC000142 310640100000003 10/8/2015 5:18:29 AM 11/3/2015 11:25:33 PM
It appears that what you want to do is something like
with ces as (select account_num,
imsi,
u.previous_month,
u.current_month
ls.status
from (select account_num,
imsi,
case
when usage_date < add_months(to_date('20151110','yyyymmdd'),-1) then usage_date
else null
end previous_month,
case
when usage_date >= add_months(to_date('20151110','yyyymmdd'),-1) then usage_date
else null
end current_month
from genevabatchuser.loadusagedata
where event_source = 'NUMEREXROMHYB' and
account_num = 'ACC000142' and
imsi in ('310640100000003','310640100000004','310640100000338','310640100000331'))u
full outer join GENEVABATCHUSER.TMOLOADIMSILIST ls
using (account_num, imsi))
select ACCOUNT_NUM,
IMSI,
MIN(PREVIOUS_MONTH) AS PREVIOUS_MONTH,
MAX(CURRENT_MONTH) AS CURRENT_MONTH
from ces
where imsi in ('310640100000003','310640100000004','310640100000338','310640100000331')
GROUP BY ACCOUNT_NUM, IMSI;
Best of luck.
Related
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
When I execute the code for partition_date = 2019-12-11 and for partition_date = 2019-07-06 I get an error saying 'The subquery returs more then one line'. For Other cases it works
select case when my_count <=1 AND status_ = 'END' then 0
when my_count >1 AND status_ IN ('END', 'RUNNING', 'START', 'ERR') THEN 1
ELSE -1
end AS W
INTO return_val
from (select
(SELECT count(DISTINCT(status)) FROM my_table
WHERE my_table.HIVE = 'hive1'
AND EXTRACT(MONTH FROM PARTITION_DATE) = EXTRACT(MONTH FROM TO_DATE('20191212', 'YYYY-MM-DD'))
AND EXTRACT(YEAR FROM PARTITION_DATE) = EXTRACT(YEAR FROM TO_DATE('20191212', 'YYYY-MM-DD'))
) my_count,
(SELECT DISTINCT(status) AS status_ FROM my_table
my_table.HIVE = 'hive1'
AND EXTRACT(MONTH FROM PARTITION_DATE) = EXTRACT(MONTH FROM TO_DATE('20191212', 'YYYY-MM-DD'))
AND EXTRACT(YEAR FROM PARTITION_DATE) = EXTRACT(YEAR FROM TO_DATE('20191212', 'YYYY-MM-DD'))
)status_
FROM dual
)x ;
RETURN(return_val);
my_table is like
STATUS |HIVE |PARTITION_DATE |
-------|-----|-------------------
END |hive1|2019-12-11 00:00:00|
START |hive1|2019-12-12 00:00:00|
NA |hive1|2019-12-13 00:00:00|
END |hive1|2019-09-12 00:00:00|
END |hive1|2019-09-13 00:00:00|
END |hive1|2019-09-14 00:00:00|
ERR |hive1|2019-08-06 00:00:00|
ERR |hive1|2019-07-06 00:00:00|
START |hive1|2019-07-07 00:00:00|
RUNNING|hive1|2019-07-08 00:00:00|
END |hive1|2019-07-09 00:00:00|
You have a few status in statement:
(SELECT DISTINCT(status) AS status_ FROM my_table
WHERE EL_F_BDP_PROC_REALTIME.HIVE = 'hive1'
AND EXTRACT(MONTH FROM PARTITION_DATE) = EXTRACT(MONTH FROM TO_DATE('20191212', 'YYYY-MM-DD'))
AND EXTRACT(YEAR FROM PARTITION_DATE) = EXTRACT(YEAR FROM TO_DATE('20191212', 'YYYY-MM-DD'))
)
and returned more one line.
For rigth execute you need return one value example:
max((SELECT DISTINCT(status) AS status_ FROM my_table
WHERE EL_F_BDP_PROC_REALTIME.HIVE = 'hive1'
AND EXTRACT(MONTH FROM PARTITION_DATE) = EXTRACT(MONTH FROM TO_DATE('20191212', 'YYYY-MM-DD'))
AND EXTRACT(YEAR FROM PARTITION_DATE) = EXTRACT(YEAR FROM TO_DATE('20191212', 'YYYY-MM-DD'))
))
Is it executing, but ,purhaps ,logic is wrong
Try it:
with tbl as (
select 'START' status, 'hive1' str, to_date('2019-12-11', 'yyyy-mm-dd') dt from dual
union all
select 'NA' status, 'hive1' str, to_date('2019-12-11', 'yyyy-mm-dd') from dual
union all
select 'END' status, 'hive1' str, to_date('2019-12-11', 'yyyy-mm-dd') from dual)
select dt, str, listagg(status, ' ') WITHIN GROUP (ORDER BY str)
from tbl
GROUP BY dt, str
Why don't you use group by,
SELECT case when my_count <=1 AND status_ = 'END' then 0
when my_count >1 AND status_ IN ('END', 'RUNNING', 'START', 'ERR') THEN 1
ELSE -1
end AS W
INTO return_val
from (select DISTINCT count(status) as my_count,status as status_ FROM my_table
WHERE EL_F_BDP_PROC_REALTIME.HIVE = 'hive1'
AND EXTRACT(MONTH FROM PARTITION_DATE) = EXTRACT(MONTH FROM TO_DATE('20191212', 'YYYY-MM-DD'))
AND EXTRACT(YEAR FROM PARTITION_DATE) = EXTRACT(YEAR FROM TO_DATE('20191212', 'YYYY-MM-DD'))
group by status)res ;
please help me to correct this error I am getting this error continuously
ORA-01861: literal does not match format string for case statement in
oracle
SELECT activity_made,
(CASE
WHEN (TO_DATE(activity_made, 'DD-Mon-YYYY')) = TRUNC(SYSDATE) THEN TO_CHAR(activity_made, 'hh12:mi PM')
WHEN TRUNC(TO_DATE(activity_made, 'MM-DD-YYYY')) BETWEEN TRUNC(SYSDATE, 'yy') AND TRUNC(SYSDATE - 1) THEN TO_CHAR(activity_made, 'Mon dd')
ELSE TO_CHAR(TO_DATE(activity_made, 'MM/DD/YYYY'), 'mm/dd/yyyy')
END)
AS actmode
FROM (SELECT (CASE
WHEN (SELECT COUNT(*)
FROM sfa_activity sa
WHERE sa.companyid = opp.companyid
AND sa.opptyid = opp.opptyid
AND sa.TYPE = 'NOTE') > 0
THEN
(SELECT NVL(MAX(TO_CHAR(sa.updat, 'YYYY/MM/DD HH24:MI:SS')), '0000/00/00 00:00:00')
FROM sfa_activity sa
WHERE sa.companyid = opp.companyid
AND sa.opptyid = opp.opptyid
AND sa.TYPE = 'NOTE')
ELSE
(SELECT NVL(MAX(TO_CHAR(sa.updat, 'YYYY/MM/DD HH24:MI:SS')), '1001/01/01 01:01:01')
/*ELSE (SELECT (MAX(TO_CHAR(sa.updat,'YYYY/MM/DD HH24:MI:SS')))*/
FROM sfa_activity sa
WHERE sa.companyid = opp.companyid
AND sa.opptyid = opp.opptyid
AND sa.TYPE <> 'NOTE')
END)
AS activity_made
FROM sfa_opportunities opp
WHERE companyid = 1192)
It seems that ACTIVITY_MADE is in yyyy/mm/dd hh24:mi:ss format (as suggested by an inline view). If simplified, your query looks like
select to_date(activity_made, ...) --> should use the same format as below, in an inline view
from (select activity_made -->TO_CHAR(sa.updat, 'YYYY/MM/DD HH24:MI:SS')
from sfa_opportunities)
So: if you used that format mask for activity_made, you should use it for TO_CHAR in the main SELECT. However, you used
dd-mon-yyyy
mm-dd-yyyy
mm/dd/yyyy
Switch to yyyy/mm/dd hh24:mi:ss in CASE.
I would recommend you to rewrite your query to something like the following:
SELECT sa.updat activity_made
, CASE WHEN TRUNC(sa.updat) = TRUNC(SYSDATE) THEN TO_CHAR(sa.updat, 'hh12:mi PM')
WHEN sa.updat > TRUNC(SYSDATE,'yy') THEN TO_CHAR(sa.updat, 'Mon dd')
ELSE TO_CHAR(sa.updat, 'mm/dd/yyyy')
END actmode
FROM sfa_activity sa
WHERE (sa.companyid, sa.opptyid) IN (SELECT opp.companyid, opp.opptyid
FROM sfa_opportunities opp
WHERE companyid = 1192)
ORDER BY CASE WHEN sa.TYPE = 'NOTE' THEN 0 ELSE 1 END, sa.updat DESC
FETCH first ROW ONLY
Your version has some drawbacks:
unnecessary type conversions
a lot of selects on the same table, that are not needed
there might be also some performance issues
The handling when no activities are made should be made outside and not in the inner select. (Makes it easier to maintain.)
Thank you all, I got the solution by applying the case inside the activity_made
/* Formatted on 2018/09/10 14:15 (Formatter Plus v4.8.5) */
SELECT activity_made,
(CASE
WHEN activity_made = '0000/00/00 00:00:00'
THEN NULL
ELSE activity_made
END
) AS actmode
FROM (SELECT (CASE
WHEN (SELECT COUNT (*)
FROM sfa_activity sa
WHERE sa.companyid = opp.companyid
AND sa.opptyid = opp.opptyid
AND sa.TYPE = 'NOTE') > 0
THEN (SELECT NVL
(MAX
(TO_CHAR
(CASE
WHEN TRUNC (sa.updat) =
TRUNC (SYSDATE)
THEN TO_CHAR
(sa.updat,
'hh12:mi PM'
)
WHEN TRUNC (sa.updat)
BETWEEN TRUNC
(SYSDATE,
'yy'
)
AND TRUNC
( SYSDATE
- 1
)
THEN TO_CHAR (sa.updat,
'Mon dd'
)
ELSE TO_CHAR (sa.updat,
'DD/MM/YYYY'
)
END
)
),
'0000/00/00 00:00:00'
)
FROM sfa_activity sa
WHERE sa.companyid = opp.companyid
AND sa.opptyid = opp.opptyid
AND sa.TYPE = 'NOTE')
ELSE (SELECT NVL
(MAX
(TO_CHAR
(CASE
WHEN TRUNC (sa.updat) =
TRUNC (SYSDATE)
THEN TO_CHAR (sa.updat,
'hh12:mi PM'
)
WHEN TRUNC (sa.updat)
BETWEEN TRUNC (SYSDATE,
'yy'
)
AND TRUNC ( SYSDATE
- 1
)
THEN TO_CHAR (sa.updat,
'Mon dd'
)
ELSE TO_CHAR (sa.updat,
'DD/MM/YYYY'
)
END
)
),
'0000/00/00 00:00:00'
)
/*ELSE (SELECT (MAX(TO_CHAR(sa.updat,'YYYY/MM/DD HH24:MI:SS')))*/
FROM sfa_activity sa
WHERE sa.companyid = opp.companyid
AND sa.opptyid = opp.opptyid
AND sa.TYPE <> 'NOTE')
END
) AS activity_made
FROM sfa_opportunities opp
WHERE companyid = 1192)
I am executing this query in oracle and it is giving me an error of missing right parenthesis at line 33. Is there anyone who can help me resolve this issue. Thank you
Here is my query
WITH t AS (
SELECT RM_LIVE.EMPLOYEE.EMPNO,
RM_LIVE.EMPNAME.FIRSTNAME,
RM_LIVE.EMPNAME.LASTNAME,
RM_LIVE.CRWBASE.BASE,
RM_LIVE.CRWCAT.crwcat,
RM_LIVE.CRWSPECFUNC.IDCRWSPECFUNC
FROM RM_LIVE.EMPBASE,
RM_LIVE.EMPLOYEE,
RM_LIVE.CRWBASE,
RM_LIVE.EMPNAME,
RM_LIVE.CRWSPECFUNC,
RM_LIVE.EMPSPECFUNC,
RM_LIVE.EMPQUALCAT,
RM_LIVE.CRWCAT
where RM_LIVE.EMPBASE.IDEMPNO = RM_LIVE.EMPLOYEE.IDEMPNO
AND RM_LIVE.EMPBASE.IDCRWBASE = RM_LIVE.CRWBASE.IDCRWBASE
AND RM_LIVE.EMPLOYEE.IDEMPNO = RM_LIVE.EMPNAME.IDEMPNO
AND RM_LIVE.EMPSPECFUNC.IDCRWSPECFUNC =RM_LIVE.CRWSPECFUNC.IDCRWSPECFUNC
AND RM_LIVE.EMPSPECFUNC.IDEMPNO =RM_LIVE.EMPLOYEE.IDEMPNO
AND RM_LIVE.EMPQUALCAT.IDEMPNO=RM_LIVE.EMPLOYEE.IDEMPNO
AND RM_LIVE.CRWCAT.IDCRWCAT = RM_LIVE.EMPQUALCAT.IDCRWCAT
AND RM_LIVE.CRWCAT.crwcat IN ('CP','FO','CM','MC')
AND RM_LIVE.CRWBASE.BASE <> 'XYZ'
AND RM_LIVE.CRWSPECFUNC.IDCRWSPECFUNC IN
('921','2' ,'1','301','17','4','3','7','302' ,'861','31',
'723','30','722 ','29 ','721','16','601','581')
AND RM_LIVE.EMPBASE.STARTDATE <= SYSDATE
AND RM_LIVE.EMPBASE.ENDDATE >= SYSDATE
AND RM_LIVE.EMPSPECFUNC.STARTDATE <= SYSDATE
AND RM_LIVE.EMPSPECFUNC.ENDDATE >= SYSDATE
AND RM_LIVE.EMPNAME.FROMDATE <=SYSDATE
AND RM_LIVE.EMPQUALCAT.STARTDATE <= SYSDATE
AND RM_LIVE.EMPQUALCAT.ENDDATE >= SYSDATE AS ta (EMPNO,EMPFIRSTNAME,EMPLASTNAME, Base, CAT, code)
)
SELECT DISTINCT
t.EMPNO,
t.EMPFIRSTNAME,
t.EMPLASTNAME,
t.Base,
t.CAT,
(ABS(oa.val1) * NVL(NULLIF((ABS(oa.val2) * ABS(oa.val3)),0),1) * ABS(oa.val4) * ABS(oa.val5) * ABS(oa.val6) * ABS(oa.val7) * ABS(oa.val8) * ABS(oa.val9)) AS "FTE VALUE"
FROM t
OUTER APPLY (SELECT MAX(CASE WHEN t2.code IN (1,2,4) THEN 0.70 ELSE -1 END) AS val1,
MAX(CASE WHEN t2.code IN (1,2) THEN 0 ELSE -1 END) AS val2,
MAX(CASE WHEN t2.code IN (4) THEN 1.29 ELSE -1 END) AS val3,
MAX(CASE WHEN t2.code IN ( 861 ) THEN 0.80 ELSE -1 END) AS val4
MAX(CASE WHEN t2.code IN (921,301,30,722,601,581) THEN 0.50 ELSE -1 END) AS val5
MAX(CASE WHEN t2.code IN (17,302,16) THEN 0.85 ELSE -1 END) AS val6
MAX(CASE WHEN t2.code IN (29,721) THEN 0.25 ELSE -1 END) AS val7
MAX(CASE WHEN t2.code IN (31,723) THEN 0.75 ELSE -1 END) AS val8
MAX(CASE WHEN t2.code IN (3,7) THEN 0.90 ELSE -1 END) AS val9
FROM t AS t2 WHERE t2.EMPNO = t.EMPNO) oa
The last line of your sub-query factoring (WITH ... AS ( ... )) clause is:
AND RM_LIVE.EMPQUALCAT.ENDDATE >= SYSDATE AS ta (EMPNO,EMPFIRSTNAME,EMPLASTNAME, Base, CAT, code)
The AS ta (...) is invalid syntax.
If you want to name the columns then you need to delete that part and change the first line to:
WITH t (EMPNO,EMPFIRSTNAME,EMPLASTNAME, Base, CAT, code) AS (
However, that is syntax introduced in Oracle 11g and won't work in Oracle 10g - if you want to support that version (and it seems you do since you've tagged it) then just explicitly alias each column:
WITH t AS (
SELECT RM_LIVE.EMPLOYEE.EMPNO,
RM_LIVE.EMPNAME.FIRSTNAME AS EMPFIRSTNAME,
RM_LIVE.EMPNAME.LASTNAME AS EMPLASTNAME,
RM_LIVE.CRWBASE.BASE,
RM_LIVE.CRWCAT.crwcat AS CAT,
RM_LIVE.CRWSPECFUNC.IDCRWSPECFUNC AS CODE
There i a simplified table myowner.mydable with at least columns order_nr,the_days, and i us a multilevel select-query
select * from (
select
The sum(..) function does not work in a inner select-query. How to do use a function in a inner select???
(select sum(the_days) from myowner.mydable where order_nr=confnr) "thedays",--this is a line 4
confnr as order_nr
from (
select order_nr as confnr,the_days from myowner.mydable WHERE order_nr='5276751'
) as t
union all
And there i get an another order_nr
select
(select sum(the_days) from myowner.mydable where order_nr=confnr) "thedays",
confnr as order_nr
from (
select order_nr as confnr,the_days from myowner.mydable WHERE order_nr='5276751'
) as t
) as t2
Select (.....) as t works. But select ((.....) as t union all (.....) as t) as t2 gives an error:
Could not execute statement.
Feature, scalar value subquery (at line 4) outside of a top level SELECT
list, is not supported.
-- (opt_Select.cxx 2238)
SQLCODE=-1001030, ODBC 3 State="HY000"
Line 1, column 1
How to get rid from the error?
If you need execute this query often, you can try to create view based on this query and use it.
Also, your subquery looks strange. This:
select
(select sum(the_days) from myowner.mydable where order_nr=confnr) "thedays",
confnr as order_nr
from (
select order_nr as confnr,the_days from myowner.mydable WHERE order_nr='5276751'
) as t
is equal to this:
select sum(the_days), order_nr from myowner.mydable where order_nr = '5276751'
group by order_nr
UPD Full query you can transform like this:
select t.category, t.def_or_indef, t.book_date, t.book_code, t.revenue,
t2.conf_nr,
t2.mydays,
t.revenue / t2.mydays
from (select category, def_or_indef, book_date, book_code,
revenue,
confnr as conf_nr
from (select CASE WHEN status = 'status1' OR status = 'status2' THEN 'category1' ELSE 'category2' END AS category,
CASE WHEN status = 'status1' OR status = 'status2' THEN 'definite' ELSE 'indefinite' END AS def_or_indef,
book_date,
book_code,
revenue,
conf_nr as confnr
from myowner.mydable
WHERE days > 0
AND book_date IS NOT NULL
AND DATE(dateadd(year,-1,dateadd(day,-day(today()),today()))) < book_date
AND book_date <= date(dateadd(day,-day(today()),today()))) as t
union all
select .. from ( select .. WHERE .. AND DATE(dateadd(day,-day(today()),today())) < resv_date and resv_date < today()
) as t,
(select conf_nr, sum(days) as mydays
from myowner.mydable
group by conf_nr) t2
I give a real query:
select * from (
select
category, def_or_indef, book_date, book_code,
(select sum(days) from myowner.mydable where conf_nr=confnr) as mydays,
revenue, confnr as conf_nr,
revenue / (select sum(days) from myowner.mydable where conf_nr=confnr) as adr
from (
select
CASE WHEN status = 'status1' OR status = 'status2' THEN 'category1' ELSE 'category2' END AS category,
CASE WHEN status = 'status1' OR status = 'status2' THEN 'definite' ELSE 'indefinite' END AS def_or_indef,
book_date, book_code, revenue, conf_nr as confnr
from myowner.mydable
WHERE days > 0 AND book_date IS NOT NULL AND
DATE(dateadd(year,-1,dateadd(day,-day(today()),today()))) < book_date AND book_date <= date(dateadd(day,-day(today()),today()))
) as t
union all
select .. from ( select .. WHERE .. AND
DATE(dateadd(day,-day(today()),today())) < resv_date and resv_date < today()
) as t
) as t2
Can you give working equal to this?