Case statement in a multiple query
Hi everyone,
This is my first time to use case statement in a multiple query. I have tried to implement it but i got no luck.. Please see below
set define off
SELECT g.GROUP_NAME as Market
,t.NAME as "Template Name"
,t.TEMPLATE_ID as "Template ID"
,(SELECT created
FROM material
where template_id = t.template_id) as "Date Created"
*,(SELECT DESTINATION_FOLDER_ID,*
CASE DESTINATION_FOLDER_ID
WHEN NULL THEN 'Upload'
ELSE 'HQ'
END
from log_material_copy
where destination_material_id in (select material_id
from material
where template_id = t.template_id ))as "Origin"
,(select material_id
from log_material_copy
where destination_material_id in (select material_id
from material
where template_id = t.template_id)) as "HQ/Upload ID"
,(SELECT COUNT (mse.ID)
FROM MATERIAL_SEND_EVENT mse, material m, creative c
WHERE mse.MATERIAL_ID = m.MATERIAL_ID
AND mse.MATERIAL_TYPE_ID = m.MATERIAL_TYPE_ID
AND m.ASSET_ID = c.id
AND c.TEMPLATE_ID = t.TEMPLATE_ID) as Sent
,(SELECT COUNT (de.ID)
FROM download_event de, material m, creative c
WHERE de.MATERIAL_ID = m.MATERIAL_ID
AND de.MATERIAL_TYPE_ID = m.MATERIAL_TYPE_ID
AND m.ASSET_ID = c.id
AND c.TEMPLATE_ID = t.TEMPLATE_ID) as Download
,(SELECT 'https://main.test.com/bm/servlet/' || 'UArchiveServlet?action=materialInfo&materialId=' || DESTINATION_MATERIAL_ID || '&materialFolderId=' || DESTINATION_FOLDER_ID
from log_material_copy
where destination_material_id in (select material_id
from material
where template_id = t.template_id)) as "URL to template on MPC layer"
--, t.AVAILABLE_FOR_TRANSFER as "Available for transfer"
FROM template t, layout l, groups g
WHERE t.LAYOUT_ID = l.LAYOUT_ID
AND l.ORGANIZATION_ID = g.IP_GROUPID
AND g.IP_GROUPID in ( 1089, 903, 323, 30, 96, 80, 544, 1169, 584, 785, 827, 31, 10, 503, 1025 )
ORDER BY g.GROUP_NAME ASC;
The one in bold is my case statement.. Please let me know what is wrong with this.
Regards,
Jas
I think you're getting the idea, but:
You're still selecting 2 columns in the (scalar) subquery. Did you read the link I posted for you?
"a) scalar subqueries - *a single row, single column query that you use in place of a "column"*, it looks like a column or function."
You must move that query outside, join to template.
Something like:
NOT TESTED FOR OBVIOUS REASONS SO YOU'LL PROBABLY NEED TO TWEAK IT A BIT
select g.group_name as market,
t.name as "Template Name",
t.template_id as "Template ID",
m.created as "Date Created",
lmc.destination_folder_id,
case lmc.destination_folder_id
when null then 'Upload'
else 'HQ'
end as "Origin"
(select material_id
from log_material_copy
where destination_material_id in
(select material_id
from material
where template_id = t.template_id)) as "HQ/Upload ID"
(select count(mse.id)
from material_send_event mse, material m, creative c
where mse.material_id = m.material_id
and mse.material_type_id = m.material_type_id
and m.asset_id = c.id
and c.template_id = t.template_id) as sent
(select count(de.id)
from download_event de, material m, creative c
where de.material_id = m.material_id
and de.material_type_id = m.material_type_id
and m.asset_id = c.id
and c.template_id = t.template_id) as download
(select 'https://main.test.com/bm/servlet/' ||
'UArchiveServlet?action=materialInfo&materialId=' ||
destination_material_id || '&materialFolderId=' ||
destination_folder_id
from log_material_copy
where destination_material_id in
(select material_id
from material
where template_id = t.template_id)) as "URL to template on MPC layer"
--, t.AVAILABLE_FOR_TRANSFER as "Available for transfer"
from template t
, layout l
, groups group by
, MATERIAL M
, LOG_MATERIAL_COPY LMC
where t.layout_id = l.layout_id
and l.organization_id = g.ip_groupid
and M.TEMPLATE_ID = t.template_id
and LMC.destination_material_id in ( select material_id
from material
where template_id = t.template_id
and g.ip_groupid in (1089,
903,
323,
30,
96,
80,
544,
1169,
584,
785,
827,
31,
10,
503,
1025)
order by g.group_name asc;
Similar Messages
-
Error in CASE statement used in Reports query
My query has lots of selection criteria. I'm trying to use CASE for that.
This is a part of my query with parameters:
AND ad.location IN (CASE &p_location
WHEN 1 THEN
(SELECT TRIM(code_nbr) FROM code_detail
WHERE code_nbr like 'LC%')
WHEN 2 THEN -- then the user can type in more than 1 locations
&p_location1|| p_location2
END)
AND --- there are more conditions after this
The query fails that invalid relational operator in this CASE statement.
Any suggestions...is there any other way to do it...Thanks in advanceI would recommend the use of lexical parameters in the before report trigger
Set up a parameter SQL_WHERE - text, 4000 chars.
In your datamodel get rid of the case statement and just put &SQL_WHERE
In the before report trigger modify your case statement to be something like:
&SQL_WHERE:=' and ad.location in ';
case when :p_location = 1 then
&SQL_WHERE:=&SQL_WHERE||' select trim(code_nbr) from code_detail etc';
end
This way you can debug and test your case statement in the pl/sql environment. Plus the query should run faster as you are moving the complex where statement into a separate part of the report -
How to optimized Multiple CASE statement
Hi..
Somebody please help me out to use most optimized Multiple CASE statement in below example..
Select
CAST(COUNT(DISTINCT(CASE WHEN period_id <350 THEN period_id END)) AS DECIMAL(30,0)) AS Column1, CAST(COUNT(DISTINCT(CASE WHEN period_id >=351 AND period_id<=375 THEN period_id END))
AS DECIMAL(30,0)) AS Column2,
CAST(COUNT(DISTINCT(CASE WHEN period_id >=376 AND period_id<=400 THEN period_id END)) AS DECIMAL(30,0)) AS Column3,
CAST(COUNT(DISTINCT(CASE WHEN period_id >=401 AND period_id<=450 THEN period_id END)) AS DECIMAL(30,0)) AS Column4,
CAST(COUNT(DISTINCT(CASE WHEN period_id >=451 AND period_id<=575 THEN period_id END)) AS DECIMAL(30,0)) AS Column5,
CAST(COUNT(DISTINCT(CASE WHEN pmc ='UVS' THEN pmc END)) AS DECIMAL(30,0)) AS Column6,
CAST(COUNT(DISTINCT(CASE WHEN pmc ='VAL' THEN pmc END)) AS DECIMAL(30,0)) AS Column7
FROM [Table1]
Thanks in Advance,
Deepak GoyalHow is an index going to help in a query with no where clause or join?
Rhetorical question but it brings to light the problem. This query is going to produce a table/index scan precisely because there's no where clause.
You need to change the way you are thinking about the problem. The problem isn't that you need a better performing case statement, the problem is that you need to minimize the number of records you ask SQL Server to read from disk. You do that
by using a where clause or by joining to a smaller table.
Here's one option which should give you the desired results while performing much better:
create index idx_period_id on Table1 (period_id)
create index idx_pmc on Table1 (pmc) include (period_id)
select (select count(distinct period_id) from Table1 where period_id < 350) as Column1,
(select count(distinct period_id) from Table1 where period_id between 351 and 375) as Column2,
(select count(distinct period_id) from Table1 where period_id between 376 and 400) as Column3,
(select count(distinct period_id) from Table1 where period_id between 401 and 450) as Column4,
(select count(distinct period_id) from Table1 where period_id between 451 and 575) as Column5,
(select count(distinct period_id) from Table1 where pmc = 'UVS') as Column6,
(select count(distinct period_id) from Table1 where pmc = 'VAL') as Column7 -
Help With A Case Statement With Multiple Variables
I apologize if this is the incorrect Forum for this type of question, but it was the closest one that I could find. I'm pretty new with SQL and am stuck on this issue. I have roughly 26 dates that I need to compare to one another. Each date is tied to a step code. I also have a Stop value that is tied directly to the "max date" of the step codes. So, I need to compare 30 dates against one another to 1st - ID the max date; 2nd - ID if the Stop value is correct; 3rd - if the stop value is incorrect, identify what the correct value would be.
At first, this seemed like it wouldn't be that hard. I wrote a query that found the max date for each step code. Then I realized that multiple step codes could have the same date. So, I tried using this case statement, but I did not get the expected results. Is there a more efficient way of getting what I need? This code seems like it's not necessary and probably the source of my issue.
CASE
WHEN FS25.ACTUAL_COMPLETION_DATE > FS.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS1.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS2.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS3.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS4.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS5.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS6.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS7.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS8.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS9.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS10.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS11.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS12.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS13.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS14.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS15.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS16.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS17.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS18.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS19.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS20.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS21.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS22.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS23.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS24.ACTUAL_COMPLETION_DATE AND L.FORECLOSURE_STOP_CODE <= '8' THEN '9'
ELSE 'UH OH'
END AS "CHANGE FC STOP TO"
Any assistance is appreciated!I think Igor pointed out a working solution before.
Applying it at your examples (you missed the operator after STOP_CODE, I assume it =):
CASE
WHEN FS25 = GREATEST(FS25, FS24, FS23) AND STOP_CODE = '9' THEN '9'
ELSE 'UH OH'
END AS 'CHANGE STOP CODE TO'
{code}
Be careful at the second example. You are checking:
{code:sql}
FS25 > FS24 OR FS25 IS NOT NULL AND FS24 IS NULL AND FS25 > FS23
OR
FS25 IS NOT NULL AND FS23 IS NULL AND STOP_CODE = '9'
{code}
Remember that AND has higher priority among operators than OR so if FS25 is greater than FS24 and FS23 the condition will be true even if STOP_CODE is not equal 9.
Regards.
Al -
Help: How to include logic in multiple CASE statements.
Hello Folks,
I have this Query where am converting from Access to Oracle. Am little confused on how to write multiple CASE statements.Apprecitae your help.
round(Sum(IIf(recovery_flg = 'NONCASH FEE RECEIVED',
IIf(feepaid < FEE, FEE - FEEPAID, FEE),
0)),
2)*(1) you can nest CASE* - CASE in a CASE
CASE WHEN ecovery_flg = 'NONCASH FEE RECEIVED' THEN
CASE WHEN feepaid < FEE THEN ....
END
ENDor
*(2) you can try to rewrite it as one CASE*
CASE WHEN ecovery_flg = 'NONCASH FEE RECEIVED' AND feepaid < FEE THEN...
WHEN ecovery_flg = 'NONCASH FEE RECEIVED' AND feepaid >= FEE THEN ...
END -
Query Tuning - using CASE statement in the WHERE clause
Hi All,
My query has been modified to use a CASE statement in the WHERE clause to consider data from certain columns based on a parameter value. This modified query is doing a full table scan and running endlessly. Please suggest what may be done to improve its performance:
Query:
SELECT LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))) AS summary_date,
os.acctnum,
os.avieworigin_refid,
COUNT(1) cnt_articleview,
SUM(NVL(autocompletedterm,0)) cnt_autocompletedterm
FROM TABLE1 os
WHERE os.acctnum IS NOT NULL
AND os.avieworigin_refid IS NOT NULL
AND os.requestdatetime IS NOT NULL
AND UPPER(os.success_ind) = 'S'
AND CASE WHEN
Param_ValueToCheck = 'FULL' AND get_date_timestamp(os.requestdatetime) BETWEEN
TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
THEN 1
WHEN
Param_ValueToCheck = 'INCR' AND os.entry_createddate BETWEEN
TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
THEN 1
END = 1
AND CASE WHEN
Param_ValueToCheck = 'FULL' AND os.entry_CreatedDate BETWEEN
TO_DATE('01-APR-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
TO_DATE('07-JUN-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
THEN 1
WHEN
Param_ValueToCheck = 'INCR' THEN 1
END = 1
GROUP BY LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))), os.acctnum,os.avieworigin_refid;Table Description:
(Number of rows : approx > 600,000,000)
Name Null Type
ARTICLEID NOT NULL NUMBER(20)
USERKEY NUMBER(10)
AVIEWORIGIN_REFID VARCHAR2(10)
SUCCESS_IND VARCHAR2(2)
ENTRY_CREATEDDATE DATE
CREATED_BY VARCHAR2(10)
FILENUMBER NUMBER(10)
LINENUMBER NUMBER(10)
ACCTNUM VARCHAR2(10)
AUTOCOMPLETEDTERM NUMBER(2)
REQUESTDATETIME VARCHAR2(19)Explain Plan
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
Plan hash value: 2224314832
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
| 0 | SELECT STATEMENT | | 590 | 33040 | 2501K (1)| 08:20:15 | | |
| 1 | HASH GROUP BY | | 590 | 33040 | 2501K (1)| 08:20:15 | | |
| 2 | PARTITION RANGE ALL| | 590 | 33040 | 2501K (1)| 08:20:15 | 1 |1048575|
|* 3 | TABLE ACCESS FULL | TABLE1 | 590 | 33040 | 2501K (1)| 08:20:15 | 1 |1048575|
PLAN_TABLE_OUTPUT
Predicate Information (identified by operation id):
3 - filter(UPPER("OS"."SUCCESS_IND")='S' AND CASE WHEN ('FULL'='FULL' AND
"OS"."ENTRY_CREATEDDATE">=TO_DATE(' 2011-04-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
"OS"."ENTRY_CREATEDDATE"<=TO_DATE(' 2011-06-07 00:00:00', 'syyyy-mm-dd hh24:mi:ss')) THEN 1 WHEN
'FULL'='INCR' THEN 1 END =1 AND "OS"."REQUESTDATETIME" IS NOT NULL AND CASE WHEN ('FULL'='FULL'
AND "ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")>=TO_DATE(' 2011-05-01 00:00:00', 'syyyy-mm-dd
hh24:mi:ss') AND "ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")<=TO_DATE(' 2011-05-31 00:00:00',
'syyyy-mm-dd hh24:mi:ss')) THEN 1 WHEN ('FULL'='INCR' AND "OS"."ENTRY_CREATEDDATE">=TO_DATE('
2011-05-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "OS"."ENTRY_CREATEDDATE"<=TO_DATE('
PLAN_TABLE_OUTPUT
2011-05-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss')) THEN 1 END =1 AND "OS"."ACCTNUM" IS NOT NULL AND
"OS"."AVIEWORIGIN_REFID" IS NOT NULL)Edited by: Chaitanya on Jun 9, 2011 2:44 AM
Edited by: Chaitanya on Jun 9, 2011 2:47 AMHi Dom,
Modified Query:
SELECT LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))) AS summary_date,
os.acctnum,
os.avieworigin_refid,
COUNT(1) cnt_articleview,
SUM(NVL(autocompletedterm,0)) cnt_autocompletedterm
FROM TABLE1 os
WHERE os.acctnum IS NOT NULL
AND os.avieworigin_refid IS NOT NULL
AND os.requestdatetime IS NOT NULL
AND UPPER(os.success_ind) = 'S'
AND (('FULL' = 'FULL'
AND (get_date_timestamp(os.requestdatetime) BETWEEN TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
AND TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
AND os.entry_CreatedDate BETWEEN TO_DATE('01-APR-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
AND TO_DATE('07-JUN-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
OR ('FULL' = 'INCR'
AND os.entry_createddate BETWEEN TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
AND TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') ))
GROUP BY LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))), os.acctnum,os.avieworigin_refid;Execute Plan:
PLAN_TABLE_OUTPUT
Plan hash value: 3615447714
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | Pstart| Pstop |
| 0 | SELECT STATEMENT | | 25125 | 1374K| | 407K (1)| 01:21:36 | | |
| 1 | HASH GROUP BY | | 25125 | 1374K| 3768K| 407K (1)| 01:21:36 | | |
| 2 | PARTITION RANGE ITERATOR| | 25125 | 1374K| | 407K (1)| 01:21:32 | 29 | 31 |
|* 3 | TABLE ACCESS FULL | TABLE1 | 25125 | 1374K| | 407K (1)| 01:21:32 | 29 | 31 |
PLAN_TABLE_OUTPUT
Predicate Information (identified by operation id):
3 - filter("OS"."ENTRY_CREATEDDATE">=TO_DATE(' 2011-04-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
UPPER("OS"."SUCCESS_IND")='S' AND "OS"."REQUESTDATETIME" IS NOT NULL AND
"ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")>=TO_DATE(' 2011-05-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
"ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")<=TO_DATE(' 2011-05-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
"OS"."ACCTNUM" IS NOT NULL AND "OS"."AVIEWORIGIN_REFID" IS NOT NULL AND "OS"."ENTRY_CREATEDDATE"<=TO_DATE('
2011-06-07 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))Edited by: Chaitanya on Jun 9, 2011 4:51 AM -
Need help in this sql query to use Case Statement
hi All,
I have the below query -
SELECT DISTINCT OFFC.PROV_ID
,OFFC.WK_DAY
,CASE
WHEN OFFC.WK_DAY ='MONDAY' THEN 1
WHEN OFFC.WK_DAY ='TUESDAY' THEN 2
WHEN OFFC.WK_DAY ='WEDNESDAY' THEN 3
WHEN OFFC.WK_DAY ='THURSDAY' THEN 4
WHEN OFFC.WK_DAY ='FRIDAY' THEN 5
WHEN OFFC.WK_DAY ='SATURDAY' THEN 6
WHEN OFFC.WK_DAY ='SUNDAY' THEN 7
END AS DOW
,OFFC.OFFC_OPENG_TIME
,OFFC.OFFC_CLSNG_TIME
FROM GGDD.PROV_OFFC_HR OFFC
WHERE OFFC.PROV_ID='0000600'
WITH UR;
this query is bringing results in 6 differnt rows with opening and closing time for each day separately. I want to generate the data in one row with each day having opening and closing time, so for 7 days, total 14 columns with opening and closing time. But i am not able to do that using case statement.
can somebody help me in achieving that.
thanks,
iamhereHi,
Welcome to the forum!
That's called a Pivot .
Instead of having 1CASE expression, have 14, one for the opening and one for the closing time each day, and do GROUP BY to combine them onto one row.
SELECT OFFC.PROV_ID
, MIN (CASE WHEN OFFC.WK_DAY ='MONDAY' THEN OFFC.OFFC_OPENG_TIME END) AS mon_opn
, MIN (CASE WHEN OFFC.WK_DAY ='MONDAY' THEN OFFC.OFFC_CLSNG_TIME END) AS mon_cls
, MIN (CASE WHEN OFFC.WK_DAY ='TUESDAY' THEN OFFC.OFFC_OPENG_TIME END) AS tue_opn
, MIN (CASE WHEN OFFC.WK_DAY ='TUESDAY' THEN OFFC.OFFC_CLSNG_TIME END) AS tue_cls
FROM GGDD.PROV_OFFC_HR OFFC
WHERE OFFC.PROV_ID = '0000600'
GROUP BY offc.prov_id
;This assumes there is (at most) only one row in the table for each distinct prov_id and weekday. If not, what do you want to do? Post a little sample data (CREATE TABLE and INSERT statements) and the results you want from that data.
The staement above works in Oracle 8.1 and up, but there's a better way (SELECT ... PIVOT) available in Oracle 11. What version are you using? (It's always a good idea to include this when you post a question.)
Edited by: Frank Kulash on Jan 6, 2011 8:22 PM -
Need help in this query using Case Statement
I have the following query which is currently existing and I am adding few more conditions based on the new requirements which is based on a particular flag set as 1 or 0.
If it is set to 1 then I should use the old query as it is and if it is set to 0 then I should add the new conditions.
Basically when the flag is set to 0, I shouldnt be including some of the records that already exists and should include only new records. This is based on the plan_type_ids in (1,2,3,4).
Hence I am using the Case statement to check if the plan_type_id is in (1,2) then do a set of not exists and if the plan_type_id in (3,4) then do set of not exists.
But when I run this query it is giving me error. What am I doing wrong?
Is there any other simple way to combine all the not exists for all of those select statements as given after the line ------------------------- into a single one?
What am I doing wrong?
I tried putting the NOT EXists before the case too but that too didnt work.
Please help. Appreciate it.
Thank you in advance.
SELECT
ee.employee_id
,'WELCOMEMSG'
,DECODE( me.member_enrollment_id
,first_enr.enrollment_id
,20
,23
) status_id
,me.member_enrollment_id
,wk.welcome_msg_id
FROM wk
,employees ee
,MEMBER_ENROLLMENTS me
,plans pl
,( SELECT employee_id
,plan_type_id
,start_date
,plan_id
,MIN(MEMBER_ENROLLMENT_ID) member_enrollment_id
FROM ( SELECT me.employee_id
,DECODE(pl.plan_type_id,1,2,pl.plan_type_id) plan_type_id
,pl.start_date
,wk.plan_id
,me.member_enrollment_id
FROM wk
,PLANS pl
,MEMBER_ENROLLMENTS me
WHERE wk.done_by = nvl('TEST' ,wk.done_by)
AND wk.welcome_msg_name <> 'NONE'
AND pl.employer_id = wk.employer_id
AND wk.employer_id = 5
AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
AND pl.plan_id = NVL(wk.plan_id,pl.plan_id)
AND me.plan_id = pl.plan_id
AND me.coverage_effective_date <> NVL(me.coverage_end_Date, me.coverage_effective_date + 1)
AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date, me.coverage_effective_date + 1)
MINUS
SELECT me.employee_id
,DECODE(pl.plan_type_id,1,2,pl.plan_type_id) plan_type_id
,pl.start_date
,NULL plan_id
,me.member_enrollment_id
FROM wk
,PLANS pl
,MEMBER_ENROLLMENTS me
WHERE wk.done_by = nvl(NULL,wk.done_by)
AND wk.welcome_msg_name <> 'NONE'
AND pl.employer_id = wk.employer_id
AND wk.employer_id = 5
AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
AND pl.plan_id = wk.plan_id
AND me.plan_id = pl.plan_id
AND me.coverage_effective_date <> NVL(me.coverage_end_Date, me.coverage_effective_date + 1)
AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date, me.coverage_effective_date + 1)
WHERE employee_id = 100
GROUP BY employee_id ,plan_type_id,start_date ,plan_id
)first_enr
,MEMBER_EVENTS mv
WHERE wk.done_by = nvl(NULL,wk.done_by)
AND wk.employer_id = ee.employer_id
AND ee.employee_id = me.employee_id
AND ee.employee_id = 100
AND me.plan_id = pl.plan_id
AND me.coverage_effective_date <> NVL(me.coverage_end_Date, me.coverage_effective_date + 1)
AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date, me.coverage_effective_date + 1)
AND is_expired(me.employee_id,me.plan_id) = 'Y'
AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
AND pl.plan_id=nvl(wk.plan_id,pl.plan_id)
AND me.employee_id = first_enr.employee_id
AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = first_enr.plan_type_id
AND pl.start_date = first_enr.start_date
AND me.member_enrollment_id = mv.member_enrollment_id (+)
AND 'WELCOMEMSG' = mv.event_name(+)
AND mv.member_enrollment_id IS NULL
AND wk.welcome_msg_name <> 'NONE'
AND NVL(first_enr.plan_id,0) = NVL( wk.plan_id,0)
AND (FN_get_all_participant(wk.employer_id) = 1
OR
(FN_get_all_participant(wk.employer_id) = 0
AND (CASE WHEN pl.plan_type_id IN (1,2)
THEN NOT EXISTS (SELECT 'X'
FROM member_accounts ma
member_enrollments men3
plans pl3
plan_types pt3
WHERE ma.member_account_id = men3.member_account_id
AND ma.employee_id = me.employee_id
AND ma.plan_id = pl3.plan_id
AND pl3.start_date < pl.START_DATE
AND TRUNC(men3.coverage_effective_date) <> TRUNC(NVL(men3.coverage_end_date, men3.coverage_effective_date + 1 ))
AND pl3.plan_type_id = pt3.plan_type_id
AND pt3.plan_type_id in (1, 2)
UNION
(SELECT 'X'
FROM member_accounts ma
member_enrollments men3
plans pl3
plan_types pt3
WHERE ma.member_account_id = men3.member_account_id
AND ma.employee_id = me.employee_id
AND ma.plan_id = pl3.plan_id
AND pl3.start_date = (pl.start_date - 365)
\ AND TRUNC(men3.coverage_effective_date) <> TRUNC(NVL(men3.coverage_end_date, men3.coverage_effective_date + 1 ))
AND pl3.plan_type_id = pt3.plan_type_id
AND pt3.plan_type_id = wk.plan_type_id
UNION
(SELECT 'X'
FROM member_accounts ma
member_enrollments men3
plans pl3
plan_types pt3
WHERE ma.member_account_id = men3.member_account_id
AND ma.employee_id = men2.employee_id
AND ma.plan_id = pl3.plan_id
AND pl3.start_date < pl2.START_DATE -- '01-Jan-2011'
AND TRUNC(men3.coverage_effective_date) <> TRUNC(NVL(men3.coverage_end_date, men3.coverage_effective_date + 1 ))
AND pl3.plan_type_id = pt3.plan_type_id
AND pt3.plan_type_id = 2
UNION
(SELECT 'X'
FROM member_accounts ma
member_enrollments men3
plans pl3
plan_types pt3
WHERE ma.member_account_id = men3.member_account_id
AND ma.employee_id = men2.employee_id
AND ma.plan_id = pl3.plan_id
AND pl3.start_date < pl2.START_DATE -- '01-Jan-2011'
AND TRUNC(men3.coverage_effective_date) <> TRUNC(NVL(men3.coverage_end_date, men3.coverage_effective_date + 1 ))
AND pl3.plan_type_id = pt3.plan_type_id
AND pt3.plan_type_id = 1
WHEN pl.plan_type_id IN (3, 4)
THEN NOT EXISTS (SELECT 'X'
FROM member_accounts ma
member_enrollments men3
plans pl3
plan_types pt3
WHERE ma.member_account_id = men3.member_account_id
AND ma.employee_id = men2.employee_id
AND nvl(ma.account_end_date, sysdate) <= trunc(sysdate)
AND ma.plan_id = pl3.plan_id
AND pl3.start_date <= pl2.START_DATE
AND TRUNC(men3.coverage_effective_date) <> TRUNC(NVL(men3.coverage_end_date, men3.coverage_effective_date + 1 ))
AND pl3.plan_type_id = pt3.plan_type_id
AND pt3.plan_type_id in (3, 4)
END
AND (CASE WHEN pl.plan_type_id IN (1,2)
ERROR at line 89:
ORA-00936: missing expressionMaybe
SELECT ee.employee_id,
'WELCOMEMSG',
DECODE(me.member_enrollment_id,first_enr.enrollment_id,20,23) status_id,
me.member_enrollment_id,
wk.welcome_msg_id
FROM wk,
employees ee,
MEMBER_ENROLLMENTS me,
plans pl,
(SELECT employee_id,
plan_type_id,
start_date,
plan_id,
MIN(MEMBER_ENROLLMENT_ID) member_enrollment_id
FROM (SELECT me.employee_id,
DECODE(pl.plan_type_id,1,2,pl.plan_type_id) plan_type_id,
pl.start_date,
wk.plan_id,
me.member_enrollment_id
FROM wk,
PLANS pl,
MEMBER_ENROLLMENTS me
WHERE wk.done_by = nvl('TEST',wk.done_by) /* same as wk.done_by = 'TEST' */
AND wk.welcome_msg_name 'NONE'
AND pl.employer_id = wk.employer_id
AND wk.employer_id = 5
AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
AND pl.plan_id = NVL(wk.plan_id,pl.plan_id)
AND me.plan_id = pl.plan_id
AND me.coverage_effective_date != NVL(me.coverage_end_Date,me.coverage_effective_date + 1)
AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date,me.coverage_effective_date + 1)
MINUS
SELECT me.employee_id,
DECODE(pl.plan_type_id,1,2,pl.plan_type_id) plan_type_id,
pl.start_date,
NULL plan_id,
me.member_enrollment_id
FROM wk,
PLANS pl,
MEMBER_ENROLLMENTS me
WHERE wk.done_by = nvl(NULL,wk.done_by) /* same as 1 = 1 */
AND wk.welcome_msg_name 'NONE'
AND pl.employer_id = wk.employer_id
AND wk.employer_id = 5
AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
AND pl.plan_id = wk.plan_id
AND me.plan_id = pl.plan_id
AND me.coverage_effective_date NVL(me.coverage_end_Date,me.coverage_effective_date + 1)
AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date, me.coverage_effective_date + 1)
WHERE employee_id = 100
GROUP BY employee_id,
plan_type_id,
start_date,
plan_id
) first_enr,
MEMBER_EVENTS mv
WHERE wk.done_by = nvl(NULL,wk.done_by)
AND wk.employer_id = ee.employer_id
AND ee.employee_id = me.employee_id
AND ee.employee_id = 100
AND me.plan_id = pl.plan_id
AND me.coverage_effective_date != NVL(me.coverage_end_Date,me.coverage_effective_date + 1)
AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date, me.coverage_effective_date + 1)
AND is_expired(me.employee_id,me.plan_id) = 'Y'
AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
AND pl.plan_id = nvl(wk.plan_id,pl.plan_id)
AND me.employee_id = first_enr.employee_id
AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = first_enr.plan_type_id
AND pl.start_date = first_enr.start_date
AND me.member_enrollment_id = mv.member_enrollment_id(+)
AND 'WELCOMEMSG' = mv.event_name(+)
AND mv.member_enrollment_id IS NULL
AND wk.welcome_msg_name != 'NONE'
AND NVL(first_enr.plan_id,0) = NVL(wk.plan_id,0)
AND (
FN_get_all_participant(wk.employer_id) = 1
OR
(FN_get_all_participant(wk.employer_id) = 0
AND NOT EXISTS(SELECT 'X'
FROM member_accounts ma,
member_enrollments men3,
plans pl3,
plan_types pt3
WHERE ma.member_account_id = men3.member_account_id
AND ma.employee_id = me.employee_id
AND ma.plan_id = pl3.plan_id
AND pl3.start_date < pl.START_DATE
AND TRUNC(men3.coverage_effective_date) != TRUNC(NVL(men3.coverage_end_date,men3.coverage_effective_date + 1))
AND pl3.plan_type_id = pt3.plan_type_id
AND pt3.plan_type_id in (1,2)
and pl.plan_type_id IN (1,2)
UNION
SELECT 'X'
FROM member_accounts ma,
member_enrollments men3,
plans pl3,
plan_types pt3
WHERE ma.member_account_id = men3.member_account_id
AND ma.employee_id = me.employee_id
AND ma.plan_id = pl3.plan_id
AND pl3.start_date = (pl.start_date - 365)
AND TRUNC(men3.coverage_effective_date) != TRUNC(NVL(men3.coverage_end_date,men3.coverage_effective_date + 1))
AND pl3.plan_type_id = pt3.plan_type_id
AND pt3.plan_type_id = wk.plan_type_id
and pl.plan_type_id IN (1,2)
UNION
SELECT 'X'
FROM member_accounts ma,
member_enrollments men3,
plans pl3,
plan_types pt3
WHERE ma.member_account_id = men3.member_account_id
AND ma.employee_id = men2.employee_id
AND ma.plan_id = pl3.plan_id
AND pl3.start_date < pl2.START_DATE -- '01-Jan-2011'
AND TRUNC(men3.coverage_effective_date) != TRUNC(NVL(men3.coverage_end_date,men3.coverage_effective_date + 1))
AND pl3.plan_type_id = pt3.plan_type_id
AND pt3.plan_type_id = (1,2)
and pl.plan_type_id IN (1,2)
UNION
SELECT 'X'
FROM member_accounts ma,
member_enrollments men3,
plans pl3,
plan_types pt3
WHERE ma.member_account_id = men3.member_account_id
AND ma.employee_id = men2.employee_id
AND trunc(nvl(ma.account_end_date,sysdate)) <= trunc(sysdate)
AND ma.plan_id = pl3.plan_id
AND pl3.start_date <= pl2.START_DATE
AND TRUNC(men3.coverage_effective_date) != TRUNC(NVL(men3.coverage_end_date,men3.coverage_effective_date + 1))
AND pl3.plan_type_id = pt3.plan_type_id
AND pt3.plan_type_id in (3,4)
and pl.plan_type_id IN (3,4)
)Regards
Etbin -
Hi, I have two issues, here is my initial code:
select
cc.name_id_no
,cc.discover_date
,cc.cla_case_no
,max(rl.year_of_incident)Non_Loss_Past_5
,rl.timestamp
from cla_case cc, rbn_loss rl
where cc.name_id_no = rl.customer_no
and rl.year_of_incident < trunc(cc.discover_date)
and rl.type_of_loss < 1000
and rl.timestamp < trunc(cc.discover_date)
and (cc.question_class = 20
or cc.question_class = 25)
and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
--and (trunc(cc.discover_date) <> (rl.year_of_incident))
group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
Now a cla_case_no can map to several year_of_incident. I only want the cla_case_no that maps to the max year_of_incident ie There should only be a single cla_case_no corresponding to the max year_of_incident.
To get around this I did the following which is not very efficient and I'm hoping it can be improved:
select distinct z.cla_case_no from (
select
cc.name_id_no
,cc.discover_date
,cc.cla_case_no
,max(rl.year_of_incident)Non_MW_Loss_Past_5
,rl.timestamp
from cla_case cc, rbn_loss rl
where cc.name_id_no = rl.customer_no
and rl.year_of_incident < trunc(cc.discover_date)
and rl.type_of_loss < 1000
and rl.timestamp < trunc(cc.discover_date)
and (cc.question_class = 20
or cc.question_class = 25)
and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
--and (trunc(cc.discover_date) <> (rl.year_of_incident))
group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
) z
Now comes the second issue: The above is actually a subquery that will link to a bigger table via cla_case_no ccx
SELECT
ie ,(select distinct z.cla_case_no from (
select cc.name_id_no, cc.discover_date ,cc.cla_case_no, max(rl.year_of_incident)Non_MW_Loss_Past_5, rl.timestamp
from cla_case cc, rbn_loss rl
where cc.name_id_no = rl.customer_no
and rl.year_of_incident < trunc(cc.discover_date)
and rl.type_of_loss < 1000
and rl.timestamp < trunc(cc.discover_date)
and (cc.question_class = 20
or cc.question_class = 25)
and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
--and (trunc(cc.discover_date) <> (rl.year_of_incident))
group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
) z
where z.cla_case_no = ccx.cla_case_no
) Non_MW_Loss_Past_5
FROM etc
Now only certain cc.cla_case_no from the subquery will corresp to the ccx_cla_case_no from the main table and the other entries will be null.
What I require is that if the subquery returns a result that IS NOT NULL to return 'Y' ELSE 'N' instead of the varies cla_case_no's and (null) entries in the Non_MW_Loss_Past_5 column
Thanks!!!
Banner:
Oracle Database 11g Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
"CORE 11.2.0.2.0 Production"
TNS for Linux: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - ProductionHi,
It looks like you have another copy of this question:
Case Statement and sub query
That's probably not your fault, but you should mark the other copy as "Answered" right away, and then you'll only have to look for replies in one place.
885178 wrote:
... Now a cla_case_no can map to several year_of_incident. I only want the cla_case_no that maps to the max year_of_incident ie There should only be a single cla_case_no corresponding to the max year_of_incident.If you know there will only be one, then you can use LAST, and you don't need GrOUP BY
To get around this I did the following which is not very efficient and I'm hoping it can be improved:
select distinct z.cla_case_no from (
select
cc.name_id_no
,cc.discover_date
,cc.cla_case_no
,max(rl.year_of_incident)Non_MW_Loss_Past_5
,rl.timestamp
from cla_case cc, rbn_loss rl
where cc.name_id_no = rl.customer_no
and rl.year_of_incident < trunc(cc.discover_date)
and rl.type_of_loss < 1000
and rl.timestamp < trunc(cc.discover_date)
and (cc.question_class = 20
or cc.question_class = 25)
and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
--and (trunc(cc.discover_date) <> (rl.year_of_incident))
group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
) zHere's one way:
SELECT MIN (cla_case_no) KEEP (DENSE_RANK LAST ORDER BY r1.year_of_incident)
AS latest_cla_case_no
FROM cla_case cc
, rbn_loss rl
WHERE cc.name_id_no = rl.customer_no
AND rl.year_of_incident > TRUNC (cc.discover_date) - 1095
AND rl.year_of_incident < TRUNC (cc.discover_date)
AND rl.type_of_loss < 1000
AND rl.timestamp < TRUNC (cc.discover_date)
AND cc.question_class IN (20, 25)
;If you'd post some sample data (CREATE TABLE and INSERT statements) and the results you want from that data, then I could test this.
Now comes the second issue: The above is actually a subquery that will link to a bigger table via cla_case_no ccx
SELECT
ie ,(select distinct z.cla_case_no from (
select cc.name_id_no, cc.discover_date ,cc.cla_case_no, max(rl.year_of_incident)Non_MW_Loss_Past_5, rl.timestamp
from cla_case cc, rbn_loss rl
where cc.name_id_no = rl.customer_no
and rl.year_of_incident < trunc(cc.discover_date)
and rl.type_of_loss < 1000
and rl.timestamp < trunc(cc.discover_date)
and (cc.question_class = 20
or cc.question_class = 25)
and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
--and (trunc(cc.discover_date) <> (rl.year_of_incident))
group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
) z
where z.cla_case_no = ccx.cla_case_no
) Non_MW_Loss_Past_5
FROM etc
Now only certain cc.cla_case_no from the subquery will corresp to the ccx_cla_case_no from the main table and the other entries will be null.
What I require is that if the subquery returns a result that IS NOT NULL to return 'Y' ELSE 'N' instead of the varies cla_case_no's and (null) entries in the Non_MW_Loss_Past_5 column
NVL2 (x, 'Y', 'N')returns 'Y' if x is NULL, and it returns 'N' if x is not NULL. X can be a scalar sub-query:
NVL2 ((SELECT ...), 'Y', 'N')You could also use an EXISTS sub-query:
CASE
WHEN EXISTS (SELECT ...)
THEN 'Y'
ELSE 'N'
END -
Case Statement in Oracle Query.
Hello Oracle Gurus,
I need suggestion on whether I should use Case statement in Oracle queries.
I have a sql statement which inserts the data by selecting data from other table. While selecting the data I have put a logic in the select statement for one column which is something like this and there are some more similar statements in the same query.
CASE
WHEN (b.ACCOUNT = 'FIN' or b.ACCOUNT ='FIN ACC' or b.ACCOUNT like '%Global Eq%' or b.ACCOUNT like '%Flexible Bond%')
THEN
'MTM'
WHEN (substr(a.CTC,-3)='MTM')
THEN
'MTM'
WHEN (substr(a.CTC,-3)='AFS' or substr(a.CTC,-3)='HTM' OR substr(a.CTC,-3)='ACC' OR substr(a.CTC,-3)='HFI' )
THEN
'ACC'
ELSE
'OTH'
END,
I wanted to get an suggestion about how much performance issue can I have due to this in my insert statement.
Let me know if you need any other information.All comments are really appreciated.Hi,
It's depends on the joins how you have performed with source table, stats and existing of indexes. If the volume of data is high - in case of any performance issue - why can't you perform the required things of logical conditions of data and popualte in one cluster so - that you can fetch out the chunks of required information with better way and in support with indexes.
Let us know the ful query your working on
Oracle version
Explain plans - get it from dbms_xplan.
- Pavan Kumar N
- ORACLE - 9i/10g - OCP
RHCE - Enterprize Linux 5.4 -
SQL Query CASE statement using two fields
Hi,
I have two fields. One is called rescategory1, the other is called rescategory2.
I'm not sure if its a CASE statement I need or some sort of WHERE clause but I want to create a query that does the following or something similar:
CASE rescategory2
WHEN rescategory1 = '44' AND rescategory2 = '1' THEN 'Backup'
WHEN rescategory1 = '44' AND rescategory2 = '2' THEN 'Hardware'
END AS [Resolution Sub Category]
Basically, I'm looking to give rescategory2 a value based on that of rescategory1 and rescategory2 combined.
How do I write this?
Cheers
Pauldo you mean this?
rescategory2 = CASE
WHEN rescategory1 = '44' AND rescategory2 = '1' THEN 'Backup'
WHEN rescategory1 = '44' AND rescategory2 = '2' THEN 'Hardware'
END
ie assigning value for rescategory2
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs -
Sql query slow due to case statement on Joins
Hi
The sql query runs very slow for 30 min when the below case statement is added on the joins. Could you please let me know how to tune it. if the case statement is not there then it runs only for 1 min.
*( CASE*
WHEN PO_DIST_GL_CODE_COMB.SEGMENT2 <> '1000'
THEN PO_DIST_GL_CODE_COMB.SEGMENT1 || PO_DIST_GL_CODE_COMB.SEGMENT2 || '_' || NVL(PO_DIST_GL_CODE_COMB.SEGMENT6,'000')
WHEN DT_REQ_ALL.EMPMGMTCD IS NOT NULL AND
PO_DIST_GL_CODE_COMB.SEGMENT2 = '1000'
THEN DT_REQ_ALL.EMPMGMTCD
END =DB2.DB2_FDW_MGMT_V.MH_CHILD )
SELECT DISTINCT
D.DB2_FDW_MGMT_V.RC_PARENT,
DT_REQ_ALL.FULL_NAME,
DT_REQ_ALL.EMP_COMPANY_CODE,
DT_REQ_ALL.EMP_COST_CENTER,
PO.PO_VENDORS.VENDOR_NAME,
PO_PO_HEADERS_ALL2.SEGMENT1,
PO_PO_HEADERS_ALL2.CREATION_DATE,
PO_DIST_GL_CODE_COMB.SEGMENT1,
PO_DIST_GL_CODE_COMB.SEGMENT2,
PO_PO_HEADERS_ALL2.CURRENCY_CODE,
PO_INV_DIST_ALL.INVOICE_NUM,
PO_INV_DIST_ALL.INVOICE_DATE,
(PO_INV_DIST_ALL.INVOICE_AMOUNT* PO_Rates_GL_DR.CONVERSION_RATE),
(NVL(to_number(PO_DIST_ALL.AMOUNT_BILLED),0) * PO_Rates_GL_DR.CONVERSION_RATE),
PO_LINES_LOC.LINE_NUM,
GL.GL_SETS_OF_BOOKS.NAME,
CASE
WHEN TRUNC(PO_PO_HEADERS_ALL2.CREATION_DATE) > PO_INV_DIST_ALL.INVOICE_DATE
THEN 1
ELSE 0
END ,
PO.PO_REQUISITION_LINES_ALL.LINE_LOCATION_ID,
TRUNC(PO_PO_HEADERS_ALL2.CREATION_DATE,'WW') + 8 WEEK_Ending
FROM
DB2.DB2_FDW_MGMT_V,
PO.PO_VENDORS,
PO.PO_HEADERS_ALL PO_PO_HEADERS_ALL2,
GL.GL_CODE_COMBINATIONS PO_DIST_GL_CODE_COMB,
AP.AP_INVOICES_ALL PO_INV_DIST_ALL,
PO.PO_DISTRIBUTIONS_ALL PO_DIST_ALL,
PO.PO_LINES_ALL PO_LINES_LOC,
GL.GL_SETS_OF_BOOKS,
PO.PO_REQUISITION_LINES_ALL,
PO.PO_LINE_LOCATIONS_ALL,
AP.AP_INVOICE_DISTRIBUTIONS_ALL PO_DIST_INV_DIST_ALL,
APPS.HR_OPERATING_UNITS,
PO.PO_REQ_DISTRIBUTIONS_ALL,
SELECT DISTINCT
PO_RDA.DISTRIBUTION_ID,
PO_RLA.requisition_line_id,
PO_RHA.DESCRIPTION PO_Descr,
PO_RHA.NOTE_TO_AUTHORIZER PO_Justification,
Req_Emp.FULL_NAME,
GL_CC.SEGMENT1 Req_Company_Code,
GL_CC.SEGMENT2 Req_Cost_Center,
Req_Emp_CC.SEGMENT1 Emp_Company_Code,
Req_Emp_CC.SEGMENT2 Emp_Cost_Center,
(Case
When GL_CC.SEGMENT2 <> 8000
Then TRUNC(GL_CC.SEGMENT1) || TRUNC(GL_CC.SEGMENT2) || '_' || NVL(GL_CC.SEGMENT6,'000')
Else TRUNC(Req_Emp_CC.SEGMENT1) || TRUNC(Req_Emp_CC.SEGMENT2) || '_' || NVL(Req_Emp_CC.SEGMENT6,'000')
End) EmpMgmtCD
FROM
PO.po_requisition_lines_all PO_rla,
PO.po_requisition_headers_all PO_rha,
PO.PO_REQ_DISTRIBUTIONS_ALL po_RDA,
GL.GL_CODE_COMBINATIONS gl_cc,
HR.PER_ALL_PEOPLE_F Req_Emp,
HR.PER_ALL_ASSIGNMENTS_F Req_Emp_Assign,
HR.hr_all_organization_units Req_Emp_Org,
HR.pay_cost_allocation_keyflex Req_Emp_CC
WHERE
PO_RDA.CODE_COMBINATION_ID = GL_CC.CODE_COMBINATION_ID and
PO_RLA.REQUISITION_LINE_ID = PO_RDA.REQUISITION_LINE_ID AND
PO_RLA.to_person_id = Req_Emp.PERSON_ID AND
PO_RLA.REQUISITION_HEADER_ID = PO_RHA.REQUISITION_HEADER_ID AND
(trunc(PO_rla.CREATION_DATE) between Req_Emp.effective_start_date and Req_Emp.effective_end_date OR
Req_Emp.effective_start_date IS NULL) AND
Req_Emp.PERSON_ID = Req_Emp_Assign.PERSON_ID AND
Req_Emp_Assign.organization_id = Req_Emp_Org.organization_id AND
(trunc(PO_rla.CREATION_DATE) between Req_Emp_Assign.effective_start_date and Req_Emp_Assign.effective_end_date OR
Req_Emp_Assign.effective_start_date IS NULL) AND
Req_Emp_Assign.primary_flag = 'Y' AND
Req_Emp_Assign.assignment_type = 'E' AND
Req_Emp_Org.cost_allocation_keyflex_id = Req_Emp_CC.cost_allocation_keyflex_id
) DT_REQ_ALL,
SELECT
FROM_CURRENCY,
TO_CURRENCY,
CONVERSION_DATE,
CONVERSION_RATE
FROM GL.GL_DAILY_RATES
UNION
SELECT Distinct
'USD',
'USD',
CONVERSION_DATE,
1
FROM GL.GL_DAILY_RATES
) PO_Rates_GL_DR
WHERE
( PO_DIST_GL_CODE_COMB.CODE_COMBINATION_ID=PO_DIST_ALL.CODE_COMBINATION_ID )
AND ( PO_DIST_ALL.LINE_LOCATION_ID=PO.PO_LINE_LOCATIONS_ALL.LINE_LOCATION_ID )
AND ( PO_PO_HEADERS_ALL2.VENDOR_ID=PO.PO_VENDORS.VENDOR_ID )
AND ( PO_PO_HEADERS_ALL2.ORG_ID=APPS.HR_OPERATING_UNITS.ORGANIZATION_ID )
AND ( GL.GL_SETS_OF_BOOKS.SET_OF_BOOKS_ID=APPS.HR_OPERATING_UNITS.SET_OF_BOOKS_ID )
AND ( PO_PO_HEADERS_ALL2.CURRENCY_CODE=PO_Rates_GL_DR.FROM_CURRENCY )
AND ( trunc(PO_PO_HEADERS_ALL2.CREATION_DATE)=PO_Rates_GL_DR.CONVERSION_DATE )
AND ( PO_DIST_ALL.REQ_DISTRIBUTION_ID=PO.PO_REQ_DISTRIBUTIONS_ALL.DISTRIBUTION_ID(+) )
AND ( PO.PO_REQ_DISTRIBUTIONS_ALL.REQUISITION_LINE_ID=PO.PO_REQUISITION_LINES_ALL.REQUISITION_LINE_ID(+) )
AND ( PO_LINES_LOC.PO_HEADER_ID=PO_PO_HEADERS_ALL2.PO_HEADER_ID )
AND ( PO.PO_LINE_LOCATIONS_ALL.PO_LINE_ID=PO_LINES_LOC.PO_LINE_ID )
AND ( PO_DIST_ALL.REQ_DISTRIBUTION_ID=DT_REQ_ALL.DISTRIBUTION_ID(+) )
AND ( PO_DIST_ALL.PO_DISTRIBUTION_ID=PO_DIST_INV_DIST_ALL.PO_DISTRIBUTION_ID(+) )
AND ( PO_INV_DIST_ALL.INVOICE_ID(+)=PO_DIST_INV_DIST_ALL.INVOICE_ID )
AND ( PO_INV_DIST_ALL.SOURCE(+) <> 'XML GATEWAY' )
AND
( NVL(PO_PO_HEADERS_ALL2.CANCEL_FLAG,'N') <> 'Y' )
AND
( NVL(PO_PO_HEADERS_ALL2.CLOSED_CODE, 'OPEN') <> 'FINALLY CLOSED' )
AND
( NVL(PO_PO_HEADERS_ALL2.AUTHORIZATION_STATUS,'IN PROCESS') <> 'REJECTED' )
AND
( TRUNC(PO_PO_HEADERS_ALL2.CREATION_DATE) BETWEEN TO_DATE('01-jan-2011') AND TO_DATE('04-jan-2011') )
AND
PO_Rates_GL_DR.TO_CURRENCY = 'USD'
AND
DB2.DB2_FDW_MGMT_V.RC_PARENT In ( 'Unavailable','Corp','Commercial' )
AND
( CASE
WHEN PO_DIST_GL_CODE_COMB.SEGMENT2 <> '1000'
THEN PO_DIST_GL_CODE_COMB.SEGMENT1 || PO_DIST_GL_CODE_COMB.SEGMENT2 || '_' || NVL(PO_DIST_GL_CODE_COMB.SEGMENT6,'000')
WHEN DT_REQ_ALL.EMPMGMTCD IS NOT NULL AND
PO_DIST_GL_CODE_COMB.SEGMENT2 = '1000'
THEN DT_REQ_ALL.EMPMGMTCD
END =DB2.DB2_FDW_MGMT_V.MH_CHILD )Explain plan. sorry can't get the explain plan from sql. this is from toad.
Plan
SELECT STATEMENT ALL_ROWSCost: 53,932 Bytes: 2,607 Cardinality: 1
79 HASH UNIQUE Cost: 53,932 Bytes: 2,607 Cardinality: 1
78 NESTED LOOPS OUTER Cost: 53,931 Bytes: 2,607 Cardinality: 1
75 NESTED LOOPS OUTER Cost: 53,928 Bytes: 2,560 Cardinality: 1
72 NESTED LOOPS Cost: 53,902 Bytes: 2,552 Cardinality: 1
69 NESTED LOOPS OUTER Cost: 53,900 Bytes: 2,533 Cardinality: 1
66 NESTED LOOPS OUTER Cost: 53,898 Bytes: 2,521 Cardinality: 1
63 HASH JOIN OUTER Cost: 53,896 Bytes: 2,509 Cardinality: 1
40 TABLE ACCESS BY INDEX ROWID TABLE PO.PO_DISTRIBUTIONS_ALL Cost: 3 Bytes: 26 Cardinality: 1
39 NESTED LOOPS Cost: 17,076 Bytes: 2,400 Cardinality: 1
37 NESTED LOOPS Cost: 17,073 Bytes: 2,374 Cardinality: 1
34 NESTED LOOPS Cost: 17,070 Bytes: 2,362 Cardinality: 1
31 NESTED LOOPS Cost: 17,066 Bytes: 2,347 Cardinality: 1
29 NESTED LOOPS Cost: 17,066 Bytes: 2,339 Cardinality: 1
26 NESTED LOOPS Cost: 17,065 Bytes: 2,312 Cardinality: 1
23 NESTED LOOPS Cost: 17,064 Bytes: 2,287 Cardinality: 1
20 NESTED LOOPS Cost: 17,062 Bytes: 2,261 Cardinality: 1
17 NESTED LOOPS Cost: 17,056 Bytes: 6,678 Cardinality: 3
15 HASH JOIN Cost: 17,056 Bytes: 6,663 Cardinality: 3
13 MERGE JOIN CARTESIAN Cost: 135 Bytes: 30,352 Cardinality: 14
5 VIEW VIEW DB2.DB2_FDW_MGMT_V Cost: 4 Bytes: 2,128 Cardinality: 1
4 SORT UNIQUE Cost: 4 Cardinality: 1
3 UNION-ALL
1 REMOTE REMOTE SERIAL_FROM_REMOTE PRDFDW.WORLD
2 FAST DUAL Cost: 3 Cardinality: 1
12 BUFFER SORT Cost: 135 Bytes: 560 Cardinality: 14
11 VIEW DB2. Cost: 131 Bytes: 560 Cardinality: 14
10 SORT UNIQUE Cost: 131 Bytes: 310 Cardinality: 14
9 UNION-ALL
7 TABLE ACCESS BY INDEX ROWID TABLE GL.GL_DAILY_RATES Cost: 65 Bytes: 270 Cardinality: 9
6 INDEX SKIP SCAN INDEX (UNIQUE) GL.GL_DAILY_RATES_U1 Cost: 64 Cardinality: 1
8 INDEX SKIP SCAN INDEX (UNIQUE) GL.GL_DAILY_RATES_U1 Cost: 64 Bytes: 4,368 Cardinality: 546
14 TABLE ACCESS FULL TABLE PO.PO_HEADERS_ALL Cost: 16,920 Bytes: 32,754 Cardinality: 618
16 INDEX UNIQUE SCAN INDEX (UNIQUE) HR.HR_ORGANIZATION_UNITS_PK Cost: 0 Bytes: 5 Cardinality: 1
19 TABLE ACCESS BY INDEX ROWID TABLE HR.HR_ORGANIZATION_INFORMATION Cost: 2 Bytes: 35 Cardinality: 1
18 INDEX RANGE SCAN INDEX HR.HR_ORGANIZATION_INFORMATIO_FK2 Cost: 1 Cardinality: 2
22 TABLE ACCESS BY INDEX ROWID TABLE HR.HR_ORGANIZATION_INFORMATION Cost: 2 Bytes: 26 Cardinality: 1
21 INDEX RANGE SCAN INDEX HR.HR_ORGANIZATION_INFORMATIO_FK2 Cost: 1 Cardinality: 1
25 TABLE ACCESS BY INDEX ROWID TABLE GL.GL_SETS_OF_BOOKS Cost: 1 Bytes: 25 Cardinality: 1
24 INDEX UNIQUE SCAN INDEX (UNIQUE) GL.GL_SETS_OF_BOOKS_U2 Cost: 0 Cardinality: 1
28 TABLE ACCESS BY INDEX ROWID TABLE PO.PO_VENDORS Cost: 1 Bytes: 27 Cardinality: 1
27 INDEX UNIQUE SCAN INDEX (UNIQUE) PO.PO_VENDORS_U1 Cost: 0 Cardinality: 1
30 INDEX UNIQUE SCAN INDEX (UNIQUE) HR.HR_ALL_ORGANIZATION_UNTS_TL_PK Cost: 0 Bytes: 8 Cardinality: 1
33 TABLE ACCESS BY INDEX ROWID TABLE PO.PO_LINES_ALL Cost: 4 Bytes: 60 Cardinality: 4
32 INDEX RANGE SCAN INDEX (UNIQUE) PO.PO_LINES_U2 Cost: 2 Cardinality: 4
36 TABLE ACCESS BY INDEX ROWID TABLE PO.PO_LINE_LOCATIONS_ALL Cost: 3 Bytes: 12 Cardinality: 1
35 INDEX RANGE SCAN INDEX PO.PO_LINE_LOCATIONS_N1 Cost: 2 Cardinality: 1
38 INDEX RANGE SCAN INDEX PO.PO_DISTRIBUTIONS_N1 Cost: 2 Cardinality: 1
62 VIEW DB2. Cost: 36,819 Bytes: 1,090 Cardinality: 10
61 HASH UNIQUE Cost: 36,819 Bytes: 2,580 Cardinality: 10
60 NESTED LOOPS Cost: 36,818 Bytes: 2,580 Cardinality: 10
57 NESTED LOOPS Cost: 36,798 Bytes: 2,390 Cardinality: 10
54 NESTED LOOPS Cost: 36,768 Bytes: 2,220 Cardinality: 10
51 NESTED LOOPS Cost: 36,758 Bytes: 1,510 Cardinality: 10
48 NESTED LOOPS Cost: 36,747 Bytes: 1,050 Cardinality: 10
45 HASH JOIN Cost: 36,737 Bytes: 960 Cardinality: 10
43 HASH JOIN Cost: 34,602 Bytes: 230,340 Cardinality: 3,490
41 TABLE ACCESS FULL TABLE HR.PER_ALL_PEOPLE_F Cost: 1,284 Bytes: 1,848,420 Cardinality: 44,010
42 TABLE ACCESS FULL TABLE PO.PO_REQUISITION_LINES_ALL Cost: 31,802 Bytes: 18,340,080 Cardinality: 764,170
44 TABLE ACCESS FULL TABLE HR.PER_ALL_ASSIGNMENTS_F Cost: 2,134 Bytes: 822,540 Cardinality: 27,418
47 TABLE ACCESS BY INDEX ROWID TABLE HR.HR_ALL_ORGANIZATION_UNITS Cost: 1 Bytes: 9 Cardinality: 1
46 INDEX UNIQUE SCAN INDEX (UNIQUE) HR.HR_ORGANIZATION_UNITS_PK Cost: 0 Cardinality: 1
50 TABLE ACCESS BY INDEX ROWID TABLE HR.PAY_COST_ALLOCATION_KEYFLEX Cost: 1 Bytes: 46 Cardinality: 1
49 INDEX UNIQUE SCAN INDEX (UNIQUE) HR.PAY_COST_ALLOCATION_KEYFLE_PK Cost: 0 Cardinality: 1
53 TABLE ACCESS BY INDEX ROWID TABLE PO.PO_REQUISITION_HEADERS_ALL Cost: 1 Bytes: 71 Cardinality: 1
52 INDEX UNIQUE SCAN INDEX (UNIQUE) PO.PO_REQUISITION_HEADERS_U1 Cost: 0 Cardinality: 1
56 TABLE ACCESS BY INDEX ROWID TABLE PO.PO_REQ_DISTRIBUTIONS_ALL Cost: 3 Bytes: 17 Cardinality: 1
55 INDEX RANGE SCAN INDEX PO.PO_REQ_DISTRIBUTIONS_N1 Cost: 2 Cardinality: 1
59 TABLE ACCESS BY INDEX ROWID TABLE GL.GL_CODE_COMBINATIONS Cost: 2 Bytes: 19 Cardinality: 1
58 INDEX UNIQUE SCAN INDEX (UNIQUE) GL.GL_CODE_COMBINATIONS_U1 Cost: 1 Cardinality: 1
65 TABLE ACCESS BY INDEX ROWID TABLE PO.PO_REQ_DISTRIBUTIONS_ALL Cost: 2 Bytes: 12 Cardinality: 1
64 INDEX UNIQUE SCAN INDEX (UNIQUE) PO.PO_REQ_DISTRIBUTIONS_U1 Cost: 1 Cardinality: 1
68 TABLE ACCESS BY INDEX ROWID TABLE PO.PO_REQUISITION_LINES_ALL Cost: 2 Bytes: 12 Cardinality: 1
67 INDEX UNIQUE SCAN INDEX (UNIQUE) PO.PO_REQUISITION_LINES_U1 Cost: 1 Cardinality: 1
71 TABLE ACCESS BY INDEX ROWID TABLE GL.GL_CODE_COMBINATIONS Cost: 2 Bytes: 19 Cardinality: 1
70 INDEX UNIQUE SCAN INDEX (UNIQUE) GL.GL_CODE_COMBINATIONS_U1 Cost: 1 Cardinality: 1
74 TABLE ACCESS BY INDEX ROWID TABLE AP.AP_INVOICE_DISTRIBUTIONS_ALL Cost: 26 Bytes: 16 Cardinality: 2
73 INDEX RANGE SCAN INDEX AP.AP_INVOICE_DISTRIBUTIONS_N7 Cost: 2 Cardinality: 37
77 TABLE ACCESS BY INDEX ROWID TABLE AP.AP_INVOICES_ALL Cost: 3 Bytes: 47 Cardinality: 1
76 INDEX RANGE SCAN INDEX (UNIQUE) AP.AP_INVOICES_U1 Cost: 2 Cardinality: 1 ThanksForming a new table "new_table" with 3 tables which particiapate in CASE statement logic.
with DT_REQ_ALL as
SELECT DISTINCT
PO_RDA.DISTRIBUTION_ID,
PO_RLA.requisition_line_id,
PO_RHA.DESCRIPTION PO_Descr,
PO_RHA.NOTE_TO_AUTHORIZER PO_Justification,
Req_Emp.FULL_NAME,
GL_CC.SEGMENT1 Req_Company_Code,
GL_CC.SEGMENT2 Req_Cost_Center,
Req_Emp_CC.SEGMENT1 Emp_Company_Code,
Req_Emp_CC.SEGMENT2 Emp_Cost_Center,
(Case
When GL_CC.SEGMENT2 8000
Then TRUNC(GL_CC.SEGMENT1) || TRUNC(GL_CC.SEGMENT2) || '_' || NVL(GL_CC.SEGMENT6,'000')
Else TRUNC(Req_Emp_CC.SEGMENT1) || TRUNC(Req_Emp_CC.SEGMENT2) || '_' || NVL(Req_Emp_CC.SEGMENT6,'000')
End) EmpMgmtCD
FROM
PO.po_requisition_lines_all PO_rla,
PO.po_requisition_headers_all PO_rha,
PO.PO_REQ_DISTRIBUTIONS_ALL po_RDA,
GL.GL_CODE_COMBINATIONS gl_cc,
HR.PER_ALL_PEOPLE_F Req_Emp,
HR.PER_ALL_ASSIGNMENTS_F Req_Emp_Assign,
HR.hr_all_organization_units Req_Emp_Org,
HR.pay_cost_allocation_keyflex Req_Emp_CC
WHERE
PO_RDA.CODE_COMBINATION_ID = GL_CC.CODE_COMBINATION_ID and
PO_RLA.REQUISITION_LINE_ID = PO_RDA.REQUISITION_LINE_ID AND
PO_RLA.to_person_id = Req_Emp.PERSON_ID AND
PO_RLA.REQUISITION_HEADER_ID = PO_RHA.REQUISITION_HEADER_ID AND
(trunc(PO_rla.CREATION_DATE) between Req_Emp.effective_start_date and Req_Emp.effective_end_date OR
Req_Emp.effective_start_date IS NULL) AND
Req_Emp.PERSON_ID = Req_Emp_Assign.PERSON_ID AND
Req_Emp_Assign.organization_id = Req_Emp_Org.organization_id AND
(trunc(PO_rla.CREATION_DATE) between Req_Emp_Assign.effective_start_date and Req_Emp_Assign.effective_end_date OR
Req_Emp_Assign.effective_start_date IS NULL) AND
Req_Emp_Assign.primary_flag = 'Y' AND
Req_Emp_Assign.assignment_type = 'E' AND
Req_Emp_Org.cost_allocation_keyflex_id = Req_Emp_CC.cost_allocation_keyflex_id
SELECT DISTINCT
D.DB2_FDW_MGMT_V.RC_PARENT,
DT_REQ_ALL.FULL_NAME,
DT_REQ_ALL.EMP_COMPANY_CODE,
DT_REQ_ALL.EMP_COST_CENTER,
PO.PO_VENDORS.VENDOR_NAME,
PO_PO_HEADERS_ALL2.SEGMENT1,
PO_PO_HEADERS_ALL2.CREATION_DATE,
PO_DIST_GL_CODE_COMB.SEGMENT1,
PO_DIST_GL_CODE_COMB.SEGMENT2,
PO_PO_HEADERS_ALL2.CURRENCY_CODE,
PO_INV_DIST_ALL.INVOICE_NUM,
PO_INV_DIST_ALL.INVOICE_DATE,
(PO_INV_DIST_ALL.INVOICE_AMOUNT* PO_Rates_GL_DR.CONVERSION_RATE),
(NVL(to_number(PO_DIST_ALL.AMOUNT_BILLED),0) * PO_Rates_GL_DR.CONVERSION_RATE),
PO_LINES_LOC.LINE_NUM,
GL.GL_SETS_OF_BOOKS.NAME,
CASE
WHEN TRUNC(PO_PO_HEADERS_ALL2.CREATION_DATE) > PO_INV_DIST_ALL.INVOICE_DATE
THEN 1
ELSE 0
END ,
PO.PO_REQUISITION_LINES_ALL.LINE_LOCATION_ID,
TRUNC(PO_PO_HEADERS_ALL2.CREATION_DATE,'WW') + 8 WEEK_Ending
FROM
( SELECT * FROM
DB2.DB2_FDW_MGMT_V,
GL.GL_CODE_COMBINATIONS PO_DIST_GL_CODE_COMB,
DT_REQ_ALL
WHERE
DB2.DB2_FDW_MGMT_V.RC_PARENT In ( 'Unavailable','Corp','Commercial' )
AND
CASE
WHEN PO_DIST_GL_CODE_COMB.SEGMENT2 <> '1000'
THEN PO_DIST_GL_CODE_COMB.SEGMENT1 || PO_DIST_GL_CODE_COMB.SEGMENT2 || '_' || NVL(PO_DIST_GL_CODE_COMB.SEGMENT6,'000')
WHEN DT_REQ_ALL.EMPMGMTCD IS NOT NULL AND
PO_DIST_GL_CODE_COMB.SEGMENT2 = '1000'
THEN DT_REQ_ALL.EMPMGMTCD
END =DB2.DB2_FDW_MGMT_V.MH_CHILD
) new_table,
PO.PO_VENDORS,
PO.PO_HEADERS_ALL PO_PO_HEADERS_ALL2,
AP.AP_INVOICES_ALL PO_INV_DIST_ALL,
PO.PO_DISTRIBUTIONS_ALL PO_DIST_ALL,
PO.PO_LINES_ALL PO_LINES_LOC,
GL.GL_SETS_OF_BOOKS,
PO.PO_REQUISITION_LINES_ALL,
PO.PO_LINE_LOCATIONS_ALL,
AP.AP_INVOICE_DISTRIBUTIONS_ALL PO_DIST_INV_DIST_ALL,
APPS.HR_OPERATING_UNITS,
PO.PO_REQ_DISTRIBUTIONS_ALL,
SELECT
FROM_CURRENCY,
TO_CURRENCY,
CONVERSION_DATE,
CONVERSION_RATE
FROM GL.GL_DAILY_RATES
UNION
SELECT Distinct
'USD',
'USD',
CONVERSION_DATE,
1
FROM GL.GL_DAILY_RATES
) PO_Rates_GL_DR
WHERE
( PO_DIST_GL_CODE_COMB.CODE_COMBINATION_ID=PO_DIST_ALL.CODE_COMBINATION_ID )
AND ( PO_DIST_ALL.LINE_LOCATION_ID=PO.PO_LINE_LOCATIONS_ALL.LINE_LOCATION_ID )
AND ( PO_PO_HEADERS_ALL2.VENDOR_ID=PO.PO_VENDORS.VENDOR_ID )
AND ( PO_PO_HEADERS_ALL2.ORG_ID=APPS.HR_OPERATING_UNITS.ORGANIZATION_ID )
AND ( GL.GL_SETS_OF_BOOKS.SET_OF_BOOKS_ID=APPS.HR_OPERATING_UNITS.SET_OF_BOOKS_ID )
AND ( PO_PO_HEADERS_ALL2.CURRENCY_CODE=PO_Rates_GL_DR.FROM_CURRENCY )
AND ( trunc(PO_PO_HEADERS_ALL2.CREATION_DATE)=PO_Rates_GL_DR.CONVERSION_DATE )
AND ( PO_DIST_ALL.REQ_DISTRIBUTION_ID=PO.PO_REQ_DISTRIBUTIONS_ALL.DISTRIBUTION_ID(+) )
AND ( PO.PO_REQ_DISTRIBUTIONS_ALL.REQUISITION_LINE_ID=PO.PO_REQUISITION_LINES_ALL.REQUISITION_LINE_ID(+) )
AND ( PO_LINES_LOC.PO_HEADER_ID=PO_PO_HEADERS_ALL2.PO_HEADER_ID )
AND ( PO.PO_LINE_LOCATIONS_ALL.PO_LINE_ID=PO_LINES_LOC.PO_LINE_ID )
AND ( PO_DIST_ALL.REQ_DISTRIBUTION_ID=DT_REQ_ALL.DISTRIBUTION_ID(+) )
AND ( PO_DIST_ALL.PO_DISTRIBUTION_ID=PO_DIST_INV_DIST_ALL.PO_DISTRIBUTION_ID(+) )
AND ( PO_INV_DIST_ALL.INVOICE_ID(+)=PO_DIST_INV_DIST_ALL.INVOICE_ID )
AND ( PO_INV_DIST_ALL.SOURCE(+) 'XML GATEWAY' )
AND
( NVL(PO_PO_HEADERS_ALL2.CANCEL_FLAG,'N') 'Y' )
AND
( NVL(PO_PO_HEADERS_ALL2.CLOSED_CODE, 'OPEN') 'FINALLY CLOSED' )
AND
( NVL(PO_PO_HEADERS_ALL2.AUTHORIZATION_STATUS,'IN PROCESS') 'REJECTED' )
AND
( TRUNC(PO_PO_HEADERS_ALL2.CREATION_DATE) BETWEEN TO_DATE('01-jan-2011') AND TO_DATE('04-jan-2011') )
AND
PO_Rates_GL_DR.TO_CURRENCY = 'USD'
-
Grouping using report query / Case statement in toplink
I have following tables
1. Student with columns id, gender
2. Subject_Score with columns id, student_id, subject_id, score
To get scores grouped by subject, I am doing
ExpressionBuilder subjScoreBuilder = new ExpressionBuilder();
ReportQuery query = new ReportQuery(SubjectScore.class);
query.addAverage("average-score",subjScoreBuilder.get("score"));
query.addGrouping(subjScoreBuilder.get("subjectId"));
Vector responses = (Vector) serverSession.executeQuery(query);
Float score = (Float) queryResult.get("average-score");
This works fine. It gives avg score per each subject
Now i want both in one query
A) avg score per subject
B) avg score per subject per gender
I want to achive this in one query
I am doing like:
ExpressionBuilder subjScoreBuilder =new ExpressionBuilder(SubjectScore.class);
ExpressionBuilder studentExpBuilder = new ExpressionBuilder(Student.class);
Expression expression = subjScoreBuilder.get("studentid").equal(studentExpBuilder.get("id")));
ReportQuery query = new ReportQuery(SubjectScore.class, expression);
query.addAverage("average-score", subjScoreBuilder.get("score"));
query.addGrouping( subjScoreBuilder.get("subjectId"));
query.addGrouping( studentExpBuilder.get("gender"));
This gives me avg scor per each subject per gender. i.e.
it applies grouping on both subjectId & gender.
This is fine.
But I also want avg score per each subject (group on subject only) in same query.
1. How can we achive it?
2. is there something like Case statement in toplink?
Thanks a lot for any help.I believe in SQL you would need two queries to do this directly, so you will need to issue two queries.
You could select the Count and Avg, this would give you all the data need to compute the Avg yourself.
i.e.
(count(male) * avg(male) + count(female) * avg(female)) / (count(male) + count(female)) -
Query Tuning - using CASE statement in the WHERE clause - Duplicate Post
Duplicate Post by mistake.
Please check
Query Tuning - using CASE statement in the WHERE clause
Edited by: Chaitanya on Jun 9, 2011 2:45 AM
Edited by: Chaitanya on Jun 9, 2011 2:46 AMDuplicate Post by mistake.
Please check
Query Tuning - using CASE statement in the WHERE clause
Edited by: Chaitanya on Jun 9, 2011 2:45 AM
Edited by: Chaitanya on Jun 9, 2011 2:46 AM -
Help with multiple case statements
Hello,
I am new to BO. I am on XI 3.0. SQL 2005. In Designer, I am trying to create a measure in a financial universe that would end up being multiple case statements within one select. This is what I tried to do, but it doesn't work with the two case statements. Can I use an ELSE leading into the second CASE WHEN somehow? How can I accomplish this? Sorry for my ignorance!
CASE WHEN dbo.ClientBudgetYear.DateStage1Approved > 01/01/1900 AND dbo.ClientBudgetMonth.Month = 12 THEN dbo.ClientBudgetMonth.Stage1Sales END
CASE WHEN dbo.ClientBudgetYear.DateStage1Approved > 01/01/1900 AND dbo.ClientBudgetMonth.Month = 11 THEN dbo.ClientBudgetMonth.Stage1Sales END
Any Suggestions?
Thanks,
HollyHolly,
I don't know enough about your data or requirement to provide a solution, however, the construct that you post will not work because it causes you to build an object with multiple case statements when only one case statement per object is permitted. From what I see in your code I would be inclined to combine the two statements into one as such:
CASE WHEN dbo.ClientBudgetYear.DateStage1Approved > 01/01/1900 AND dbo.ClientBudgetMonth.Month in (11,12) THEN dbo.ClientBudgetMonth.Stage1Sales else null END
Thanks,
John
Maybe you are looking for
-
Error in viewing logon page,inspite of restoring original par file
Hi, I was trying to modify,the logon par file,because it was not getting deployed,it was becoming .err, I reverted back to the original logon par,and still even that gets deployed to .err. I renamed the original par,changed the reference in authschem
-
I have a weird request I have never thought I would get. I have a VIP user that wants to receive no calendar invites / meeting invites from: - anyone outside of our organization (ie: from the internet) - only certain users from INSIDE our organizatio
-
Labview Software/Hardware Developer Seeks Work
Labview Developer living in Canada seeks work developing things in the areas of: biomedical engineering, telecommunications, life sciences; or any project dealing with data acquisition, applied statistics, or control systems. You can contact
-
I have discovered how to upload videos onto Itunes and my Ipad2, using a program called HANDBRAKE...However in Itunes and on the Ipad2 the video thumnails are either black in Itunes or a funny coloured "negative" picture......Does anyone know how to
-
Stop LR from automatically creating TIFF or PSD files when opening in PS.
Every time I open a RAW file into Photoshop CS5 from LR3, it automatically creates and saves a PSD or TIFF file. Is there a way to stop lightroom from doing this? When I used LR2, I saved these files only if I choose to. I always convert my files to