Help on converting query with case into decode
Hi Can anyone help me to revise my plsql query? Below is a portion og the procedure I am changing to meet Oracle 8i.
SELECT SUM(NVL(CASE WHEN (weeks='wk1' ) THEN qty END, 0)) wk1,
SUM(NVL(CASE WHEN (weeks='wk2' ) THEN qty END, 0)) wk2,
SUM(NVL(CASE WHEN (weeks='wk3' ) THEN qty END, 0)) wk3,
SUM(NVL(CASE WHEN (weeks='wk4' ) THEN qty END, 0)) wk4
FROM TABLE1
Thanks in advance.
pls try this....(code not tested as no create table etc provided).
select dt
from (select decode(is_tbd,
'yes',
decode(is_tbd_order,
'no',
'tbd',
decode(sign(ex_fac_date - (v_asofdate + 131)),
1,
'tbd',
null),
decode(sign(ex_fac_date - (v_asofdate + 131)),
1,
'tbd',
null)),
null) dt
from dual
union all
select decode(sign(ex_fac_date - (v_asofdate - 1)),
1,
decode(sign(ex_fac_date - (v_asofdate + 5)),
0,
decode(is_tbd, 'no', 'wk1', null),
null),
null)
from dual
union all
select decode(sign(ex_fac_date - (v_asofdate + 6)),
1,
decode(sign(ex_fac_date - (v_asofdate + 12)),
0,
decode(is_tbd, 'no', 'wk2', null),
null),
null)
from dual
select decode(sign(ex_fac_date - (v_asofdate + 13)),
1,
decode(sign(ex_fac_date - (v_asofdate + 19)),
0,
decode(is_tbd, 'no', 'wk3', null),
null),
null)
from dual
union all
select decode(sign(ex_fac_date - (v_asofdate - 1)),
0,
decode(is_tbd, 'no', 'past_due', null),
null)
from dual
where dt is not null
Regards
Similar Messages
-
Like Query with Case insensitive
Hi ,
I have select query with Like, need to add case insenstive for that.
E.g
Select * from emp where empname like ('D%').
The above query will fetch only names starting with 'D' not the name starting with 'd'.But i need to fetch all the name starting with D or d. Can anyone help me out with this query.
Thanks
DuraiHi
Use uppercase for example:
Select * from emp where upper(empname) like ('D%');
Ott Karesz
http://www.trendo-kft.hu -
Help in to convert sql from CASE to DECODE in 11g
11gr2, Windows.
Can somebody help me out in re-writing below sql from CASE statement to DECODE ?
SELECT INVOICE_ID,sum(TOTAL_EXCL_VAT),LEVEL2 as descr,
sum(case when instr(LEVEL2,'Internet')>0 then SUM_QUANTITY else 0 end) as KB --- >> Here
from rator_cdr.INVOICE_DETAIL_LINE
WHERE INVOICE_ID ='000000000000000000' and CALLER='0000000000' and LEVEL1='Gesprekskosten'
group by LEVEL2,INVOICE_ID;ThanksThere is no difference in performance. CASE is a standard syntax that is relatively easy for any developer to read and follow. DECODE is an Oracle-specific function that is much less flexible (you can only use equality conditions, for example) and generally results in more cryptic code.
Justin -
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 -
Help needed in writing SQL CASE or DECODE statement
Hi experts,
I need to write a SQL to select order_num, cntry_cde, prod_id and Qty by joining order_num on PROD_ORDER and PROD_ORDER_TXT.
Here is my sample data
PRODORDER_
order_num cntry_cde Prod_id Qty
100 US A1 5
101 US A2 10
102 AU A3 4
103 AU A4 9
104 IN A5 3
PRODORDER_TXT_
order_num cntry_cde Prod_id
100 US A1
101 US A2
102 NZ A3
103 AU A4
104 A5
Here is the requirement,
1) If the cntry_cde in PROD_ORDER is same as cntry_cde in PROD_ORDER_TXT then select PROD_ORDER.cntry_cde (orders 100, 101, 103)
2) If they are different, pick the country code from PROD_ORDER_TXT (order 102, AU <> NZ)
3) If they are different and PROD_ORDER_TXT.cntry_cde is NULL, I cannot use it as cntry_cde in my report (order 104). It happenend just because of the bad data at source.
I cannot avoid it. Then simply use the cntry_cde from PROD_ORDER
Output expected
100 US A1 5
101 US A2 10
102 NZ A3 4 -- AU changed to NZ
103 AU A4 9
104 IN A5 3 -- IN retained as PROD_ORDER_TXT.cntry_cde is null
sample table creation and insert statements are below
create table prod_order
(order_num number,
cntry_cde CHAR(2),
prod_id VARCHAR2(6),
qty number)
create table prod_order_txt
(order_num number,
cntry_cde CHAR(2),
prod_id VARCHAR2(6))
insert into prod_order values (100, 'US', 'A1',5);
insert into prod_order values (101, 'US', 'A2',1);
insert into prod_order values (102, 'AU', 'A3',4);
insert into prod_order values (103, 'AU', 'A4',9);
insert into prod_order values (104, 'IN', 'A5',3);
insert into prod_order_txt values (100,'US','A1');
insert into prod_order_txt values (101,'US','A2');
insert into prod_order_txt values (102,'NZ','A3');
insert into prod_order_txt values (103,'AU','A4');
insert into prod_order_txt values (104,NULL,'A5');
commit;
Thanks for your help in advance
Edited by: sarvan on Mar 28, 2012 1:39 PMHello
Thank you for posting all of the ddl and test data along with your expected output - very helpful!. One small point would be to remember to type {noformat}{noformat} before and after any section of code or data in your post so that formatting is retained. Anyway, this should be a simple join and a combination of CASE and NVL...Select
po.order_num,
CASE
WHEN po.cntry_cde != NVL(pot.cntry_cde,po.cntry_cde)
THEN
pot.cntry_cde
ELSE
po.cntry_cde
END cntry_code,
po.prod_id,
po.qty
FROM
prod_order po
JOIN
prod_order_txt pot
ON
( po.order_num = pot.order_num
ORDER_NUM CN PROD_I QTY
100 US A1 5
101 US A2 1
102 NZ A3 4
103 AU A4 9
104 IN A5 3
5 rows selected.
HTH
David
Edited by: Bravid on Mar 28, 2012 8:32 AM
corrected != -
my table : workingdate
sno name startdate
1 ss 1-12-2011
2 bb 1-11-2011
I can update name or start date or both .i will pass new date,name to update through front end.
I need to validate the condition in case statement i.e. when the new date passed check whether it is greater then start date. if it is greater then it should update with new start date.
if no then it should be update it will old value.
i using below query
update workingdate set name='sss',startdate=(
case
when
(TO_DATE('2011-12-10','yyyy-MM-dd') > (select TO_DATE(startdate,'yyyy-MM-dd') from workingdate where sno=1))
then TO_DATE('2011-12-10','yyyy-MM-dd')
else
select TO_DATE(start_date,'yyyy-MM-dd') from workingdate where sno=1
end
where sno=1;
but when i execute the above query it shows me
Error report:
SQL Error: ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action:
how could i resolve this.......................Check this out
UPDATE workingdate
SET NAME='sss',
startdate=(CASE WHEN (TO_DATE('2011-12-10','yyyy-MM-dd') > startdate THEN TO_DATE('2011-12-10','yyyy-MM-dd') ELSE start_date END)
WHERE sno=1;Cheers
Kanchana -
UDF: How to convert string with sign into integer
Hi
I have a number e.g 123456 (xsd:string) and sign - or + (xsd:string) and both are concatenated and fed into UDF. I want this string to be converted into Integer. If I use the below kind of statement it is not working and throwing the below error.
int BilledAmount = Integer.parseInt(b[0]);
Exception:
RuntimeException in Message-Mapping transformation: Exception:[java.lang.NumberFormatException: For input string: "+000000000006684"]
Any guess on this issue ??
Regards
KumarHey buddy,
Hope you are doing gr8, I'm getting ready leaving this friday..:-)
Aamir b[0] doesn't correspond to the first character of the string. He has multiple input values, hence he is using Context. See my structure and UDF I mentioned above, actually the above is his requirement I believe.
Yes, If you have + symbol in front of String and if you try to conver to Integer object it will throw an exception. Coz it doesn't make sense to add + symbol at all. if you don't have any sign in front it implies it's positive, am I right? But in case of negative, you need to explicitly include - symbol in front.
This is the reason that java couldn't able to convert the string that has + symbol in front, but it can in case it has - symbol in front.
I hope it clears a bit..
raj. -
I have a number of "old" (2009) drawings in Micrografx Designer format, where I would like to modify some of these in the Adobe Illustrator package I got now. Anybody out there that has some good ideas or that may be able to help me?
I did contact Corel, who purchased Micrografx a couple of years ago, and they cannot help (maybe because I am an Apple osx user).
Hope to hear from anyone.CorelDraw, both CorelDraw proper and CorelDesigner purport to open them. I believe you will loose any dimension lines. Could be wrong, but I only looked at a single thread in the Corel forums.
You could download a trial--Windows only I am afraid--and give it a try.
I have CorelDraw installed here if you would like me to see how the faithfully the process really works getting the DRW files into CD and then into Illy. If you would like me to try, either upload one to dropbox.com and post the link here if it isn't sensitive info, or feel free to send me a PM with your email address and I'll send you an email you can respond to and attach a file.
Take care, Mike -
Help! Convert iPhoto folder back into a package!
I had to have my cloud backup company send me back a hard disk containing my iphoto library because my local copy was corrupted. When I received it today, the iPhoto library came back as a folder. It has everything in it, but it's viewed as a folder, not a package. So when I double click it, it doesn't open iPhoto. It's just treated like a folder.
How do I make it back as a package?I doubt if you'll be able to restore your library to working order. Keith's suggestion is worth trying but I'm afraid that all of the links between image files and database files are damaged beyond repair.
The following will be able to get your photos back in a new library with the same event grouping as you had (but probably not the same Event names).
Starting over from scratch with new library
Start over with a new library and import the Originals (iPhoto 09 and earlier) or the Masters (iPhoto 11) folder from your original library as follows:
1. Open the library folder and locate the Masters or Originals folder.
2. Launch iPhoto with the Option key held down and, when asked, select the option to create a new library.
3. Drag the subfolders of the Originals (iPhoto 09 and earlier) or the Masters (iPhoto 11) folder from the open iPhoto Library package into the open iPhoto window a few at a time.
This will create a new library with the same Events (but not necessarily the same Event names) as the original library but will not keep the metadata, albums, books slideshows and other projects.
Note: your current library will be left untouched for further attempts at a fix if so desired. -
Looking help in Default clause with CASE statement ?
I have a table perform(timings date , Mor_Eve varchar2(20) )
Requirements is
(a) If time in timing column is < 12:00 then automatically insert 'MORNING' in Mor_Eve column
ELSE
automatically insert 'EVENING' in Mor_Eve column.
Is there any way of using default clause in create table that insert Morning or Evening in the Mor_Eve column automatically depending on the Timings column ?Hi,
Welcome to the forum!
All the responses above are good.
Why do you need to store this at all?
If you want to display it automatically, create a view, like this:
CREATE OR REPLACE VIEW perform_v
AS
SELECT timings
, CASE TO_CHAR (timings, 'AM')
WHEN 'AM' THEN 'MORNING'
WHEN 'PM' THEN 'EVENING'
ELSE 'NOT ENTERED' -- Skip this line if you want NULL
END AS mor_eve
FROM perform
;Always do DML on the table, but query the view when you want to see mor_eve.
I'm not saying there's no reason to store this; I'm just asking if you have one.
If you must store it, a DEFAULT clause won't work.
Starting in Oracle 11, you can have a virtual column. (You can have an index on a virtual column, but why would you want an index on a column that only had 2 values?)
In any version, you can write a trigger that autmoatically populates the mor_eve column whenever you INSERT or UPDATE. -
Help for a query with minus clause
I want to write a query like this:
select a.field
from table_1 a
minus
select b.field
from table_2 b
where not exists (select 1
from table_3 c
where c.field = a.field);
Unfortunately I get:
ERROR at line 8:
ORA-00904: invalid column name
Can you help me?
Thanks!Hi,
Mark1970 wrote:
I want to write a query like this:
select a.field
from table_1 a
minus
select b.field
from table_2 b
where not exists (select 1
from table_3 c
where c.field = a.field);
Unfortunately I get:
ERROR at line 8:
ORA-00904: invalid column name
Can you help me?
Thanks!Table_1 and its alias, a, are only in scope in the first branch of the query, before the keyword MINUS. The second branch, after MINUS, must be a complete self-contained query.
There are many ways to re-write the query, including:
select a.field
from table_1 a1
minus
select b.field
from table_2 b
where not exists (
select 1
from table_3 c
JOIN table_1 a ON c.field = a.field
);It's suspicious that the EXISTS sub-query does not depend on anything in table_2. Are you sure this is what you want to do?
If you'd like help, post a little sample data (CREATE TABLE and INSERT statements for all three tables) and the results you want from that data. -
Need help Designing a Query -with RKFs and CKF or alternate
Hi,
Trying to create a query off a HR Headcount IC - to display number of top performing employees (based on appraisal attribute) by region and by function. The format is as follows:
Created 3 RKF's in column structure where
1RKF is KF "Employee Numbers" for characteristics restrictions of date range1, appraisal rating=1,2, employee status =active etc.
2RKF is KF "Employee Numbers" for characteristics restrictions of date range1, appraisal rating=#, employee status =active etc.
3RKF is KF "Employee Numbers" for characteristics restrictions of date range2, appraisal rating=1,2, employee status =active etc.
1CKF is Boolean result of (1RKF OR (2RKF AND 3RKF)
Was able to display correctly all top performing employees in the following format. When "Employee" was selected to row.
Top Performer (CKF)
Employee 1----
1
Employee 2----
1
Employee 3----
1
Employee 4----
1
Employee 5----
1
Employee 6----
1
Employee 7----
1
When tried to display the same by Region by Function or even by only Region (Row Char) it doesnt work. Data is non-sensical.
# of Top performers
Region 1 -
FunctionA----
5
Region 1 -
FunctionB----
2
Region 1 -
FunctionC----
3
Region 2 -
FunctionA----
4
Region 2 -
FunctionB----
0
Region 2 -
FunctionC----
7
Region 2 -
FunctionD----
5
Understand that the Boolean CKF is not working correctly. What am I doing wrong ? Please advice fix or alternates.
Thanks.Hi Pradeep,
not possible to use ranking since Appraisal rating is a characterstics not a key figure value. Any other suggestions..
Thanks
Arun -
[SOLVED]Please help me convert this xrandr script into a xorg.conf
Hi!
First of all, I hope this is posted in the right forum section.
My laptop is often connected to my tv when I'm at home, using only that screen. I have a xrandr script pasted in /usr/share/config/kdm/Xsetup that sets the resolution right for KDM, but it does not run early enough, so login screen looks weird, 70% of the screen is moved to the left and the rest is moved to the right. Kind of hard to explain. I am thinking a proper xorg.conf would solve this issue, but I don't really know how that would work out if the tv is not connected. Here is the script:
#!/bin/bash
if xrandr -q | grep "HDMI1 connected" > /dev/null ; then
xrandr --output HDMI1 --mode 1360x768
xrandr --output LVDS1 --off
else
xrandr --output LVDS1 --mode 1366x768
xrandr --output HDMI1 --off
fi
Rather basic, checks if tv is connected and makes proper adjustments if so. Would a xorg.conf work, or is there a file parsed earlier that I could use? KDM is started with inittab method. Thanks in advance!
Last edited by KlavKalashj (2011-06-07 10:27:27)KlavKalashj wrote:...
Sorry for going off-topic, but I love that nick name. Got that from that
Simpsons episode, right?
For those who don't know:
http://www.imdb.com/title/tt0701230/quotes wrote:Khlav Kalash Vendor: Fresh Khlav kalash! Get your khlav kalash!
Homer Simpson: Hey, uh, could you go across the street and get me a slice of pizza?
Khlav Kalash Vendor: No pizza, only khlav kalash.
Homer Simpson: Aw, shoot... Oh, all right, all right, gimme one bowl.
Khlav Kalash Vendor: [whips out something reddish on a stick] No bowl, stick, stick.
Homer Simpson: [Homer buys one, tries it] Oh, geez! That's just awful.
[finishes it]
Homer Simpson: Now, what do you have to wash that awful taste out of my mouth?
Khlav Kalash Vendor: Mountain Dew or Crab Juice.
Homer Simpson: Blecch! Ew! Sheesh! I'll take a crab juice.
[Homer drinks the can of crab juice, then several more]
Homer Simpson: [burps] Uh-oh. Uh, you got a men's room in there?
Khlav Kalash Vendor: [disturbed] Only khlav kalash. Men's room in tower. Tower! Observation deck! -
Please convert decode query to case expression
Please convert decode query to case expression
Decode(Sign(id), 1, id, null) positive, Decode(Sign(id), -1, id, null) negative from dual;this is a serious forums that help people, if you want that we do your "homeworks" i must told you that you should pay us for that.
Edited by: Fran on 05-jun-2013 23:41
1002966
Handle: 1002966
Status Level: Newbie
Registered: Apr 28, 2013
Total Posts: 20
Total Questions: 12 (12 unresolved) -
Help build a query !!!
There are two tables. One consists of members with expenses and next with fixed percentile. I need to build a single query based upon following
1. Need to know the number of members that fall under a fixed range of percent like 10%, 40%, 50%. Suppose, total no of members = 50 then
10% -> 5
40% -> 20
50% -> 25
2. Now I need to rank the members as per their expenses. So, if following be the expenses of the members sorted by expenses
A1 100
A2 99
A3 98
A4 97
A5 96
A6 95
A50 50
then
total expense should be found out as
10% [sum of top 5 : From A1 to A5] = 100 + 99 + 98 + 97 + 96 = 490
40% [sum of next 20: From A6 to A26]
50% [sum of next 25: From A27 to A50]
Please help !!!!!
Edited by: josleen on Jan 11, 2010 7:50 AMHi,
There are a number of analytic functions for dealing with percentiles.
Depending on how you want to handle ties, how you count duplicate values, and other factors, PERCENT_RANK might be best for this problem.
You really should post the information Centinul requested whenever you have a question.
If you can phrase your question in terms of commonly available tables, then you don't have to post them.
For example, to group employees from the scott.emp table according to their salaries, and get a total of salaries by group, you could create a groups table and join it to the emp table, like this:
CREATE TABLE groups
( seq_id NUMBER
, label VARCHAR2 (20)
, low_pct NUMBER -- lowest number IN this group
, high_pct NUMBER -- lowest number NOT IN this group, but in the next highest group
INSERT INTO groups (seq_id, label, low_pct, high_pct) VALUES (1, 'Top 10%', 0, 10);
INSERT INTO groups (seq_id, label, low_pct, high_pct) VALUES (2, '10%-50%', 10, 50);
INSERT INTO groups (seq_id, label, low_pct, high_pct) VALUES (3, 'Bottom 50%', 50, 101);
COMMIT;
WITH got_pct AS
SELECT ename
, sal
, 100 * PERCENT_RANK () OVER ( ORDER BY sal DESC
, empno
) AS pct
FROM scott.emp
SELECT g.label
, SUM (p.sal) AS group_total
FROM got_pct p
JOIN groups g ON p.pct >= g.low_pct
AND p.pct < g.high_pct
GROUP BY g.label
ORDER BY MIN (g.seq_id)
;Output:
LABEL GROUP_TOTAL
Top 10% 8000
10%-50% 12875
Bottom 50% 8150If you need help understanding a query with sub-queries, it helps to run the sub-query by iteself.
In this case:
WITH got_pct AS
SELECT ename
, sal
, 100 * PERCENT_RANK () OVER ( ORDER BY sal DESC
, empno
) AS pct
FROM scott.emp
SELECT *
FROM got_pct
;produces this output:
ENAME SAL PCT
KING 5000 0
SCOTT 3000 7.69230769
FORD 3000 15.3846154
JONES 2975 23.0769231
BLAKE 2850 30.7692308
CLARK 2450 38.4615385
ALLEN 1600 46.1538462
TURNER 1500 53.8461538
MILLER 1300 61.5384615
WARD 1250 69.2307692
MARTIN 1250 76.9230769
ADAMS 1100 84.6153846
JAMES 950 92.3076923
SMITH 800 100In case of a tie, PERCENT_RANK assigns the same result to all rows with the same input value. The reason I included empno in the analytic ORDER BY clause was to prevent ties. Otherwise, both SCOTT and FORD (with the exact same sal=3000) would be assigned pct=7.69230769, and the "Top 10%" would then include 3 out of 14 rows (over 21% of the rows), which, outside of Lake Woebegone, shouldn't happen.
Edited by: Frank Kulash on Jan 11, 2010 11:02 AM
Explanation added.
Maybe you are looking for
-
Weird iCal bug after 10.4.8 update
After updating to Mac OS X 10.4.8 I get the following display bug with my iCal reminders, i.e. the reminder appears way off to the side of the screen. This happens on both of my machines: http://web.mac.com/csearles/iWeb/Site%202/Photos%202.html G 4
-
Urgent Problem in Report Dev 2000
hi I have called a report in the following way com:='R30CLI32 server=repserver report='||store_var||'\reports\NOTDONE.rdf userid='||user||'/'||pass|| ' destype=file desname='||store_var1||'\'||:global.vc_comp_code||:global.module_
-
Acrobat 7.0.5 pro
Ciao, mi hanno installato sul mio pc la versione acrobat 7.0.5 pro, sebbene essa sia in lingua italiana, spagnola ed inglese, non riesco a portarla in italiano, mi da unicamente quella inglese. Grazie Luigi
-
To send a mail along with a csv attachment.
Hi All, my requirement is to send a mail along with a csv file attached to it. Could someone provide me their valuable comments that will guide me. I am using the function module. SO_NEW_DOCUMENT_ATT_SEND_API1.
-
RoboHelp project keeps crashing
I have been working with a large RH project since December that is linked to a 600 page FM book. I haven't had any problems until last Tuesday (1/20). When I was trying to update the project, I received error messages regarding several FM files that