Using Decode - Age Grouping???
Hi,
I am wondering if we can use decode/sign functions to achieve the following. If so I would like to know how without using stored procedures.
Depending on a person's age, I need to determine the person's AGE_GROUP. I will be decoding AGE and I need the AGE_GROUP in the below ranges.
Example:
AGE_GROUP (VARCHAR2)
<20
21-40
41-60
61-80
81-100
100How best can I achieve this? Thanks in advance
CASE statement is what you want
SQL> SELECT emp_name
2 ,age
3 ,CASE
4 WHEN age < 21 THEN '<21'
5 WHEN age BETWEEN 21 AND 40 THEN '21-40'
6 WHEN age BETWEEN 41 AND 60 THEN '41-60'
7 WHEN age BETWEEN 61 AND 80 THEN '61-80'
8 WHEN age BETWEEN 81 AND 100 THEN '81-100'
9 WHEN age > 101 THEN '>101'
10 END AS age_group
11 FROM employees
12 /
EMP_NAME AGE AGE_GR
John 18 <21
Mary 19 <21
Fred 24 21-40
Anne 36 21-40
Victor 103 >101Regards,
Steve Rooney
Similar Messages
-
Display in alv according to age group
Hi Friends
i have data in internal table
I have to group it according to age
i mean on age group
Which Needs to Display in ALV
so how should i group them in ALV
OUTPUT like this :
EMPNUM EMPLOYEENAME DATE OFBIRTH/AGE
AGE GROUP: 0-29
999999 DOE JANE A. 01/01/1980 27
999999 DOE JOHN A. 01/01/1980 28
TOTALS: EMP 2
AGE GROUP: 30-34
999999 DOE JANE A. 01/01/1980 32
999999 DOE JOHN A. 01/01/1980 33
TOTALS: EMP 2
AGE GROUP: 35-39
999999 DOE JANE A. 01/01/1980 37
999999 DOE JOHN A. 01/01/1980 36Hi,
Add a new field type c of length 30 in the final internal table and do as below.
loop at itab.
if itab-age GE 20 or itab-age LE 29
move : 'Age group 20 to 30' to itab-newfield.
modify itab.
clear itab.
else.
"Repeat same logic for different age groups.
endif.
"Do subtotaling by new field of itab and age. And use gt_fieldcat-no_out = 'X' for newfield, so that newfield will not be displayed.
Thanks,
Sriram Ponna. -
Hi all
We can use decode function in the Select columns as well as Group by Columns as well as Having Clause
Can we use decode function in the where clause also. If yes can u give small sample
Suresh Bansal""DECODE in the WHERE clause"
http://psoug.org/reference/decode_case.html
WHERE empid = DECODE(posn,
0, st.areadir,
1, st.areamgr,
2, NVL(st.storemgr1, st.storemgr2),
3, NVL(st.asstmgr1, NVL(st.asstmgr2,
st.asstmgr3))) -
Query to find out sum by using decode or case
Hi, I have data like below and required an output as given below, could you please help me to get the same by using decode / case .....Thanks for your help
INSNAME INSID STATUS
AAA 1000 Scheduled
AAA 1000 Processing
BBB 1001 Inspector
BBB 1001 Scheduled
BBB 1001 Processing
BBB 1001 Inspector
CCC 1002 Scheduled
CCC 1002 Processing
CCC 1002 Inspector
CCC 1002 Scheduled
CCC 1002 Processing
CCC 1002 Inspector
Required Output...
INSNAME INSID sum_of_scheduled sum_of_Processing sum_of_Inspector
AAA 1000 1 1 0
BBB 1001 1 1 2
CCC 1002 2 2 2And if you want it with CASE statement:
WITH test_table AS
SELECT 'AAA' insname, 1000 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'AAA' insname, 1000 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Inspector' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Inspector' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Inspector' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Inspector' status FROM DUAL
SELECT insname
,insid
,SUM(CASE WHEN status = 'Scheduled'
THEN 1
ELSE 0
END
) sum_of_scheduled
,SUM(CASE WHEN status = 'Processing'
THEN 1
ELSE 0
END
) sum_of_processing
,SUM(CASE WHEN status = 'Inspector'
THEN 1
ELSE 0
END
) sum_of_inspector
FROM test_table
GROUP BY insname
,insid;
Regards
Arun -
Using decode function without negative values
Hi friends
I am using oracle 11g
I have at doubt regarding the following.
create table Device(Did char(20),Dname char(20),Datetime char(40),Val char(20));
insert into Device values('1','ABC','06/13/2012 18:00','400');
insert into Device values('1','abc','06/13/2012 18:05','600');
insert into Device values('1','abc','06/13/2012 18:55','600');
insert into Device values('1','abc','06/13/2012 19:00','-32768');
insert into Device values('1','abc','06/13/2012 19:05','800');
insert into Device values('1','abc','06/13/2012 19:10','600');
insert into Device values('1','abc','06/13/2012 19:15','900');
insert into Device values('1','abc','06/13/2012 19:55','1100');
insert into Device values('1','abc','06/13/2012 20:00','-32768');
insert into Device values('1','abc','06/13/2012 20:05','-32768');
Like this I am inserting data into table for every 5 minutes Here i need the result like
output:
Dname 18:00 19:00 20:00
abc 400 -32768 -32768
to retrieve this result i am using decode function
SELECT Dname,
MAX(DECODE ( rn , 1,val )) h1,
MAX(DECODE ( rn , 2, val )) h2,
FROM
(SELECT Dname,Datetime,row_number() OVER
(partition by Dname order by datetime asc) rn FROM Device
where substr(datetime,15,2)='00' group by Dname.
According to above data expected result is
Dname 18:00 19:00 20:00
abc 400 600(or)800 1100
This means I dont want to display negative values instead of that values i want to show previous or next value.
Edited by: 913672 on Jul 2, 2012 3:44 AMAre you looking for something like this?
select * from
select dname,
datetime,
val,
lag(val) over (partition by upper(dname) order by datetime) prev_val,
lead(val) over (partition by upper(dname) order by datetime) next_val,
case when nvl(val,0)<0 and lag(val) over (partition by upper(dname) order by datetime) >0 then
lag(val) over (partition by upper(dname) order by datetime)
else
lead(val) over (partition by upper(dname) order by datetime)
end gt0_val
from device
order by datetime
where substr(datetime,15,2)='00';Please take a look at the result_column gt0_val.
Edited by: hm on 02.07.2012 04:06 -
How to use decode to calculate sum for different date range
I'm stuck with decode() function:
I have a table like this:
(project_id, approve_date, value, builder_code)
I want to write a SQL query to get sum of values for different month of the approve_date, and group by builder_code)
The result is like this:
builder_code Sum(value)_Sep-03 Sum(value)_Oct-03 Sum(value)_Nov03
1001 1,299 1,322 990
1002 3,332 1,222 333
I tried to use decode for this question but could not get the answer.
Thanks a lotI don't think you need a DECODE() here. I'd do something like this-
create table builder (
project_id number,
approve_date date,
value number,
builder_code number
insert into builder values( 1, to_date('09-01-2003', 'MM-DD-YYYY'), 100, 990 )
insert into builder values( 2, to_date('09-03-2003', 'MM-DD-YYYY'), 150, 990 )
insert into builder values( 3, to_date('09-05-2003', 'MM-DD-YYYY'), 250, 990 )
insert into builder values( 3, to_date('09-05-2003', 'MM-DD-YYYY'), 250, 333)
SELECT sept.builder_code, sept.sept_sum, oct.oct_sum
FROM (SELECT builder_code, sum(value) sept_sum
FROM builder
WHERE approve_date >= to_date('09-01-2003','MM-DD-YYYY')
AND approve_date < to_date('10-01-2003','MM-DD-YYYY')
GROUP BY builder_code) sept,
(SELECT builder_code, sum(value) oct_sum
FROM builder
WHERE approve_date >= to_date('10-01-2003','MM-DD-YYYY')
AND approve_date < to_date('11-01-2003','MM-DD-YYYY')
GROUP BY builder_code) oct
WHERE oct.builder_code(+) = sept.builder_code
BUILDER_CODE SEPT_SUM OCT_SUM
333 250
990 500Justin
Distributed Database Consulting, Inc.
www.ddbcinc.com/askDDBC -
Hello,
i have question regarding PL/SQL using DECODE when i use following qurey in SQL plus it is working fine but when i try to run in tode it only display value of g.
everything else is blank........
so can u plz assist me on that .....
thanks in advance!!!!!!!!!!
SELECT
b.g,b.s,
COUNT(DECODE(a.u,'R',a.rep)) AS R,
COUNT(DECODE(a.u,'u',a.rep)) AS U,
COUNT(DECODE(a.u,'C',a.rep))AS C,
COUNT(DECODE(a.u,'I',a.rep)) AS I,
COUNT(DECODE(a.u,NULL,a.rep)) AS B
FROM AREA a, data b
WHERE a.rep = '454637'
AND a.rep = b.emp
GROUP BY b.g,b.sVery strange.
Find out if there are alterations to your SQL*PLUS envirionment. Warren mentioned user/database wierdness; the ALTER SCHEMA and other commands can affect privilges and what objects you can see. Sometimes these commands are buried in LOGIN.SQL and GLOGIN.SQL files (for SQL*PLUS), and more hard to find they can call files buried on a network somewhere.
Try the query without the DECODE() and see what's in the table without trying to modify the values in both envirionments. -
Update age group picklist based on value entered in date of birth
Hi Gurus,
Have a requirement where in user would enter the date of birth on a contact record and would like to populate another custom field called age group at the time of creating a new record.
The condition is some what like
Age
1 – 20 Teens
21-40 Young Adults
41-60 Adults
60 – 100 Seniors
I have tried using the age field but problem is that age is populated only when the record is saved.
Update the age group via workflow works fine but the saved record has to be modified before triggering the workflow. we would like to avoid the additional step of modifying the record
Is there any option to calculate age based on the value entered for date of birth - something like DOB Year - current year before saving a record or via post default without using workflows.
Thanks in advance,
ArunHi
The answer is yes, using the post default functionlaity.
take a look in the folowign example:
IIf(ToChar(Timestamp(),'DD/MM/YYYY')- [<DateofBirth>] < 20,'20','30')
You will need to make something like that, but replace the '30' with another IIf expression, and so on for the next interval.
note that date of birth should be between suqare brackets [ ] (it is shown here with underline for some reason...)
Good luck
Guy
Edited by: Shinshan on 08:45 30/12/2009
Edited by: Shinshan on 08:46 30/12/2009
Edited by: Shinshan on 08:47 30/12/2009
Edited by: Shinshan on 08:47 30/12/2009 -
How to use Decode with a variable
Maybe you've seen this done.
I want to take statement strMenu4 and subtract the previous value
that is stored in an array as shown in strMenu5.
My problem is this change as shown in strMenu5 produces
an error code:
ORA-00904 invalid column name
So either I'm coding something wrong or it can't be done within the
SUM(decode) construct. If I replace the variable with a literal
like 1000 the code works:
SUM(decode(name,'DBWR lru scans', c1.value) - 1000) DBWR_lru_scans
but with an array variable or even a constant the code fails:
SUM(decode(name,'DBWR lru scans', c1.value) - Array(3)) DBWR_lru_scans, "
Any ideas how I can use Decode with a variable and not have it interrupt it
as a invalid column name?
Thanks,
Joseph Karpinski
[email protected]
[email protected]
strMenu4 = "SELECT d1.instance, " _
& " to_char(sysdate, 'dd-mon-yy hh24:mi:ss') start_time, " _
& " SUM(decode(name,'DBWR buffers scanned', c1.value)) DBWR_buffers_scanned, " _
& " SUM(decode(name,'DBWR lru scans', c1.value)) DBWR_lru_scans, " _
& " SUM(decode(name,'db block gets', c1.value)) db_block_gets , " _
& " SUM(decode(name,'consistent gets', c1.value)) consistent_gets , " _
& " SUM(decode(name,'physical reads', c1.value)) physical_reads " _
& " from dual, v$sysstat c1, v$thread d1 " _
& " group by d1.instance "
strMenu5 = "SELECT d1.instance, " _
& " to_char(sysdate, 'dd-mon-yy hh24:mi:ss') start_time, " _
& " SUM(decode(name,'DBWR buffers scanned', c1.value) - Array(2)) DBWR_buffers_scanned, " _
& " SUM(decode(name,'DBWR lru scans', c1.value) - Array(3)) DBWR_lru_scans, " _
& " SUM(decode(name,'db block gets', c1.value) - Array(4)) db_block_gets , " _
& " SUM(decode(name,'consistent gets', c1.value) - Array(5)) consistent_gets , " _
& " SUM(decode(name,'physical reads', c1.value) - Array(6)) physical_reads " _
& " from dual, v$sysstat c1, v$thread d1 " _
& " group by d1.instance "Re-posting in the SQL and PL/SQL forum.
It's more appropriate.
Thanks,
Closing -
Hello,
I have used Decode in my code, but i am getting error. Please help me on it.
*<code>*
SELECT DECODE(("Incident Customer"."Customer ID"),'emailuser',COUNT("Incident Customer"."Customer ID"),COUNT("Incident Customer"."Customer ID")) saw_0, "Incident Basic Details"."Incident Status" saw_1, "Incident Customer"."Customer ID" saw_2 FROM "Enterprise Remedy" WHERE ("Incident Assignment"."Assigned Group" = 'Standard Oracle Production Support') AND ("Incident Date/System"."Submit Date(GMT)" BETWEEN timestamp '2013-02-04 01:30:00' AND timestamp '2013-02-04 10:30:00') AND ("Incident Basic Details"."Incident Status" IN('Resolved','Closed')) GROUP BY "Incident Customer"."Customer ID" ORDER BY saw_1, saw_2
*</code>*
Error:
Odbc driver returned an error (SQLExecDirectW).
Error Details
Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P:OI2DL65P
State: HY000. Code: 4264403529. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 27002] Near <(>: Syntax error [nQSError: 26012] . (HY000)
SQL Issued: {call NQSGetQueryColumnInfo('SELECT "Incident Basic Details"."Incident Status", "Incident Customer"."Customer ID", DECODE(("Incident Customer"."Customer ID"),''emailuser'',COUNT("Incident Customer"."Customer ID"),COUNT("Incident Customer"."Customer ID")) FROM "Enterprise Remedy"')}
SQL Issued: SELECT "Incident Basic Details"."Incident Status", "Incident Customer"."Customer ID", DECODE(("Incident Customer"."Customer ID"),'emailuser',COUNT("Incident Customer"."Customer ID"),COUNT("Incident Customer"."Customer ID")) FROM "Enterprise Remedy"
Thanks,
JitenA doctor should know that you can't just transplant any kind of code from system A to system B...wouldn't work with organ transplants either, would it.
If you have a look at the formula editor in either Answers or the Admin Tool, you will notice the absence of DECODE as a supported function. So unless you want to wrap your DECODE into an EVALUATE wrapper because you doj't want to re-write it, you'll have to reproduce the funcitonality with the funciton actually supported by OBIEE:
CASE WHEN "Incident Customer"."Customer ID" = 'emailuser' then COUNT("Incident Customer"."Customer ID") else COUNT("Incident Customer"."Customer ID") end
^-- which is what your decode does... -
I have one table which stores candidates' response. The structure is like this
(Seatno, Questionnumber, Answer).
And I have another table Master as (Questionnumber, Answer)
Now I want to calculate marks by comparing Candidate's response with Master.
I tried to use decode and case. But was not successful.
My query with decode was ...
select c1.candidatesrno, sum(decode(c1.answer,(c1.answer=m1.answer),1,0))
from candidate c1,master q1
where c1.question=m1.question
group by c1.candidatesrno
And query with CASE was...
select c1.candidatesrno,
case when (c1.answer=m1.answer) then 1 else 0
end resultset
from candidate c1,master m1
where c1.questionnumber=m1.questionnumber
group by c1.candidatesrno
Can anybody help ?I want to compare candidate's response with master
table.
Now for each question of Candidate, if its answer
matches with answer in Master table I have to give 1
marks...if it does not match I have to give 0
marks...like this there are two-three conditions...
Something like this ?
test@ORA10G>
test@ORA10G> with master as (
2 select 'Q1' as question_num, 'A1' as answer_num from dual union all
3 select 'Q2', 'A2' from dual union all
4 select 'Q3', 'A3' from dual),
5 candidate_response as (
6 select 'S1' as seat_num, 'Q1' as question_num, 'A1' as answer_num from dual union all
7 select 'S1', 'Q2', 'A2' from dual union all
8 select 'S1', 'Q3', 'A3' from dual union all
9 select 'S2', 'Q1', 'A5' from dual union all
10 select 'S2', 'Q2', 'A6' from dual union all
11 select 'S2', 'Q3', 'A3' from dual)
12 --
13 select
14 cr.seat_num,
15 cr.question_num,
16 cr.answer_num,
17 case when m.question_num is null and m.answer_num is null then '0 point'
18 else '1 point'
19 end as points
20 from candidate_response cr, master m
21 where cr.question_num = m.question_num(+)
22 and cr.answer_num = m.answer_num(+)
23 order by cr.seat_num, cr.question_num, cr.answer_num;
SE QU AN POINTS
S1 Q1 A1 1 point
S1 Q2 A2 1 point
S1 Q3 A3 1 point
S2 Q1 A5 0 point
S2 Q2 A6 0 point
S2 Q3 A3 1 point
6 rows selected.
test@ORA10G>
test@ORA10G> with master as (
2 select 'Q1' as question_num, 'A1' as answer_num from dual union all
3 select 'Q2', 'A2' from dual union all
4 select 'Q3', 'A3' from dual),
5 candidate_response as (
6 select 'S1' as seat_num, 'Q1' as question_num, 'A1' as answer_num from dual union all
7 select 'S1', 'Q2', 'A2' from dual union all
8 select 'S1', 'Q3', 'A3' from dual union all
9 select 'S2', 'Q1', 'A5' from dual union all
10 select 'S2', 'Q2', 'A6' from dual union all
11 select 'S2', 'Q3', 'A3' from dual)
12 --
13 select cr.seat_num, count(*) as score
14 from candidate_response cr, master m
15 where cr.question_num = m.question_num
16 and cr.answer_num = m.answer_num
17 group by cr.seat_num
18 order by cr.seat_num;
SE SCORE
S1 3
S2 1
test@ORA10G>
test@ORA10G>pratz -
SAP Open Enrollment Error : Age group could not be determined
Hi Gurus,
We have been trying to configure Open Enrollment and when I try to go in the backend using HRBEN0001, it lists all the eligible plans and lets you complete the Open enrolllment, however in the Error List one error pops up saying "Age Group Cannot be determined". I have a spouse listed in the system, but his birthday has been entered so it should be able to calculate the age group without any issues.
When we login to the portal and try to click on the Open Enrollment tab, no plans show up but only the error "Age Group cannot be determined" shows up.
We have configured three variants for Voluntary Life and Spouse Life for the age groups 1-69, 70-74 and 75+. Could this be the reason of the error?
Has anyone encountered this error before? and if yes, can you please let me know how to resolve this?
Any suggestions will be appreciated.
Thanks
KavitaHi Kavitha,
Can you tell me how u resolved the issue???
Regards -
Age group to make it time dependent display as per transaction date
Hi All,
I have a doubt in displaying a attribute as time dependent. Below is my scenario.
I have a cube where am capturing the transaction data of the customer. Currently while the customer data loading using the customer number i do look up to the customer master data and findout the D.O.B and calculate the Age group(bucket) and punch it in the transactin cube itself. Note Age group is calculated based on the DOB and the transaction date. It was working fine.
But issue raised today customer is registered and he is allowed to do some transactions immediately after that, and the personal details like DOB, address is captured latter. Hence all the transactions until his DOB is maintained in the source system is puched in cube w/o DOB.
Hence i do not want to have it in the cube and want to have the age group to be fetched from attribute / some other mean. But in case if i make the agegroup as attribute of the customer it will show only the current age. But i need to show the agegroup as on the transactions date.
For ex. today customer age is 25 and day after tmr his age changes to 26. And in this case if i execute the report for whole month under 25 i should get the transaction and under 26 age i should get the respective transaction.
how should u achieve this. please help me out.
Note: Having a virtual KF seems createing the performance issue because data volume pulling out is more in numbers.
thanks in advance
PremHi,
In this scenario i think Virtual Key figure is inevitable.
Hope that helps.
Regards
Mr Kapadia
*Assigning points is the way to say thanks.** -
How do I use Panorama / Tab Groups with the keyboard?
Yes I know that CTRL-SHIFT-E opens the panorama window, and then I can use the mouse to organize my tabs into groups.
But how do I do this with the keyboard?
I've [http://lifehacker.com/#!5719596/switch-between-tab-groups-in-firefox-with-a-keyboard-shortcut read] [http://ubuntuforums.org/showthread.php?t=1705714 that] Ctrl-`should move me through tab groups. That doesn't work for me on my Danish keyboard. (Where the ' and ` chars are weird. But is how they are on a valid standard Danish keyboard) I've tried changing the keyboard to USA and then moving through tab groups works fine.
In short: Pretend I don't have a mouse. How do I use Panorama / Tab Groups?Sorry. These are both known bugs:
[https://bugzilla.mozilla.org/show_bug.cgi?id=587010 Bug 587010] - Add keyboard UI for manipulated groups and tabs in Tab Candy
and
[https://bugzilla.mozilla.org/show_bug.cgi?id=626594 Bug 626594] - shortcut for switching tab groups is badly accessible on many non-US keyboard layouts -
I need to know if there is any way to use a range of values from
database and decode to certain text. I am able to do with one
value.
for example:
DECODE(column_name,'216767111','Unlimited',column_name)
above argument works with one value only. How about a range,
ex: 216767000 to 216767111. I need to use only SQL. No PL/SQL.
Kinldly need some body's help
Thanks
MunisWhich version of the database? If it's 8i+ then you can use
the CASE function
for example:
(CASE WHEN column_name between 216767000 and 216767111
THEN 'Unlimited' ELSE column_name END)
This won't work in PL/SQL because they're introducing a CASE
statement does soemthing different.
rgds, APCHello Andrew
Thank you for response. I am using 8i. 8.1.6. However using
CASE, I get inconsistent data type, ORA-00932: inconsistent
datatypes. I able to work it out with other response using
DECODE(sign(. Do you have any idea why i am getting this error.
If time permits, let me know
Maybe you are looking for
-
Problem with trees and memory handling.
Hi there, I just want to know if, when I create a large binary tree, and I re-"pointed" the root pointer to another node somewhere below in that same tree (say, a terminal node), will the "upper" parts of the tree (starting from the new root node) be
-
HST6.0 Header and footer in reports
Hi, We have an application generated from designer 1.3.2 using Headstart, and now we need to upgrade that one to designer 6.0, Everyting works ok with the forms but when we try to generate reports we don't get any header and footer. I have looked int
-
Tiger DVD Player Crashing!
So all of a sudden when I put a DVD movie in my eMac, DVD Player opens for like 2 seconds & then I get the typical Crash. I click retry and it crashes again. I know the DVD Drive itself is good because I can watch a DVD with VLC. I went to look for A
-
Change the description of a field in a SAP QUERY
Can i change the description of a field in a SAP QUERY report in the output of the report.or only the standard descriptions can be displayed. Edited by: mysap query on May 12, 2009 7:23 AM
-
Open Multiple Variable Editor in VI
Hi folks, is there any way to open the Multiple Variable Editor programmatically in a VI? ATM i'm migrating a measurement logging system from LabVIEW 7 to LabVIEW 2010 which means i'm mostly rewriting the code for use with shared variables. It would