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
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 -
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 -
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. -
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.
ThanksI 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 -
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 -
CASE Statement in Where Condition with Multi Valued parameter in SSRS
Hi All,
I am little confused while using CASE statement in Where condition in SSRS. Below is my scenario:
SELECT
Logic here
WHERE
Date IN (@Date)AND
(CASE
WHEN NAME LIKE 'ABC%' THEN 'GROUP1'
WHEN ID IN ('123456', '823423','74233784') THEN 'GROUP2'
WHEN ABC_ID IS NULL THEN 'GROUP3'
ELSE 'GROUP4'
END ) IN (@GROUP)
So above query uses WHERE condition with CASE statement from @GROUP parameter. I want to pass this parameter as multi- valued parameter and hence I have used CASE statement IN (@GROUP).
For @Date one dataset will pass the available and default values and
for @GROUP parameters, another dataset will pass the available and default values.
But this is not working as expected. Please suggest me where I am making mistake in the query.
Maruthu | http://sharepoint-works.blogspot.comHi Maruthu,
According to your description, I create a sample report in my local environment. It works as I expected. In your scenario, if the selected values from the Date parameter contains some of the Date field values, the selected values from the GROUP parameter
contains some of GROUPS (‘GROUP1’,’GROUP2’,’GROUP3’,’GROUP4’) and the corresponding when statement is executed , then the dataset returns the corresponding values.
In order to trouble shoot this issue, could you tell us what results are you get and what’s your desired results? If possible, you can post the sample data with sample dataset, then we can make further analysis and help you out.
Thanks,
Katherine Xiong
Katherine Xiong
TechNet Community Support -
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 -
How to manipulate arrays using case statements and boolean conditions?
In the vi that is attached I am trying to compare two values in two different arrays and delete the value that is equal to zero. The values of each array are rounded to the closest integer. Then I attempted to apply the ">0" boolean as the condition for my case statement, but I am getting an error. Any tips on manipulating arrays with case statements?
Attachments:
Patient Movement Monitoring.vi 141 KBThank you!!! that was a huge help. I don't think I need the case structures at all. The next part of the code compares the 4 values in the array.
If columns 0 and 1 are both positive -> output 1
If column 0 is negative and 1 is positive -> output 2
If columns 0 and 1 are both negative -> output 3
If column 0 is positive and 1 is negative -> output 4
(0 is x-axis value, 1 is y-axis value.....outputs are assigning quadrants to the combination)
Only one of the "AND" booleans will return true for each index. Is there a way to initialize another array of outputs 1-4 depending on which AND returns true?
Attachments:
Patient Movement Monitoring.vi 144 KB -
Checking for the condition types using case statement
hi folks,
I have a lot of condition types that I have to check for and I am using case statement to do that. The code goes like this.
case wac-kschl.
when 'ZRAT' OR 'ZAGR' OR 'ZRCR' OR
'Y098' OR 'Y007' OR 'ZREW' OR 'Y106' OR 'ZTSR' OR 'Y127' OR 'Y125' OR 'Y126' OR 'Y124' OR 'Y157' OR 'Y092' OR 'Y085' OR 'Y090' OR 'ZMZD'
OR 'Y215' OR 'Y214' OR 'Y111' OR 'ZC$D' OR 'ZAUD'.
up till here it is working on errors and when I add few more condition types to the case statement it is throwing the error.
I have to check for all the condition types out here.
How can I correct it? Is there a better way to do it?
thanks
SanthoshHi Santhosh,
I think that your CASE statement has a flaw. The line length of one of the lines is too large. You need to insert a carriage-return to shorten it (or press the button 'Pretty Printer').
The code would look nicer like this:[code] CASE wac-kschl.
WHEN 'ZRAT' OR 'ZAGR' OR 'ZRCR' OR 'Y098' OR 'Y007' OR 'ZREW'
OR 'Y106' OR 'ZTSR' OR 'Y127' OR 'Y125' OR 'Y126' OR 'Y124'
OR 'Y157' OR 'Y092' OR 'Y085' OR 'Y090' OR 'ZMZD' OR 'Y215'
OR 'Y214' OR 'Y111' OR 'ZC$D' OR 'ZAUD' OR 'Z001' OR 'Z002'
OR 'Z003' OR 'Z004' OR 'Z005' OR 'Z006' OR 'Z007' OR 'Z008'
OR 'Z009' OR 'Z010' OR 'Z011' OR 'Z012' OR 'Z013' OR 'Z014'.
Do your thing here
WRITE: / 'OK'.
WHEN OTHERS.
WRITE: / 'NOT OK'.
ENDCASE.[/code]If this will not work for you, you could try a different approach:[code]* Local definition
DATA:
var_list(1024).
Build variable string for checking
CONCATENATE 'ZRAT ZAGR ZRCR Y098'
'Y007 ZREW Y106 ZTSR'
'Y127 Y125 Y126 Y124'
'Y157 Y092 Y085 Y090'
'ZMZD Y215 Y214 Y111'
'ZC$D ZAUD'
INTO var_list
SEPARATED BY space.
Check if the correct value is supplied
IF var_list CS wac-kschl.
Do your thing here
WRITE: / 'OK'.
ENDIF.[/code]Hope this helps you a bit.
Regards,
Rob. -
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 -
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
Maybe you are looking for
-
Could not sign in to facetime, network connection error message.
Hi, I have Macbookpro running snowleopard OS X 10.6.8. I purchased facetime 1.0.2 from the app store and am having a problems trying to sign in. I keep getting a message to check with network connection. I have facetime on my ipod touch 5th gener
-
Download and Upload Modulepool program
hai all, Could any one say how to Download and Upload Modulepool program from sap. Thanks, Jeevan.
-
I constantly keep getting thsi error message and have no idea how to fix this issue. Is there anything I can do to fix it?
-
Mp3's not playing back correctly
Hey guys/gals, I'm in need of alittle help. I just installed iTunes 10.5.0.142 and i've been using iTunes 10.0 previous to this and now i find that all my songs that are in MP3 format are all scrambled. I've walked through everything i can think of,
-
Dear Experts, Is there any way through which we can maintain daily employee task sheet? This task sheet will contain details like, Personnel Number, Date of task, title of task, date of completion of task etc. Is this scenario possible through SAP HC