Case statement help
Hi Gurus,
I have a requirement like this,
I want to extract the service request which closed in last month and opened in the last three months of closing month (close month -3).
When open month is previous month and timestampdiff(tsi_month, open month(close month), openmonth(close month -3) then nbr of SRs.
How do i bring the close month in open month value.
Thanks
I am trying to enter the following statement for the above requirment
CASE WHEN "Core"."Close Date"."Close Month" = VALUEOF(PREVIOUS_MONTH) AND TimeStampDiff(SQL_TSI_Month, "Core"."Open Date"."Open Month" = VALUEOF(PREVIOUS_MONTH) , "Core"."Open Date"."Open Month" = VALUEOF(PREVIOUS_MONTH) - 3 ) <= 3 THEN 1 ELSE 0 END
When i say okay, it gives the error
[nQSError: 27002] Near <=>: Syntax error [nQSError: 26012] .
Can anyone help me out.
Thanks
Similar Messages
-
case
when datepart(mm,[SDate]) between '9' and '11' and datepart(mm,[PDate] ) between '9' and '11'
Then 'Q1_'
when datepart(mm,[SDate]) between '12' and '02' and datepart(mm,[PDate] ) between '12' and '02'
Then 'Q2_'
when datepart(mm,[SDate]) between '03' and '05' and datepart(mm,[PDate] ) between '03' and '05'
Then 'Q3_'
when datepart(mm,[SDate]) between '06' and '08' and datepart(mm,[PDate] ) between '06' and '08'
Then 'Q4_'
else 'N/A' end as QTR
from DDA_2015_FSR
Datatype for SDate and Pdate
2014-09-01 00:00:00
I need help with above case statement, When I run the case statement I get 'N/A FSY_2015 ' When I add the following where clause to validate the results. In my table I do have records for all four quarters, Except Q1, all other rows I get 'N/A
FSY_2015 '
Am i missing something? Can someone help me here?
where
datepart(m,[SDate])
between
'12'
and
'02'
and
datepart(m,[PDate]
between
'12'
and
'02'
FMThere is no CASE statement in SQL; it is a CASE expression. Big difference.
Since SQL is a database language, we prefer to do look ups and not calculations. They can be optimized while temporal math messes up optimization. A useful idiom is a report period calendar that everyone uses so there is no way to get disagreements in the DML.
The report period table gives a name to a range of dates that is common to the entire enterprise.
CREATE TABLE Something_Report_Periods
(something_report_name CHAR(10) NOT NULL PRIMARY KEY
CHECK (something_report_name LIKE <pattern>),
something_report_start_date DATE NOT NULL,
something_report_end_date DATE NOT NULL,
CONSTRAINT date_ordering
CHECK (something_report_start_date <= something_report_end_date),
etc);
These report periods can overlap or have gaps. I like the MySQL convention of using double zeroes for months and years, That is 'yyyy-mm-00' for a month within a year and 'yyyy-00-00' for the whole year. The advantages are that it will sort with the ISO-8601
data format required by Standard SQL and it is language independent. The pattern for validation is '[12][0-9][0-9][0-9]-00-00' and '[12][0-9][0-9][0-9]-[01][0-9]-00'
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
Switch Case Statement Help...
Hello,
I am trying to get my pages through Case Statement But...
Can any one Help me.. here is my code..
This is my Buttons Code... My buttons are on 1st Frame of My FLA..
stop();
btn1.addEventListener(MouseEvent.CLICK, varTest);
btn2.addEventListener(MouseEvent.CLICK, varTest2);
function varTest(event:MouseEvent):void
var page = 1;
gotoAndStop(20);
function varTest2(event:MouseEvent):void
var page = 2;
gotoAndStop(20);
and this is my 20th frame code. (here I'm checking variable)
import flash.events.Event;
addEventListener(Event.ENTER_FRAME,checkCaseMe);
function checkCaseMe(event:Event)
switch (page)
case "1" :
info_txt.text = "page1";
break;
case "2" :
info_txt.text = "page1";
break;
default :
info_txt.text = "page00";
When I test the movie.. Error Occured (Scene 1, Layer 'Actions', Frame 21, Line 6 1120: Access of undefined property page. )
Can Anyone help please.. How do I correct this....???
Thanks...You should learn to use the trace() function to help you troubleshoot code yourself. In each of your button functions include:
trace("btn clicked"); // will help you know for sure that the buttons are working
And in frame 20, you should add a trace as well to determine whether you actually get there when you think you don't...
trace("in frame 20");
Lastly, your code for frame 20 is likely not what you need for a couple of reasons...
1) If you think the ENTER_FRAME listener is used to detect/trigger when the timeline enters a frame, that is not what it does. What it does is continually process the function that it calls at the frame rate of the file. It is normally only used when you want to repeatedly trigger some functionality.
2) If you assign number values to the page variable, you do not want to use String values in your cases.
All you should need in frame 20 is...
switch (page)
case 1 :
info_txt.text = "page1";
break;
case 2 :
info_txt.text = "page1";
break;
default :
info_txt.text = "page00"; -
Case statement help needed bit tricky
I have a requirement for a Column Quarters which will idenitfy records based on their service and paid paids.
Output of Column should be
QTR
FSY2015Q1
Our Quarters start like this Q1= 9/1 /2014-11.30.2014 , Q2 12/1/2014 -02/28/2015, Q3: 3/1/2015 - 05/31/2015 Q4:06/01/2015 -08/31/2015.
With the below Case statement records that hit Q1 should be for the FSY upcoming. records in Sept, Oct, November of 2014, would actually be for FSY15Q1 (not FSY14Q1). How can i correct
this ?
case
when datepart(mm, Service_Through_Date ) between '9' and '11' and datepart(mm, c.paid_date ) between '9' and '11'
Then 'Q1 '
when datepart(mm, Service_Through_Date ) in ('01','02', '12' ) and datepart(mm, paid_date ) in ('01','02' , '12' )
Then 'Q2 '
when datepart(mm, Service_Through_Date ) between '03' and '05' and datepart(mm, paid_date ) between '03' and '05'
Then 'Q3 '
when datepart(mm, Service_Through_Date ) between '06' and '08' and datepart(mm, paid_date ) between '06' and '08'
Then 'Q4 '
else ' ' end + ' ' + 'FSY ' + '' + cast(datepart(Year, Service_Through_Date)as char(7)) as QTR
FMYour best bet here is going to be to create a calendar table, which has your quarters in it.
I wrote an article on them here: http://social.technet.microsoft.com/wiki/contents/articles/29260.tsql-calendar-functions-and-tables.aspx.
For your particular scenario you'll need to add a column to the existing code, something like this:
SELECT CASE WHEN DATEPART(MONTH,@date) IN (9,10,11) THEN 'FSY'+RIGHT(YEAR(@date)+1,2)+'Q1'
WHEN DATEPART(MONTH,@date) IN (12) THEN 'FSY'+RIGHT(YEAR(@date)+1,2)+'Q2'
WHEN DATEPART(MONTH,@date) IN (1,2) THEN 'FSY'+RIGHT(YEAR(@date),2)+'Q2'
WHEN DATEPART(MONTH,@date) IN (3,4,5) THEN 'FSY'+RIGHT(YEAR(@date),2)+'Q3'
WHEN DATEPART(MONTH,@date) IN (6,7,8) THEN 'FSY'+RIGHT(YEAR(@date),2)+'Q4'
END AS fiscalQuarterName
Once you have this column, you can simply join to the calendar table:
SELECT service_through_date, paid_date, sc.fiscalQuarterName, pc.fiscalQuarterName
FROM myTable t
INNER JOIN calendar sc
ON t.service_through_date = c.today
INNER JOIN calendar pc
ON t.paid_date = pc.today
If you can't, or don't want to impliment this, you could solve the issue in your cast statement by moving the year into the CASE, like this:
SELECT CASE WHEN DATEPART(MONTH, Service_Through_Date ) BETWEEN 9 AND 11 AND DATEPART(MONTH, c.paid_date) BETWEEN 9 AND 11 THEN 'FSY'+RIGHT(YEAR(Service_Through_Date)+1,2)+'Q1'
WHEN DATEPART(MONTH, Service_Through_Date ) = 12 AND DATEPART(MONTH, c.paid_date) = 12 THEN 'FSY'+RIGHT(YEAR(Service_Through_Date)+1,2)+'Q2'
WHEN DATEPART(MONTH, Service_Through_Date ) BETWEEN 1 AND 2 AND DATEPART(MONTH, paid_date) BETWEEN 1 AND 2 THEN 'FSY'+RIGHT(YEAR(Service_Through_Date),2)+'Q2'
WHEN DATEPART(MONTH, Service_Through_Date ) BETWEEN 3 AND 5 AND DATEPART(MONTH, paid_date) BETWEEN 3 AND 5 THEN 'FSY'+RIGHT(YEAR(Service_Through_Date),2)+'Q3'
WHEN DATEPART(MONTH, Service_Through_Date ) BETWEEN 6 AND 8 AND DATEPART(MONTH, paid_date) BETWEEN 6 AND 8 THEN 'FSY'+RIGHT(YEAR(Service_Through_Date),2)+'Q4'
ELSE 'FSY'+RIGHT(YEAR(Service_Through_Date),2)
END
Don't forget to mark helpful posts, and answers. It helps others to find relevant posts to the same question. -
Case Statement Help needed [Nested]
Hi all,
I need to write a case statement my requirement is
ENC.EET.40 E_Disposition populated from either “E_ Status” field of HP_Response_table, “File Status” of HP _TTT_Response_Table,
or “E_Status” of DDA_R_Table. If no data in any of these fields, default to “Submitted”
So my case stmt should look like
case when E_Status is null then 'SUBMITTED'
when e_status is null then 'Submitted'
when FileStatus is null then 'Submitted'
Else case when E_status is not null then ............?????????????
else case when
Im Confused.. Can someone help me ?
FMselect ...
coalesce (resp.E_Status, resp2.FileStatus, dda.E_Status, 'Submitted')
from ...
Obviously I have used alias for the respective tables according to your logic. -
Formatted search case statement help
Hi all,
I am trying to write a case statement in a formatted search, but am running into an error when I add a condition involving the item code. The code works find with the first WHEN statement involving the customer code $[$4.0.0] however, I receive an internal error when I add the second part involving the item code $[$38.1.1] Please advise.
SELECT
CASE WHEN $[$4.0.0] = 'C00023'
THEN 'E002'
WHEN $[$38.1.1] = 'ItemA'
THEN 'J002'
END
THank you!
JaneHi Jane,
Where do you assign this FMS? If it is on the header, the second condition will not work. If it is line level, try change it to $[$38.1.0\].
Thanks,
Gordon -
Conditional case statement help.!
Hello Forum Members,
I have a table with stores order along with order dates and other information. I have a requirement to write a query to get the nominations from that order's table according to the logic mentioned below.
Nom1 = Initial order date and next order within 7 days from initial order date then its considered as R1
Nom2 = next activity within 7 seven on the same ord_id from its R1 date , this goes on as the order dates increases for same or single ord_no
Final Rn is Count (Rn)
Rn %-- Count(Rn) / Count Distinct ( ord_no ) * 100
Example data output:
ORD_DATE
ORD_ID
1_of_x
2_of_x
3_of_x
4_of_x
5_of_x
6_of_x
Final RnX
Rn%
7/10/2013
10095V1
1
0
0
0
0
0
8/3/2013
10095V1
1
0
0
0
0
0
8/8/2013
10095V1
0
1
0
0
0
0
8/12/2013
10095V1
0
0
1
0
0
0
9/6/2013
10095V1
1
0
0
0
0
0
DDL:
create table xx_ord_noms
ord_no varchar(30),
ord_date date,
ord_id varchar2(30)
Sample Data:
INSERT INTO xx_ord_noms (ord_no, ord_date, ord_id) VALUES ('10091', TO_DATE('07/10/2013','MM/DD/YYYY'), '10091');
INSERT INTO xx_ord_noms (ord_no, ord_date, ord_id) VALUES ('10091', TO_DATE('08/03/2013','MM/DD/YYYY'), '10091');
INSERT INTO xx_ord_noms (ord_no, ord_date, ord_id) VALUES ('10091', TO_DATE('08/08/2013','MM/DD/YYYY'), '10091');
INSERT INTO xx_ord_noms (ord_no, ord_date, ord_id) VALUES ('10091', TO_DATE('08/12/2013','MM/DD/YYYY'), '10091');
INSERT INTO xx_ord_noms (ord_no, ord_date, ord_id) VALUES ('10091', TO_DATE('09/06/2013','MM/DD/YYYY'), '10091');
commit;
I was planning to doing something like this but I am not getting the logic for R1 .. R7
select ord_no,
ord_date,
ord_id,
prev_dt,
(to_date(ord_date) - to_date(prev_dt)) data_diff,
case
when prev_dt is null or
(to_date(ord_date) - to_date(prev_dt)) < 7 then
'R1'
when (to_date(ord_date) - to_date(prev_dt)) > 7 THEN
'R2'
ELSE
nULL
END
FROM (select a.*,
(SELECT MAX(tmp2.ord_date)
FROM xx_ord_noms tmp2
WHERE a.ord_id = tmp2.ord_id
AND tmp2.ord_date < a.ord_date) prev_dt
from xx_ord_noms a);
Could someone give me advises or suggestions on how we can get the expected output.
Thanks in advance.LAG and LEAD are good when you want to look forward or back a fixed number of rows, but in this case you don't know how far back you'll need to go.
I've got a solution. it calculates the Rn as a number, not a column position, but you can transform that with CASE statements if you want. I did it in two main steps:
First I calculated the Rn by comparing the date of the current and previous row. To do that, I generated a row number using the row_number() analytic function:
select ord_no,
ord_date,
ord_id,
numrow
, Rn
from xx_ord_noms
model
partition by (ord_no)
dimension by (row_number() over (partition by ord_no order by ord_date) numrow)
measures (ord_date, 0 Rn, ord_id )
rules update
rn[1] = 1,
rn[ANY] = CASE when trunc(ord_date[cv()]) - trunc(ord_date[cv() - 1]) < 7 then rn[cv()-1]+1 else 1 end
ORD_NO ORD_DATE ORD_ID NUMROW RN
10091 10-Jul-2013 00:00:00 10091 1 1
10091 03-Aug-2013 00:00:00 10091 2 1
10091 08-Aug-2013 00:00:00 10091 3 2
10091 12-Aug-2013 00:00:00 10091 4 3
10091 06-Sep-2013 00:00:00 10091 5 1
Then I needed to identify all the rows of each series. I defined firstDt as the date of the first item in the series:
firstDt[1] = ord_date[cv()]
firstDt[ANY] = CASE when rn[cv()]= 1 then ord_date[cv()] else firstDt[cv()-1] end
With that, the FinalRn is just an analytic max(rn) over (partition by firstDt)
I'm not clear on what R% is. Your descriptions are unclear and I think they're inconsistent.
select ord_no, ord_date, ord_id
, Rn, finalRn, 100*finalRn/totCnt PCT1, 100 * totRn/totCnt PCT2
, totRn
from xx_ord_noms
model
partition by (ord_no)
dimension by (row_number() over (partition by ord_no order by ord_date) numrow)
measures (ord_date, 0 Rn, ord_id, to_date(null) firstDt, 0 finalRn, 0 totRn, 0 totCnt )
rules upsert all
rn[1] = 1
, rn[ANY] = CASE when trunc(ord_date[cv()]) - trunc(ord_date[cv() - 1]) < 7 then rn[cv()-1]+1 else 1 end
, firstDt[1] = ord_date[cv()]
, firstDt[ANY] = CASE when rn[cv()]= 1 then ord_date[cv()] else firstDt[cv()-1] end
, finalRn[ANY] = max(rn) over (partition by firstDt)
, totCnt[ANY] = count(*) over()
, totRn[ANY] = count(case when rn = 1 then 1 else null end) over ()
ORD_NO ORD_DATE ORD_ID RN FINALRN PCT1 PCT2 TOTRN
10091 10-Jul-2013 00:00:00 10091 1 1 20 60 3
10091 03-Aug-2013 00:00:00 10091 1 3 60 60 3
10091 08-Aug-2013 00:00:00 10091 2 3 60 60 3
10091 12-Aug-2013 00:00:00 10091 3 3 60 60 3
10091 06-Sep-2013 00:00:00 10091 1 1 20 60 3
Regards,
David -
SQL Case statement Help....
Hi , I have to Compare two columns to see if they are matching or not....and give the count() of overrides if they are not matching/
I have got that with the below SQL:
Count of Overrides =
select count(Case when Col_1 != Col_2 then 1 else null end) Overrides
from table_1, table_2
where table_1.id = table_2.id
But now i have to look at two columns and get the count()
I have
col_1 and col_2 in table_1 and
col_3 and col_4 in table_2
(I have to compare col_1 with col_2 and...... col_3 with col_4 and if either of them is not matching.... count it as an override
Will the following SQL return correct values?
Count of Overrides =
Select
Count( Case when (col_1 ! = col_2) or (col_3 != col_4) then 1 else null end) Overrides
from table_1, table_2
where table_1.id = table_2.id
Thanks"yes, it can be rewritten as a decode. but why bother?"
Well, although I do like Sy's code better, the decode version does naturally handle NULLs unlike the case.
SQL> WITH t AS (
2 SELECT 1 col_1, 1 col_2, 2 col_3, 2 col_4 FROM dual UNION ALL
3 SELECT 1, 1, 2, 3 FROM dual UNION ALL
4 SELECT 1, 2, 3, 3 FROM dual UNION ALL
5 SELECT null, 1, 2, 2 FROM dual UNION ALL
6 SELECT 1, 1, 2, null FROM dual UNION ALL
7 SELECT 1, 2, 3, 4 FROM dual)
8 SELECT col_1, col_2, col_3, col_4,
9 CASE WHEN (col_1 ! = col_2) or (col_3 != col_4) then 1 else null end Ocase,
10 DECODE(col_1, col_2, DECODE(col_3, col_4, NULL, 1), 1) odecode
11 FROM t;
COL_1 COL_2 COL_3 COL_4 OCASE O
1 1 2 2
1 1 2 3 1 1
1 2 3 3 1 1
1 2 2 1
1 1 2 1
1 2 3 4 1 1John -
Please help: Case Statement - ORA-01722: invalid number Error
Hi All,
I'm trying to use Case statement to recode Cancelled statuses into two groups
'Reject' if the difference between first date and last date is less than 29 and
'Accept' if the difference between first date and last date is greater than 30
Here is my 'test' data:
STATUS FIRST_DATE LAST_DATE
Transfer 10/08/2011 10/09/2011
Mover 10/08/2011 15/09/2011
Cancel 10/08/2011 16/09/2011
Cancel 10/08/2011 5/09/2011
Here is the syntax
select a.*,
(CASE WHEN a.STATUS ='Cancel' THEN (round(a.LAST_DATE-a.FIRST_DATE))
ELSE 0
END) CAN_DAYS,
(CASE WHEN 'CAN_DAYS' >29 THEN 'Reject'
WHEN 'CAN_DAYS' <30 THEN 'Accept'
END) Reject_Accept
from test a
The first CASE statement works fine and gives me this
STATUS FIRST_DATE LAST_DATE CAN_DAYS
Transfer 10/08/2011 10/09/2011 0
Mover 10/08/2011 15/09/2011 0
Cancel 10/08/2011 16/09/2011 37
Cancel 10/08/2011 5/09/2011 26
but the second CASE produces ORA-01722: invalid number Error. It is something to do with the CAN_DAYS data type, Oracle doesn't see it as Numeric i think.
The result i want to see would be
STATUS FIRST_DATE LAST_DATE CAN_DAYS Reject_Accept
Transfer 10/08/2011 10/09/2011 0
Mover 10/08/2011 15/09/2011 0
Cancel 10/08/2011 16/09/2011 37 Accept
Cancel 10/08/2011 5/09/2011 26 Reject
Could someone please help me to fix the syntax?
Thank you in advance,This seems to give you your desired results...
with test as
( select 'Transfer' as status, to_date('10/08/2011') as first_date, to_date('10/09/2011') as last_date from dual union all
select 'Mover' , to_date('10/08/2011') , to_date('15/09/2011') from dual union all
select 'Cancel' , to_date('10/08/2011') , to_date('16/09/2011') from dual union all
select 'Cancel' , to_date('10/08/2011') , to_date('05/09/2011') from dual
select status
, first_date
, last_date
, CAN_DAYS
, CASE
WHEN CAN_DAYS = 0 THEN NULL
WHEN CAN_DAYS < 30 THEN 'Reject'
WHEN CAN_DAYS >= 30 THEN 'Accept'
END Reject_Accept
from ( select status
, first_date
, last_date
, CASE
WHEN STATUS ='Cancel' THEN round(LAST_DATE-FIRST_DATE)
ELSE 0
END CAN_DAYS
from test
; -
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 -
Case Statement in a Where clause help
Oracle Database 11g Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
"CORE 11.2.0.1.0 Production"
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
Hello,
I have an APEX application that I need to build a SQL statement for a LOV (List of Values). I have a hidden filed that contains the customer type which can be an 'R' or 'B'. The query needs to be able to display two different result sets based on the customer type of 'R' or 'B'.
If the customer type is 'R' then:
SELECT drg_descr d, drg_code r
FROM distance_ranges
WHERE drg_min_miles IN (0,5)
ORDER BY drg_min_milesIf the customer type is 'B' then:
SELECT drg_descr d, drg_code r
FROM distance_ranges
WHERE drg_min_miles IN (0,5,10,15,20)
ORDER BY drg_min_milesCan someone help me with what I think needs to be a case statement?
Thanks,
JoeHi,
You can try CASE statement with WHERE clause
SELECT drg_descr d, drg_code r
FROM distance_ranges
WHERE (CASE param_cust_type
WHEN(param_cust_type='R') THEN (drg_min_miles IN (0, 5)
WHEN (param_cust_type='B') THEN (drg_min_miles IN (0,5,10,15,20)
END;
Please try and let me know if anything wrong.
Anyone from the forum comment my code if there is any wrong.
Thanks!
Naresh -
CASE Statement error in function -- Please help!
Hi All,
I created a function in my report.
It has a SQL query similar to the one below. The query works fine in SQL plus.
It has a CASE statement. I am getting error at line4 at the select statement. Error is: "Encountered symbol 'SELECT'....."
Can we use CASE statements like this in reports that use SELECT statements for RETURN EXPRESSIONS. Do we have to do any special?
Can someone help me out of this trouble?
THanks in advance.
SELECT PARENT_id,
CASE WHEN EXISTS (SELECT PARENT_id FROM CHILD CH1 WHERE CH1.PARENT_id = PARENT.PARENT_id AND UPPER(CH1.description) LIKE '%ABC%') THEN
(SELECT CH2.MOD_id FROM CHILD CH2 WHERE CH2.PARENT_id = PARENT.PARENT_id AND UPPER(CH2.description) LIKE '%ABC%')
ELSE
(SELECT MOD_id FROM
(SELECT MOD_id,PARENT_id FROM CHILD CH3 ORDER BY started) MOD2 WHERE MOD2.PARENT_id = PARENT.PARENT_id AND ROWNUM = 1
END
) MOD_ID
FROM PARENT;take out the parentheses after the PARENT_ID and see
like below
SELECT parent_id,
CASE
WHEN EXISTS (
SELECT parent_id
FROM CHILD ch1
WHERE ch1.parent_id = PARENT.parent_id
AND UPPER (ch1.description) LIKE '%ABC%')
THEN (SELECT ch2.mod_id
FROM CHILD ch2
WHERE ch2.parent_id = PARENT.parent_id
AND UPPER (ch2.description) LIKE '%ABC%')
ELSE (SELECT mod_id
FROM (SELECT mod_id, parent_id
FROM CHILD ch3
ORDER BY started) mod2
WHERE mod2.parent_id = PARENT.parent_id AND ROWNUM = 1)
END AS mod_id
FROM PARENT; -
Need help on case statements to validate records
Hi Experts ,
My table :
seq_num
col2
col3
col4
1
A
12345
P
2
B
1
123%23
3
C
1
23AB
4
D
1
20131001
5
E
1
6
A
13245
Q
7
B
1
12345
8
C
2
1234*AB
9
D
5
20140112
10
E
1
00020
my output
seq_num
col2
col3
col4
Status
Reason
1
A
12345
P
Valid
2
B
1
123%23
invalid
Special Character for col4
3
C
1
23AB
Valid
4
D
1
20131001
Valid
5
E
1
invalid
null for col4
6
A
13245
Q
invalid
Invalid character col4 || invalid number for col3
7
B
1
12345
Valid
8
C
2
1234*AB
Invalid
Special Character col4 ||invalid col3
9
D
5
20140112
invalid
Future dates col4 ||invalid col3
10
E
1
00020
Valid
Sql :
with t as
( select 1 as seq_num,'A' as col2 ,12345 as col3 ,'P' as col4 from dual
union all
select 2 ,'B',1,'123%23' from dual
union all
select 3,'C',1,'23AB' from dual
union all
select 4,'D',1,'21-02-2013' from dual
union all
select 5,'E',1,null from dual
union all
select 6,'A,13245,'Q' from dual
union all
select 7,'B',1,12345 from dual
union all
select 8,'C',2,'1234*AB' from dual
union all
select 9,'D',5,'25-01-2014' from dual
union all
select 10,'E',1,20 from dual
I am applying rules on col3 and col4 for each records row-wise.
I need case statements to populate status and reason columns after applying below rules
Rules
Col3 :
For A record ,it should be 12345 always .
For B,C,D,E , record should be always 1
col4
For A record , it should be either P or R
No null values for all A, B,C,D,E records
for B record , it dont contain special charecters
for C RECORD , it dont contain special charecters
for D record ,it should not contain future dates (dates are in yyyymmdd format and less than sysdates are valid )
I have other columns as well ,as i not included here
.It would be great if you Could help on case statements
Thanks and Regards,
SumanthI've adjusted Gregs nice example a bit. This should work:
with w_base as (
select seq_num, col2, col3, col4,
case when (col2 = 'A' AND col3 = 12345 )
OR (col2 in ('B','C','D','E') AND col3 = 1)
then '' else '||invalid col3' end ||
case when (col2 = 'A' AND col4 not IN ( 'P', 'R' ) )
then '||invalid col4' else '' end ||
case when (col2 IN ( 'B', 'C' ) AND col4 != translate(col4, 'a!@#$%^*()','a') )
then '||special character for col4' else '' end ||
case when (col2 = 'D' AND col4 >= to_char(sysdate,'yyyymmdd') )
then '||future dates col4' else '' end
reason
from ( select 1 as seq_num, 'A' as col2, 12345 as col3, 'P' as col4 from dual union all
select 2, 'B', 1, '123%23' from dual union all
select 3, 'C', 1, '23AB' from dual union all
select 4, 'D', 1, '20130212' from dual union all
select 5, 'E', 1, null from dual union all
select 6, 'A', 13245, 'Q' from dual union all
select 7, 'B', 1, '12345' from dual union all
select 8, 'C', 2, '1234*AB' from dual union all
select 9, 'D', 5, '20140125' from dual union all
select 10, 'E', 1, '20' from dual )
Select seq_num, col2, col3, col4,
case when reason is null then 'Valid' else 'Invalid' end status,
substr(reason, 3 ) reason
from w_base
It returns
SEQ_NUM
COL2
COL3
COL4
STATUS
REASON
1
A
12345
P
Valid
2
B
1
123%23
Invalid
special character for col4
3
C
1
23AB
Valid
4
D
1
20130212
Valid
5
E
1
Valid
6
A
13245
Q
Invalid
invalid col3||invalid col4
7
B
1
12345
Valid
8
C
2
1234*AB
Invalid
invalid col3||special character for col4
9
D
5
20140125
Invalid
invalid col3||future dates col4
10
E
1
20
Valid
edited some bugs :) now it should be fine! really -
Help: How to use Case statement in Interface ODI11g?
Hi
From my Source base i am getting 'Year' Values and i want translate these values into a code in interface and after translate want to push to Target system.
Example:
From source Database i am getting value for
Year
2010
2011
2012
When i get Year 2010 i want to modify value into 'FY10'
when i get Year 2011 i want to modify value into 'FY11'
and same for Year 2012 TO 'FY12'
I was trying to do by Case statement but didn't got success.
I dont want to create Lookup table in source system.
Any help in this regards.
Thanks
Regards
Sher
Edited by: Sher Ullah Baig on Aug 26, 2012 5:52 PMCASE
WHEN source_column='2010' THEN 'FY10'
WHEN source_column='2011' THEN 'FY11'
WHEN source_column='2012' THEN 'FY12'
END
Maybe you are looking for
-
Advice required fo new SSD/HDD setup and OS/App re-install
Hi, Over the last few weeks my HDD has been failing verification (4 times in the last fortnight ). I have used both Disk Utility and DiskWarrior to repair the drive and each time the outcome has been successful, only to have Verify Disk report errors
-
When is a .psd file NOT a .psd file?
As a result of leaning how to make a mask, I discovered that I could also paste directly from the net into an AI page. It's the "saving" that costs the question. My reviewer, who checks out my design ads and brochures does not like .psd's to be "Pla
-
Database Lite and JDeveloper / ADF
Hello, I'm planning to create a small application which will run on mobile PC in an off-line mode, having synchronization with master DB once connected to the proper network. The main architecture on the target PC may be : - Oracle Lite - GlassFish -
-
Captivate 8 Smart Shapes as Buttons to show on all slides
I am having to create custom navigation for my Captivate. I know I can either put them on my Master Slide and select the option "Show Master Slide Objects on Top", or I can put the on the first slide and have them show until the end of the project. I
-
Problem accessing BI Publisher.
Hi, Am a newbie trying to learn OBIEE. Am facing issues accessing BI Publisher. When I access BI Publisher as an Administrator, it lets me in but all that I could see in the 'Reports' tab is that there are 2 folders 'Shared Folder' and 'My Folder' an