How to remove WITH clauses Oracle query - sql

I have a query where I have to remove the WITH clauses and it still have to return the same results. One of the clauses should go in to Join I guess and the other one in having ? Am I in a right direction...
Any suggestion
with QA_m as(
select adr_id, max(eff_ts)as EFF_TS
from addr group by adr_id)
,
QA_address as(select q.adr_id,q.EFF_TS,d.COUNTRY_ID,d.city,d.POST_CODE,d.STREET,d.UNIT_NBR,d.ADL_INFO
from QA_m q join ADDR d
on q.adr_id=d.adr_id and q.EFF_TS=d.EFF_TS)
select
c.SRGT_KEY_VAL as Customer_id,
CAST(i.EFF_TS as date) as "EFF_DT",
i.EFF_TS,
'9999-12-31' as END_DT,
'N' as DEL_IND,
'I' as CUSTOMER_TYPE,
case when ctr.NAME = 'Canada' then 'Y'
else 'N'
END as RESIDENCE_FLAG,
NVL(ctr.name,'N/A') as country,
NVL((trim(TITLE) ||' '||trim(FIRST_NAME)||' '||trim(LAST_NAME)),' ') as NAME,
NVL((trim(CITY)||' '||trim(POST_CODE)||' '||trim(STREET)||' '||trim(UNIT_NBR)||' '||trim(ADL_INFO)),' ') as ADDRESS,
case when i.BIRTH_DATE=TO_date('9999-12-31') then NULL
else TRUNC(months_between(sysdate, i.BIRTH_DATE) / 12)
end AGE,
case when substr(GENDER,1,1) = 'M' then 'M'
when substr(GENDER,1,1) = 'm' then 'M'
when substr(GENDER,1,1) = 'F' then 'F'
when substr(GENDER,1,1) = 'f' then 'F'
else NULL
end as GENDER,
NULL as VAT_NUMBER,
NULL as BRANCH,
NULL as EMPLOYEES
from IDV i
join CSTMR_SRGT_KEY c
on i.IDV_ID=c.ntrl_key_val
left join QA_address B
on i.adr_ID=b.adr_id
left join COUNTRY ctr
on ctr.COUNTRY_ID=b.COUNTRY_ID
where SRC_STM_ID = 100
and i.END_TS='9999-12-31 23:59:59.999999000'
and i.DEL_IND='N';

These are subqueries, so - just put them into appropriate places (see comments which indicate that):
SELECT c.srgt_key_val AS customer_id,
CAST (i.eff_ts AS DATE) AS "EFF_DT",
i.eff_ts,
'9999-12-31' AS end_dt,
'N' AS del_ind,
'I' AS customer_type,
CASE WHEN ctr.name = 'Canada' THEN 'Y' ELSE 'N' END AS residence_flag,
NVL (ctr.name, 'N/A') AS country,
NVL (
( TRIM (title)
|| ' '
|| TRIM (first_name)
|| ' '
|| TRIM (last_name)),
' ') AS name,
NVL (
( TRIM (city)
|| ' '
|| TRIM (post_code)
|| ' '
|| TRIM (street)
|| ' '
|| TRIM (unit_nbr)
|| ' '
|| TRIM (adl_info)),
' ') AS address,
CASE
WHEN i.birth_date = TO_DATE ('9999-12-31') THEN NULL
ELSE TRUNC (MONTHS_BETWEEN (SYSDATE, i.birth_date) / 12)
END age,
CASE
WHEN SUBSTR (gender, 1, 1) = 'M' THEN 'M'
WHEN SUBSTR (gender, 1, 1) = 'm' THEN 'M'
WHEN SUBSTR (gender, 1, 1) = 'F' THEN 'F'
WHEN SUBSTR (gender, 1, 1) = 'f' THEN 'F'
ELSE NULL
END AS gender,
NULL AS vat_number,
NULL AS branch,
NULL AS employees
FROM idv i
JOIN cstmr_srgt_key c ON i.idv_id = c.ntrl_key_val
LEFT JOIN ( --> this is QA_address
SELECT q.adr_id,
q.eff_ts,
d.country_id,
d.city,
d.post_code,
d.street,
d.unit_nbr,
d.adl_info
FROM ( --> this is QA_m
SELECT adr_id, MAX (eff_ts) AS eff_ts
FROM addr
GROUP BY adr_id) q
JOIN addr d
ON q.adr_id = d.adr_id
AND q.eff_ts = d.eff_ts) b
ON i.adr_id = b.adr_id
LEFT JOIN country ctr ON ctr.country_id = b.country_id
WHERE src_stm_id = 100
AND i.end_ts = '9999-12-31 23:59:59.999999000'
AND i.del_ind = 'N';

