CASE.. WHEN statement.. Query not working
Hi,
Can someone help me to correct the following code:
select * from(
select col1,col2,
CASE
WHEN
Percentage >=75 THEN 'red'
ELSE
'green'
end as color from (select
ROUND(((COUNT(CASE WHEN class ='A' THEN 1 END)/
nullif((SUM(CASE WHEN class ='B' THEN 1 END)+ SUM(CASE WHEN class ='C' THEN 1 END)),0))*100),1)) as Percentage, col4,col5
from table
group by class,col2
order by class;P.S: Percentage is the alias for the complex query, I am using.... it is not the column name.
Front end is APEX, once the code is syntactically correct, I can use it in APEX. So didn't post it in APEX forum.
Hi,
Emy_Jack wrote:
I am sorry..
OK the structure of the table is the following
TABLE NAME: STATISTICS
STRUCTURE:
REGION VARCHAR2(50),
COUNTRY VARCHAR2(50),
SITE VARCHAR2(50),
GROUP VARCHAR2(50),
GROUP isn't the best name for a column; it will get mixed up with the keyword GROUP used in GROUP BY.
Why not name your column something that is not an Oracle keyword, such as grp or stats_group?
LANGUAGE VARCHAR2(50),
PEOPLE VARCHAR2(50)Posting the table structure is better than nothing, but it would be even better to post a working CREATE TABLE statement, so the people who want to help you can re-create the problem for themselves, and test their ideas.
So the query is:
SELECT * FROM(
SELECT REGION,COUNTRY,SITE,
ROUND(((COUNT(CASE WHEN GROUP_FIRST ='A' THEN 1 END)/
There's no column called group_<b>first</b> in the table structure you posted.
NULLIF((SUM(CASE WHEN GROUP ='B' THEN 1 END)+
SUM(CASE WHEN GROUP ='A' THEN 1 END)),0))*100),1)
AS "PERCENTAGE",
CASE
WHEN
"PERCENTAGE" >=75 THEN 'red'You can't define a column alias (such as percentage) and reference it in the same SELECT clause. In fact, the ORDER BY clause is the only place where you can use a column alias in the same sub-query where it was defined.
If you define a column alais in a sub-query, then you can use it anywhere you want to in a super-query. In this query, do the GROUP BY and compute percentage in a sub-query, then compute color (referencing percentage) in the main query.
ELSE
'green'
end "Color", group,peopleIf group and people are not in the GROUP BY clause, then you can't use them by themselves in the SELECT clause like this.
FROM
STATISTICS
WHERE GROUP IN ('B','A')
GROUP BY REGION,COUNTRY,SITE
ORDER BY REGION,COUNTRY);ORDER BY in a sub-query is usually just a waste of resources. The order will not necessarily be preserved in the main result set. Don't use ORDER BY in the sub-query here, use it only in the main query.
ERROR:
ORA-00904: "PERCENTAGE": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 8 Column: 5Edited by: Emy_Jack on Mar 16, 2013 3:06 AMAs an example of how to use a sub-query to define an alias:
WITH got_percentage AS
SELECT region, country, site
, AVG ( CASE
WHEN group_first = 'A' THEN 100
ELSE 0
END
) AS percentage
FROM statistics
WHERE group_first IN ('A', 'B')
GROUP BY region, country, site
SELECT region, country, site
, percentage -- if wanted
, CASE
WHEN percentage >= 75 THEN 'red'
ELSE 'green'
END AS color
FROM got_percentage
ORDER BY region_country
;If you'd care to post CREATE TABLE and INSERT statements for a little sample data, and the results you want from that sample data, then I could test this.
Edited by: Frank Kulash on Mar 16, 2013 7:17 AM
Added query.
Similar Messages
-
SUM(CASE WHEN statement is not returning proper result.
Hi Folks,
I'm an end-user on OBIEE 11g creating a custom analysis. I'm trying to SUM a particular case statement: SUM(CASE WHEN "MyMeasureColumn.Here" LIKE '%~25~%' OR "MyMeasureColumn.Here" LIKE '%~26~%' OR "MyMeasureColumn.Here" LIKE '%~27~%' OR "MyMeasureColumn.Here" LIKE '%~28~%' THEN 1 ELSE 0 END)
The result should be about 6,700 however I am getting only 1,900. Any ideas?>
Ive done a manual validation of the statement (in excel) and know its near 6700. I think it has something to do with the type of data it is?
>
Well there are only three possibilities that I can think of.
1. The data in Excel is different than the data in Oracle
2. The 'manual validation' is different than what the query that you are using does
3. Both #1 and #2
You are the only one with access to the data.
You are the only one that has any idea what your 'manual validation' was.
If necessary you will need to use the brute force 'divide and conquer' method.
Use a small amount of data and compare your 'manual validation to your query results. If they are not the same figure out why.
If they are the same then repeat the process with another set of the data. -
Case when statement not working
hi there, I am trying to work out how to get my case statement to work.
I have got the following code.
select pthproto.pthdbo.cnarole.tpkcnarole, pthproto.pthdbo.cnaidta.formataddr as formataddr, cnaidta.dateeffect as maxdate, isnull(cast (pthproto.pthdbo.cnaaddr.prefix1key as varchar (50)),'') + ' ' + isnull(cast (pthproto.pthdbo.cnaaddr.prefix2key
as varchar (50)),'')+ ' ' + isnull(cast (pthproto.pthdbo.cnaaddr.prefix3key as varchar (50)),'') + ' ' + isnull (cast (pthproto.pthdbo.cnaaddr.houseidkey as varchar (100)),'') + ' ' + isnull (cast (pthproto.pthdbo.cnaaddr.component1
as varchar (100)),'') + ' ' + isnull (cast (pthproto.pthdbo.cnaaddr.component2 as varchar (100)),'') + ' ' + isnull (cast (pthproto.pthdbo.cnaaddr.component3 as varchar (100)),'') + ' ' + isnull (cast (pthproto.pthdbo.cnaaddr.component4
as varchar (100)),'') + ' ' + isnull (cast (pthproto.pthdbo.cnaaddr.component5 as varchar (100)),'') as mailaddress, row_number() over(partition by pthproto.pthdbo.cnarole.tpkcnarole order by cnaidta.dateeffect desc) as rn into #address from pthproto.pthdbo.cnarole
inner join pthproto.pthdbo.cnaidty on cnarole.tfkcnaidty =cnaidty.tpkcnaidty inner join pthproto.pthdbo.cnaidta on cnaidty.tpkcnaidty = cnaidta.tfkcnaidty inner join pthproto.pthdbo.cnaaddr on cnaidta.tfkcnaaddr = cnaaddr.tpkcnaaddr order by cnaidta.dateeffect
select *, case when mailaddress is not null then mailaddress else formataddr end as test from #address where tpkcnarole = '18306695'
The case when statement is struggling with how i have created the column mailaddress. As it does seem to understand when it is null. In the example I have got there is no value in any of the columns to create
the mailaddress. Hence why I am referencing it from elsewhere. Due to having a way on the system where it picks up data from 2 different places. The mailaddress is always correct if there is one, hence why
trying to reference that one first. So how do i change this case when statement to work ?It's ok I have fixed my own problem
when
(mailaddress
is
null
or mailaddress
=
then formataddr
else mailaddress
end
as test
case -
Using case when statement in the select query to create physical table
Hello,
I have a requirement where in I have to execute a case when statement with a session variable while creating a physical table using a select query. let me explain with an example.
I have a physical table based on a select table with one column.
SELECT 'VALUEOF(NQ_SESSION.NAME_PARAMETER)' AS NAME_PARAMETER FROM DUAL. Let me call this table as the NAME_PARAMETER table.
I also have a customer table.
In my dashboard that has two pages, Page 1 contains a table with the customer table with column navigation to my second dashboard page.
In my second dashboard page I created a dashboard report based on NAME_PARAMETER table and a prompt based on customer table that sets the NAME_ PARAMETER request variable.
EXECUTION
When i click on a particular customer, the prompt sets the variable NAME_PARAMETER and the NAME_PARAMETER table shows the appropriate customer.
everything works as expected. YE!!
Now i created another table called NAME_PARAMETER1 with a little modification to the earlier table. the query is as follows.
SELECT CASE WHEN 'VALUEOF(NQ_SESSION.NAME_PARAMETER)'='Customer 1' THEN 'TEST_MART1' ELSE TEST_MART2' END AS NAME_PARAMETER
FROM DUAL
Now I pull in this table into the second dashboard page along with the NAME_PARAMETER table report.
surprisingly, NAME_PARAMETER table report executes as is, but the other report based on the NAME_PARAMETER1 table fails with the following error.
Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P
State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 16001] ODBC error state: S1000 code: 1756 message: [Oracle][ODBC][Ora]ORA-01756: quoted string not properly terminated. [nQSError: 16014] SQL statement preparation failed. (HY000)
SQL Issued: SET VARIABLE NAME_PARAMETER='Novartis';SELECT NAME_PARAMETER.NAME_PARAMETER saw_0 FROM POC_ONE_DOT_TWO ORDER BY saw_0
If anyone has any explanation to this error and how we can achieve the same, please help.
Thanks.Hello,
Updates :) sorry.. the error was a stupid one.. I resolved and I got stuck at my next step.
I am creating a physical table using a select query. But I am trying to obtain the name of the table dynamically.
Here is what I am trying to do. the select query of the physical table is as follows.
SELECT CUSTOMER_ID AS CUSTOMER_ID, CUSTOMER_NAME AS CUSTOMER_NAME FROM 'VALUEOF(NQ_SESSION.SCHEMA_NAME)'.CUSTOMER.
The idea behind this is to obtain the data from the same table from different schemas dynamically based on what a session variable. Please let me know if there is a way to achieve this, if not please let me know if this can be achieved in any other method in OBIEE.
Thanks. -
CASE Statement is not working Derived table
Hi All,
in the bello SQL Statement case statement is not working in derived table. I am new to creation of derived table if any body knows plz kinldy help me out on this.
SELECT x.market, x.droprate as med1
FROM
(select upper(market_name) as market, fulldate as date_value,
(sum([Dy_LOT_DROPS_N][Dy_OB_HO_DROPS][Dy_NonRF_Drop]))/
nullif(sum(CASE WHEN (month(BBHDLY.FullDate)}>= 6 and { year(BBHDLY.FullDate)} = 2011) or {fn year(IDENSLABBHDLY.FullDate)} > 2011
THEN BBHDLY.Dy_Calls - BBHDLY.Dy_HO_CHAN_ALLOC ELSE BBHDLY.Dy_Calls END),0)*100 as droprate
from BBHDLY sla
inner join Dim mkt
on sla.bts_name = mkt.bts_name and sla.SectorID = mkt.Sector_Id
where fulldate >= GETDATE()-46
group by market_name, fulldate) x,
(select market_name as market, fulldate as date_value,
(sum([Dy_LOT_DROPS_N][Dy_OB_HO_DROPS][Dy_NonRF_Drop]))/
nullif(sum(CASE WHEN ({fn month(BBHDLY.FullDate)}>= 6 and {fn year(BBHDLY.FullDate)} = 2011) or {fn year(BBHDLY.FullDate)} >
2011 THEN BBHDLY.Dy_Calls - BBHDLY.Dy_HO_CHAN_ALLOC ELSE BBHDLY.Dy_Calls END),0)*100 as droprate
from BBHDLY sla
inner join Dim mkt
on sla.bts_name = mkt.bts_name and sla.SectorID = mkt.Sector_Id
where fulldate >=GETDATE()-46
group by market_name, fulldate) y
where x.market = y.market
GROUP BY x.droprate, x.market
HAVING
SUM(CASE WHEN y.droprate <= x.droprate
THEN 1 ELSE 0 END)>=(COUNT(*)+1)/2 AND
SUM(CASE WHEN y.droprate >= x.droprate
THEN 1 ELSE 0 END)>=(COUNT(*)/2)+1
ThanksIt looks like SQL Server or Sybase given that you're using getdate().
As such, Vinesh's comment to use decode is wrong - decode is Oracle syntax.
Looking at your statement again, I've noticed the following:
you have no { to match the first } - not sure why you're using them anyway.
you haven't given x.market a name - use x.market as market instead
use coalesce instead of nullif if you're on SQL Server. -
Case statement insert not working
This cursor is retrieving data for each pidm
SPRIDEN_PIDM ACTIVITY1 ACTIVITY2 ACTIVITY3 ACTIVITY4 ACTIVITY5 ACTIVITY6
2287953 RA RC RF RJ RN RU
CURSOR activities_cursor
IS
SELECT spriden_pidm,
REGEXP_SUBSTR (szcasup_activities,
'[[:alpha:]]+',
1,
1
) activity1,
REGEXP_SUBSTR (szcasup_activities,
'[[:alpha:]]+',
1,
2
) activity2,
REGEXP_SUBSTR (szcasup_activities,
'[[:alpha:]]+',
1,
3
) activity3,
REGEXP_SUBSTR (szcasup_activities,
'[[:alpha:]]+',
1,
4
) activity4,
REGEXP_SUBSTR (szcasup_activities,
'[[:alpha:]]+',
1,
5
) activity5,
REGEXP_SUBSTR (szcasup_activities,
'[[:alpha:]]+',
1,
6
) activity6
FROM saturn_midd.szcasup, saturn.spriden
WHERE szcasup_common_appl_id = spriden_id
AND spriden_ntyp_code = 'CAPP';Then I fetched the results in variadles
FETCH activities_cursor
INTO v_pidm, v_activity1, v_activity2, v_activity3, v_activity4,
v_activity5,v_activity6;
Created a loop with a case statement
LOOP
FETCH activities_cursor
INTO v_pidm, v_activity1, v_activity2, v_activity3, v_activity4,
v_activity5,v_activity6;
EXIT WHEN activities_cursor%NOTFOUND;
IF activities_cursor%FOUND
THEN
CASE
WHEN (v_activity1 IS NOT NULL)
THEN
INSERT INTO saturn.sorints
(sorints_pidm, sorints_ints_code,
sorints_activity_date)
values
v_pidm,
v_activity1,
sysdate
WHEN (v_activity2 IS NOT NULL)
THEN
INSERT INTO saturn.sorints
(sorints_pidm, sorints_ints_code,
sorints_activity_date)
values
v_pidm,
v_activity2,
sysdate
WHEN (v_activity3 IS NOT NULL)
THEN
INSERT INTO saturn.sorints
(sorints_pidm, sorints_ints_code,
sorints_activity_date)
values
v_pidm,
v_activity3,
sysdate
WHEN (v_activity4 IS NOT NULL)
THEN
INSERT INTO saturn.sorints
(sorints_pidm, sorints_ints_code,
sorints_activity_date)
values
v_pidm,
v_activity4,
sysdate
END CASE;
END IF;
END LOOP;
It is working just for one activity (activity1) The variable definition is correctv_activity1 sorints.sorints_ints_code%TYPE;
v_activity2 sorints.sorints_ints_code%TYPE;
v_activity3 sorints.sorints_ints_code%TYPE;
v_activity4 sorints.sorints_ints_code%TYPE;
v_activity5 sorints.sorints_ints_code%TYPE;
v_activity6 sorints.sorints_ints_code%TYPE;
Edited by: peace4all on Aug 16, 2009 9:42 AMpeace4all wrote:
It is working just for one activity (activity1) The variable definition is correct
Well, what else did you expect? CASE evaluates brances starting first one. If WHEN condition is not met it continues to next one. As soon as WHEN condition is met, THEN clause is executed after that CASE exits. You need to use IF statement, not case:
with t as (
select to_date('01/11/2005','dd/mm/yyyy') Start_Date,to_date('13/11/2005','dd/mm/yyyy') End_Date,13 Days,'CMP1' Component,1000.00 Amount from dual union all
select to_date('01/11/2005','dd/mm/yyyy'),to_date('13/11/2005','dd/mm/yyyy'),13,'CMP2',826.67 from dual union all
select to_date('15/11/2005','dd/mm/yyyy'),to_date('20/11/2005','dd/mm/yyyy'),6,'CMP1',413.33 from dual union all
select to_date('15/11/2005','dd/mm/yyyy'),to_date('20/11/2005','dd/mm/yyyy'),6,'CMP2',166.67 from dual union all
select to_date('21/11/2005','dd/mm/yyyy'),to_date('28/11/2005','dd/mm/yyyy'),8,'CMP1',633.00 from dual union all
select to_date('21/11/2005','dd/mm/yyyy'),to_date('28/11/2005','dd/mm/yyyy'),8,'CMP2',111.00 from dual union all
select to_date('21/11/2005','dd/mm/yyyy'),to_date('28/11/2005','dd/mm/yyyy'),8,'CMP3',22.00 from dual union all
select to_date('21/11/2005','dd/mm/yyyy'),to_date('28/11/2005','dd/mm/yyyy'),8,'CMP4',333.00 from dual union all
select to_date('01/12/2005','dd/mm/yyyy'),to_date('15/12/2005','dd/mm/yyyy'),15,'CMP1',2480.00 from dual union all
select to_date('01/12/2005','dd/mm/yyyy'),to_date('15/12/2005','dd/mm/yyyy'),15,'CMP2',1488.00 from dual union all
select to_date('01/12/2005','dd/mm/yyyy'),to_date('15/12/2005','dd/mm/yyyy'),15,'CMP3',496.00 from dual union all
select to_date('01/12/2005','dd/mm/yyyy'),to_date('15/12/2005','dd/mm/yyyy'),15,'CMP4',200.00 from dual union all
select to_date('16/12/2005','dd/mm/yyyy'),to_date('31/12/2005','dd/mm/yyyy'),16,'CMP1',522.00 from dual union all
select to_date('16/12/2005','dd/mm/yyyy'),to_date('31/12/2005','dd/mm/yyyy'),16,'CMP2',166.00 from dual union all
select to_date('16/12/2005','dd/mm/yyyy'),to_date('31/12/2005','dd/mm/yyyy'),16,'CMP3',242.00 from dual
SELECT P_Month,
SUM(LEAST(LAST_DAY(P_Month),end_date) - GREATEST(P_Month,start_date) + 1) Total_Days
FROM t,
SELECT ADD_MONTHS(first_month,LEVEL - 1) P_Month
FROM (
SELECT TRUNC(MIN(start_date),'MM') first_month,
TRUNC(MAX(end_date),'MM') last_month
FROM t
CONNECT BY LEVEL <= MONTHS_BETWEEN(last_month,first_month) + 1
WHERE P_Month BETWEEN TRUNC(start_date,'MM') and end_date
GROUP BY P_Month
ORDER BY P_Month
with t as (
select to_date('01/11/2005','dd/mm/yyyy') Start_Date,to_date('13/11/2005','dd/mm/yyyy') End_Date,13 Days,'CMP1' Component,1000.00 Amount from dual union all
select to_date('01/11/2005','dd/mm/yyyy'),to_date('13/11/2005','dd/mm/yyyy'),13,'CMP2',826.67 from dual union all
select to_date('15/11/2005','dd/mm/yyyy'),to_date('20/11/2005','dd/mm/yyyy'),6,'CMP1',413.33 from dual union all
select to_date('15/11/2005','dd/mm/yyyy'),to_date('20/11/2005','dd/mm/yyyy'),6,'CMP2',166.67 from dual union all
select to_date('21/11/2005','dd/mm/yyyy'),to_date('28/11/2005','dd/mm/yyyy'),8,'CMP1',633.00 from dual union all
select to_date('21/11/2005','dd/mm/yyyy'),to_date('28/11/2005','dd/mm/yyyy'),8,'CMP2',111.00 from dual union all
select to_date('21/11/2005','dd/mm/yyyy'),to_date('28/11/2005','dd/mm/yyyy'),8,'CMP3',22.00 from dual union all
select to_date('21/11/2005','dd/mm/yyyy'),to_date('28/11/2005','dd/mm/yyyy'),8,'CMP4',333.00 from dual union all
select to_date('01/12/2005','dd/mm/yyyy'),to_date('15/12/2005','dd/mm/yyyy'),15,'CMP1',2480.00 from dual union all
select to_date('01/12/2005','dd/mm/yyyy'),to_date('15/12/2005','dd/mm/yyyy'),15,'CMP2',1488.00 from dual union all
select to_date('01/12/2005','dd/mm/yyyy'),to_date('15/12/2005','dd/mm/yyyy'),15,'CMP3',496.00 from dual union all
select to_date('01/12/2005','dd/mm/yyyy'),to_date('15/12/2005','dd/mm/yyyy'),15,'CMP4',200.00 from dual union all
select to_date('16/12/2005','dd/mm/yyyy'),to_date('31/12/2005','dd/mm/yyyy'),16,'CMP1',522.00 from dual union all
select to_date('16/12/2005','dd/mm/yyyy'),to_date('31/12/2005','dd/mm/yyyy'),16,'CMP2',166.00 from dual union all
select to_date('16/12/2005','dd/mm/yyyy'),to_date('31/12/2006','dd/mm/yyyy'),381,'CMP3',242.00 from dual
SELECT P_Month,
SUM(LEAST(LAST_DAY(P_Month),end_date) - GREATEST(P_Month,start_date) + 1) Total_Days
FROM t,
SELECT ADD_MONTHS(first_month,LEVEL - 1) P_Month
FROM (
SELECT TRUNC(MIN(start_date),'MM') first_month,
TRUNC(MAX(end_date),'MM') last_month
FROM t
CONNECT BY LEVEL <= MONTHS_BETWEEN(last_month,first_month) + 1
WHERE P_Month BETWEEN TRUNC(start_date,'MM') and end_date
GROUP BY P_Month
ORDER BY P_Month
LOOP
FETCH activities_cursor
INTO v_pidm, v_activity1, v_activity2, v_activity3, v_activity4,
v_activity5,v_activity6;
EXIT WHEN activities_cursor%NOTFOUND;
IF v_activity1 IS NOT NULL
THEN
INSERT INTO saturn.sorints
(sorints_pidm, sorints_ints_code,
sorints_activity_date)
values
v_pidm,
v_activity1,
sysdate
ELSIF v_activity2 IS NOT NULL
THEN
INSERT INTO saturn.sorints
(sorints_pidm, sorints_ints_code,
sorints_activity_date)
values
v_pidm,
v_activity2,
sysdate
ELSIF v_activity3 IS NOT NULL
THEN
INSERT INTO saturn.sorints
(sorints_pidm, sorints_ints_code,
sorints_activity_date)
values
v_pidm,
v_activity3,
sysdate
ELSIF v_activity4 IS NOT NULL
THEN
INSERT INTO saturn.sorints
(sorints_pidm, sorints_ints_code,
sorints_activity_date)
values
v_pidm,
v_activity4,
sysdate
END IF;
END LOOP;SY. -
Update query not working in the JDBC sender Communication channel
Hi,
We are working on JDBC to File scenario. As per the configuration, XI should pick the data from SQL database every 20 secs and should update the corresponding flag. We are using subquery in the select and update statement as both header and detail tables are involved.
Now the issue is, select query is working fine but update statement is not working as expected. It is somehow updating some other records rather than doing for the ones selected by the adapter.
Moreover logSQLstatement is also not working. Hence we are unable to identify the records which are getting updated.
Please advise.Hi Rumi,
See Question 8. Transaction Handling (Sender) in [SAP Note 831162 - FAQ: XI 3.0 / PI 7.0 / PI 7.1 JDBC Adapter|https://websmp130.sap-ag.de/sap(bD1wdCZjPTAwMQ==)/bc/bsp/spn/sapnotes/index2.htm?numm=831162].
8. Transaction Handling (Sender)
Q: If I have the following configured in a JDBC Sender:
Select Query:
SELECT column FROM TABLENAME WHERE FLAG = "TRUE"
Update Query:
UPDATE TABLENAME SET FLAG = "FALSE" WHERE FLAG = "TRUE"
How do I know that the JDBC adapter will not update newly added rows (rows that were
added between the time that the SELECT and UPDATE queries were executed) that were
not read in the initial SELECT query?
A: The SELECT and the UPDATE are run in the same DB transaction, i.e. both statements
have the same view on the database.
Make sure that both statements use the same WHERE clause. An additional
requirement for the correct operation of this scenario is the configuration of
an appropriate transaction isolation level on the database
(i.e., repeatable_read or serializable). You might also consider using a
"SELECT FOR UPDATE" statement instead of a plain SELECT statement to
ensure proper locking on the database. "SELECT FOR UPDATE"
is not supported in MS SQL database. In this case please make use of an
appropriate transaction isolation level on the database. For more details
please contact your DB vendors.
After, see Transaction Handling Issues in [SAP Note 1039779 - JDBC Adapter issues(Escape character,Transaction handling)|https://websmp130.sap-ag.de/sap(bD1wdCZjPTAwMQ==)/bc/bsp/spn/sapnotes/index2.htm?numm=1039779].
Best Regards.
Pedro Baroni -
Using more than one case when statement
Hi there,
I have a question on using case when statements.
Currently I have a table where it shows me mulitple dates.
Order Saledate TransferDate StartDate Enddate GetDate
I have created a where statement to show me records against the transferdate dependant if it appears within the date range of the year, or if it appears within the startdate and today's date.
(lpatran.trandate between targets.startdate
and (case when targets.enddate < getdate() then targets.enddate else getdate() end))
However on some of the records the transfer date is null, and so they want to use the saledate as the date to use.
So I created
(case when Lpatran.trandate is not null then (Lpatran.trandate between targets.startdate
and (case when targets.enddate < getdate() then targets.enddate else getdate()))
else (lpatran.saledate between targets.startdate
and (case when targets.enddate < getdate() then targets.enddate else getdate())) end )
However it gives me an incorrect syntax near the word ‘between ‘
Is there a simple fix for this or does sql not allow me to do this?I have created a new column called transferdate and done a case when statement
CASE
WHEN lpatran.trandate
IS
NULL
THEN lpatran.saledate
ELSE lpatran.trandate
END
AS Transferdate,
Then created a temptable and did a select * from temptable. Created a where statement from transferdate, which does work and gives me the correct data.
TransferDate between startdate
and(case
when enddate
<
getdate()
then enddate
else
getdate()
end))
However I wanted to know if there was a way of obtaining the same data without having to use the temptable ? -
Problem with case when statement, when doing an insert.
Hi there, I have written the following coding, that has got an issue, due to not being allowed null values to be inserted into a column on a table.
,Case
WHENCL.LocationISNOTNULLANDCL.[Floor]ISNOTNULLANDCL.RoomISNULLTHEN
(SELECTTop1
FL.FloorIDFROMPMIS.dbo.ConsentLocationF
JOINtbBuildingBONB.BuildingNumber=F.LocationCOLLATELatin1_General_CI_AS
JOINtbFloorFLONFL.BuildingID=B.BuildingID
WHEREF.Location=CL.LocationANDFL.FloorName=CL.[Floor]COLLATELatin1_General_CI_AS)
WHENCL.LocationISNOTNULLANDCL.[Floor]ISNOTNULLANDCL.RoomISNOTNULLTHEN
(SELECTTop1
R.RoomIDFROMPMIS.dbo.ConsentLocationF
JOINtbBuildingBONB.BuildingNumber=F.LocationCOLLATELatin1_General_CI_AS
JOINtbFloorFLONFL.BuildingID=B.BuildingID
JOINtbRoomRONR.FloorID=FL.FloorID
WHEREF.Location=CL.LocationANDFL.FloorName=CL.[Floor]COLLATELatin1_General_CI_ASANDR.RoomNumber=CL.RoomCOLLATELatin1_General_CI_AS)
ENDasParentID
I have written this case when statement above to find a ParentID. I have got the case when statement to work individually, e.g. I get the correct records back. However when I put it into a insert statement, as I want this ParentID to go
into a column in a table I get an error with the following message.
Cannot insert the value NULL into column 'ParentID', table 'K2_Master_4_Test.dbo.tbConsentLink'; column does not allow nulls. INSERT fails.
Can someone point me in the right direction, as to what I need to do to get this case when statement working? Also no I cannot change the structure of the table to allow for null values either. So I need to modifiy this one.The error is valid because your table column has 'not null' constraint and when your case expression does not satisfy either of the 'when' conditions, it is returning NULL to the insert statement.
you can get around this by defining a generic value. Simple Example
select case when <<Column>>=1 then 'First' When <<Column>>=2 then 'Second' Else 'Last' End
In this case, when column value is not 1 or 2, it will insert 'Last'.
So, you have to do something like this...i.e add ELSE condition to your case..
Hope it Helps!! -
Insert statement will not work if select statement has less number of colum
Hi,
One of my thread is already resolved on the following URL : unable to insert rows into the table
DROP TABLE TEMP;
CREATE TABLE TEMP AS SELECT * FROM CASE_101 WHERE 1=2;
DECLARE
S VARCHAR2(200);
STMT VARCHAR2(500);
begin
for C in (select TABLE_NAME INTO S from USER_TABLES where TABLE_NAME like 'CASE%' TABLE_NAME NOT like '%OLD' and Num_ROWS > 0 order by TABLE_NAME) loop
STMT := 'INSERT INTO TEMP SELECT * FROM ';
STMT:=STMT || C.TABLE_NAME;
EXECUTE IMMEDIATE STMT;
dbms_output.put_line(c.table_name);
end loop;
end;
i am facing now some different; almost all the tables have same number of columns except in few of tables have some additional columns. As above i am creating a table "TEMP" who has highest column temp(by doing some manual process). The table who has less columns than "TEMP" table : Insert statement will not work. 'INSERT INTO TEMP SELECT * FROM less_columns_table_name'.
Please let me know , how can i execute proper way.
Thanks.
Best Regards
Arshaduser13360241 wrote:
Hi,
One of my thread is already resolved on the following URL : unable to insert rows into the table
DROP TABLE TEMP;
CREATE TABLE TEMP AS SELECT * FROM CASE_101 WHERE 1=2;
DECLARE
S VARCHAR2(200);
STMT VARCHAR2(500);
begin
for C in (select TABLE_NAME INTO S from USER_TABLES where TABLE_NAME like 'CASE%' TABLE_NAME NOT like '%OLD' and Num_ROWS > 0 order by TABLE_NAME) loop
STMT := 'INSERT INTO TEMP SELECT * FROM ';
STMT:=STMT || C.TABLE_NAME;
EXECUTE IMMEDIATE STMT;
dbms_output.put_line(c.table_name);
end loop;
end;
i am facing now some different; almost all the tables have same number of columns except in few of tables have some additional columns. As above i am creating a table "TEMP" who has highest column temp(by doing some manual process). The table who has less columns than "TEMP" table : Insert statement will not work. 'INSERT INTO TEMP SELECT * FROM less_columns_table_name'.
Please let me know , how can i execute proper way.
Thanks.
Best Regards
Arshadmore often than not "TEMP" tables are NOT required & are highly inefficient in Oracle.
Either only specify explicit column in TEMP to get data,
or provide value for "extra" column in TEMP -
Insert statement is not working for z table.
Hi experts,
My insert statement is not working.
I have used follwing code to update z table .
INSERT ztable FROM TABLE gt_table.
here i have checked gt_table and its filled up with all the records properly.
now the problem is in this table i have 15 fields and it inserts 14 fields of it but
the last field is never inserted though in gt_table i can see value for last fields also.
I have added this field in ztable recently . so i also used se14 to adjust table but still i am facing same problem.
please help me out.
thanks,
Neo> > Table maintainance will have nothing to do with
> this
> > issue.
>
> It does sometimes when you are trying to see the
> values from SM30 instead of SE16. The value may be
> there, but it may just not seen in SM30 because the
> table maintenance hasn't registered the addition of
> new field.
>
> Another place to look at is the activation log to see
> if there are any warnings issued there.
You shouldn't use SM30 to view table entries. You use this transaction to maintain the table entries. Pure and Simple. -
Hello everybody,
I have the following SQL statement. To verify that p01.euro IS NOT NULL, I think I should add a "CASE WHEN" statement into the code, the problem is, that I don't know how to this here... The "CASE WHEN" statement should be for the subselect - statement I've marked in red color...
Perhaps you have an idea... Thanks
select
(select apex_item.select_list_from_query (5,name,'select name from cn_pl_projektidee order by idee_id asc', 'style="width:130px"', NULL) name
from cn_pl_projektidee s01 where s01.idee_id = t1.idee_id) idee,
(select htmldb_item.text(6,sum(stunden))stunden_ilp from cn_pl_std_peplanung t02
where abt_id = '1'
and t02.pe_id = t1.pe_id
and t02.version_id = t1.version_id
and t02.idee_id = t1.idee_id) stunden_ilp,
(select htmldb_item.text(7,sum(stunden))stunden_ild from cn_pl_std_peplanung t02
where abt_id = '3'
and t02.pe_id = t1.pe_id
and t02.version_id = t1.version_id
and t02.idee_id = t1.idee_id) stunden_ild,
(select htmldb_item.text(8,sum(stunden))stunden_ilm from cn_pl_std_peplanung t02
where abt_id = '4'
and t02.pe_id = t1.pe_id
and t02.version_id = t1.version_id
and t02.idee_id = t1.idee_id) stunden_ilm,
(select htmldb_item.text(9,sum(stunden))stunden_ief from cn_pl_std_peplanung t02
where abt_id = '9'
and t02.pe_id = t1.pe_id
and t02.version_id = t1.version_id
and t02.idee_id = t1.idee_id) stunden_ief,
(select htmldb_item.text(10,sum(stunden))stunden_ir from cn_pl_std_peplanung t02
where abt_id = '10'
and t02.pe_id = t1.pe_id
and t02.version_id = t1.version_id
and t02.idee_id = t1.idee_id) stunden_ir,
(select sum(euro) from cn_pl_primaerkostenplanung p01
where p01.pe_id = t1.pe_id
and p01.version_id = t1.version_id
and p01.idee_id = t1.idee_id) summe_la,
"CASE WHEN p01.euro IS NOT NULL THEN"
(select sum((sum(t02.stunden) * k01.euro_std * 8)+(select sum(p01.euro) from cn_pl_primaerkostenplanung p01
where p01.pe_id = t1.pe_id
and p01.version_id = t1.version_id
and p01.idee_id = t1.idee_id))
from cn_pl_std_peplanung t02, cn_pl_sap_leistungsarten k01
where k01.l_art_id = t1.l_art_id
and t02.pe_id = t1.pe_id
and t02.version_id = t1.version_id
and t02.idee_id = t1.idee_id
group by t02.pe_id, k01.euro_std) "END" kosten_ges,
(select distinct htmldb_item.hidden(2,pe_id) pe_id from cn_pl_std_peplanung t02
where t02.pe_id = t1.pe_id
and t02.version_id = t1.version_id) pe_id,
(select distinct htmldb_item.hidden(3,l_art_id) l_art_id from cn_pl_std_peplanung t02
where t02.pe_id = t1.pe_id
and t02.version_id = t1.version_id) l_art_id,
(select distinct htmldb_item.hidden(4,version_id) version_id from cn_pl_std_peplanung t02
where t02.pe_id = t1.pe_id
and t02.version_id = t1.version_id) version_id
from cn_pl_std_peplanung t1, cn_pl_projektelemente z1, cn_pl_version u1, cn_pl_projektidee s1
where t1.version_id = '&P6_VERSION_WAHL.' and t1.pe_id ='&P6_HELP_PRODET.' and t1.l_art_id ='&P6_L_ART_ID.'
group by t1.pe_id, t1.version_id, t1.idee_id, t1.l_art_id order by t1.idee_id descNot sure if the rest is ok, but syntactically you can go for
SELECT (SELECT apex_item.select_list_from_query (
5,
name,
'select name from cn_pl_projektidee order by idee_id asc',
'style="width:130px"',
NULL
name
FROM cn_pl_projektidee s01
WHERE s01.idee_id = t1.idee_id)
idee,
(SELECT htmldb_item.text (6, SUM (stunden)) stunden_ilp
FROM cn_pl_std_peplanung t02
WHERE abt_id = '1'
AND t02.pe_id = t1.pe_id
AND t02.version_id = t1.version_id
AND t02.idee_id = t1.idee_id)
stunden_ilp,
(SELECT htmldb_item.text (7, SUM (stunden)) stunden_ild
FROM cn_pl_std_peplanung t02
WHERE abt_id = '3'
AND t02.pe_id = t1.pe_id
AND t02.version_id = t1.version_id
AND t02.idee_id = t1.idee_id)
stunden_ild,
(SELECT htmldb_item.text (8, SUM (stunden)) stunden_ilm
FROM cn_pl_std_peplanung t02
WHERE abt_id = '4'
AND t02.pe_id = t1.pe_id
AND t02.version_id = t1.version_id
AND t02.idee_id = t1.idee_id)
stunden_ilm,
(SELECT htmldb_item.text (9, SUM (stunden)) stunden_ief
FROM cn_pl_std_peplanung t02
WHERE abt_id = '9'
AND t02.pe_id = t1.pe_id
AND t02.version_id = t1.version_id
AND t02.idee_id = t1.idee_id)
stunden_ief,
(SELECT htmldb_item.text (10, SUM (stunden)) stunden_ir
FROM cn_pl_std_peplanung t02
WHERE abt_id = '10'
AND t02.pe_id = t1.pe_id
AND t02.version_id = t1.version_id
AND t02.idee_id = t1.idee_id)
stunden_ir,
(SELECT SUM (euro)
FROM cn_pl_primaerkostenplanung p01
WHERE p01.pe_id = t1.pe_id
AND p01.version_id = t1.version_id
AND p01.idee_id = t1.idee_id)
summe_la,
CASE
WHEN p01.euro IS NOT NULL
THEN
( SELECT SUM( (SUM (t02.stunden) * k01.euro_std * 8)
+ (SELECT SUM (p01.euro)
FROM cn_pl_primaerkostenplanung p01
WHERE p01.pe_id = t1.pe_id
AND p01.version_id = t1.version_id
AND p01.idee_id = t1.idee_id))
FROM cn_pl_std_peplanung t02, cn_pl_sap_leistungsarten k01
WHERE k01.l_art_id = t1.l_art_id
AND t02.pe_id = t1.pe_id
AND t02.version_id = t1.version_id
AND t02.idee_id = t1.idee_id
GROUP BY t02.pe_id, k01.euro_std)
END
kosten_ges,
(SELECT DISTINCT htmldb_item.hidden (2, pe_id) pe_id
FROM cn_pl_std_peplanung t02
WHERE t02.pe_id = t1.pe_id AND t02.version_id = t1.version_id)
pe_id,
(SELECT DISTINCT htmldb_item.hidden (3, l_art_id) l_art_id
FROM cn_pl_std_peplanung t02
WHERE t02.pe_id = t1.pe_id AND t02.version_id = t1.version_id)
l_art_id,
(SELECT DISTINCT htmldb_item.hidden (4, version_id) version_id
FROM cn_pl_std_peplanung t02
WHERE t02.pe_id = t1.pe_id AND t02.version_id = t1.version_id)
version_id
FROM cn_pl_std_peplanung t1,
cn_pl_projektelemente z1,
cn_pl_version u1,
cn_pl_projektidee s1
WHERE t1.version_id = '&P6_VERSION_WAHL.'
AND t1.pe_id = '&P6_HELP_PRODET.'
AND t1.l_art_id = '&P6_L_ART_ID.'
GROUP BY t1.pe_id,
t1.version_id,
t1.idee_id,
t1.l_art_id
ORDER BY t1.idee_id DESC -
Using case when statement or decode stament in where clause
hi gems..
i have a problem in the following query..
i am trying to use case when statement in the where clause of a select query.
select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
cr.salary as salary
from customer_details cr
where (case when '>' = '>' then 'cr.salary > 5000'
when '>' = '<' then 'cr.salary < 5000'
when '>' = '=' then 'cr.salary = 5000'
else null
end);
the expression in the when clause of the case-when statement will come from UI and depending on the choice i need to make the where clause.
thats why for running the query, i have put '>' in that place.
so the original query will look like this(for your reference):
select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
cr.salary as salary
from customer_details cr
where (case when variable = '>' then 'cr.salary > 5000'
when variable = '<' then 'cr.salary < 5000'
when variable = '=' then 'cr.salary = 5000'
else null
end);
so, in actual case,if the user selects '>' then the filter will be "where cr.salary > 5000"
if the user selects '<' then the filter will be "where cr.salary < 5000"
if the user selects '=' then the filter will be "where cr.salary = 5000"
but i am getting the error "ORA 00920:invalid relational operator"
please help..thanks in advance..Hi,
select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
cr.salary as salary
from customer_details cr
where ( v_variable = 'bigger'
and cr.salary > 5000
or ( v_variable = 'less'
and cr.salary < 5000
or ( v_variable = 'eq'
and cr.salary = 5000
)Edited by: user6806750 on 22.12.2011 14:56
For some reason I can't write in sql '<', '>', '=' -
"IN" operator in Case When statement
Hi
When I'm trying to use "IN" operator in the "Case When" statement , the following error is being displayed : " [nQSError: 27002] Near <In>: Syntax error [nQSError: 26012] " .
Please let me know wether we can use "IN" oprerator in the Case When statement.
Thanks.Hi
Below is the code in which I'm trying to use "IN" operator in the CASE WHEN statement :
CASE WHEN Month=1 then Year IN (2009,2010) else 0 end
Above - (Year and month are of integer datatype, hence i did not use single quotes)
Thanks -
Problem Summary
Cancel Query not working on 11.5.10
Problem Description
While finding orders, window pops up the cancel query but when user tries to click the cancel button, query does not cancel.jemar98 wrote:
Problem Summary
Cancel Query not working on 11.5.10
Problem Description
While finding orders, window pops up the cancel query but when user tries to click the cancel button, query does not cancel.Please post the details of the application release, database version and OS.
Was this working before? If yes, any changes been done recently?
Please review (Canceling Long Running Queries in Oracle Applications 11i [ID 138159.1]) and make sure you complete all the steps.
Thanks,
Hussein -
Hide mouse cursor when typing is not working in IE11 Windows7
Hide mouse cursor when typing is not working in IE11 Windows7 - I saw a post that devs were looking at this in 2011? Slow progress huh?
This is really annoying when you have to move the cursor to see what you are typing...Hi,
Please first make sure that Mouse Properties\Pointer options\Hide pointer while typing is ticked.
If this option is already ticked, then try following steps to troubleshoot:
Update the driver for the mouse to get the best performance.
Choose a different cursor as Rob said for a test.
If this issue occurred recently, check if you have any newly installed programs or IE add-ons which cause this issue, you can test this in no add-ons mode, click start button, type "internet" in the search box, choose "internet explorer (No add-on)".
* I test in My IE, I have Hide pointer while typing checked, and in some typing boxes in IE the cursor will hide automatically, while some are not, but I think it's not an annoying issue for me.*
Regard
Yolanda
TechNet Community Support
Maybe you are looking for
-
My Mac's background changed to white and i can't change it back
Dear anybody, Yesterday I plugged in my external harddisk, and I just moving some folders from one partition to another. then in the middle of progress, it opened an error window and the external hd disappeared from my desktop icon and finder. I trie
-
Install 32-bit Oracle on a 64-bit OS
Hi, for some reason,i have to install 32-bit oracle on a SUN X4450.As you know ,it's X86 and OS on it is solaris 10 for soalris X86. Both the CPU and OS are 64-bit. All i want to know is that if there are some limits on server's RAM,such as RAM can't
-
HT1918 The credit on my account is not being used for purchases
I have redeemed two cards and am not being given the option of using the credit on my account from the cards purchased. I do not want to use a credit card at all. Why is Itunes so inefficient? How can I use the credit?
-
Mail 6.2 very sluggish in mac osx 10.8.2
mail 6.2 very sluggish in mac osx 10.8.2
-
I enabled auditing on database and used the following to enable: AUDIT ALL BY myuser BY ACCESS; AUDIT SELECT TABLE, UPDATE TABLE, INSERT TABLE, DELETE TABLE BY myuser BY ACCESS; AUDIT EXECUTE PROCEDURE BY myuser BY ACCESS;Now, I wanted to remove audi