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 AM
Are 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
Similar Messages
-
How can we use DECODE function in where clause.
Hi Guys,
I have to use DECODE function in where clause.
like below
select * from tab1,tab2
where a.tab1 = b.tab2
and decode(code, 'a','approved')
in this manner its not accepting?
Can any one help me on this or any other aproach?
Thanks
-LKR>
I am looking for to decode the actual db value something in different for my report.
like if A then Accepted
elseif R then Rejected
elseif D then Denied
these conditions I have to check in where clause.
>
what are you trying to do?
may be you are looking for
select * from tab1,tab2
where a.tab1 = b.tab2
and
(decode(:code, 'A','Accepted') = <table_column>
or
decode(:code, 'R','Rejected') = <table_column>
or
decode(:code, 'D','Denied') = <table_column>
) -
Hello Friends,
I have a query that has different columns and I am not sure what the data type of each column is ...
I want to use decode function which displays the following if the value of the column is like this ..
if the value of column is -714E ie -700000000000000 then display as .A
if the value of column is -814E ie -800000000000000 then display as .B
NOTE ; don't know which column is having the value - and i don't know the data type of the column is ..
got to use the decode function for all the columns selected ..
I want to use decode function pls let me know how to write for this kind of requirement.
appreciate your help in this rgds.
thanks/kumarDear Sir / Madam,
Thanks for your understanding.
Right now I am facing a unique problem.
I am using decode function in a select statement. The columns are dynamic some columns are of type number and some are varchar datatypes. I want to apply decode function for every column irrespective of datatype.
What i am finding difficult is if the column is a number datatype , the decode funciton is working good but if the column datatype is varchar or char datatype then i am getting ..
here's the decode function..
decode (CHAI.EVNDRIVE,
-800000000000000,'.A',
-700000000000000, '.B',
-600000000000000 ,'.C',
-500000000000000 , '.D',
-400000000000000 , '.E',
-300000000000000 , '.F',
-200000000000000, '.G',
-100000000000000 , '.H',
-1000000000, '.R' ,CHAI.EVNDRIVE ) EVNDRIVE
report error:
ORA-01722: invalid number
pls let me know how to over come this kind of scenario.
is their a way to find whether the column datatype is number or char , so that i can check the column datatype before the decode funciton is applied ..
thanks ..
kumar -
How to use DECODE function in Exspression?
Hi,
Can we use DECODE in Expression?
I'm trying to use DECODE function but there is an error during the validation. But when i validate the mapping, it is successfully compiled but it is failed during deployment.
But if I use CASE instead of DECODE, it works fine.
Can we use DECODE in OWB???
Thanks
RajHi,
In OWB 10gR2, if your are using only one DECODE in an expression, it's working. The package will compile when deploying the mapping. OWB will replace the DECODE by a CASE.
But when you are using nested decode in an expression ( for example : decode(col1, 1, 'M', decode(col2, 'Madame', 'Mme', null)) ) only the first one is replaced by a case at deployment.
In ROW_BASED mode, text of the expression is used outside of an sql statement and deployment will fails with "PLS-00204: function or pseudo-column 'DECODE' may be used inside a SQL statement only."
If operating mode for the mapping is set to SET_BASED, it's working because the expression is used in an sql statement.
I have logged a SR in metalink for this issue and a bug is opened (bug 5414112).
But I agree with you, it's better to use case statement.
Bernard -
How to use Decode Function in Webi / Designer - BOE XI 3.1
Hi All,
I have a SQL query which needs to include in the webi report.
Below is the query :
SELECT
SECURITY.SEC_CUSIP_NO "CUSIP",
SECURITY.STY_SEC_TY_CD "SECURITY TYPE",
SECURITY.SEC_DERIVED_DESC_TX "SECURITY DESCRIPTION",
SECURITY.sec_dep_teleg_de "FED DESCRIPTION",
SEC_STND_PR "STANDARD/FACTORED PRICE",
SEC_STND_PR_EFF_DT "STANDARD/FACTORED PRICE Date",
SECURITY.SEC_YIELD_PR "YIELD PRICE",
SECURITY.SEC_YIELD_PR_DT "YIELD RICE EFFECTIVE DATE",
STND_PR_VND_VENDOR_CD "PRICE SOURCE",
SEC_MATURITY_DT "MATURITY DATE",
SEC_ISSUE_DT "ISSUE DATE",
CASE WHEN SECURITY.STY_SEC_TY_CD IN ('BA','CD','CDD','CDE','CDM','CDV','CP','CPD') THEN
(position.PSN_AVAIL_PAR_VL + position.PSN_COLLAT_PAR_VL) * (SECURITY.SEC_STND_PR) / 100
ELSE
(position.PSN_AVAIL_PAR_VL + position.PSN_COLLAT_PAR_VL) * ( ( SECURITY.SEC_YIELD_PR * DECODE (SECURITY.SEC_PRIN_FT, 0, 1, SECURITY.SEC_PRIN_FT))) / 100
* DECODE(SECURITY.STY_SEC_TY_CD, 'AMP', DECODE(NVL(SECURITY.SEC_MULTIPLIER_UNIT_VL,0), 0, 1, SECURITY.SEC_MULTIPLIER_UNIT_VL), 'MMP', DECODE(NVL(SECURITY.SEC_MULTIPLIER_UNIT_VL,0), 0, 1, SECURITY.SEC_MULTIPLIER_UNIT_VL), 'AMPT', DECODE(NVL(SECURITY.SEC_MULTIPLIER_UNIT_VL,0), 0, 1, SECURITY.SEC_MULTIPLIER_UNIT_VL), 1 )
END
) "MARKET VALUE"
FROM SECURITY SECURITY
, position position
WHERE STND_PR_VND_VENDOR_CD = 'VT'
AND SEC_MATURITY_DT > SYSDATE-1
and SECURITY.sec_sys_id=position.sec_sys_id
I could create a report which has selected objects and defined where condition from the above query. I have also created a variable for 'when' & 'then' condition but stuck at else part which contains 'DECODE' function.
Please suggestions how to write the same at the report or designer level.
Thanks,
Rameez ShaikhHi Rameez,
Looking at the query you can create the object directly in the universe, either create two objects one for inner decode and use it in outer case logic. In report it is nothing but nested if logic. For eg. If(a=b;1;(if(a=c;2;3))
Thanks
Gaurav -
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 -
Problem using DECODE() function with a Query of Queries
I
posted
on my blog about an issue I was having trying to use the PL/SQL
DECODE() function with a Coldfusion Query of Queries. This function
works fine when you query a database for information. However, when
you query another query, it seems that CF doesn't recognize it. I
got errors stating that it found a left parenthesis where it
expected a FROM key word. Here is a simplified version of what I am
trying to do:
quote:
<!--- Simulated query; similar to what I was calling from
my database --->
<cfscript>
qOriginal = queryNew("Name,Email,CountryCode",
"VarChar,VarChar,VarChar");
newRow = queryAddRow(qOriginal, 5);
querySetCell(qOriginal, "Name", "Joe", 1);
querySetCell(qOriginal, "Email", "[email protected]", 1);
querySetCell(qOriginal, "CountryCode", "AMER", 1);
querySetCell(qOriginal, "Name", "Sally", 2);
querySetCell(qOriginal, "Email", "[email protected]", 2);
querySetCell(qOriginal, "CountryCode", "AMER", 2);
querySetCell(qOriginal, "Name", "Bob", 3);
querySetCell(qOriginal, "Email", "[email protected]", 3);
querySetCell(qOriginal, "CountryCode", "ASIA", 3);
querySetCell(qOriginal, "Name", "Mary", 4);
querySetCell(qOriginal, "Email", "[email protected]", 4);
querySetCell(qOriginal, "CountryCode", "EURO", 4);
querySetCell(qOriginal, "Name", "John", 5);
querySetCell(qOriginal, "Email", "[email protected]", 5);
querySetCell(qOriginal, "CountryCode", "EURO", 5);
</cfscript>
<cfquery name="qCountries" dbtype="query">
SELECT DISTINCT(CountryCode) AS CountryCode,
DECODE(states, "AMER", "North America & Canada",
"EURO", "Europe & Africa", "ASIA", "Japan &
Asia","") CountryName
FROM qOriginal
ORDER BY CountryCode
</cfquery>
<cfdump var="#qCountries#">
<!--- ========== END OF CODE ========== --->
So running this returned the following error:
Query Of Queries syntax error.
Encountered "(. Incorrect Select Statement, Expecting a
'FROM', but encountered '(' instead, A select statement should have
a 'FROM' construct.
Does anybody know why this doesn't work? Is it just not
supported? Please note that I have also tried to use the CASE()
function instead of DECODE() and that resulted in basically the
same error. For now I an looping over my distinct query with a
switch statement and manually loading a new query with the data how
I want it. But it would be a lot cleaner and less code to have the
DECODE() to work. Thx!DECODE() is an Oracle function, not generic SQL. Q-of-Q is a
very limited subset of SQL and lacks many functions and clauses
available in standard SQL, especially what you may be used to using
in your particular RDBMS.
See
Query
of Queries user guide
Phil -
Using DECODE Function to change data
I am trying to use the Decode function in a SQL statement to find a field that has a specific type, and when it finds that type, I want to blank out the results in a different field.
For example:
DECODE(ADDR_TYPE,'HOME',PHONE='') HOME_PHONEsomething like:
SQL> with t as
2 (select 219 id,
3 'BUS' addr_type,
4 '505-555-5555' phone
5 from dual
6 union
7 select 219 id,
8 'HOME' addr_type,
9 null phone
10 from dual
11 union
12 select 220 id,
13 'BUS' addr_type,
14 '101-111-1111' phone
15 from dual
16 union
17 select 220 id,
18 'HOME' addr_type,
19 null phone
20 from dual
21 union
22 select 223 id,
23 'BUS' addr_type,
24 '202-222-2222' phone
25 from dual
26 union
27 select 224 id,
28 'HOME' addr_type,
29 '303-333-3333' phone
30 from dual
31 union
32 select 225 id,
33 'BUS' addr_type,
34 null phone
35 from dual
36 union
37 select 226 id,
38 'HOME' addr_type,
39 null phone
40 from dual)
41 select a.id,
42 a.addr_type,
43 decode(a.addr_type,'BUS',phone,null) phone
44 from (select id, addr_type, phone,
45 row_number() over (partition by id order by id, decode(addr_type,'BUS',1,2)) rn
46 from t) a
47 where a.rn = 1;
ID ADDR PHONE
219 BUS 505-555-5555
220 BUS 101-111-1111
223 BUS 202-222-2222
224 HOME
225 BUS
226 HOME
6 rows selected.
SQL> -
How to use decode function in oracle apex 3.2.1
Hello Friends,
how to use decode
var_decode := 'decode'||'('|| var1 ||','|| -800000000000000||','||'.A'||','||
appreciate your help.
regards/kumar
Edited by: kumar73 on Apr 23, 2010 12:25 PM
Edited by: kumar73 on Apr 23, 2010 12:27 PMHey just on this particular point, there's a handy PL/SQL method for escaping quotation characters which may reduce the complexity of the string.
For example, if I had a string:
I can't understand why quoting my 'quote' characters never seems to "work"!and I wanted to pass that into a variable, I could do this:
vc_foo:= 'I can''t understand why my ''quote'' characters never seems to "work"!';or I could use the q function, whereby you enclose your string within a quote delimiter of your choice, like so:
vc_foo:= q'^I can't understand why quoting my 'quote' characters never seems to "work"!^';
note: I used the caret (^) character by convention but any valid character will do
...all of which leaves your original string more or less unadulterated.
I find this particularly useful for generating dynamic SQL and dynamic PL/SQL, as it leave the query looking a tad more readable, although if you are chaining together multiple strings with double-pipes, it can make it a little muddier. I find it particularly useful if I'm going to deal with string data from, for example, a field with Irish-derived surnames such as "O'Reilly"...
Give it a go! -
Order by clause using decode function
Hi everybody,
i need below order in my report.
Connecticut
greenwich
stamford
bridgeport
New York
NYC
wrestcher
byram
Georgia
atlanta
athens
oconny
first i need above order in my view out put.
so in order by clause i used first decode function for State ordering
and in second decode function for city ordering.
i do not need order by ascending or descending.
so pls anybody can help me.
any help is greatly appreciated.
thanks.add asc after the decode. default is desc
-
Using conditional formatting on negative values in Web Analysis 931
Hi All,
Can I please have your thoughts on the below:
We open a web analysis report in Analyze View.
The scenarios are in the columns and we are trying to apply conditional formatting on negative values.
We right click the scenario in the columns and click Formatting...
We then tick the Conditional Formatting check box and we want to add a condition on negative numbers.
However we can only enter positive values as the - sign is not accepted. This means we cannot apply conditional formatting on negative values.
Does anyone know how this can be done? Am I missing the obvious?
Thanks for your help.
SebGotcha. Ok. Have you thought about creating a new hidden calculated ColumnB whereby you multiply the values in ColumnA by -1 and then set your formatting based on the result in ColumnB. If the value in B is positive then you know A was a negative number so you can go from there, in a round about way? I don't have time at the moment to think it all the way out but something to think about. If your formatting involves just colors you could use Traffic Lighting to set the color on ColumnA based on result in ColumnB. Traffic Lighting out of the box is limited but it's a start until you have time to build your own.
-Karen -
Hi
Help needed.... i need to create column as L, R in fact table in business layer using below mentioned logic
select count(decode (type, '41',1,'51',1,'61',1))L, count(decode(type, '71',1,'31',1,'81',1))R
Waiting for the reply soon.
thanks & regards
Edited by: user11933655 on 29-Oct-2009 10:26Similar thread/question:
Re: How to count distinct excluding a value in business layer?
You may use:
L -> case when table.column in ('41', '51', '61') then 1 else 0 end
R -> case when table.column in ('71', '31', '81') then 1 else 0 end
Assuming that table.column is dimension attribute and you joined this table dimension to a fact table (steps 2 and 3 from the link above, add physical join to dimension table in the logical fact table source). L and R are two logical columns, using COUNT or SUM as aggregation rule.
Follow steps 1-7 (1-9 for complete) from the link above.
Regards
Goran
http://108obiee.blogspot.com -
I'm trying to decode the following, but I seem to be having a syntax issue:
decode
p.balls,p.strikes,
1,0 '1-0 Count',
2,0 '2-0 Count',
3,0 '3-0 Count',
0,1 '0-1 Count',
0,2 '0-2 Count',
1,1 '1-1 Count',
2,1 '2-1 Count',
3,1 '3-1 Count',
0,2 '0-2 Count',
1,2 '1-2 Count',
2,2 '2-2 Count',
) as Count,what I'm trying to have happen is anytime the balls column has a '1' and the strike column has a '0' to decode to '1-0 Count' etc...Decode compares the value of the first paramter with the values of the even parameters and returns the following odd parameters value when the first match is found. If the last parameter is an even numbered parameter it is taken as the default when no other matches are found.
So your code compares p.balls to p.strikes and if they match returns a 1 then it runs into a syntax error since you don't have any commas after the forth column.
If you want to perform a multicolumn compare in the manner you are showing you will need to use a CASE statement instead:
case when (p.balls, p.strikes) in ( (1, 0) ) then '1-0 Count'
when (p.balls, p.strikes) in ( (2, 0) ) then '2-0 Count'
when (p.balls, p.strikes) in ( (3, 0) ) then '3-0 Count'
when (p.balls, p.strikes) in ( (0, 1) ) then '0-1 Count'
when (p.balls, p.strikes) in ( (0, 2) ) then '0-2 Count'
end as "COUNT" -
Using DAQmx functions without installation
Good evening everybody,
I have an instrumentation problem linked to DAQmx. I would like to communicate with an instrument using NIDAQmx tasks. I use an environment program system : visual studio 2003. At the end, I want to produce a dll. This dll, I want to put it on another computer. The question is the following. If I want to launch the dll by calling it, will I need NI-DAQ on the other computer or a software to use the dll ?
When I tried to produce an exe with a VI, I needed to have Labview RunTime to launch the exe. If I want to launch the dll, will I be able to use the dll alone on the computer, without the installation of NIDAQ. Can I use the tasks only with the dll (since I need Run Time for the .exe, do I need Run Time also). ?
I am sorry, it must be not clear. But I am available to explain you more the problem.
Best regards.
Thank you.
gautier
Solved!
Go to Solution.Since you have LabVIEW installed on your computer, the Run-Time is not required for running LabVIEW executables.
But if you decide to use your LabVIEW executable on an other computer, you will need to install the LabVIEW Run-Time on it. You can also build an installer which will contain your program and addtionnal components (such as LabVIEW Run-Time and required drivers). Building an installer avoid to download and install the components separately.
For more information regarding installer, you can look into LabVIEW's help (Building an Installer (Windows)) or follow Core 2 training course.
If your program uses NI-488.2 driver, you will need to install it on your computer.
Installing LabVIEW Run-Time is only required if your program which contains DLL calls is a LabVIEW executable.
Regards,
Jérémy C.
National Instruments France
#adMrkt{text-align: center;font-size:11px; font-weight: bold;} #adMrkt a {text-decoration: none;} #adMrkt a:hover{font-size: 9px;} #adMrkt a span{display: none;} #adMrkt a:hover span{display: block;}
Travaux Pratiques d'initiation à LabVIEW et à la mesure
Du 2 au 23 octobre, partout en France -
Using aggregrate functions without group by
Hi
I have a query which is
select empno,deptno,count(*) from emp group by empno,deptno;
Is there any thing which will help me to return empno,deptno without using group by clause?
Appreciate your help on the above?
Thanks & Regards
Thakur Manoj RThis will give the same result:
select empno, deptno, count(*) over (partition by empno, deptno) from emp;If you want to see the number of employees in the same department you could use:
select empno, deptno, count(*) over (partition by deptno) from emp;But what is your intention? What is wrong about "group by"?
Edited by: hm on 27.01.2011 00:10
Maybe you are looking for
-
i feel like im having all sorts of woes recently with my mac mini, so to try cheer myself up i thought id plug my mac mini into my HDTV via the VGA port knowing that i would have trouble with the DVI-I/D/everything else so i plugged the VGA into the
-
Flash Video Encoder for WMV - DW8 vs CS5
Hi, I'm trying to convert a WMV file into Flash. I know that this can be done using DW8's Flash Video Encoder ....but it seems this function is no longer availabe in CS5. I would be grateful if someone confirm that this understanding is true. Many t
-
Dear All, I have worked on smartform and also worked on driver program for Purchase order forms. Problem:i have tried code for telefax by passing 'device' in contrl paramaters and also in the output options in my fname in function module. solution th
-
How to see the Complete Line In SO10??
Hi All, In the satndard text So10, when we enter any text and if it exceeds more than the length of the line. It is not shown to us in the display. In order to see the text what are the control keys we use? Can anyone give me the idea?? Cheers, Simha
-
Je tiens à préciser que c'est pour une utilisation personnelle seulement c'est plus un achat avec lequel je vais m'amuser j'en ai ni besoin pour le travail ni pour l'école ..... J'ai besoin d'un portable avec lequel je pourrais utiliser excel , word