Case statement in order by clause
Hi,
I have written the below query which is having CASE statement in ORDER BY clause. Please let me know what mistake i have done in the query because am getting "Missing Keyword" Error.
SELECT opn_quest_id, seq_nbr
FROM opinion_question
order by case when :p=1 then
opn_quest_id,seq_nbr
else
opn_quest_id
end;
Thanks,
Santhosh.S
Try Ignore the following solution.
SELECT opn_quest_id, seq_nbr
FROM opinion_question
ORDER BY CASE
WHEN : p = 1 THEN opn_quest_id || seq_nbr
ELSE opn_quest_id
END;
What are the data type of the corresponding columns used in the CASE Statement? I have assumed it to be strings.
!http://www.mysmiley.net/imgs/smile/sad/sad0049.gif! My Apologies....
Regards,
Jo
Edited by: Joice John on Jul 13, 2009 3:07 AM
Wrong Solution. Corrected by Sven.
Similar Messages
-
Query Tuning - using CASE statement in the WHERE clause
Hi All,
My query has been modified to use a CASE statement in the WHERE clause to consider data from certain columns based on a parameter value. This modified query is doing a full table scan and running endlessly. Please suggest what may be done to improve its performance:
Query:
SELECT LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))) AS summary_date,
os.acctnum,
os.avieworigin_refid,
COUNT(1) cnt_articleview,
SUM(NVL(autocompletedterm,0)) cnt_autocompletedterm
FROM TABLE1 os
WHERE os.acctnum IS NOT NULL
AND os.avieworigin_refid IS NOT NULL
AND os.requestdatetime IS NOT NULL
AND UPPER(os.success_ind) = 'S'
AND CASE WHEN
Param_ValueToCheck = 'FULL' AND get_date_timestamp(os.requestdatetime) BETWEEN
TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
THEN 1
WHEN
Param_ValueToCheck = 'INCR' AND os.entry_createddate BETWEEN
TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
THEN 1
END = 1
AND CASE WHEN
Param_ValueToCheck = 'FULL' AND os.entry_CreatedDate BETWEEN
TO_DATE('01-APR-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') AND
TO_DATE('07-JUN-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
THEN 1
WHEN
Param_ValueToCheck = 'INCR' THEN 1
END = 1
GROUP BY LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))), os.acctnum,os.avieworigin_refid;Table Description:
(Number of rows : approx > 600,000,000)
Name Null Type
ARTICLEID NOT NULL NUMBER(20)
USERKEY NUMBER(10)
AVIEWORIGIN_REFID VARCHAR2(10)
SUCCESS_IND VARCHAR2(2)
ENTRY_CREATEDDATE DATE
CREATED_BY VARCHAR2(10)
FILENUMBER NUMBER(10)
LINENUMBER NUMBER(10)
ACCTNUM VARCHAR2(10)
AUTOCOMPLETEDTERM NUMBER(2)
REQUESTDATETIME VARCHAR2(19)Explain Plan
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
Plan hash value: 2224314832
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
| 0 | SELECT STATEMENT | | 590 | 33040 | 2501K (1)| 08:20:15 | | |
| 1 | HASH GROUP BY | | 590 | 33040 | 2501K (1)| 08:20:15 | | |
| 2 | PARTITION RANGE ALL| | 590 | 33040 | 2501K (1)| 08:20:15 | 1 |1048575|
|* 3 | TABLE ACCESS FULL | TABLE1 | 590 | 33040 | 2501K (1)| 08:20:15 | 1 |1048575|
PLAN_TABLE_OUTPUT
Predicate Information (identified by operation id):
3 - filter(UPPER("OS"."SUCCESS_IND")='S' AND CASE WHEN ('FULL'='FULL' AND
"OS"."ENTRY_CREATEDDATE">=TO_DATE(' 2011-04-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
"OS"."ENTRY_CREATEDDATE"<=TO_DATE(' 2011-06-07 00:00:00', 'syyyy-mm-dd hh24:mi:ss')) THEN 1 WHEN
'FULL'='INCR' THEN 1 END =1 AND "OS"."REQUESTDATETIME" IS NOT NULL AND CASE WHEN ('FULL'='FULL'
AND "ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")>=TO_DATE(' 2011-05-01 00:00:00', 'syyyy-mm-dd
hh24:mi:ss') AND "ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")<=TO_DATE(' 2011-05-31 00:00:00',
'syyyy-mm-dd hh24:mi:ss')) THEN 1 WHEN ('FULL'='INCR' AND "OS"."ENTRY_CREATEDDATE">=TO_DATE('
2011-05-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "OS"."ENTRY_CREATEDDATE"<=TO_DATE('
PLAN_TABLE_OUTPUT
2011-05-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss')) THEN 1 END =1 AND "OS"."ACCTNUM" IS NOT NULL AND
"OS"."AVIEWORIGIN_REFID" IS NOT NULL)Edited by: Chaitanya on Jun 9, 2011 2:44 AM
Edited by: Chaitanya on Jun 9, 2011 2:47 AMHi Dom,
Modified Query:
SELECT LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))) AS summary_date,
os.acctnum,
os.avieworigin_refid,
COUNT(1) cnt_articleview,
SUM(NVL(autocompletedterm,0)) cnt_autocompletedterm
FROM TABLE1 os
WHERE os.acctnum IS NOT NULL
AND os.avieworigin_refid IS NOT NULL
AND os.requestdatetime IS NOT NULL
AND UPPER(os.success_ind) = 'S'
AND (('FULL' = 'FULL'
AND (get_date_timestamp(os.requestdatetime) BETWEEN TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
AND TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
AND os.entry_CreatedDate BETWEEN TO_DATE('01-APR-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
AND TO_DATE('07-JUN-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
OR ('FULL' = 'INCR'
AND os.entry_createddate BETWEEN TO_DATE('01-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS')
AND TO_DATE('31-MAY-2011 00:00:00','DD-MON-YYYY HH24:MI:SS') ))
GROUP BY LAST_DAY(TRUNC(TO_TIMESTAMP(os.requestdatetime, 'yyyymmddhh24:mi:ss.ff4'))), os.acctnum,os.avieworigin_refid;Execute Plan:
PLAN_TABLE_OUTPUT
Plan hash value: 3615447714
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | Pstart| Pstop |
| 0 | SELECT STATEMENT | | 25125 | 1374K| | 407K (1)| 01:21:36 | | |
| 1 | HASH GROUP BY | | 25125 | 1374K| 3768K| 407K (1)| 01:21:36 | | |
| 2 | PARTITION RANGE ITERATOR| | 25125 | 1374K| | 407K (1)| 01:21:32 | 29 | 31 |
|* 3 | TABLE ACCESS FULL | TABLE1 | 25125 | 1374K| | 407K (1)| 01:21:32 | 29 | 31 |
PLAN_TABLE_OUTPUT
Predicate Information (identified by operation id):
3 - filter("OS"."ENTRY_CREATEDDATE">=TO_DATE(' 2011-04-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
UPPER("OS"."SUCCESS_IND")='S' AND "OS"."REQUESTDATETIME" IS NOT NULL AND
"ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")>=TO_DATE(' 2011-05-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
"ODS"."GET_DATE_TIMESTAMP"("REQUESTDATETIME")<=TO_DATE(' 2011-05-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
"OS"."ACCTNUM" IS NOT NULL AND "OS"."AVIEWORIGIN_REFID" IS NOT NULL AND "OS"."ENTRY_CREATEDDATE"<=TO_DATE('
2011-06-07 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))Edited by: Chaitanya on Jun 9, 2011 4:51 AM -
Query Tuning - using CASE statement in the WHERE clause - Duplicate Post
Duplicate Post by mistake.
Please check
Query Tuning - using CASE statement in the WHERE clause
Edited by: Chaitanya on Jun 9, 2011 2:45 AM
Edited by: Chaitanya on Jun 9, 2011 2:46 AMDuplicate Post by mistake.
Please check
Query Tuning - using CASE statement in the WHERE clause
Edited by: Chaitanya on Jun 9, 2011 2:45 AM
Edited by: Chaitanya on Jun 9, 2011 2:46 AM -
Case Statement in a Where clause help
Oracle Database 11g Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
"CORE 11.2.0.1.0 Production"
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
Hello,
I have an APEX application that I need to build a SQL statement for a LOV (List of Values). I have a hidden filed that contains the customer type which can be an 'R' or 'B'. The query needs to be able to display two different result sets based on the customer type of 'R' or 'B'.
If the customer type is 'R' then:
SELECT drg_descr d, drg_code r
FROM distance_ranges
WHERE drg_min_miles IN (0,5)
ORDER BY drg_min_milesIf the customer type is 'B' then:
SELECT drg_descr d, drg_code r
FROM distance_ranges
WHERE drg_min_miles IN (0,5,10,15,20)
ORDER BY drg_min_milesCan someone help me with what I think needs to be a case statement?
Thanks,
JoeHi,
You can try CASE statement with WHERE clause
SELECT drg_descr d, drg_code r
FROM distance_ranges
WHERE (CASE param_cust_type
WHEN(param_cust_type='R') THEN (drg_min_miles IN (0, 5)
WHEN (param_cust_type='B') THEN (drg_min_miles IN (0,5,10,15,20)
END;
Please try and let me know if anything wrong.
Anyone from the forum comment my code if there is any wrong.
Thanks!
Naresh -
CASE in an order by clause?
Can I use a case statement in an order by clause to acheive the following?
I have set up a parameter (:p_order) and placed it in a case statement in my order by clause. The idea is that the user will select a value from a LoV in the report and then dependant upon which one they picked, it will order by their selection. The problem I'm hitting is that the CASE statement seems to want to return an actual value instead of just returning the alias name to complete the order by clause. Here is the syntax I'm using:
ORDER BY
CASE WHEN :p_order = 'INVOICE NUMBER' THEN INVOICE_NUMBER
WHEN :p_order = 'INVOICE DATE' THEN INVOICE_DATE
WHEN :p_order = 'VENDOR' THEN VENDOR_NAME
WHEN :p_order = 'CHECK NUMBER' THEN CHECK_NUMBER
WHEN :p_order = 'CHECK DATE' THEN CHECK_DATE
WHEN :p_order = 'CHECK AMOUNT' THEN CHECK_AMOUNT
WHEN :p_order = 'DOA CHECK NUMBER' THEN DOA_CHECK_NUMBER
WHEN :p_order = 'DOA CHECK DATE' THEN DOA_CHECK_DATE
WHEN :p_order = 'DOA CHECK AMOUNT' THEN DOA_CHECK_AMOUNT
END
Any ideas? How can I force the case statement to return the alias name instead of a value or is that defiance in the face of what case is? Thanks!
SteveNote, however, that if you want to dynamically choose the ORDER, you have to use dynamic SQL, not static SQL.
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
How do I use the CASE statement in the where clause?
Hello Everyone,
I have 2 queries that do what I need to do but I am trying to learn how to use the CASE statement.
I have tried to combine these 2 into one query using a case statement but don't get the results I need.
Could use some help on how to use the case syntax to get the results needed.
thanks a lot
select segment_name,
product_type,
count (distinct account_id)
FROM NL_ACCT
where
ind = 'N'
and
EM_ind = 'N'
and product_type in ('TAX','PAY')
and acct_open_dt between (cast('2006-01-17' as date)) and (cast('2006-01-17' as date) + 60)
GROUP BY 1,2
order by product_type
select segment_name,
product_type,
count (distinct account_id)
FROM NL_ACCT
where
ind = 'N'
and
EM_ind = 'N'
and product_type not in ('TAX','PAY')
and acct_open_dt between (cast('2006-01-17' as date)) and (cast('2006-01-17' as date) + 30)
group by 1,2
order by product_typeSomething like:
SELECT segment_name, product_type,
SUM(CASE WHEN account_id IN ('TAX','PAY') and
acct_open_dt BETWEEN TO_DATE('2006-01-17', 'yyyy-mm-dd') and
TO_DATE('2006-01-17', 'yyyy-mm-dd') + 60 THEN 1
ELSE 0 END) tax_pay,
SUM(CASE WHEN account_id NOT IN ('TAX','PAY') and
acct_open_dt BETWEEN TO_DATE('2006-01-17', 'yyyy-mm-dd') and
TO_DATE('2006-01-17', 'yyyy-mm-dd') + 30 THEN 1
ELSE 0 END) not_tax_pay
FROM NL_ACCT
WHERE ind = 'N' and
em_ind = 'N' and
acct_open_dt BETWEEN TO_DATE('2006-01-17', 'yyyy-mm-dd') and
TO_DATE('2006-01-17', 'yyyy-mm-dd') + 60
GROUP BY segment_name, product_type
ORDER BY product_typeNote: You cannor GROUP BY 1,2, you need to explicitly name the columns to group by.
HTH
John -
Case statement with group by clause
SELECT STP, CASE WHEN Alternate IS NULL THEN 3 ELSE 4 END as Ind,
CASE WHEN Alternate IS NULL THEN 'New Address' ELSE 'New Location' END as Description , Count(*) Rec_Cnt
FROM t_Ids
group by STP, CASE WHEN Alternate IS NULL THEN 3 ELSE 4 END, CASE WHEN Alternate IS NULL THEN 'New Address' ELSE 'New Location'
ORDER BY 1,2,3
I need a query something like this. Does anyone has any idea on this???You're missing the END on the GROUP BY Case statement, but otherwise this looks fine.
What problem are you having?
Also, please post DDL
Thanks
Carl -
Sort problem in a sql statement with Order by clause
Hello,
I execute this statement :
Select keyart from article order by keyart desc;
and i got this result :
KEYART
VI-ZFI/GN
VI-WWAD110
VI-WV83
VI-WVLZ816
VI-WVLZ811
VI-WVLP6
VI-WVLA9C3
VI-WVLA4R5
VI-WVLA2.8
VI-WVLA12B
VI-WVD9N
KEYART
VI-WVCP210
VI-WVCP100
VI-WVCM200
VI-WVCM140
VI-WVCM110
VI-WVCM100
VI-WVBP70
VI-WVBP110
VI-WVBP102
VI-WVBP100
VI-WVBM900
KEYART
VI-WVBM90
VI-WVBM80
VI-WVBM190
VI-WVBM170
VI-WVBM140
VI-WJ523
VI-WJ521
VI-WJ450
VI-WJ410
VI-WJSQ308
VI-WD7/MK
KEYART
VI-WD2N
VI-VTL-W
VI-VPT4023
VI-TVB125
VI-TC21S3M
VI-PTZFI/G
VI-PS 18M
VI-PAR/MON
VI-PAR/CAM
VI-MY-1CD
VI-MY-DS
KEYART
VI-MY-CA
VI-MO/BTM
VI-MJ-1ADA
VI-MJ-DS
VI-MA6024
VI-KXA11BS
VI-JC43BL
VI-JCM1000
VI-IR60071
VI-IR600BU
VI-IR-WIDE
KEYART
VID-WV95
VID-WV83
VID-WV80
VID-WV42
VID-WV40
VID-WVLF93
VID-WVLA6B
VID-WVLA4.
VID-WVLA36
VID-WVLA18
VID-WVLA12
KEYART
VID-WVBM90
VID-WVBM80
VID-WVBL90
VID-WVBB10
VI-DIVERS
VI-CAMERA
VI-AG6730
VI-AG6124B
VI-AG6040
TRV-TT1AS
TRV-TTE914
We can see that rows like VID- are between rows like VI- . It seems that order by doesn't take in charge the '-' Ok. But when i execute this statement :
select keyart from articles where keyart > 'VI-ZFI/GN' ;
-- VI-ZFI/GN is my bigger result in my first query
i got this result
KEYART
VID-WVBB10
VID-WVBL90
VID-WVBM80
VID-WVBM90
VID-WVLA12
VID-WVLA18
VID-WVLA36
VID-WVLA4.
VID-WVLA6B
VID-WVLF93
VID-WV40
KEYART
VID-WV42
VID-WV80
VID-WV83
VID-WV95
In this cas it seems that order by take in charge the '-'!!!
How can i do to have have no row in my second select or that the 2 queries order the row the same?
Please help me , it's quite urgent ....
thanks a lot
Fab
nullHi,
With the data provided by you,i created the same set of table with the values.Tried the same query,when sorting it considers '-' also and then only it sorts.For me the order was perferct with 'VID' values first and then 'VI' values.
when i issued the second query the values of 'VID' alone came .
i think something has gone wrong when the data has been feed.maybe u have entered the data in the lowercase and some values in uppercase.
For me with the same set of values it is working perfectly.
null -
Order By clause in Receiver JDBC adapter
Hello All,
Is it possible to construct a select statement that uses an Order by clause in JDBC receiver adapter?
ex : I would like the JDBC adapter to from a query as follows:
<b>Select * from EMP where sal = 10000 order by empid</b>
We are able to construct the select statement without order by clause but not with it.
Can any one suggest me how to do it. This is bit urgent for us.
Thanks
Abinashhi,
did you try with
SQL_DML
http://help.sap.com/saphelp_nw04/helpdata/en/2e/96fd3f2d14e869e10000000a155106/content.htm
Regards,
michal -
Issue with TableAdapter Configuration Wizard and CASE statements
I am having an issue with using any CASE statements in the select clause for the Table Adapter Configuration Wizard (TCW). I can take any query without a CASE and it will run fine, but inserting any CASE statement in the Select such as:
SELECT
(CASE WHEN :ras_sort = 'D' THEN TO_CHAR(a.TRANS_DATE,'YYYYMMDD')
WHEN :ras_sort = 'C' THEN c.CLAIMDEP_NAME
WHEN :ras_sort = 'A' THEN TO_CHAR(a.ACCOUNT_NUMBER)
END) sort1
FROM xxxx
causes the TCW to error with "error in list of function arguments, 'THEN' not recognized" and several other errors. I have tried with and without parameters in the CASE and have tried both forms of the CASE statement. I have tried multiple queries, all that run with no problem outside of TCW and ODP, but this seems to be a real issue
Is this a known problem and are there any workarounds that will allow use of the TCW?
Thanks,
DaveHi Ashish,
perhaps you launch the Web service wizard from the wrong location (i.e. not selecting the SEI you want to use). Did you strictly follow the steps described in the <a href="http://help.sap.com/saphelp_nwce10/helpdata/en/44/f36fa8fd1d41aae10000000a114a6b/frameset.htm">documentation</a> for creating the web service? -
Case statement in where clause ??
Hello gurus,
Can we use case statements in where clause ?? Any example will be great!
And also i would like to know, besides CASE and DECODE statements, Is there any way we can use IF ELSE statements in SELECT clause or in WHERE clause ?
Thank you!!Hi,
user642297 wrote:
Hoek,
Thanks for the reply
Whatever you return from 'then' should match your criteria.I didnt get this part...can you elaborate this part ?? Thank you!!Remember what a CASE expression does: it returns a single value in one of the SQL data types (or NULL).
You're probably familiar with conditions such as
WHERE col = 1Inthe example above, col could be replaced by any kind of expression: a function call, and operation (such as "d * 24") or a CASE expression, which is exactly what Hoek posted:
where case
when col = 6 then 1
when col = 9 then 1
end = 1;I think what Hoek meant about mnatching was this: since the CASE expression is being compared to a NUMBER, then every THEN clause (as well as the ELSE, if there is one) should return the same data type. You can't have one THEN clause return a NUMBER, and another one in the same CASE expression return a DATE, like this:
where case
when col = 6 then 1
when col = 9 then SYSDATE -- WRONG! Raises ORA-00932: inconsistent datatypes
end = 1;
By the way, it's rare when a CASE expression really helps in a WHERE clause. CASE is great for doing conitional stuff in places where you otherwise can't (in the ORDER BY clause, for example), but the WHERE clause was designed for conditions.
Hoek was just trying to give a simple example. If you really wanted those results, it would be simpler to say:
where col = 6
or col = 9and simpler still to say
where col IN (6, 9) -
Dual Case Column names fail in select ORDER BY clause
This one is solved, but I thought someone might be able to explain why it happens.
I'm a newbie, since I just downloaded Oracel XE last night, and am trying it out.
I set up a trivial table with 4 text columns. As it happened, I used dual case for some of my column names, such as 'Nickname'.
When trying out the Query Builder, the Select statement failed whenever I added a sort.
After some head scratching and back-and-forth with the sample data base (HR), I finally renamed all the columns to upper case. (e.g. column 'Nickname' became 'NICKNAME'). Then it works.
Seems like a bug, but maybe it's just a feature. Here's the code generated by the Query Builder. As you can see, there are no quotes around the Table name or column names in the ORDER BY clause (and the dual case version of the column name works if I put the quotes in manually and run it with SQL command.) Seems quirky.
select "TEAMS"."NICKNAME" as "NICKNAME",
"TEAMS"."CITY" as "CITY",
"TEAMS"."DIVISION" as "DIVISION",
"TEAMS"."CONFERENCE" as "CONFERENCE"
from "TEAMS" "TEAMS"
order by TEAMS.NICKNAME ASCWelcome to forum. :)
Now, answering your question - no i don't think this is a bug.
It is your code - which create this bug.
Now, question is - what happen?
Just check your select and from clause. You put all the names are in double quote. When you put anything within double quotes - it become a case sensitive. So, the problem occurs there.
Let's see ->
satyaki>
satyaki>select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for 32-bit Windows: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production
Elapsed: 00:00:01.11
satyaki>
satyaki>create table s_otn
2 (
3 id number(4)
4 );
Table created.
Elapsed: 00:00:02.14
satyaki>
satyaki>set lin 80
satyaki>
satyaki>desc s_otn;
Name Null? Type
ID NUMBER(4)
satyaki>
satyaki>desc S_OTN;
Name Null? Type
ID NUMBER(4)
satyaki>
satyaki>desc s_OtN;
Name Null? Type
ID NUMBER(4)
satyaki>
satyaki>drop table s_otn;
Table dropped.
Elapsed: 00:00:05.22
satyaki>
satyaki>
satyaki>create table "S_otn"
2 (
3 id number(4)
4 );
Table created.
Elapsed: 00:00:00.12
satyaki>
satyaki>
satyaki>desc S_otn;
ERROR:
ORA-04043: object S_otn does not exist
satyaki>
satyaki>desc S_OTN;
ERROR:
ORA-04043: object S_OTN does not exist
satyaki>
satyaki>desc s_otn;
ERROR:
ORA-04043: object s_otn does not exist
satyaki>
satyaki>desc "S_OTN";
ERROR:
ORA-04043: object "S_OTN" does not exist
satyaki>
satyaki>desc "S_otn";
Name Null? Type
ID NUMBER(4)
satyaki>Got me?
Regards.
Satyaki De. -
How to use a case statement in where clause
Hi All,
I have a requirement which is to bring all the claims that are created in the last month.So, i wrote a query something like this
select * from claims
where
(Month(ClaimOpenDate) = Month(Getdate())-1 and year(claimopendate) = year(getDate()))
which would give me any new claims created in last month of current year, but this condition fails if we are in the first month of a new year( lets say if we are in 2016 jan then month(getdate())-1 would be 0 and year(getdate()) would be 2016 so we dont
find any records where year is 2016 and month is 0 for claimopen).
So, i would like to use a case statament or something which can help me get around this one.
Can someone please help me with any suggestions?
ThanksHi Jason,
Thanks a lot for your help. This is what exactly i am looking for but i just gave a sample query above below is my original query
select
row_number() over (order by [ClaimNumber]) as DataElementName
,c.PolicyNumber as PolicyNum
, c.FirstName as CustNameF
,c.LastName as CustNameL
,c.PolicyForm as PolType
,'Homesite' as Company
,[ClaimNumber] as ClaimNum
,E.office as Ofc
,e.Supervisior_FullName as Team
, RIGHT(e.adjuster_Name ,LEN(e.adjuster_Name)- charindex(',' ,e.adjuster_Name)) as FORepF
, case when charindex(',' ,e.adjuster_Name) <> 0 then left(e.adjuster_Name,charindex(',' ,e.adjuster_Name)-1) else e.adjuster_Name end as FORepL
,e.AdjusterID as RepC -- not sure
,CONVERT ( varchar,c.LossDate ,101) as DOL
,convert (varchar,c.ClaimOpenDate,101) as DOR
,rtrim(c.Loss_State) as LossSt
,c.Loss_ZipCode as LossZIP
,c.Loss_City as LossCity
,c.LossType as FOL
,'' as PR
,'' as PRNum
,1 as FeaNum
,'HO' as FeaType
,case when rtrim(c.claimStatus)= 'Closed' then 'Closed' else 'Open' end as FeaStat
,'' as FeaOpen
,'' as FeaClosed
,s.PaymentIndemnityAmount as PaidAmt
,s.ReserveIndemnityAmount as Reserve
,'' as Sub
,'' as Sal
,'' as FeatOwnOfc
,e.Supervisior_FullName as FeatOwnTeam
,RIGHT(e.adjuster_Name ,LEN(e.adjuster_Name)- charindex(',' ,e.adjuster_Name)) as FeatOwnRepF
,case when charindex(',' ,e.adjuster_Name) <> 0 then left(e.adjuster_Name,charindex(',' ,e.adjuster_Name)-1) else e.adjuster_Name end as FeatOwnRepL
,e.AdjusterID as FeatOwnRepCode
,NULL AS Description --not sure
from [Stg].[HS_DW_RV_Claims] c
inner join [dbo].[Claims_Primary_Adjuster] a on a.CLAIM_NUMBER = c.ClaimNumber
inner join [dbo].[vw_Adjuster] e on e.adjuster_Name = a.primary_ADJUSTER
left outer join [Stg].[HS_DW_LossClaimSummary] s on c.ClaimKey=s.ClaimKey
where c.LoadSource = 'CMS'
and
(s.PaymentIndemnityAmount <>0 or s.PaymentExpenseAmount <>0)
and
ClaimOpenDate BETWEEN DATEADD(mm, DATEDIFF(mm, 0, CURRENT_TIMESTAMP) -1, 0) AND DATEADD(mm, DATEDIFF(mm, 0, CURRENT_TIMESTAMP), 0)
UNION ALL
select
row_number() over (order by [ClaimNumber]) as DataElementName
,c.PolicyNumber as PolicyNum
, c.FirstName as CustNameF
,c.LastName as CustNameL
,c.PolicyForm as PolType
,'Homesite' as Company
,[ClaimNumber] as ClaimNum
,E.office as Ofc
,e.Supervisior_FullName as Team
, RIGHT(e.adjuster_Name ,LEN(e.adjuster_Name)- charindex(',' ,e.adjuster_Name)) as FORepF
, case when charindex(',' ,e.adjuster_Name) <> 0 then left(e.adjuster_Name,charindex(',' ,e.adjuster_Name)-1) else e.adjuster_Name end as FORepL
,e.AdjusterID as RepC -- not sure
,CONVERT ( varchar,c.LossDate ,101) as DOL
,convert (varchar,c.ClaimOpenDate,101) as DOR
,rtrim(c.Loss_State) as LossSt
,c.Loss_ZipCode as LossZIP
,c.Loss_City as LossCity
,c.LossType as FOL
,'' as PR
,'' as PRNum
,1 as FeaNum
,'HO' as FeaType
,case when rtrim(c.claimStatus)= 'Closed' then 'Closed' else 'Open' end as FeaStat
,'' as FeaOpen
,'' as FeaClosed
,s.PaymentIndemnityAmount as PaidAmt
,s.ReserveIndemnityAmount as Reserve
,'' as Sub
,'' as Sal
,'' as FeatOwnOfc
,e.Supervisior_FullName as FeatOwnTeam
,RIGHT(e.adjuster_Name ,LEN(e.adjuster_Name)- charindex(',' ,e.adjuster_Name)) as FeatOwnRepF
,case when charindex(',' ,e.adjuster_Name) <> 0 then left(e.adjuster_Name,charindex(',' ,e.adjuster_Name)-1) else e.adjuster_Name end as FeatOwnRepL
,e.AdjusterID as FeatOwnRepCode
,DESCRIPTION --not sure
from Stg.IG_Document D
inner join [Stg].[HS_DW_RV_Claims] c on D.PARENTREF = C.ClaimNumber
inner join [dbo].[Claims_Primary_Adjuster] a on a.CLAIM_NUMBER = c.ClaimNumber
inner join [dbo].[vw_Adjuster] e on e.adjuster_Name = a.primary_ADJUSTER
left outer join [Stg].[HS_DW_LossClaimSummary] s on c.ClaimKey=s.ClaimKey
where c.LoadSource = 'CMS'
and
DESCRIPTION like '%Denial Letter%'
and
ClaimOpenDate BETWEEN DATEADD(mm, DATEDIFF(mm, 0, CURRENT_TIMESTAMP) -1, 0) AND DATEADD(mm, DATEDIFF(mm, 0, CURRENT_TIMESTAMP), 0)
So if i use your logic in the end for both the where clauses its been more than 10 minutes and the query is still running however if i use my old method it doesnt even take a second. Looks like its affecting the execution plan. Any suggestions to get around
this one please?
Thanks -
Using CASE in the join statement in the AND clause. Where's the problem?
All,
I have my code, where based on the case statement, I would like to restrict the number of records in my JOIN condition. It's the CASE statement that I have used, that's giving me the problem. Not sure, what the problem is. The error says (in my case expression):
Msg 102, Level 15, State 1, Line 59
Incorrect syntax near '>'.
Select a.Col1, a.Col2, b.Col1, b.Col2, b.Col3
From Table1 a
Join Table2 b
On a.id = b.id
Join Table3 c
On c.FiltId = a.FiltId
And
Case
When b.Col3 <> 5 --b.Col3 is an INT field
Then (b.OutDate >= DATEADD(DAY, -30, c.InsertDate) And b.OutDate <= DATEADD(DAY, 30, c.InsertDate))
When b.Col3 = 5
Then (b.InDate >= DATEADD(DAY, 5, c.InsertDate))
End
Join Table 4 -- Other Join conditions and where clause conditionsYou do not know the CASE is an expression and fields are not columns. This is fundamental and probably means that a lot of your code needs work.
You can probably re-write the query with something like this:
SELECT A.col1, A.col2, B.col1, B.col2, B.col3
FROM Table_1 AS A, Table_2 AS B, Table_3 AS C
WHERE A.generic_id = B.generic_id
AND C.foobar_id = A.foobar_id
AND CASE
WHEN B.col3 <> 5
AND (B.something_out_date
<= DATEADD(DAY, -30, C.something_insertion_date)
THEN 'T'
WHEN B.col3 = 5
AND (B.something_out_date
>= DATEADD(DAY, -30, C.something_insertion_date)
THEN 'T'
ELSE 'F' END = 'T' ;
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
Regarding case in ORDER BY clause
Hi ALL,
I am having scenario that a flag value is passed ,if the falg is 'Y' ,i need to do order by with 2columns ,if the flag is 'N' no need do order by
i tried and i am able use only single column in the case statement.
Regardsselect last_name,emp_no,case when sal>1000 then 'Y' else 'N' end flag
from emp
order by
case flag
when 'Y' then 1
else 2
end;
Edited by: Mahanam on Dec 20, 2010 1:13 AM
Maybe you are looking for
-
Error in license check - Error getting the hardware key
Hello, I'm trying to run SAP NetWeaver 7.0 - Java and ABAP Trial Version on Linux - VMware Edition but I still have a issue to log on, message : "error in license check" I try everything I found in forums, I have tested the license, see the trace bel
-
Net Value in Sales order is not picking up for manual conditions.
Dear Gurus, I created a new pricing procedure for a sales document, where 2 manual conditions will given in sales order. But after giving manual conditions, the system is only taking one condition value as net value. I given Base value description an
-
How to increase the performance of Weblogic server 7.0?
How to increase the performance of Weblogic server 7.0 ? also, how do i avoid typing the server login and password evertime I start the webserver?
-
everytime I try and use the convert function within adobe, that I purchased, it gives me an error message, "an error occured while signing in". I tried changing my password and it still gives me that error. Can you help
-
Photoshop Elements 11 filter plug-ins
Hi, I've seen in PSE documentation that it supports filter plug-ins (see http://helpx.adobe.com/pdf/photoshop-elements_reference.pdf page 57) but I can't find the documentation related to plug-in development anywhere. It's not part of Photoshop SDK,