Related

How to take where clause conditions from table column in oracle SQL or plsql

How to take where clause conditions from table column in oracle plsql.
E.g. data in table
Condition
1.sourceSystemId = 'SN'
2.AND(coverageType='AD',amountType1='PREMIUM',premiumFrequency='REGULAR',yearOfPremium='1')
e.g query:
select * from xyz where rule='abc' and "sourceSystemId = 'SN'"
select * from xyz where rule='abc' AND(coverageType='AD',amountType1='PREMIUM',premiumFrequency='REGULAR',yearOfPremium='1')
Not entirely sure what you're asking here, but I would imagine that
select * from xyz where rule='abc' AND(coverageType='AD',amountType1='PREMIUM',premiumFrequency='REGULAR',yearOfPremium='1')
would become
select * from xyz
where rule='abc'
AND coverageType='AD'
and amountType1='PREMIUM'
and premiumFrequency='REGULAR'
and yearOfPremium='1'
I suppose you want something like :
DECLARE
l_query VARCHAR2(2000) := 'select * from xyz where rule=''abc''';
l_result xyz%ROWTYPE;
l_cursor SYS_REFCURSOR;
BEGIN
dbms_output.put_line(l_query);
FOR clause IN (SELECT condition
FROM conditions)
LOOP
l_query := l_query||' AND '||clause.condition;
END LOOP;
OPEN l_cursor FOR l_query;
LOOP
FETCH l_cursor INTO l_result;
EXIT WHEN l_cursor%NOTFOUND;
..
-- your processing
END LOOP;
CLOSE l_cursor;
END;
Here is example of SQL solution. I used justt first and last condition but you can get them all...
WITH
xyz As
(
Select 1 "ID", 'abc' "RULE", 'AD' "COVERAGETYPE", 'PREMIUM' "AMOUNTTYPE1", 'REGULAR' "PREMIUMFREQUENCY", '1' "YEAROFPREMIUM" From Dual
UNION
Select 2 "ID", 'abc' "RULE", 'BF' "COVERAGETYPE", 'ORDINARY' "AMOUNTTYPE1", 'EXTRA' "PREMIUMFREQUENCY", '2' "YEAROFPREMIUM" From Dual
UNION
Select 3 "ID", 'abc' "RULE", 'AD' "COVERAGETYPE", 'PREMIUM' "AMOUNTTYPE1", 'REGULAR' "PREMIUMFREQUENCY", '1' "YEAROFPREMIUM" From Dual
),
conditions As
(
SELECT UPPER('coverageType=AD,amountType1=PREMIUM,premiumFrequency=REGULAR,yearOfPremium=1') "CND" From Dual
)
SELECT
x.ID, x.RULE, x.COVERAGETYPE, x.AMOUNTTYPE1, x.PREMIUMFREQUENCY, x.YEAROFPREMIUM
FROM
xyz x
INNER JOIN
conditions c ON(1=1)
WHERE
x.RULE = 'abc' And
x.COVERAGETYPE = CASE WHEN InStr(c.CND || ',', 'COVERAGETYPE=') = 0 THEN x.COVERAGETYPE
ELSE SubStr(SubStr(c.CND || ',', InStr(c.CND || ',', 'COVERAGETYPE=') + Length('COVERAGETYPE=')), 1, InStr(SubStr(c.CND || ',', InStr(c.CND || ',', 'COVERAGETYPE=') + Length('COVERAGETYPE=') + 1), ',')) END And
x.YEAROFPREMIUM = CASE WHEN InStr(c.CND || ',', 'YEAROFPREMIUM=') = 0 THEN x.YEAROFPREMIUM
ELSE SubStr(SubStr(c.CND || ',', InStr(c.CND || ',', 'YEAROFPREMIUM=') + Length('YEAROFPREMIUM=')), 1, InStr(SubStr(c.CND || ',', InStr(c.CND || ',', 'YEAROFPREMIUM=') + Length('YEAROFPREMIUM=') + 1), ',')) END
Result:
ID RULE COVERAGETYPE AMOUNTTYPE1 PREMIUMFREQUENCY YEAROFPREMIUM
1 abc AD PREMIUM REGULAR 1
3 abc AD PREMIUM REGULAR 1

