Case sensitve in selection statement
Hi All
I like to write a select statement which is able to get the values irrespective of case sesitive
ie
select * from table_name where column_name like('%sun%')
selection of columns should happen even the column name is '%SUN%' or '%Sun%' ie '%sun%' should not be case sensitive....Can anyone suggest me query for this
Note: the query needs to work in oracel server. Also changing the session ie alter session set NLS_SORT=BINARY_CI ;
alter session set NLS_COMP=LINGUISTIC; cannot be done.
So please help me to get the selection purely through query
With thanks and regards
Anoop
Use LOWER or UPPER,
so, for example:
lower(column_name) like '%sun%'
-- OR
upper(column_name) like '%SUN%'
Similar Messages
-
Case or decode in select statement for comparison
Hi,
How can I do a comparison
like
if sal < 50, output 1.1 * comm
if sal > 50 and < = 100 output 1.2 * comm
else output comm
in a single select statement.
I tried using case and decode but I am not sure how to use it in comparison operations.
Please helpuse the 'case' construct:
SELECT
NORMAL_FIELD
, (CASE
WHEN (SAL < 50) THEN 1.1 * COMM
WHEN (SAL > 50) AND (SAL <= 100) THEN 1.2 * COMM
ELSE COMM
END
) AS CALCULATED_COMM
FROM
TB_xxxx
WHERE xxxx
hope this helps -
Use of Case in Select statement
Hi All,
Can anyone tell me whats wrong im doing in the following select statement. I am selecting most this fields from Global Temperory Table and few from normal DB tables.
select h.Taskid,h.Flowid,fd.DESCRIPTION Flowname,h.Stepid, sd.DESCRIPTION Stepname,h.Lockby,ud.USERNAME,h.Status,case Date_initiated when (Currval.Testid = Nextval.Testid) Then Date_initiated := NULL Else Date_initiated := Date_initiated End case,case UserTime When ((Currval-1).Testid IS NULL or Currval.Testid <> (Currval-1).Testid) Then UserTime := StartTime When (Currval.Testid = (Currval-1).Testid or Currval.Testid <> Nextval.Testid or Nextval.Testid IS NULL) Then UserTime = (Currval-1).Endtime End case,case StartTime When(Currval.Stepid = Nextval.Stepid or Currval.Stepid=9999) Then StartTime := NULL Else StartTime := StartTime End case,h.EndTime,h.LockTime,h.Drawer,h.FileNo,h.DocType from GlobalTempTest h inner join USERDEFINITION ud on h.Lockby=ud.USERID inner join FLOWDEFINITION fd on h.Flowid=fd.FLOWID inner join STEPDEFINITION sd on (h.Stepid=sd.STEPID and h.Flowid=sd.FLOWID)
Hope so get some response soon.
Thanks in AdvanceYou have to rewrite your code like this:
CASE WHEN (Currval.Testid = Nextval.Testid) THEN NULL
ELSE Date_initiated
END New_Date_initiatedCurrval/Nextval as shown in your example will not work. Please explain what you mean by previous value comparison.
C. -
Case sensitive statement in the select-statement
Hi All,
i have a table in the abap-dictionary filled with names...when i try to select them with the select-statement with condition:
table-name_column like 'some_name'
I have encountered some problems...the inquiry is case-sensitive. What i want to do is to read the value from the abap-dictionary table uppercase or lowercase and compare it with the needed value also translated in uppercase or lowercase.
The only idea i have is to select all values of the dictionary table into an internal table and to translate the values there in uppercase or lowercase and then to loop trough it. But this approach would cost a lot of performance.
Do someone has an other proposal?check...
Select statement
It would be difficult , because it is based on how data is stored in the data base , now consider the scenario of the system i am using , we can material description as 'test' or 'TEST' , 'Test' .
If in your system there are only stored in either caps or small the you can perform the select twice .
But i myself dont find it to be such a good solution , but you can give it a try
There is one more solution specific to material description ,and that is in the table MAKT there is a field MAKTG , which stored the description in uppercase , so for this you can first convert the description to uppercase and then perform select on this field -
Hello,
in Oracle8 I've the following problem:
The statement
SELECT ''' | ''||' || 'rpad'
|| '(''' || column_name
|| ''',' ||
(CASE WHEN (data_type = 'VARCHAR2' AND
data_length > 50) THEN
to_number('50')
WHEN (data_type = 'CHAR' AND
data_length > 50) THEN
to_number('50')
WHEN (data_type = 'CLOB' AND
data_length > 50) THEN
to_number('50')
WHEN (data_type = 'DATE' AND
data_length != 19) THEN
to_number('19')
ELSE
data_length
END )
|| ', '' '')||' AS column_name
FROM dba_tab_columns
WHERE
table_name = upper('DBA_TABLES') AND
owner = upper('SYS')
ORDER BY column_id;
works fine but defining the same statement as a cursor in a PL/SQL Procedure, I got a
Error: PLS-00103: Encountered the symbol "CASE" when expecting one of the following:
( - + mod not null others <an identifier>
<a double-quoted delimited-identifier> <a bind variable> avg
count current exists max min prior sql stddev sum variance
execute forall time timestamp interval date
<a string literal with character set specification>
<a number> <a single-quoted SQL string>
The same procedure can be compiled in Oracle9i without errors.
Does anybody know if there are any restrictions using CASE statements in a PL/SQL CURSOR in Oracle8?
Thanks
PatricHi,
I am encountering the same problem.
Can u suggest the decode statement used instead of case
for this select clause which uses the CASE statement ?
SELECT
CASE
WHEN S.cmpy_num = S.cpty_borg_num THEN
''OURS''
ELSE
''THEIRS''
END AS SDIOwner,
From TABLE -
Case in Select Statement - Ramya
Hi All,
Could any body help me out ,how to use the CASE in select Statement
Thanks in Advancehere is an example:
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7566 JONES MANAGER 7839 02-APR-81 2975 1000 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7934 MILLER CLERK 7782 23-JAN-82 1300 10
14 rows selected.
SQL> select empno,
2 ename,
3 job,
4 case when job = 'PRESIDENT' then 1
5 when job = 'MANAGER' then 2
6 when job = 'SUPERVISOR' then 3
7 else 4
8 end rank
9 from emp;
EMPNO ENAME JOB RANK
7566 JONES MANAGER 2
7902 FORD ANALYST 4
7839 KING PRESIDENT 1
7698 BLAKE MANAGER 2
7782 CLARK MANAGER 2
7369 SMITH CLERK 4
7499 ALLEN SALESMAN 4
7521 WARD SALESMAN 4
7654 MARTIN SALESMAN 4
7788 SCOTT ANALYST 4
7844 TURNER SALESMAN 4
7876 ADAMS CLERK 4
7900 JAMES CLERK 4
7934 MILLER CLERK 4
14 rows selected.
SQL> -
If statement in select statement alias
I have the following select statement. It has the alias Survivors, Deaths and "All Cases". Is it posible to use :P_LANGUAGE variable to say that -- IF :P_LANGUAGE = FRENCH THEN alias are Survivants for survivors, Décès for Deaths, Tous_les_cas for All Cases. Please advise
SELECT ALL T_NTR_MULTIBAR.CAT, T_NTR_MULTIBAR.NUM_CASES_LEFTBAR AS Survivors,
T_NTR_MULTIBAR.NUM_CASES_MIDDLEBAR AS Deaths, T_NTR_MULTIBAR.NUM_CASES_RIGHTBAR AS "All Cases"
FROM T_NTR_MULTIBAR
WHERE INSTANCE_NUM = :P_INSTANCENUM
order by ORDERSYou may not be able to add this condition inside the SQL Statement. But you can add this condition outside the statement, if you're using PL/SQL...
IF :p_language = french THEN
SELECT ALL t_ntr_multibar.cat,
t_ntr_multibar.num_cases_leftbar AS survivors,
t_ntr_multibar.num_cases_middlebar AS deaths,
t_ntr_multibar.num_cases_rightbar AS "All Cases"
ELSE
END IF; -
How to get all values from an interval using select statement
Hi,
Is it possible to write a select statement that returns all values from an interval? Interval boundaries are variable.
something like this:
select (for x in 1,1024 loop x end loop) from dual
(this, of course, doesn't work)
The result in this example should be 1024 rows of numbers from 1 to 1024. These numbers are parameters, so it is not possible to create a table with predefined values.
Thanks in advance for your help,
Mia.For your simple case, with a lower boundary of 1, you can use:
SELECT rownum
FROM all_objects
WHERE rownum <= 1024For a set of number between say 50 - 100, you can use something like:
SELECT rownum + (50 - 1)
FROM all_objects
WHERE rownum <= (100 - 50 + 1)Note, that all_objects was used only because it generally has a lot of rows. Any table with at least the number of rows in your range will work.
TTFN
John -
Using if logic in the where clause of a select statement
I have a select clause. And in the select clause there is a variable all_off_trt that can be 'Y' or 'N'.
In the where clause I want to make it so that if a form variable is checked and all_off_trt is 'Y' then
exclude it else if the form variable isn't checked then select it no matter what all_off_trt is.
Is there any way to include either and if statement or a case statement within the where clause to acheive this? If not is there another way of doing it?
Basically I am looking for a case statement like this
case
when all_off_trt = 'Y' and mail_para.code = 'Y' then false
else true
end
Message was edited by:
Tugnutt7Ok, so that really doesn't solve my problem. I have 3 different fields that I need to do that with. Each combining in a select statement to print an email list, as well as other thing limiting the where clause.
This is currently what I have, tested and working 100%.
cursor email_cur is
select unique p.email,s.all_off_trt,s.all_deceased,s.no_enroll
from participant p, trialcom t, ethics s
where p.status='A'
and p.surname=t.surname
and p.initials=t.initials
and s.trial_cd = t.tricom
and s.centre = t.centre
and p.email is not null
and (t.centre in (select code from mail_parameters where user_name=user and mail_para='CENTRE')
or 'XX' in (select code from mail_parameters where user_name=user and mail_para='CENTRE'))
and (t.tricom in (select code from mail_parameters where user_name=user and mail_para='TRIAL')
or 'XX' in (select code from mail_parameters where user_name=user and mail_para='TRIAL'))
and (t.role in (select code from mail_parameters where user_name=user and mail_para='ROLE')
or 'XX' in (select code from mail_parameters where user_name=user and mail_para='ROLE'))
and (p.country in (select code from mail_parameters where user_name=user and mail_para='COUNTRY')
or 'XX' in (select code from mail_parameters where user_name=user and mail_para='COUNTRY'))
and (t.represent in (select code from mail_parameters where user_name=user and mail_para='REPRESENT')
or 'XX' in (select code from mail_parameters where user_name=user and mail_para='REPRESENT'));
This is in a program unit that runs when a button is clicked. At the end of that I need to add on the 3 case statements that help further narrow down the selection of emails to be printed. Then it prints the emails selected from this statement into a file. So it has to be done right in the select statement. The three table variables are the all_off_trt, all_deceased, and no_enroll. The form has 3 checkboxes. One for each, that when checked (giving the variable associated with the checkboxes a value of 'Y') excludes all emails that have a 'Y' in the coresponding table variable. -
Select statement in a function does Full Table Scan
All,
I have been coding a stored procedure that writes 38K rows in less than a minute. If I add another column which requires call to a package and 4 functions within that package, it runs for about 4 hours. I have confirmed that due to problems in one of the functions, the code does full table scans. The package and all of its functions were written by other contractors who have been long gone.
Please note that case_number_in (VARCHAR2) and effective_date_in (DATE) are parameters sent to the problem function and I have verified through TOAD’s debugger that their values are correct.
Table named ps2_benefit_register has over 40 million rows but case_number is an index for that table.
Table named ps1_case_fs has more than 20 million rows but also uses case_number as an index.
Select #1 – causes full table scan runs and writes 38K rows in a couple of hours.
{case}
SELECT max(a2.application_date)
INTO l_app_date
FROM dwfssd.ps2_benefit_register a1, dwfssd.ps2_case_fs a2
WHERE a2.case_number = case_number_in and
a1.case_number = a2.case_number and
a2.application_date <= effective_date_in and
a1.DOCUMENT_TYPE = 'F';
{case}
Select #2 – runs – hard coding values makes the code to write the same 38K rows in a few minutes.
{case}
SELECT max(a2.application_date)
INTO l_app_date
FROM dwfssd.ps2_benefit_register a1, dwfssd.ps2_case_fs a2
WHERE a2.case_number = 'A006438' and
a1.case_number = a2.case_number and
a2.application_date <= '01-Apr-2009' and
a1.DOCUMENT_TYPE = 'F';
{case}
Why using the values in the passed parameter in the first select statement causes full table scan?
Thank you for your help,
Seyed
Edited by: user11117178 on Jul 30, 2009 6:22 AM
Edited by: user11117178 on Jul 30, 2009 6:23 AM
Edited by: user11117178 on Jul 30, 2009 6:24 AMHello Dan,
Thank you for your input. The function is not determinsitic, therefore, I am providing you with the explain plan. By version number, if you are refering to the Database version, we are running 10g.
PLAN_TABLE_OUTPUT
Plan hash value: 2132048964
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
| 0 | SELECT STATEMENT | | 324K| 33M| 3138 (5)| 00:00:38 | | |
|* 1 | HASH JOIN | | 324K| 33M| 3138 (5)| 00:00:38 | | |
| 2 | BITMAP CONVERSION TO ROWIDS | | 3 | 9 | 1 (0)| 00:00:01 | | |
|* 3 | BITMAP INDEX FAST FULL SCAN| IDX_PS2_ACTION_TYPES | | | | | | |
| 4 | PARTITION RANGE ITERATOR | | 866K| 87M| 3121 (4)| 00:00:38 | 154 | 158 |
| 5 | TABLE ACCESS FULL | PS2_FS_TRANSACTION_FACT | 866K| 87M| 3121 (4)| 00:00:38 | 154 | 158 |
Predicate Information (identified by operation id):
1 - access("AL1"."ACTION_TYPE_ID"="AL2"."ACTION_TYPE_ID")
3 - filter("AL2"."ACTION_TYPE"='1' OR "AL2"."ACTION_TYPE"='2' OR "AL2"."ACTION_TYPE"='S')
Thank you very much,
Seyed -
Using Select statement in IF condition?
hi all,
Can i use select statement in IF COndition in pl sql ?
eg like- if( select 1 from ASD) then
end if;There is no way to do any kind of select statement inside if conditions.
Why don't test simple cases like this first?
An example to show it.
SQL> begin
2 if exists (select 1 from dual) then
3 dbms_output.put_line('ok');
4 end if;
5 end;
6 /
if exists (select 1 from dual) then
ERRORE alla riga 2:
ORA-06550: line 2, column 5:
PLS-00204: function or pseudo-column 'EXISTS' may be used inside a SQL
statement only
ORA-06550: line 2, column 2:
PL/SQL: Statement ignored
SQL> begin
2 if ( (select count(*) from dual) > 0 ) then
3 dbms_output.put_line('ok');
4 end if;
5 end;
6 /
if ( (select count(*) from dual) > 0 ) then
ERRORE alla riga 2:
ORA-06550: line 2, column 8:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:
( - + case mod new not null others <an identifier>
<a double-quoted delimited-identifier> <a bind variable> avg
count current exists max min prior sql stddev sum variance
execute forall merge time timestamp interval date
<a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
<an alternatively-quoted string literal with character set specification>
<an alternativ
ORA-06550: line 2, column 33:
PLS-00103: Encountered the symbol ")" when expecting one of the following:
. , @ ; for <an identifier>
<a double-quoted delimited-identifier> group having intersect
minus order partition start subpartition union where connect
SQL> begin
2 if ( 0 in (select count(*) from dual) ) then
3 dbms_output.put_line('ok');
4 end if;
5 end;
6 /
if ( 0 in (select count(*) from dual) ) then
ERRORE alla riga 2:
ORA-06550: line 2, column 12:
PLS-00405: subquery not allowed in this context
ORA-06550: line 2, column 2:
PL/SQL: Statement ignoredBye Alessandro -
Values from a Multi-Select in the where clause of a Select statement
I have a web page that solicits query parameters from the user.
The selections that the user makes will populate the WHERE clause of a Select statement.
One of the controls on the page is a multi-select control.
When this page posts, I would like to execute a Select statement wherein the selected values from this control appear in the .. Column IN ( <list here> ) portion of the WHERE clause.
This is an extremely common scenario, but I cannot seem to locate a how-to or message thread that addresses this specific case.
I have an idea that it may involve dynamic SQL or Execute Immediate, but cannot seem to pin down the answer.
Any help would be greatly appreciated!anonymous - As illustrated here: Re: Search on a typed in list of values
Scott -
Return multiple values from a function to a SELECT statement
I hope I've provided enough information here. If not, just let me know what I'm missing.
I am creating a view that will combine information from a few tables. Most of it is fairly straightforward, but there are a couple of columns in the view that I need to get by running a function within a package. Even this is fairly straightforward (I have a function named action_date in a package called rp, for instance, which I can use to return the date I need via SELECT rp.action_date(sequence_number).
Here's the issue: I actually need to return several bits of information from the same record (not just action_date, but also action_office, action_value, etc.) - a join of the tables won't work here as I'll explain below. I can, of course, run a separate function for each statement but that is obviously inefficient. Within the confines of the view select statement however, I'm not sure how to return each of the values I need.
For instance, right now, I have:
Table1:
sequence_number NUMBER(10),
name VARCHAR(30),
Table2:
Table1_seq NUMBER(10),
action_seq NUMBER(10),
action_date DATE,
action_office VARCHAR(3),
action_value VARCHAR(60),
I can't simply join Table1 and Table2 because I have to do some processing in order to determine which of the matching returned rows I actually need to select. So the package opens a cursor and processes each row until it finds the one that I need.
The following works but is inefficient since all of the calls to the package will return columns from the same record. I just don't know how to return all the values I need into the SELECT statement.
CREATE VIEW all_this_stuff AS
SELECT sequence_number, name,
rp.action_date(sequence_number) action_date,
rp.action_office(sequence_number) action_office,
rp.action_value(sequence_number) action_value
FROM table1
Is there a way to return multiple values into my SELECT statement or am I going about this all wrong?
Any suggestions?
Thanks so much!Hi,
What you want is a Top-N Query , which you can do using the analytic ROW_NUMBER function in a sub-query, like this:
WITH got_rnum AS
SELECT action_seq, action_dt, action_office, action_type, action_value
, ROW_NUMBER () OVER ( ORDER BY action_date
, action_seq
, action_serial
) AS rnum
FROM table2
WHERE action_code = 'AB'
AND action_office LIKE 'E' -- Is this right?
SELECT action_seq, action_dt, action_office, action_type, action_value
FROM got_rnum
WHERE rnum = 1
;As written, this will return (at most) one row.
I suspect you'll really want to get one row for each group , where a group is defined by some value in a table to which you're joining.
In that case, add a PARTITION BY clause to the ROW_NUMBER function.
If you'd post a little sample data (CREATE TABLE and INSERT statements), I could show you exactly how.
Since I don't have your tables, I'll show you using tables in the scott schema.
Here's a view that has data from the scott.dept table and also from scott.emp, but only for the most senior employee in each department (that is, the employee with the earliest hiredate). If there happens to be a tie for the earliest hiredate, then the contender with the lowest empno is chosen.
CREATE OR REPLACE VIEW senior_emp
AS
WITH got_rnum AS
SELECT d.deptno
, d.dname
, e.empno
, e.ename
, e.hiredate
, ROW_NUMBER () OVER ( PARTITION BY d.deptno
ORDER BY e.hiredate
, e.empno
) AS rnum
FROM scott.dept d
JOIN scott.emp e ON d.deptno = e.deptno
SELECT deptno
, dname
, empno
, ename
, hiredate
FROM got_rnum
WHERE rnum = 1
SELECT *
FROM senior_emp
;Output:
. DEPTNO DNAME EMPNO ENAME HIREDATE
10 ACCOUNTING 7782 CLARK 09-JUN-81
20 RESEARCH 7369 SMITH 17-DEC-80
30 SALES 7499 ALLEN 20-FEB-81
By the way, one of the conditions in the query you posted was
action_office LIKE 'E'which is equivalent to
action_office = 'E'(LIKE is always equivalent to = if the string after LIKE doesn't contain any wildcards.)
Did you mean to say that, or did you mean something like this:
action_office LIKE 'E%'instead? -
Issue with Past Month data in the Select Statement
Hi,
I written the following query,
SELECT /*+NO_MERGE(A)*/
CASE
WHEN snap_shot_date > a.q3
AND snap_shot_date <= a.q4
THEN q4char
WHEN snap_shot_date > a.q2 AND snap_shot_date <= a.q3
THEN q3char
WHEN snap_shot_date > a.q1 AND snap_shot_date <= a.q2
THEN q2char
WHEN snap_shot_date > a.q0 AND snap_shot_date <= a.q1
THEN q1char
END snap_shot_date,
CASE
WHEN snap_shot_date > a.q3 AND snap_shot_date <= a.q4
THEN 1
WHEN snap_shot_date > a.q2 AND snap_shot_date <= a.q3
THEN 2
WHEN snap_shot_date > a.q1 AND snap_shot_date <= a.q2
THEN 3
WHEN snap_shot_date > a.q0 AND snap_shot_date <= a.q1
THEN 4
END sort_by,
pillar3_exposure_class_code, pd_band_description,
ROUND (SUM (p3.ead_post_sec_post_crm)),
ROUND (SUM (notional_principle)),
DECODE (SUM (notional_principle),
0, 0,
ROUND ( SUM (DECODE (exposure_type,
'UNDRAW', ead_post_sec_post_crm,
0
/ SUM (notional_principle),
4
DECODE (SUM (p3.ead_post_sec_post_crm),
0, 0,
ROUND ( SUM (pd_value * p3.ead_post_sec_post_crm)
/ SUM (p3.ead_post_sec_post_crm),
2
DECODE (SUM (p3.ead_post_sec_post_crm),
0, 0,
ROUND (SUM (rwa) / SUM (p3.ead_post_sec_post_crm), 4)
DECODE (SUM (p3.ead_post_sec_post_crm),
0, 0,
ROUND ( SUM (lgd_rate * p3.ead_post_sec_post_crm)
/ SUM (p3.ead_post_sec_post_crm),
2
TO_CHAR (MAX (a.max_date), 'FMMonth DD, YYYY')
FROM summary.pillar3 p3,
(SELECT DISTINCT (month_end_date) max_date,
LAST_DAY (month_end_date) q4,
TO_CHAR (LAST_DAY (month_end_date),
'MON YYYY'
) q4char,
ADD_MONTHS (LAST_DAY (month_end_date), -3) q3,
TO_CHAR
(ADD_MONTHS (LAST_DAY (month_end_date), -3),
'MON YYYY'
) q3char,
ADD_MONTHS (LAST_DAY (month_end_date), -6) q2,
TO_CHAR
(ADD_MONTHS (LAST_DAY (month_end_date), -6),
'MON YYYY'
) q2char,
ADD_MONTHS (LAST_DAY (month_end_date), -9) q1,
TO_CHAR
(ADD_MONTHS (LAST_DAY (month_end_date), -9),
'MON YYYY'
) q1char,
ADD_MONTHS (LAST_DAY (month_end_date), -12) q0
FROM rcdwstg.stg_bcar_detail) a
WHERE snap_shot_date BETWEEN ADD_MONTHS (a.max_date, -12) AND a.max_date
GROUP BY CASE
WHEN snap_shot_date > a.q3 AND snap_shot_date <= a.q4
THEN q4char
WHEN snap_shot_date > a.q2 AND snap_shot_date <= a.q3
THEN q3char
WHEN snap_shot_date > a.q1 AND snap_shot_date <= a.q2
THEN q2char
WHEN snap_shot_date > a.q0 AND snap_shot_date <= a.q1
THEN q1char
END,
CASE
WHEN snap_shot_date > a.q3 AND snap_shot_date <= a.q4
THEN 1
WHEN snap_shot_date > a.q2 AND snap_shot_date <= a.q3
THEN 2
WHEN snap_shot_date > a.q1 AND snap_shot_date <= a.q2
THEN 3
WHEN snap_shot_date > a.q0 AND snap_shot_date <= a.q1
THEN 4
END,
pillar3_exposure_class_code,
pd_band_description
ORDER BY 2 DESC;I have written the query to get the latest 12 months data from a table and split that into 4 quarter to show in the Cognos Report. But when the table having 13th or the past months data, the select statement is showing Empty values in the first two columns and fetching the 13 month data too.
Can anyone help me in this to avoid the problem.
Thanks
Radha KWHERE snap_shot_date BETWEEN ADD_MONTHS(TRUNC(a.max_date, 'MM'), -11) AND a.max_date
.... -
How to add a dummy row in the result set of a SELECT statement.
Hello Everyone -
I have requirment to add a dummy row in the result set of a SELECT statement.
For e.g. lets say there is a table Payment having following colums:
Payment_id number
status varchar2(10)
amount number
payment_date date
so here is the data :-
Payment_id Status Amount payment_date
1 Applied 100 12/07/2008
2 Reversed 200 01/ 06/2009
3 Applied 300 01/ 07/2009
Here is my SQL
Select * form payment where payment_date >= 01/01/2009
Output will be
2 Reversed 200 01/ 06/2009
3 Applied 300 01/ 07/2009
My desired output is below
2 Reversed 200 01/ 06/2009
3 Applied 300 01/ 07/2009
2 Reversed -200 01/ 06/2009 ------(Dummy Row)
Thrid row here is the dummy row which I want to add when status is "Reversed"
I would be very thankful for any kind of help in this regard ...
Thanks,
GauravCartesion joining against a dummy table is a useful method of creating a dummy row:
with my_tab as (select 1 cust_id, 1 Payment_id, 'Applied' Status, 100 Amount, to_date('12/07/2008', 'mm/dd/yyyy') payment_date from dual union all
select 1 cust_id, 2 Payment_id, 'Reversed' Status, 200 Amount, to_date('01/06/2009', 'mm/dd/yyyy') payment_date from dual union all
select 1 cust_id, 3 Payment_id, 'Applied' Status, 300 Amount, to_date('01/06/2009', 'mm/dd/yyyy') payment_date from dual union all
select 2 cust_id, 1 Payment_id, 'Applied' Status, 100 Amount, to_date('12/07/2008', 'mm/dd/yyyy') payment_date from dual union all
select 2 cust_id, 2 Payment_id, 'Reversed' Status, 200 Amount, to_date('01/05/2009', 'mm/dd/yyyy') payment_date from dual union all
select 2 cust_id, 3 Payment_id, 'Applied' Status, 300 Amount, to_date('01/06/2009', 'mm/dd/yyyy') payment_date from dual union all
select 2 cust_id, 4 Payment_id, 'Reversed' Status, -400 Amount, to_date('01/06/2009', 'mm/dd/yyyy') payment_date from dual union all
select 2 cust_id, 5 Payment_id, 'Applied' Status, 500 Amount, to_date('01/07/2009', 'mm/dd/yyyy') payment_date from dual),
--- end of mimicking your table
dummy as (select 'Reversed' col1, 1 rn from dual union all
select 'Reversed' col1, 2 rn from dual)
select mt.cust_id,
mt.payment_id,
mt.status,
decode(dummy.rn, 2, -1*mt.amount, mt.amount) amount,
mt.payment_date
from my_tab mt,
dummy
where mt.status = dummy.col1 (+)
order by mt.cust_id, mt.payment_id, dummy.rn nulls first;
CUST_ID PAYMENT_ID STATUS AMOUNT PAYMENT_DATE
1 1 Applied 100 07/12/2008
1 2 Reversed 200 06/01/2009
1 2 Reversed -200 06/01/2009
1 3 Applied 300 06/01/2009
2 1 Applied 100 07/12/2008
2 2 Reversed 200 05/01/2009
2 2 Reversed -200 05/01/2009
2 3 Applied 300 06/01/2009
2 4 Reversed -400 06/01/2009
2 4 Reversed 400 06/01/2009
2 5 Applied 500 07/01/2009Edited by: Boneist on 07-Jan-2009 23:10
NB. You may have to mess around with the ordering if that's not come back in the order you wanted. You didn't mention what the rules were for any expected ordering though, so I've made up my own *{;-)
Also, I added an identifier (cust_id) to differentiate between different sets of payments, since that's usually the case. Remove that if it's not applicable for your case.
Maybe you are looking for
-
I'm unable to access my music. My ipod keeps asking me for the network and password. I have my network but can't figure out the password. Help!
-
In BP master, payment terms
Hi Experts, In BP master, payment terms have credit limit and commitment limit, I want to know how it works. Anyone guide me. Thanks and regards, Manikandan
-
Spotlight Content indexing Problem
Spotlight will not index content. It does index the names of files, but when I do a content search, it merely performs the names search. I tried the privacy/index command but that does not work. Anyone else having these problems. BTW, this started af
-
My macbookpro that I dual boot with windows 7 has stop working
I dual boot my macbook pro with windows 7 but now it has stop working. how can I get the mac os and windows 7 both working again?
-
I made a 2 hour movie in iMovie and burned it to a 4.7Gig Verbatum DVD-R. I wanted to save it as an image file in case i needed to make more copies later but thought I'd try it first before deleting the project. It seemed to go fine but the file size