How to write the query using Index
Hi All,
I have to fetch the records from Database table using Index, how can i write the query using Index. Can any body plz send me the sample code.
Help Me,
Balu.
Hi,
See the below Example.
select * from vbak up to 100 rows
into table t_vbak
where vbeln > v_vbeln.
sort t_vbak by vbeln descending.
read table t_vbak index 1.
Regards,
Ram
Pls reward points if helpful.
Similar Messages
-
How to write the query for year revenue and avg
Hello Team
I have the table
create table sale (sale_id varchar2(32) not null, sale_epoch_microsec numeric(18,0)
not null, sale_amount_cents numeric(18,0) not null, customer_id varchar2(32)
not null, industry_id varchar2(32) not null, product_id varchar2(32) not null)The industry the customer is considered to be in can change from sale to sale. sale_epoch_microsec is microseconds since epoch; sale_amount_cents is the value of the sale in pennies.
The data in this is:
Insert into SALE
(SALE_ID, SALE_EPOCH_MICROSEC, SALE_AMOUNT_CENTS, CUSTOMER_ID, INDUSTRY_ID,
PRODUCT_ID)
Values
('s1', 200000000, 69985484589459, 'c1', 'i1',
'p1');
Insert into SALE
(SALE_ID, SALE_EPOCH_MICROSEC, SALE_AMOUNT_CENTS, CUSTOMER_ID, INDUSTRY_ID,
PRODUCT_ID)
Values
('s2', 200000000, 69985484589459, 'c2', 'i2',
'p2');
Insert into SALE
(SALE_ID, SALE_EPOCH_MICROSEC, SALE_AMOUNT_CENTS, CUSTOMER_ID, INDUSTRY_ID,
PRODUCT_ID)
Values
('s3', 6579000000, 6.99675342390895E16, 'c3', 'i3',
'p3');
Insert into SALE
(SALE_ID, SALE_EPOCH_MICROSEC, SALE_AMOUNT_CENTS, CUSTOMER_ID, INDUSTRY_ID,
PRODUCT_ID)
Values
('s4', 6.5866459684979E17, 6.99675343454391E17, 'c4', 'i4',
'p4');
1.) SQL: Write the SQL query to determine, year-over-year, the total revenue and avg. per-product revenue from customers in the 'Gaming' industry.josh1612 wrote:
As in the question it's mentioned that the date is in microseconds,
So i need the guidence to convert that to the date .Convert microseconds to seconds and then you may be able to use the function below which was found here:
http://www.oracle.com/technology/pub/articles/oracle_php_cookbook/fuecks_dates.html
CREATE OR REPLACE
FUNCTION unixts_to_date(unixts IN PLS_INTEGER) RETURN DATE IS
* Converts a UNIX timestamp into an Oracle DATE
unix_epoch DATE := TO_DATE('19700101000000','YYYYMMDDHH24MISS');
max_ts PLS_INTEGER := 2145916799; -- 2938-12-31 23:59:59
min_ts PLS_INTEGER := -2114380800; -- 1903-01-01 00:00:00
oracle_date DATE;
BEGIN
IF unixts > max_ts THEN
RAISE_APPLICATION_ERROR(
-20901,
'UNIX timestamp too large for 32 bit limit'
ELSIF unixts < min_ts THEN
RAISE_APPLICATION_ERROR(
-20901,
'UNIX timestamp too small for 32 bit limit' );
ELSE
oracle_date := unix_epoch + NUMTODSINTERVAL(unixts, 'SECOND');
END IF;
RETURN (oracle_date);
END; -
How can rewrite the Query using Analytical functions ?
Hi,
I have the SQL script as shown below ,
SELECT cd.cardid, cd.cardno,TT.TRANSACTIONTYPECODE,TT.TRANSACTIONTYPEDESC DESCRIPTION,
SUM (NVL (CASE tt.transactiontypecode
WHEN 'LOAD_ACH'
THEN th.transactionamount
END, 0)
) AS load_ach,
SUM
(NVL (CASE tt.transactiontypecode
WHEN 'FUND_TRANSFER_RECEIVED'
THEN th.transactionamount
END,
0
) AS Transfersin,
( SUM (NVL (CASE tt.transactiontypecode
WHEN 'FTRNS'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'SEND_MONEY'
THEN th.transactionamount
END, 0)
)) AS Transferout,
SUM (NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_ACH'
THEN th.transactionamount
END, 0)
) AS withdrawal_ach,
SUM (NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_CHECK'
THEN th.transactionamount
END, 0)
) AS withdrawal_check,
( SUM (NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_CHECK_FEE'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'REJECTED_ACH_LOAD_FEE'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_ACH_REV'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_CHECK_REV'
THEN th.transactionamount
END,
0
) +
SUM
(NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_CHECK_FEE_REV'
THEN th.transactionamount
END,
0
) +
SUM
(NVL (CASE tt.transactiontypecode
WHEN 'REJECTED_ACH_LOAD_FEE_REV'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'OVERDRAFT_FEE_REV'
THEN th.transactionamount
END, 0)
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'STOP_CHECK_FEE_REV'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'LOAD_ACH_REV'
THEN th.transactionamount
END, 0)
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'OVERDRAFT_FEE'
THEN th.transactionamount
END, 0)
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'STOP_CHECK_FEE'
THEN th.transactionamount
END, 0)
)) AS Fee,
th.transactiondatetime
FROM carddetail cd,
transactionhistory th,
transactiontype tt,
(SELECT rmx_a.cardid, rmx_a.endingbalance prev_balance, rmx_a.NUMBEROFDAYS
FROM rmxactbalreport rmx_a,
(SELECT cardid, MAX (reportdate) reportdate
FROM rmxactbalreport
GROUP BY cardid) rmx_b
WHERE rmx_a.cardid = rmx_b.cardid AND rmx_a.reportdate = rmx_b.reportdate) a
WHERE th.transactiontypeid = tt.transactiontypeid
AND cd.cardid = th.cardid
AND cd.cardtype = 'P'
AND cd.cardid = a.cardid (+)
AND CD.CARDNO = '7116734387812758335'
--AND TT.TRANSACTIONTYPECODE = 'FUND_TRANSFER_RECEIVED'
GROUP BY cd.cardid, cd.cardno, numberofdays,th.transactiondatetime,tt.transactiontypecode,TT.TRANSACTIONTYPEDESC
Ouput of the above query is :
CARDID CARDNO TRANSACTIONTYPECODE DESCRIPTION LOAD_ACH TRANSFERSIN TRANSFEROUT WITHDRAWAL_ACH WITHDRAWAL_CHECK FEE TRANSACTIONDATETIME
6005 7116734387812758335 FUND_TRANSFER_RECEIVED Fund Transfer Received 0 3.75 0 0 0 0 21/09/2007 11:15:38 AM
6005 7116734387812758335 FUND_TRANSFER_RECEIVED Fund Transfer Received 0 272 0 0 0 0 05/10/2007 9:12:37 AM
6005 7116734387812758335 WITHDRAWAL_ACH Withdraw Funds via ACH 0 0 0 300 0 0 24/10/2007 3:43:54 PM
6005 7116734387812758335 SEND_MONEY Fund Transfer Sent 0 0 1 0 0 0 19/09/2007 1:17:48 PM
6005 7116734387812758335 FUND_TRANSFER_RECEIVED Fund Transfer Received 0 1 0 0 0 0 18/09/2007 7:25:23 PM
6005 7116734387812758335 LOAD_ACH Prepaid Deposit via ACH 300 0 0 0 0 0 02/10/2007 3:00:00 AM
I want the output like for Load_ACH there should be one record etc.,
Can any one help me , how can i rewrite the above query using analytical functions .,
SekharNot sure of your requirements but this mayhelp reduce your code;
<untested>
SUM (
CASE
WHEN tt.transactiontypecode IN
('WITHDRAWAL_CHECK_FEE', 'REJECTED_ACH_LOAD_FEE', 'WITHDRAWAL_ACH_REV', 'WITHDRAWAL_CHECK_REV',
'WITHDRAWAL_CHECK_FEE_REV', 'REJECTED_ACH_LOAD_FEE_REV', 'OVERDRAFT_FEE_REV','STOP_CHECK_FEE_REV',
'LOAD_ACH_REV', 'OVERDRAFT_FEE', 'STOP_CHECK_FEE')
THEN th.transactionamount
ELSE 0) feeAlso, you might want to edit your post and use [pre] and [/pre] tags around your code for formatting. -
How to write the query to find the duplicate rows?
Hi,
How will I find the duplicate rows in the table?
I have a table called ITEM
Select item_no,mfg_id from item
Item Mfg_ID
178 06278
178 06589
238 5489
289 7895
289 7596
Now I want to find only the duplicate rows i.e.,178 & 289 is coming twice.
How will get the all duplicate rows in the table?nihar2 wrote:
Convert to
SELECT it.item,
im.mfg_item,
im.manufacturer,
im.description
FROM (SELECT it.item,
im.mfg_item,
im.manufacturer,
im.description,
count(it.item) over(partition by it.item order by it.item) cnt
FROM item it,item_mfg im
WHERE it.u##item=im.u##item)
WHERE cnt > 1
But I tried It show not valid identifier.You can't use the table aliases from the sub-query...
SELECT item,
mfg_item,
manufacturer,
description
FROM (SELECT it.item,
im.mfg_item,
im.manufacturer,
im.description,
count(it.item) over(partition by it.item order by it.item) cnt
FROM item it,item_mfg im
WHERE it.u##item=im.u##item)
WHERE cnt > 1 -
Please let me know how to write the Query to fetch data from tables
Hi Folks,
Please let me know how to get the data from different tables using the functionality SQ03,SQ02 and SQ01 .
Helpful answers will points awarded.
Regards,
Ram.Dear Ram,
Please find the below link which gives in detail with screen shots.
[SAP Query|http://media.techtarget.com/searchSAP/downloads/Teach_yourself_SAP_C20.pdf#search=%22CREATE%20REPORT%20USING%20SQVI%20%2C%20SAP%22]
Thanks
Murtuza -
How to Find the Query used by the Workbook ?
Hi ,
I am currently involved in trouble shooting a work book where some master data values are not being displayed.
How do i find, which Query the work books is using ? or Let me know as to how should i proceed in solving the same .Hi,
If you have access to BW Admin workbench... then choose the option metadatarepository(left side) of the BW admin workbench, search for the workbook(Technical name) and click on the "Network display dataflow" link.
If you want to know the relationship between workbook and the queries:
Check table RSRWORKBOOK which contains the WORKBOOKID and the GENUNIID.
Table RSRREPDIR contains GENUNIID, query name RNAME, AUTHOR, LASTUSER, INFOCUBE
Table RSRINDEXT contains the WORKBOOKID and workbook TITLE
You should join the tables by GENUNIID and select in RSRREPDIR the rows that have COMPTYPE='REP' for queries and object version active/modified.
With this information an infoset query can be created to list all workbook-query-user relationship (and even the query-infocube relationship).
Hope this helps u...
Regards,
KK. -
How to write the quires using into corresponding fields?
Hi,
How can i write the below quires using into corresponding fields statement.
DATA :ws_d_budat TYPE bkpf-budat,
ws_c_artnr TYPE ce10001-artnr,
1) SELECT SINGLE budat
INTO ws_d_budat
FROM bkpf
WHERE awtyp = 'VBRK'
AND awkey = wa_vbeln-vbeln.
2)SELECT vbeln posnr matnr
FROM vbrp
INTO TABLE i_matnr
FOR ALL ENTRIES IN i_vbeln
WHERE vbeln = i_vbeln-vbeln
AND matnr = ws_c_artnr
AND posnr NE ' '.
3) SELECT bukrs belnr gjahr budat awkey
INTO TABLE i_budat
FROM bkpf
FOR ALL ENTRIES IN i_awkey
WHERE awtyp = 'VBRK'
AND awkey = i_awkey-awkey.
Sudhakar1) SELECT SINGLE budat
INTO ws_d_budat
FROM bkpf
WHERE awtyp = 'VBRK'
AND awkey = wa_vbeln-vbeln. -
> <b>Not possible</b>
2)SELECT vbeln posnr matnr
FROM vbrp
INTO corresponding fields of TABLE i_matnr
FOR ALL ENTRIES IN i_vbeln
WHERE vbeln = i_vbeln-vbeln
AND matnr = ws_c_artnr
AND posnr NE ' '.
3) SELECT bukrs belnr gjahr budat awkey
INTO corresponding fields of TABLE i_budat
FROM bkpf
FOR ALL ENTRIES IN i_awkey
WHERE awtyp = 'VBRK'
AND awkey = i_awkey-awkey.
Reward points if helpful.
Regards.
Srikanta Gope -
How to write this query using correlation subquery or non exists clause
-- Tables description.
--step-1- 4 employees present in EMP table.
--step-2- each employee having 1 country_no in EMP_DOCS table.3 employees having same country_no(i.e emp's 1,2,3)
--step-3- 1 emp document can have multiple items.In this case each employee having one each in the EMP_ITEMS table.
--step-4- 1 EMPLOYEE can have Multiple Documents so we have a relation between EMP_ITEMS and DOCUMENT_ITEMS.whatever items present in EMP_ITEMS those items will be inserted into DOCUMENT_ITEMS.so we have a item-item relation.
--so we stored EMP_ITEMS id in EMP_ITEMS_REF_ID_1 of DOCUMENT_ITEMS table.
-- step-5- DOCUMENT-INVOICE has 1-1 relation once invoice is created we stored invoice id in DOCUMENT table.
--This is the requirement.Let's say in this example 3 employees are using same country_no and 4th employee is using another country_no
--which is not used by other 3 employees.
--Condtion-1:
--if all of the employees have created INVOICE which is using same country_no of different country_no then the query should display all records.
--Condition-2:
--if any one of the employee not created INVOICE which is using same country_no of other employees then remaining employees also should not come in the query
-- even though invoice is created by other employees.
--Condition-3:
--if any one of the employee not created even DOCUMENT which is using same country_no of other employees then remaining employees also should not come in the query
-- even though invoice is created by other employees.
I hope I explain the conditions clearly.if you understand well by looking at the data i posted below may i know what is the final result should be displayed?
create table EMP
ID NUMBER not null,
TYPE VARCHAR2(1)
alter table EMP
add constraint ID primary key (ID);
create table EMP_DOCS
ID NUMBER not null,
EMP_ID NUMBER,
COUNTRY_NO VARCHAR2(15)
alter table EMP_DOCS
add constraint PK_EMP_DT primary key (ID);
alter table EMP_DOCS
add constraint FK_EMP_DT foreign key (EMP_ID)
references EMP (ID);
create table EMP_ITEMS
ID NUMBER not null,
EMP_DOC_ID NUMBER
alter table EMP_ITEMS
add constraint PK_EMP_ITEMS_DT primary key (ID);
alter table EMP_ITEMS
add constraint FK_EMP_ITEMS_DT foreign key (EMP_DOC_ID)
references EMP_DOCS (ID);
create table DOCUMENT
ID NUMBER not null,
DOCNO VARCHAR2(15),
INVOICE_REF_1 NUMBER
alter table DOCUMENT
add constraint DOC_PK_ID primary key (ID);
create table DOCUMENT_ITEMS
ID NUMBER not null,
DOC_ID NUMBER,
EMP_ITEMS_REF_ID_1 NUMBER
alter table DOCUMENT_ITEMS
add constraint PK_DOCUMENT_ITEMS_DT primary key (ID);
alter table DOCUMENT_ITEMS
add constraint FK_DOCUMENT_ITEMS_DT foreign key (DOC_ID)
references DOCUMENT (ID);
create table INVOICE
ID NUMBER not null,
INVOICE_NO VARCHAR2(15)
alter table INVOICE
add constraint INVOICE_PK_ID primary key (ID);
INSERT INTO EMP ( ID, TYPE ) VALUES (
1, 'A');
INSERT INTO EMP ( ID, TYPE ) VALUES (
2, 'A');
INSERT INTO EMP ( ID, TYPE ) VALUES (
3, 'A');
INSERT INTO EMP ( ID, TYPE ) VALUES (
4, 'A');
INSERT INTO EMP_DOCS ( ID, EMP_ID, COUNTRY_NO ) VALUES (
1, 1, 'INDIA');
INSERT INTO EMP_DOCS ( ID, EMP_ID, COUNTRY_NO ) VALUES (
2, 2, 'INDIA');
INSERT INTO EMP_DOCS ( ID, EMP_ID, COUNTRY_NO ) VALUES (
3, 3, 'INDIA');
INSERT INTO EMP_DOCS ( ID, EMP_ID, COUNTRY_NO ) VALUES (
4, 4, 'USA');
INSERT INTO EMP_ITEMS ( ID, EMP_DOC_ID ) VALUES (
1, 1);
INSERT INTO EMP_ITEMS ( ID, EMP_DOC_ID ) VALUES (
2, 2);
INSERT INTO EMP_ITEMS ( ID, EMP_DOC_ID ) VALUES (
3, 3);
INSERT INTO EMP_ITEMS ( ID, EMP_DOC_ID ) VALUES (
4, 4);
INSERT INTO DOCUMENT ( ID, DOCNO, INVOICE_REF_1 ) VALUES (
1, 'DOC1', 1);
INSERT INTO DOCUMENT ( ID, DOCNO, INVOICE_REF_1 ) VALUES (
2, 'DOC1', 2);
INSERT INTO DOCUMENT ( ID, DOCNO, INVOICE_REF_1 ) VALUES (
3, 'DOC3', 0);
INSERT INTO DOCUMENT ( ID, DOCNO, INVOICE_REF_1 ) VALUES (
4, 'DOC4', 3);
INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
1, 1, 1);
INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
2, 2, 2);
INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
3, 2, 2);
INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
4, 3, 3);
INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
5, 4, 4);
INSERT INTO INVOICE ( ID, INVOICE_NO ) VALUES (
1, 'INV1');
INSERT INTO INVOICE ( ID, INVOICE_NO ) VALUES (
2, 'INV2');
INSERT INTO INVOICE ( ID, INVOICE_NO ) VALUES (
3, 'INV3');
commit;
-- I have written below query to satisfy above conditions but still required results are not coming..
SELECT *
FROM EMP E
WHERE E.TYPE = 'A'
AND NOT EXISTS (SELECT *
FROM EMP_DOCS EDOC1,
EMP_DOCS EDOC2,
EMP E1,
EMP_ITEMS EMPI,
DOCUMENT_ITEMS DOCITM,
DOCUMENT DOC,
INVOICE INV
WHERE EDOC1.EMP_ID = E.ID
AND EDOC2.EMP_ID = E1.ID
AND EDOC1.ID = EMPI.EMP_DOC_ID
AND EDOC1.COUNTRY_NO = EDOC2.COUNTRY_NO
AND EMPI.ID = DOCITM.EMP_ITEMS_REF_ID_1
AND DOCITM.DOC_ID = DOC.ID
AND INV.ID = DOC.INVOICE_REF_1);DB version:oracle 10g;10.2
-
How to split the query using rowid
can anybody pls tell me logic to spliting the sql query by using rowid through dba_extent...
Using Parallel Execution will help if it is the DB that is the bottleneck, but if on the other hand it is your client program that is the bottleneck (and the DB is supplying the data as fast as it can) then splitting the query into rowid ranges and having multiple extract clients will have benefit.
1) Each extract process should read DBA_EXTENTS for the given object and select the extents it should process (Ideally 1 extract process per Data File).
2) Use the DBMS_ROWID.ROWID_CREATE to build low and high rowid ranges based on the BLOCK_ID and BLOCKS from DBA_EXTENTS. Note you will also have to look up the DATA_OBJECT_ID for the Table in question.
Note. Because we won't know the Row Number of the last row in the last block, use the first row in the first block after the last block and then use <.
3) Add the ROWID hint to your query.
4) Use >= "Low Rowid" < "High Rowid" (Note. use of < rather than <=)
This method is only safe when the table in question is not being updated, as each extract process will be running its own read consistent view, and so there is a danger that transaction updating 2 rows could have only half of the update extracted (if 1 row was in 1 extent after it had been extracted and another row was in another extent before it was extracted).
Only 1 rowid range can be passed to the query at a time (you can supply more, but a ROWID access will no longer be performed, it will simply to a FTS). -
i have a table with the fields
d_date, d_type varchar2(10), d_time number(2,3)
the data in this table is
10/10/2007 analyse 150
11/10/2007 analyse 150
10/10/2007 create 150
11/10/2007 create 150
10/10/2007 Design 200
11/10/2007 Design 200
how to create a query to get the following
analyse create design total
300 300 400 1000
30% 30% 40% 100%SQL>
SQL> With t As
2 (
3 Select 1 col1, 'analyse' col2, 150 col3 From dual Union All
4 Select 2, 'analyse', 150 From dual Union All
5 Select 1, 'create',150 From dual Union All
6 Select 2, 'create', 150 From dual Union All
7 Select 1, 'Design', 200 From dual Union All
8 Select 2, 'Design', 200 From dual
9 )
10 Select Unique
11 col2,
12 Sum(col3) over(Partition By col2 Order By col2) sum_col2,
13 Sum(col3) over() tot
14 ,Sum(col3) over(Partition By col2 Order By col2)/Sum(col3) over()*100||'%' perc
15 From t ;
COL2 SUM_COL2 TOT PERC
analyse 300 1000 30%
Design 400 1000 40%
create 300 1000 30%
SQL> Message was edited by:
Nicloei W -
How to avoid the query using the index?
select *from cstb_cut ;
cust_id cust_name
10001 xxxx
10002 yyyy
10003 zzzz
Please find the above table and 10000 records are there in that table and also one index was there on cust_id.
but my query don't want to use the index?how it will be done?Hi,
DEVI suvarna wrote:
select *from cstb_cut ;
cust_id cust_name
10001 xxxx
10002 yyyy
10003 zzzz
Please find the above table and 10000 records are there in that table and also one index was there on cust_id.
but my query don't want to use the index?how it will be done?
The index doesn't help for this query, so it's not used.
The main purpose of indexes is the help find a small number (like 1) of rows quickly. You're asking for all rows; getting each one through the index would be slower than just getting all of them directly.
Sometimes (but not here) the index can be used instead of the table, when the index contains all the data you need from the table. An index on cust_id doesn't tell you anything about any other column, such as cust_name. Since you asked to have all columns displayed, it has to fetch data from the table, not the index. -
How to write a query use between two months
Hello,
I have two views v1 and v2. Both have two columns c1 and c2: c1 with mon-yyyy format, c2 is a number. I am writing a query like this;
select v1.c2, v2.c2 from v1, v2
where v1.c1 = v2.c1 and v1.c1 between to_date('Jan-2011','mon-yyyy')
and to_date('Dec-2011','mon-yyyy')
i got: ORA-01858: a non-numeric character was found where a numeric was expected
V1 and V2 value are like these:
Mon-2011 89
Feb-2011 33
Mar-2011 45
Apr-2011 98
How to make it work.
Thanks for your help.
JenHello Jen Hu,
Try this:
with t as ( select 'feb-2011' as c1, 33 as nr from dual union
select 'mar-2011',45 from dual union
select 'apr-2011',98 from dual union
select 'jun-2011',86 from dual union
select 'jul-2011',78 from dual union
select 'aug-2011',87 from dual union
select 'sep-2011',68 from dual union
select 'oct-2011',56 from dual union
select 'nov-2011',68 from dual union
select 'dec-2011',55 from dual union
select 'jan-2012',54 from dual
) SELECT * FROM T
where to_date(c1,'mon-yyyy') between to_date('aug-2011','mon-yyyy') and to_date('dec-2011','mon-yyyy');@Tauceef:
>
Tauceef wrote:
You have to store full date, the conversion you are doing to_date('jan-2011','mon-yyyy') is not at all accepted, this is where you are getting that error.
>
Why not? Have you tried:
select to_date('jan-2011','mon-yyyy') from dual;Once you convert the character string into date it is easier to compare!
I agree with @clcarter's point:
>
Firstly, if you want to compare dates, use a DATE datatype, not varchar2. If the varchar2 has invalid dates you'll have all sorts of trouble. 'Feb-31-2011' is a perfectly valid string. But its not a date.
>
But if you have better mechanism of valid string input as in here:
http://apex-plugin.com/oracle-apex-plugins/item-plugin/month-picker_148.html
then problems of invalid string input are minimized!
Hope it helps!
Regards,
Kiran -
How to write the given query using 'ANY ' operator
Hi,
How to write the given query using 'ANY ' operator , I dont need to fetch to grade_master table twice in database, just need to fetch within the result set.
SELECT dsg_code,dsg_name,dsg_grade FROM designation_master WHERE dsg_orgn='&&Orgn' and dsg_ctry='&&ctry'
And dsg_loc ='&&loc' And dsg_oru = '&&oru' and dsg_grade in decode('&&radio_group',
1, SELECT grd_code FROM grade_master WHERE grd_osm_code in (Select grd_osm_code FROM grade_master WHERE grd_orgn='&&Orgn' and grd_ctry='&&ctry' And grd_loc ='&&loc' And grd_oru = '&&oru' and grd_code ='&&emp_grade'),
2, SELECT grd_code FROM grade_master WHERE grd_osm_code > (Select grd_osm_code FROM grade_master WHERE grd_orgn='&&orgn' and grd_ctry='&&ctry' and grd_loc ='&&loc' And grd_oru = '&&oru' and grd_code),
3, SELECT grd_code FROM grade_master WHERE grd_osm_code < (Select grd_osm_code FROM grade_master WHERE grd_orgn='&&orgn' and grd_ctry='&&ctry' And grd_loc ='&&loc' And grd_oru = '&&oru' and grd_code ='&&emp_grade'))
thanks
rincyHi,
One thing I understood my your issue is you want to perform, execution of query once or fetch the results sets my minimizing the number of times executions of queries. It would be hard for us to check in this way, atleast provide some temporary data and some business rules. Only I can IN, >, < (queries logical conditons on inner query)
- Pavan Kumar N
- ORACLE OCP - 9i/10g
https://www.oracleinternals.blogspot.com -
How to make sql to use index/make to query to perform better
Hi,
I have 2 sql query which results the same.
But both has difference in SQL trace.
create table test_table
(u_id number(10),
u_no number(4),
s_id number(10),
s_no number(4),
o_id number(10),
o_no number(4),
constraint pk_test primary key(u_id, u_no));
insert into test_table(u_id, u_no, s_id, s_no, o_id, o_no)
values (2007030301, 1, 1001, 1, 2001, 1);
insert into test_table(u_id, u_no, s_id, s_no, o_id, o_no)
values (2007030302, 1, 1001, 1, 2001, 2);
insert into test_table(u_id, u_no, s_id, s_no, o_id, o_no)
values (2007030303, 1, 1001, 1, 2001, 3);
insert into test_table(u_id, u_no, s_id, s_no, o_id, o_no)
values (2007030304, 1, 1001, 1, 2001, 4);
insert into test_table(u_id, u_no, s_id, s_no, o_id, o_no)
values (2007030305, 1, 1002, 1, 1001, 2);
insert into test_table(u_id, u_no, s_id, s_no, o_id, o_no)
values (2007030306, 1, 1002, 1, 1002, 1);
commit;
CREATE INDEX idx_test_s_id ON test_table(s_id, s_no);
set autotrace on
select s_id, s_no, o_id, o_no
from test_table
where s_id <> o_id
and s_no <> o_no
union all
select o_id, o_no, s_id, s_no
from test_table
where s_id <> o_id
and s_no <> o_no;
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=6 Card=8 Bytes=416)
1 0 UNION-ALL
2 1 TABLE ACCESS (FULL) OF 'TEST_TABLE' (TABLE) (Cost=3 Card=4 Bytes=208)
3 1 TABLE ACCESS (FULL) OF 'TEST_TABLE' (TABLE) (Cost=3 Card=4 Bytes=208)
Statistics
223 recursive calls
2 db block gets
84 consistent gets
0 physical reads
0 redo size
701 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
5 sorts (memory)
0 sorts (disk)
8 rows processed
-- i didnt understand why the above query is not using the index idx_test_s_id.
-- But still it is faster
select s_id, s_no, o_id, o_no
from test_table
where (u_id, u_no) in
(select u_id, u_no from test_table
minus
select u_id, u_no from test_table
where s_id = o_id
or s_no = o_no)
union all
select o_id, o_no, s_id, s_no
from test_table
where (u_id, u_no) in
(select u_id, u_no from test_table
minus
select u_id, u_no from test_table
where s_id = o_id
or s_no = o_no);
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=16 Card=2 Bytes=156)
1 0 UNION-ALL
2 1 FILTER
3 2 TABLE ACCESS (FULL) OF 'TEST_TABLE' (TABLE) (Cost=3 Card=6 Bytes=468)
4 2 MINUS
5 4 INDEX (UNIQUE SCAN) OF 'PK_TEST' (INDEX (UNIQUE)) (Cost=1 Card=1 Bytes=26)
6 4 TABLE ACCESS (BY INDEX ROWID) OF 'TEST_TABLE' (TABLE) (Cost=2 Card=1 Bytes=78)
7 6 INDEX (UNIQUE SCAN) OF 'PK_TEST' (INDEX (UNIQUE)) (Cost=1 Card=1)
8 1 FILTER
9 8 TABLE ACCESS (FULL) OF 'TEST_TABLE' (TABLE) (Cost=3 Card=6 Bytes=468)
10 8 MINUS
11 10 INDEX (UNIQUE SCAN) OF 'PK_TEST' (INDEX (UNIQUE)) (Cost=1 Card=1 Bytes=26)
12 10 TABLE ACCESS (BY INDEX ROWID) OF 'TEST_TABLE' (TABLE) (Cost=2 Card=1 Bytes=78)
13 12 INDEX (UNIQUE SCAN) OF 'PK_TEST' (INDEX (UNIQUE)) (Cost=1 Card=1)
Statistics
53 recursive calls
8 db block gets
187 consistent gets
0 physical reads
0 redo size
701 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
4 sorts (memory)
0 sorts (disk)
8 rows processed
-- The above query is using index PK_TEST. But still it has FULL SCAN to the
-- table two times it has the more cost.
1st query --> SELECT STATEMENT Optimizer=ALL_ROWS (Cost=6 Card=8 Bytes=416)
2nd query --> SELECT STATEMENT Optimizer=ALL_ROWS (Cost=16 Card=2 Bytes=156)
My queries are:
1) performance wise which query is better?
2) how do i make the 1st query to use an index
3) is there any other method to get the same result by using any index
Appreciate your immediate help.
Best regards
MuthuHi William
Nice...it works.. I have added "o_id" and "o_no" are in part of the index
and now the query uses the index
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=2 Card=8 Bytes=416)
1 0 UNION-ALL
2 1 INDEX (FULL SCAN) OF 'IDX_TEST_S_ID' (INDEX) (Cost=1 Card=4 Bytes=208)
3 1 INDEX (FULL SCAN) OF 'IDX_TEST_S_ID' (INDEX) (Cost=1 Card=4 Bytes=208)
Statistics
7 recursive calls
0 db block gets
21 consistent gets
0 physical reads
0 redo size
701 bytes sent via SQL*Net to client
507 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
But my questions are:
1) In a where clause, if "<>" condition is used, then, whether the system will use the index. Because I have observed in several situations even though the column in where clause is indexed, since the where condition is "like" or "is null/is not null"
then the index is not used. Same as like this, i assumed, if we use <> then indexes will not be used. Is it true?
2) Now, after adding "o_id" and "o_no" columns to the index, the Execution plan is:
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=2 Card=8 Bytes=416)
1 0 UNION-ALL
2 1 INDEX (FULL SCAN) OF 'IDX_TEST_S_ID' (INDEX) (Cost=1 Card=4 Bytes=208)
3 1 INDEX (FULL SCAN) OF 'IDX_TEST_S_ID' (INDEX) (Cost=1 Card=4 Bytes=208)
Before it was :
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=6 Card=8 Bytes=416)
1 0 UNION-ALL
2 1 TABLE ACCESS (FULL) OF 'TEST_TABLE' (TABLE) (Cost=3 Card=4 Bytes=208)
3 1 TABLE ACCESS (FULL) OF 'TEST_TABLE' (TABLE) (Cost=3 Card=4 Bytes=208)
Difference only in Cost (reduced), not in Card, Bytes.
Can you explain, how can i decide which makes the performace better (Cost / Card / Bytes). Full Scan / Range Scan?
On statistics also:
Before:
Statistics
52 recursive calls
0 db block gets
43 consistent gets
0 physical reads
0 redo size
701 bytes sent via SQL*Net to client
507 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
After:
Statistics
7 recursive calls
0 db block gets
21 consistent gets
0 physical reads
0 redo size
701 bytes sent via SQL*Net to client
507 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
Difference in recursive calls & consistent gets.
Which one shows the query with better performance?
Please explain..
Regards
Muthu -
How to write sql query that display comma suppurated result using Group by
Hi,
I am having data like bellow ,
Above result got from joining two tables VMTAGroupClient,VMTAipNames .
Query i have written to display above result is,
select vgc.VMTAGroupId,vn.VMTAName from VMTAGroupClient vgc inner join VMTAipNames vn
on vgc.VMTAID=vn.VMTANameID group by vgc.VMTAGroupId,vn.VMTAName
using the VMTAGroupId column how to write query to display result result like,
VMTAGroupID VMTAs
1 VMTA1,VMTA3
2 VMTA2,VMTA4,VMTA5
Regards,
Anwar ShaikSatheesh,
Here in my case data need to read from two tables VMTAGroupClient, VMTAipNames.
VMTAGroupId is in one table and VMTAName column in some other table.Iin both the tables VMTAID is common.
Please check the above result displayed data from two tables.
can we write same query using join?
Anwar Shaik
Maybe you are looking for
-
How do i move a movie from my PC to my ipad?
how do I move a movie from my PC to my ipad?
-
InDesign docs won't print on Epson CX 6600
Interesting problem. Computer is a "basketball" iMac 17" LCD G4 with OSX 10.3.9. Tried to print to an Epson CX6600 multifunction printer/scanner/copier, after the CX5200 died. Sending the 6600 any doc done in InDesign stalls to "Jobs Stopped" after s
-
Hi, I just moved to apple from PC. I have an iMac with OS X (Lion) and iWorks. In Pages, I'm having trouble in opening the file created in MS Word 2007. Any help please?
-
I understand that the camera connection can read photos and videos from a flash drive formatted as ms dos. What about documents? Is that possible?
-
Will PSE3 Third Party Plug-Ins Work On My New iMac ?
Over the past 5+ years I have accumulated quite a few third party plug-ins for both PSE2 and PSE3 running on my Apple eMac G4. I will shortly be taking delivery of a new Intel based iMac,and whilst previous posts on this forum show that I can run PSE