Creating a stored procedure for the select statement in oracle [duplicate]

I am wanting to store a Rownum as a variable rather than use a costly Join.
I need to get this from a Select statement as the Rownum will be different on various environments so it cannot be a literal string in the code.
For context, this query is executed on Oracle Siebel CRM schema and it retrieves some products of specific type and attributes.
I tried using the following SQL code in Toad and Oracle SQL Developer, however I am getting the following error:
PLS-00428: an INTO clause is expected in this SELECT statement
Here is the code
DECLARE
PROD_ROW_ID varchar(10) := NULL;
BEGIN
SELECT ROW_ID INTO VIS_ROW_ID FROM SIEBEL.S_PROD_INT WHERE PART_NUM = 'S0146404';
BEGIN
SELECT rtrim(VIS.SERIAL_NUM) || ',' || rtrim(PLANID.DESC_TEXT) || ',' ||
CASE
WHEN PLANID.HIGH = 'TEST123'
THEN
CASE
WHEN to_date(PROD.START_DATE) + 30 > sysdate
THEN 'Y'
ELSE 'N'
END
ELSE 'N'
END
|| ',' || 'GB' || ',' ||
rtrim(to_char(PROD.START_DATE, 'YYYY-MM-DD'))
FROM SIEBEL.S_LST_OF_VAL PLANID
INNER JOIN SIEBEL.S_PROD_INT PROD
ON PROD.PART_NUM = PLANID.VAL
INNER JOIN SIEBEL.S_ASSET NETFLIX
ON PROD.PROD_ID = PROD.ROW_ID
INNER JOIN SIEBEL.S_ASSET VIS
ON VIS.PROM_INTEG_ID = PROD.PROM_INTEG_ID
INNER JOIN SIEBEL.S_PROD_INT VISPROD
ON VIS.PROD_ID = VISPROD.ROW_ID
WHERE PLANID.TYPE = 'Test Plan'
AND PLANID.ACTIVE_FLG = 'Y'
AND VISPROD.PART_NUM = VIS_ROW_ID
AND PROD.STATUS_CD = 'Active'
AND VIS.SERIAL_NUM IS NOT NULL;
END;
END;
/
In PLSQL block, columns of select statements must be assigned to variables, which is not the case in SQL statements.
The second BEGIN's SQL statement doesn't have INTO clause and that caused the error.
DECLARE
PROD_ROW_ID VARCHAR (10) := NULL;
VIS_ROW_ID NUMBER;
DSC VARCHAR (512);
BEGIN
SELECT ROW_ID
INTO VIS_ROW_ID
FROM SIEBEL.S_PROD_INT
WHERE PART_NUM = 'S0146404';
BEGIN
SELECT RTRIM (VIS.SERIAL_NUM)
|| ','
|| RTRIM (PLANID.DESC_TEXT)
|| ','
|| CASE
WHEN PLANID.HIGH = 'TEST123'
THEN
CASE
WHEN TO_DATE (PROD.START_DATE) + 30 > SYSDATE
THEN
'Y'
ELSE
'N'
END
ELSE
'N'
END
|| ','
|| 'GB'
|| ','
|| RTRIM (TO_CHAR (PROD.START_DATE, 'YYYY-MM-DD'))
INTO DSC
FROM SIEBEL.S_LST_OF_VAL PLANID
INNER JOIN SIEBEL.S_PROD_INT PROD
ON PROD.PART_NUM = PLANID.VAL
INNER JOIN SIEBEL.S_ASSET NETFLIX
ON PROD.PROD_ID = PROD.ROW_ID
INNER JOIN SIEBEL.S_ASSET VIS
ON VIS.PROM_INTEG_ID = PROD.PROM_INTEG_ID
INNER JOIN SIEBEL.S_PROD_INT VISPROD
ON VIS.PROD_ID = VISPROD.ROW_ID
WHERE PLANID.TYPE = 'Test Plan'
AND PLANID.ACTIVE_FLG = 'Y'
AND VISPROD.PART_NUM = VIS_ROW_ID
AND PROD.STATUS_CD = 'Active'
AND VIS.SERIAL_NUM IS NOT NULL;
END;
END;
/
References
http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/static.htm#LNPLS00601
http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/selectinto_statement.htm#CJAJAAIG
http://pls-00428.ora-code.com/

Can someone explain this function in postresql?Here no join used then how using multiple tables?

CREATE OR REPLACE FUNCTION public.usp_get_licensedetail_report(
from_date date,
to_date date,
district_code1 character varying,
service_code1 integer,
license_grade1 character varying)
RETURNS TABLE(ack_no character varying, generate_list_no character varying, applicant_name_eng character varying, mobile_no character varying, firm_name_eng character varying, block_name character varying, district_name_eng character varying, license_no character varying, address text, service_name character varying, pincode character varying, license_valid_upto text)
LANGUAGE 'plpgsql'
COST 100
VOLATILE
ROWS 1000
AS $BODY$
DECLARE p_license_grade character varying(50);
begin
IF service_code1 in(1,2,25) THEN
IF(license_grade1 = 'A') THEN
p_license_grade = 'SG,C1,C2,C3';
ELSE
p_license_grade = license_grade1;
END IF;
ELSIF service_code1 in(6,7,28,10,11,30,14,15,27) THEN
IF(license_grade1 = 'A')THEN
p_license_grade = 'G1,G2';
ELSE
p_license_grade = license_grade1;
END IF;
ELSIF service_code1 in(18,19,29) THEN
IF(license_grade1 = 'A')THEN
p_license_grade = 'HV,EHV';
ELSE
p_license_grade = license_grade1;
END IF;
END IF;
RETURN query SELECT l.ack_no,l.generate_list_no,l.applicant_name_eng,l.mobile_no,l.firm_name_eng,b.block_name_eng as block_name,d.district_name_eng,s.license_no,
CASE when l.service_code=1 or l.service_code=2 or l.service_code=18 or l.service_code=19 then l.firm_add_houseno || ',' || l.firm_add_locality || ',' || l.firm_add_landmark || ',' || l.firm_add_cityvillage || ',' || l.firm_add_roadstreet || ',' || b.block_name_eng
ELSE l.address_building || ',' || l.address_cityvillage || ',' || l.address_locality || ',' || l.address_landmark || ',' || l.address_street || block_name_eng end address,m.service_name,
CASE when l.service_code=1 or l.service_code=2 or l.service_code=18 or l.service_code=19 then l.firm_pincode else l.pincode end pincode, to_char(s.license_valid_upto,'dd/MM/yyyy')as license_valid_upto
FROM application_license l, districts d, blocks b, mst_license s, mst_services m
WHERE case when l.service_code in(1,2,18,19) then l.firm_district else
l.district_code end = d.district_code and case when l.service_code in(1,2,18,19) then l.firm_taluk else l.taluk_code end= b.block_code and
l.ack_no=s.ack_no and m.service_code=l.service_code and cast(l.ack_dt as date) between from_date and to_date and l.service_code=service_code1 and
case when l.service_code in (19, 18, 1, 25, 29, 2) then l.firm_district else l.district_code end=district_code1 and
l.license_grade in ( SELECT lic_grade FROM REGEXP_SPLIT_TO_TABLE(p_license_grade, ',') as lic_grade ) and
l.generate_list_no <>'' order by generate_list_no,applicant_name_eng;
End;
$BODY$;
In the above code no join used like left join or inner join, then how used multiple tables and can someone guess what might be this REGEXP_SPLIT_TO_TABLE , there is no table named like this in tables, and views then what might be this....help me out guys
There are joins. They are just CROSS JOINs represented using the archaic comma notation.
The WHERE clause is just filtering these down to INNER JOINs, making the query all the more confusing.
The FROM clause should really be written using proper, explicit, standard, readable JOIN syntax:
FROM application_license l JOIN
districts d
ON (case when case when l.service_code in(1,2,18,19) then l.firm_district else l.district_code end) = d.district_code end JOIN
blocks b
ON (case when l.service_code in(1,2,18,19) then l.firm_taluk else l.taluk_code end) = b.block_code JOIN
mst_license s
ON l.ack_no = s.ack_no JOIN
mst_services m
ON m.service_code = l.service_code
(I wouldn't have CASE expressions in the ON or WHERE clauses.)
And the WHERE clause as:
WHERE cast(l.ack_dt as date) between from_date and to_date and
l.service_code = service_code1

Concat two subqueries into one column

I'm trying to concat two of my subqueries together.
Is there a trick to allow this to happen? It doesn't like my '||' after the first subquery
Select
pi.name || ',' || pi.lastName || ',' ||
(select middleName from MiddleNameTable where id = pi.id) MiddleName || ','
|| (select formalName from formalName where id = pi.id) formalName
from
mytable pi
I was dumb, I don't need the middleName Alias
I should just it without the middle alias since i'm joining all together anyway
Select pi.name || ',' || pi.lastName || ',' ||
(select middleName from MiddleNameTable where id = pi.id) || ','
||(select formalName from formalName where id = pi.id) as fullName
from mytable pi
Well, yes - you can do it if syntax is correct (sample data in lines #1 - 7; query begins at line #8):
SQL> with
2 mytable (id, name, lastname) as
3 (select 1, 'Brand', 'Erson' from dual),
4 middlenametable (id, middlename) as
5 (select 1, 'S' from dual),
6 formalname (id, formalname) as
7 (select 1, 'Overflow' from dual)
8 Select pi.name || ',' || pi.lastName || ',' ||
9 (select m.middleName from MiddleNameTable m where m.id = pi.id) || ','
10 ||(select f.formalName from formalName f where f.id = pi.id) as full_name
11 from mytable pi;
FULL_NAME
----------------------
Brand,Erson,S,Overflow
But, why would you do it that way? What's wrong with a simple join?
SQL> with
2 mytable (id, name, lastname) as
3 (select 1, 'Brand', 'Erson' from dual),
4 middlenametable (id, middlename) as
5 (select 1, 'S' from dual),
6 formalname (id, formalname) as
7 (select 1, 'Overflow' from dual)
8 select pi.name ||','|| pi.lastname ||','|| m.middlename ||','|| f.formalname as full_name
9 from mytable pi left join middlenametable m on m.id = pi.id
10 left join formalname f on f.id = pi.id;
FULL_NAME
----------------------
Brand,Erson,S,Overflow
SQL>
I show you my option, maybe you'll think why you need CONCAT function if it is only for two arguments...
I use it sometimes because is more clean.
The point is there, is CONCAT accept only two arguments, but you can do nested function.
SELECT
'PERICO' || ',' ||
'DE' || ',' ||
CONCAT(
(SELECT 'LOS' || ',' FROM DUAL),
(SELECT 'PALOTES' FROM DUAL))
FROM DUAL;
Or
SELECT
CONCAT(
CONCAT('PERICO' || ',',
'DE' || ',')
,
CONCAT(
(SELECT 'LOS' || ',' FROM DUAL),
(SELECT 'PALOTES' FROM DUAL))
)
FROM DUAL;
result --> PERICO,DE,LOS,PALOTES

Query optimization for multiple subsets

I have a "personnel basic information table", and it will be referenced by many "personnel subset tables" (there is no relationship between these subset tables, they are just a subset of the basic information of the personnel).
Now there is a requirement to merge the "personnel subset" into a column based on the person's foreign key and display it as a field of the "personnel basic information" (after the "personnel subset" is merged, each person record will only display one row) .
And the basic personnel information and personnel subset information will be passed into the query conditions, so these conditions are dynamically assembled.
It has been implemented now, but the problem is that the query needs to be optimized. Execution will be particularly slow once.
Database is oracle 11g.
Here is the sql generated by a certain query:
WITH in_hr_work_party_current AS (
SELECT
*
FROM
hr_work_party
WHERE
status = '1'
AND removaltime IS NULL
AND (
appointorg IS NOT NULL
OR NAME IS NOT NULL
)
),
qv_hr_work_party_current AS (
SELECT
empid,
listagg (
DECODE (
appointorg, NULL, '', 'oidstart' || appointorg || 'oidend-'
) || NVL (NAME, 'xx'),
','
) WITHIN GROUP (
ORDER BY
positiongrade ASC,
positionorder ASC,
appointtime ASC
) AS party_current_info
FROM
in_hr_work_party_current
GROUP BY
empid
),
in_position_postwork_main AS (
SELECT
po.empid,
working_coid_name AS post_main_coid_name,
working_poid_name AS post_main_poid_name,
officedepname AS post_main_officedepname,
post AS post_main_post,
posttype AS post_main_posttype,
workprof AS post_main_workprof,
starttime AS post_main_starttime,
FLOOR (
CEIL (
MONTHS_BETWEEN (
NVL (
TO_DATE (po.endtime, 'yyyy-MM-dd'),
SYSDATE
),
TO_DATE (po.starttime, 'yyyy-MM-dd')
)
) / 12
) || 'xx' AS post_main_years,
epo.entrytime AS post_main_entrytime,
ROW_NUMBER () OVER (
PARTITION BY po.empid
ORDER BY
po.opttime DESC
) AS rn
FROM
hr_work_postworkinfo po,
hr_work_empposition epo,
v_waf_ac_organ_base dep
WHERE
DEP.OID = PO.officedepid
AND epo.wepid = po.wepid
AND po.status = '1'
AND po.officetype = '00'
AND is_date (po.starttime) = 1
AND (
is_date (po.endtime) = 1
OR po.endtime IS NULL
)
AND epo.empsort = '01'
AND epo.subrelation = '01'
AND dep.orule LIKE '-xxxxx-%'
),
qv_position_postwork_main AS (
SELECT
*
FROM
in_position_postwork_main
WHERE
rn = 1
),
in_postworkinfo AS (
SELECT
po.*,
epo.entrytime,
epo.leavetime,
epo.empsort,
epo.empstatus,
epo.subrelation,
(
working_coid_name || DECODE (
working_poid_name, NULL, '', '-' || working_poid_name
) || DECODE (
officedepname, NULL, '', '-' || officedepname
)
) orgname,
FLOOR (
CEIL (
MONTHS_BETWEEN (
NVL (
TO_DATE (po.endtime, 'yyyy-MM-dd'),
SYSDATE
),
TO_DATE (po.starttime, 'yyyy-MM-dd')
)
) / 12
) postyear,
MOD (
CEIL (
MONTHS_BETWEEN (
NVL (
TO_DATE (po.endtime, 'yyyy-MM-dd'),
SYSDATE
),
TO_DATE (po.starttime, 'yyyy-MM-dd')
)
),
12
) postmonth,
ROW_NUMBER () OVER (
PARTITION BY po.empid
ORDER BY
po.starttime DESC
) rn,
COUNT (*) OVER (PARTITION BY po.empid) ct
FROM
hr_work_postworkinfo po,
HR_WORK_EMPPOSITION EPO,
V_WAF_AC_ORGAN_BASE DEP
WHERE
po.wepid = EPO.wepid
AND PO.officedepid = DEP.OID
AND is_date (starttime) = 1
AND (
is_date (endtime) = 1
OR endtime IS NULL
)
AND epo.empstatus != '05'
AND DEP.orule LIKE '-xxxxxx-%'
),
qv_hr_position_postwork AS (
SELECT
empid,
listagg (
'[' || starttime || 'xx' || NVL (endtime, 'xx') || 'xx' || orgname || 'xx' || NVL (post, 'xx') || 'xx' || NVL (posttype, 'xx') || 'xx' || DECODE (
postyear, 0, '', postyear || 'xx'
) || DECODE (
postmonth, 0, '', postmonth || 'xxxx'
) || 'xx' || NVL (postgradation, 'xx') || 'xxxx' || entrytime || 'xx' || empsort || 'xx' || empstatus || 'xx' || subrelation || ']',
','
) WITHIN GROUP (
ORDER BY
starttime DESC
) || (
CASE WHEN ct >= 5 THEN ',...' ELSE '' END
) work_info
FROM
in_postworkinfo
WHERE
rn <= 5
GROUP BY
empid,
ct
),
in_hr_emp_basicinfo AS (
SELECT
emp.*
FROM
hr_emp_basicinfo emp
),
qv_hr_emp_basicinfo_view AS (
SELECT
in_hr_emp_basicinfo.*
FROM
in_hr_emp_basicinfo
),
in_party AS (
SELECT
hr_work_party.NAME,
hr_work_party.empid,
hr_work_party.appointno,
hr_work_party.appointtime,
hr_work_party.removaltime,
hr_work_party.positiongrade,
NVL (
waf_ac_organ.NAME, hr_work_party.appointorg
) apporg,
ROW_NUMBER () OVER (
PARTITION BY hr_work_party.empid
ORDER BY
appointtime DESC,
removaltime DESC,
positiongrade ASC,
positionorder ASC
) rn,
COUNT (*) OVER (PARTITION BY hr_work_party.empid) ct
FROM
hr_work_party,
waf_ac_organ
WHERE
hr_work_party.status = '1'
AND hr_work_party.appointorg = waf_ac_organ.OID (+)
AND (
hr_work_party.appointorg IS NOT NULL
OR hr_work_party.NAME IS NOT NULL
)
),
qv_hr_work_party AS (
SELECT
empid,
listagg (
'[' || appointtime || 'xx' || NVL (removaltime, 'xx') || ' ' || apporg || '-' || NVL (NAME, 'xx') || DECODE (
NVL (appointno, 'xx'),
'xx',
'',
' - ' || appointno
) || ']',
','
) WITHIN GROUP (
ORDER BY
rn ASC
) || (
CASE WHEN ct > 10 THEN ',...' ELSE '' END
) app_party,
listagg (
'[' || appointtime || 'xx' || NVL (removaltime, 'xx') || ' ' || apporg || '-' || NVL (NAME, 'xx') || DECODE (
positiongrade, NULL, '', '(' || positiongrade || ')'
) || ']',
','
) WITHIN GROUP (
ORDER BY
rn ASC
) || (
CASE WHEN ct > 10 THEN ',...' ELSE '' END
) AS party_grade
FROM
in_party
WHERE
rn <= 10
GROUP BY
empid,
ct
)
SELECT
*
FROM
(
SELECT
dumb_table.*,
ROWNUM dumb_rn
FROM
(
SELECT
qv_hr_work_party_current.party_current_info,
qv_position_postwork_main.post_main_coid_name,
qv_position_postwork_main.post_main_poid_name,
qv_position_postwork_main.post_main_officedepname,
qv_position_postwork_main.post_main_years,
qv_hr_emp_basicinfo_view.age,
qv_hr_emp_basicinfo_view.NAME,
qv_hr_position_postwork.work_info
FROM
qv_hr_work_party_current,
qv_position_postwork_main,
qv_hr_work_party,
qv_hr_position_postwork,
qv_hr_emp_basicinfo_view
WHERE
1 = 1
AND qv_hr_emp_basicinfo_view.empid = qv_hr_work_party_current.empid(+)
AND qv_hr_emp_basicinfo_view.empid = qv_position_postwork_main.empid(+)
AND qv_hr_emp_basicinfo_view.empid = qv_hr_work_party.empid (+)
AND qv_hr_emp_basicinfo_view.empid = qv_hr_position_postwork.empid
AND qv_hr_emp_basicinfo_view.empid IS NOT NULL
ORDER BY
qv_hr_emp_basicinfo_view.sno
) dumb_table
)
WHERE
dumb_rn <= 30
AND dumb_rn > 0
enter image description here