Order By Clause based on Account
Hi,
I have a query which pulls the list of GL account details. But i have specific account needs to be at the bottom. Ordery Asc/desc will not work in this case.
IS there any ways that i can conditionally order by..
Please help..
Thanks,
Genoo
TABLE STRUCTURE
CREATE TABLE LA.LA_PE_GL_JOURNAL_REPORTS_ITF
REQUEST_ID NUMBER(15) NOT NULL,
CREATED_BY NUMBER(15) NOT NULL,
CREATION_DATE DATE NOT NULL,
LAST_UPDATED_BY NUMBER(15) NOT NULL,
LAST_UPDATE_DATE DATE NOT NULL,
LAST_UPDATE_LOGIN NUMBER(15) NOT NULL,
ORGANIZATION_NAME VARCHAR2(30 BYTE),
FUNCTIONAL_CURRENCY_CODE VARCHAR2(15 BYTE),
BATCH_NAME VARCHAR2(100 BYTE),
BATCH_STATUS VARCHAR2(80 BYTE),
BATCH_EFFECTIVE_DATE DATE,
BATCH_POSTED_DATE DATE,
BATCH_RUN_TOTAL_DR NUMBER,
BATCH_RUN_TOTAL_CR NUMBER,
BATCH_RUN_TOTAL_ACCTD_DR NUMBER,
BATCH_RUN_TOTAL_ACCTD_CR NUMBER,
BATCH_PERIOD_NAME VARCHAR2(15 BYTE),
BATCH_TYPE VARCHAR2(240 BYTE),
BATCH_JOURNAL_TYPE VARCHAR2(80 BYTE),
BATCH_APPROVAL_STATUS VARCHAR2(80 BYTE),
BATCH_DESCRIPTION VARCHAR2(240 BYTE),
BATCH_BUDGET_CONTROL_STATUS VARCHAR2(80 BYTE),
BATCH_DATE_CREATED DATE,
HEADER_NAME VARCHAR2(100 BYTE),
HEADER_CATEGORY VARCHAR2(25 BYTE),
HEADER_SOURCE VARCHAR2(25 BYTE),
HEADER_RUN_TOTAL_DR NUMBER,
HEADER_RUN_TOTAL_CR NUMBER,
HEADER_RUN_TOTAL_ACCTD_DR NUMBER,
HEADER_RUN_TOTAL_ACCTD_CR NUMBER,
BEGIN_BALANCE NUMBER,
END_BALANCE NUMBER,
CURRENCY_CODE VARCHAR2(25 BYTE),
EXCHANGE_RATE NUMBER,
EXCHANGE_DATE DATE,
EXCHANGE_TYPE VARCHAR2(30 BYTE),
HEADER_EXTERNAL_REF VARCHAR2(80 BYTE),
DOC_SEQUENCE_NAME VARCHAR2(30 BYTE),
DOC_SEQUENCE_VALUE NUMBER,
HEADER_LAST_UPDATE_DATE DATE,
HEADER_LAST_UPDATED_BY NUMBER(15),
TAX_STATUS VARCHAR2(80 BYTE),
HEADER_EFFECTIVE_DATE DATE,
HEADER_DESCRIPTION VARCHAR2(240 BYTE),
HEADER_ENCUMBRANCE_TYPE VARCHAR2(30 BYTE),
HEADER_BUDGET_NAME VARCHAR2(15 BYTE),
JE_LINE_NUM NUMBER(15),
SUBLEDGER_DOC_SEQ_NAME VARCHAR2(30 BYTE),
SUBLEDGER_DOC_SEQ_VALUE NUMBER,
ENTERED_DR NUMBER,
ENTERED_CR NUMBER,
ACCOUNTED_DR NUMBER,
ACCOUNTED_CR NUMBER,
JE_ACCOUNT VARCHAR2(240 BYTE),
JE_ACCOUNT_DESC VARCHAR2(240 BYTE),
JE_BALANCE VARCHAR2(240 BYTE),
JE_BALANCE_DESC VARCHAR2(240 BYTE),
JE_NATACCT VARCHAR2(240 BYTE),
JE_NATACCT_DESC VARCHAR2(240 BYTE),
JE_CC VARCHAR2(240 BYTE),
JE_CC_DESC VARCHAR2(240 BYTE),
TAX_CODE VARCHAR2(50 BYTE),
TAX_TYPE VARCHAR2(80 BYTE),
LINE_DESCRIPTION VARCHAR2(240 BYTE),
STAT_AMOUNT NUMBER,
EFFECTIVE_DATE DATE,
LINE_REFERENCE VARCHAR2(240 BYTE),
SOURCE_REFERENCE VARCHAR2(240 BYTE),
SET_OF_BOOKS_ID NUMBER(15),
ACCOUNTING_BALANCING_SEGMENT VARCHAR2(25 BYTE),
COMPANY_NAME VARCHAR2(80 BYTE),
TAX_PAYER_ID VARCHAR2(50 BYTE),
ADDRESS_LINE VARCHAR2(120 BYTE),
CITY VARCHAR2(25 BYTE),
STATE VARCHAR2(25 BYTE),
COUNTRY VARCHAR2(25 BYTE),
ZIP VARCHAR2(20 BYTE),
DOC_SEQUENCE_ID NUMBER,
LINE_ATTRIB1 VARCHAR2(150 BYTE),
LINE_ATTRIB2 VARCHAR2(150 BYTE),
LINE_ATTRIB3 VARCHAR2(150 BYTE),
LINE_ATTRIB4 VARCHAR2(150 BYTE),
LINE_ATTRIB5 VARCHAR2(150 BYTE),
LINE_ATTRIB6 VARCHAR2(150 BYTE),
LINE_ATTRIB7 VARCHAR2(150 BYTE),
LINE_ATTRIB8 VARCHAR2(150 BYTE),
LINE_ATTRIB9 VARCHAR2(150 BYTE),
LINE_ATTRIB10 VARCHAR2(150 BYTE),
LINE_ATTRIB11 VARCHAR2(150 BYTE),
LINE_ATTRIB12 VARCHAR2(150 BYTE),
LINE_ATTRIB13 VARCHAR2(150 BYTE),
LINE_ATTRIB14 VARCHAR2(150 BYTE),
LINE_ATTRIB15 VARCHAR2(150 BYTE),
LINE_ATTRIB16 VARCHAR2(150 BYTE),
LINE_ATTRIB17 VARCHAR2(150 BYTE),
LINE_ATTRIB18 VARCHAR2(150 BYTE),
LINE_ATTRIB19 VARCHAR2(150 BYTE),
LINE_ATTRIB20 VARCHAR2(150 BYTE),
HEADER_ATTRIB1 VARCHAR2(150 BYTE),
HEADER_ATTRIB2 VARCHAR2(150 BYTE),
HEADER_ATTRIB3 VARCHAR2(150 BYTE),
HEADER_ATTRIB4 VARCHAR2(150 BYTE),
HEADER_ATTRIB5 VARCHAR2(150 BYTE),
HEADER_ATTRIB6 VARCHAR2(150 BYTE),
HEADER_ATTRIB7 VARCHAR2(150 BYTE),
HEADER_ATTRIB8 VARCHAR2(150 BYTE),
HEADER_ATTRIB9 VARCHAR2(150 BYTE),
HEADER_ATTRIB10 VARCHAR2(150 BYTE),
PAY_METHOD VARCHAR2(25 BYTE),
TRANSACTION_NUM NUMBER(15),
BANK_NAME VARCHAR2(60 BYTE),
BANK_ACCOUNT VARCHAR2(30 BYTE),
JE_HEADER_ID NUMBER,
PARTY_SUPPLIER_NAME VARCHAR2(240 BYTE),
PAY_METHOD_CODE_SUNAT VARCHAR2(5 BYTE),
PAY_METHOD_DESC_SUNAT VARCHAR2(200 BYTE)
Query - Before change
SELECT DISTINCT je_natacct local_acct, je_natacct_desc local_acct_desc
FROM la.la_pe_gl_journal_reports_itf
WHERE 1 = 1 AND request_id = 39550567
Query Output
LOCAL_ACCT LOCAL_ACCT_DESC
669112 OTRAS CARGAS NO DEDUCIBLES
332111 COSTO DE ADQUISICION O PRODUCCION EDIFICACIONES
621111 SUELDOS Y SALARIOS
899999 CONVERSION CLEARING
639511 SERVICIOS CONTABLES
627311 SEGURO SALUD-ACC TRABAJO
New Query
SELECT DISTINCT je_natacct local_acct, je_natacct_desc local_acct_desc
FROM la.la_pe_gl_journal_reports_itf
WHERE 1 = 1 AND request_id = 39550567
ORDER BY DECODE(je_natacct,'201111',2,
'611111',3,
'791111',4,
'941111',5,
'951111',6,
'971111',7,
1);
New Query - Output
LOCAL_ACCT LOCAL_ACCT_DESC
332111 COSTO DE ADQUISICION O PRODUCCION EDIFICACIONES
621111 SUELDOS Y SALARIOS
899999 CONVERSION CLEARING
639511 SERVICIOS CONTABLES
627311 SEGURO SALUD-ACC TRABAJO
201111 MERCADERIAS-MIOL
Similar Messages
-
Order by clause based on parameter value
hi,
i am using reports 6i with 10 g db
I have to create a report sorted in order of four fields. there is a parameter for selecting which field should come first in order by clause.
ie the user has provision to view the report in selected sorted order
suppose the four fields are emp_id, name, dept_id, desig .
if the user select dept_id , then the order by clause should be in order of dept_id,emp_id,name,desig
Please help to solve this scenario.
ThanksHi Rinz,
Use the query as follows :
select column_name1, column_name1, ....
from table_name
&order_by_clauseNow in the AFTER-PARAMETER-FORM trigger set the order by clause as below depending upon requirements :
if :sort_column = dept_id then
:order_by_clause := 'order by dept_id,emp_id,name,desig';
else
:order_by_clause := 'order by emp_id, dept_id, name,desig';
end if;
Here sort_column is User parameter which will be selected by user, provide list of Values for it.
Hope this helps
Best Regards
Arif Khadas -
In Report,order by clause is not working
Hi All,
For the report i prepared the query in this one am using the order by clause,i run the query in the sql plus working fine am getting the data based on the order by clause.when i use the same query for the report am getting the data but not in the order(i.e order by clause in not working).
Please help me how to do this one.
Thanks in Advance.
Regards,
Leelakrishna.GHi Dora,
Data is coming in the proper sequence,but in the first group data is not coming(In first group data first line only displaying,remaining data is not comming).
My req is:
The report is executing based on the creation date(i.e.,from creation date to today).
In this so many Purchase requisitions will be there for this one different approval in different times will be there.
For Ex:
we will take 2 PR number only(for the first one 3 approvals,second one 4 levels of approval).
1. one supplier 1person raised by 1PR submit
No Action
Approve
2. 2Supplier 2personraised by 2PR Submit
NO Action
Forword
Approve
with ur suggestion am trying data is gettin fine but first group first line is coming remaining data is not coing.
EX:
1. one supplier 1person raised by 1PR submit
No Action
Approve
2PR Submit
NO Action
Forword
Approve
Note:"2. 2Supplier 2personraised by " this kind of lines data is missing.
Can you pls check and suggest me how i can do this.
Thanks in Advance.
Regards,
Leelakrishna.G -
Slow SQL output when table alias is NOT used in order by clause
Hi guys,
My query is based on Oracle 9208
I have a table TAB1 with 68000 records with transaction_id as the primary key in this table (unique index).
I have another TAB2 with the same number of records again with transaction_id in this table (foreign key to above).
I have the below query that gets executed via an application:-
SELECT TO_CHAR(V1.TRANSACTION_ID), V1.POLICY_ID, V1.REQUEST_TYPE
FROM <VIEW> V1 WHERE V1.CERT_SERIAL_NUM=56192 AND
(V1.AUTH_GROUP_ID=0 OR V1.AUTH_GROUP_ID=1) AND ROWNUM <= 3 ORDER
BY TRANSACTION_ID ASC
The above view V1 is created as below:-
CREATE OR REPLACE FORCE VIEW "V1"
("TRANSACTION_ID",
"PARENT_TRANSACTION_ID",
"CA_DN_ID",
"AUTH_GROUP_ID",
"POLICY_ID",
"REQUEST_TYPE",
"REQUEST_STATUS",
"EE_DN_HASH",
"EE_DN",
"EE_EMAIL_HASH",
"EE_EMAIL",
"KEY_USAGE",
"SMART_CARD_SERIAL",
"CERT_TYPE",
"CERT_SERIAL_NUM",
"CERT_INDEX",
"RENEWAL_FLAG",
"ARCHIVE_FLAG",
"TIME_RECEIVED",
"DOWNLOADED",
"REQUEST_DATA",
"ACTION",
"STEP_NUM")
AS
SELECT
T1.transaction_id,
T1.parent_transaction_id,
T1.ca_dn_id,
V2.auth_group_id,
V2.policy_id,
T1.request_type,
T1.request_status,
T2.ee_dn_hash,
T2.ee_dn,
T2.ee_email_hash,
T2.ee_email,
T2.key_usage,
T2.smart_card_serial,
T2.cert_type,
T2.cert_serial_num,
T2.cert_index,
T2.renewal_flag,
T2.archive_flag,
T1.time_received,
T1.downloaded,
T1.request_data,
V2.action,
V2.step_num
FROM TAB1
<ANOTHER VIEW> V2,
TAB2 T1,
TAB2 T2
WHERE
T1.transaction_id = T2.transaction_id
AND
V2.policy_id = T1.policy_id
order by transaction_id;
The query at the top runs within milliseconds if the VIEW is created as :-
order by t2.transaction_id
But without the alias "t2" in the order by, the query takes about 1 1/2 minutes
Can you tell me why? I thought if you ordering by primary key (lesser number of values compared to foreign key values), the query should be faster..no?
Thanks in advanceThanks for keeping up with this issue Hemant.
Here are the plans for each case.
I would be very interested in how you'd recognize which plan is the best and what are the derivatives.
I don't much (or rather anything!) what is 'card' values, 'cost' values etc which I believe are used to decide the best plan of the lot.
Thanks again
Note TAB1 and TAB2 are from view definition posted initially
1) Execution Plan for VIEW1 <<-- With ORDER BY" clause but no table ailas (order by transaction_id)
SQL> EXPLAIN PLAN FOR SELECT TO_CHAR(QT.TRANSACTION_ID), QT.POLICY_ID, QT.REQUEST_TYPE
2 FROM <VIEW1> QT WHERE QT.CERT_SERIAL_NUM=24293 AND
3 (QT.AUTH_GROUP_ID=0 OR QT.AUTH_GROUP_ID=1) AND ROWNUM <= 3 ORDER
4 BY TRANSACTION_ID ASC
5 /
Explained.
Elapsed: 00:00:01.00
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
| Id | Operation | Name | Rows | Bytes | Cost |
| 0 | SELECT STATEMENT | | 3 | 195 | 17 |
|* 1 | COUNT STOPKEY | | | | |
| 2 | VIEW | VIEW1 | 17 | 1105 | 17 |
|* 3 | SORT ORDER BY STOPKEY | | 17 | 38573 | 17 |
| 4 | NESTED LOOPS | | 17 | 38573 | 10 |
| 5 | MERGE JOIN CARTESIAN | | 1 | 167 | 9 |
| 6 | VIEW | VIEW2 | 1 | 52 | 8 |
| 7 | SORT UNIQUE | | 1 | 156 | 8 |
| 8 | NESTED LOOPS | | 1 | 156 | 6 |
| 9 | NESTED LOOPS | | 1 | 143 | 6 |
| 10 | NESTED LOOPS OUTER | | 1 | 117 | 5 |
|* 11 | HASH JOIN | | 1 | 104 | 5 |
| 12 | NESTED LOOPS | | 1 | 52 | 2 |
|* 13 | TABLE ACCESS FULL | TAB3 | 1 | 39 | 2 |
|* 14 | INDEX UNIQUE SCAN | (PK_TAB4) | 1 | 13 | |
| 15 | TABLE ACCESS FULL | TAB5 | 82 | 4264 | 2 |
| 16 | VIEW PUSHED PREDICATE | View3 | 1 | 13 | |
| 17 | NESTED LOOPS | | 1 | 52 | 2 |
| 18 | NESTED LOOPS | | 1 | 39 | 2 |
|* 19 | INDEX UNIQUE SCAN | (PK_TAB6) | 1 | 13 | 1 |
|* 20 | INDEX RANGE SCAN | (PK_TAB7) | 1 | 26 | 1 |
|* 21 | INDEX UNIQUE SCAN | (PK_TAB8) | 1 | 13 | |
| 22 | TABLE ACCESS BY INDEX ROWID| TAB9 | 3 | 78 | 1 |
|* 23 | INDEX UNIQUE SCAN | (PK_TAB9) | 1 | | |
|* 24 | INDEX UNIQUE SCAN | (PK_TAB10)| 1 | 13 | |
| 25 | BUFFER SORT | | 1 | 115 | 9 |
| 26 | TABLE ACCESS BY INDEX ROWID | TAB2 | 1 | 115 | 1 |
|* 27 | INDEX RANGE SCAN | (TAB2_IDX2)| 1 | | |
|* 28 | TABLE ACCESS BY INDEX ROWID | TAB1 | 12 | 25224 | 1 |
|* 29 | INDEX UNIQUE SCAN | (PK_TAB1) | 1 | | |
Predicate Information (identified by operation id):
1 - filter(ROWNUM<=3)
3 - filter(ROWNUM<=3)
11 - access("TAB5"."PATH_ID"="TAB4"."PATH_ID")
13 - filter("TAB3"."AUTH_GROUP_ID"<>(-1) AND ("TAB3"."AUTH_GROUP_ID"=0 OR "TAB3"."AUTH_GROUP_ID"=1))
14 - access("TAB3"."PATH_ID"="TAB4"."PATH_ID")
19 - access("TAB5"."DOMAIN_ID"="TAB6"."DOMAIN_ID")
20 - access("TAB6"."DOMAIN_ID"="TAB7"."DOMAIN_ID")
21 - access("TAB7"."RULE_ID"="TAB8"."RULE_ID")
23 - access("TAB9"."POLICY_ID"="TAB5"."POLICY_ID")
24 - access("TAB9"."ASSOCIATED_FORM_ID"="TAB10"."FORM_ID")
27 - access("TAB2"."CERT_SERIAL_NUM"=24293)
28 - filter("View2"."POLICY_ID"="TAB1"."POLICY_ID")
29 - access("TAB1"."TRANSACTION_ID"="TAB2"."TRANSACTION_ID")
Note: cpu costing is off
54 rows selected.
Elapsed: 00:00:01.81
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 COLLECTION ITERATOR (PICKLER FETCH) OF 'DISPLAY'
Statistics
39 recursive calls
0 db block gets
629 consistent gets
0 physical reads
104 redo size
5169 bytes sent via SQL*Net to client
405 bytes received via SQL*Net from client
5 SQL*Net roundtrips to/from client
31 sorts (memory)
0 sorts (disk)
54 rows processed
2) Execution Plan for VIEW1 <<-- With ORDER BY" clause and table alias (order by TAB2.transaction_id)
SQL> explain plan for SELECT TO_CHAR(QT.TRANSACTION_ID), QT.POLICY_ID, QT.REQUEST_TYPE
2 FROM <VIEW1> QT WHERE QT.CERT_SERIAL_NUM=30003 AND
3 (QT.AUTH_GROUP_ID=0 OR QT.AUTH_GROUP_ID=1) AND ROWNUM <= 3 ORDER
4 BY TRANSACTION_ID ASC
5 /
Explained.
Elapsed: 00:00:10.20
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
| Id | Operation | Name | Rows | Bytes | Cost |
| 0 | SELECT STATEMENT | | 3 | 195 | 14 |
| 1 | SORT ORDER BY | | 3 | 195 | 14 |
|* 2 | COUNT STOPKEY | | | | |
| 3 | VIEW | VIEW1 | 17 | 1105 | 13 |
| 4 | NESTED LOOPS | | 17 | 38573 | 13 |
| 5 | MERGE JOIN CARTESIAN | | 1 | 167 | 12 |
|* 6 | TABLE ACCESS BY INDEX ROWID | TAB2 | 1 | 115 | 4 |
| 7 | INDEX FULL SCAN | (TAB2_IDX) | 94 | | 1 |
| 8 | BUFFER SORT | | 1 | 52 | 8 |
| 9 | VIEW | VIEW2 | 1 | 52 | 8 |
| 10 | SORT UNIQUE | | 1 | 156 | 8 |
| 11 | NESTED LOOPS | | 1 | 156 | 6 |
| 12 | NESTED LOOPS | | 1 | 143 | 6 |
| 13 | NESTED LOOPS OUTER | | 1 | 117 | 5 |
|* 14 | HASH JOIN | | 1 | 104 | 5 |
| 15 | NESTED LOOPS | | 1 | 52 | 2 |
|* 16 | TABLE ACCESS FULL | TAB3 | 1 | 39 | 2 |
|* 17 | INDEX UNIQUE SCAN | (PK_TAB4) | 1 | 13 | |
| 18 | TABLE ACCESS FULL | TAB5 | 82 | 4264 | 2 |
| 19 | VIEW PUSHED PREDICATE | View3 | 1 | 13 | |
| 20 | NESTED LOOPS | | 1 | 52 | 2 |
| 21 | NESTED LOOPS | | 1 | 39 | 2 |
|* 22 | INDEX UNIQUE SCAN | (PK_TAB6) | 1 | 13 | 1 |
|* 23 | INDEX RANGE SCAN | (PK_TAB7) | 1 | 26 | 1 |
|* 24 | INDEX UNIQUE SCAN | (PK_TAB8) | 1 | 13 | |
| 25 | TABLE ACCESS BY INDEX ROWID| TAB9 | 3 | 78 | 1 |
|* 26 | INDEX UNIQUE SCAN | (PK_TAB9) | 1 | | |
|* 27 | INDEX UNIQUE SCAN | (PK_TAB10) | 1 | 13 | |
|* 28 | TABLE ACCESS BY INDEX ROWID | TAB1 | 12 | 25224 | 1 |
|* 29 | INDEX UNIQUE SCAN | (PK_TAB1) | 1 | | |
Predicate Information (identified by operation id):
2 - filter(ROWNUM<=3)
6 - filter("TAB2"."CERT_SERIAL_NUM"=30003)
14 - access("TAB5"."PATH_ID"="TAB4"."PATH_ID")
16 - filter("TAB3"."AUTH_GROUP_ID"<>(-1) AND ("TAB3"."AUTH_GROUP_ID"=0 OR "TAB3"."AUTH_GROUP_ID"=1))
17 - access("TAB3"."PATH_ID"="TAB4"."PATH_ID")
22 - access("TAB5"."DOMAIN_ID"="TAB6"."DOMAIN_ID")
23 - access("TAB6"."DOMAIN_ID"="TAB7"."DOMAIN_ID")
24 - access("TAB7"."RULE_ID"="TAB8"."RULE_ID")
26 - access("TAB9"."POLICY_ID"="TAB5"."POLICY_ID")
27 - access("TAB9"."ASSOCIATED_FORM_ID"="TAB10"."FORM_ID")
28 - filter("VIEW2"."POLICY_ID"="TAB1"."POLICY_ID")
29 - access("TAB1"."TRANSACTION_ID"="TAB2"."TRANSACTION_ID")
Note: cpu costing is off
53 rows selected.
Elapsed: 00:00:08.29
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 COLLECTION ITERATOR (PICKLER FETCH) OF 'DISPLAY'
Statistics
1079 recursive calls
0 db block gets
597 consistent gets
21 physical reads
0 redo size
5177 bytes sent via SQL*Net to client
405 bytes received via SQL*Net from client
5 SQL*Net roundtrips to/from client
63 sorts (memory)
0 sorts (disk)
53 rows processed
3) Execution Plan for VIEW1 <<-- Without any "ORDER BY" clause
SQL> explain plan for SELECT TO_CHAR(QT.TRANSACTION_ID), QT.POLICY_ID, QT.REQUEST_TYPE
2 FROM <VIEW1> QT WHERE QT.CERT_SERIAL_NUM=30003 AND
3 (QT.AUTH_GROUP_ID=0 OR QT.AUTH_GROUP_ID=1) AND ROWNUM <= 3 ORDER
4 BY TRANSACTION_ID ASC
5 /
Explained.
Elapsed: 00:00:10.20
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
| Id | Operation | Name | Rows | Bytes | Cost |
| 0 | SELECT STATEMENT | | 3 | 213 | 11 |
| 1 | SORT ORDER BY | | 3 | 213 | 11 |
|* 2 | COUNT STOPKEY | | | | |
| 3 | NESTED LOOPS | | 17 | 1207 | 10 |
| 4 | MERGE JOIN CARTESIAN | | 1 | 32 | 9 |
| 5 | VIEW | VIEW2 | 1 | 26 | 8 |
| 6 | SORT UNIQUE | | 1 | 156 | 8 |
| 7 | NESTED LOOPS | | 1 | 156 | 6 |
| 8 | NESTED LOOPS | | 1 | 143 | 6 |
| 9 | NESTED LOOPS OUTER | | 1 | 117 | 5 |
|* 10 | HASH JOIN | | 1 | 104 | 5 |
| 11 | NESTED LOOPS | | 1 | 52 | 2 |
|* 12 | TABLE ACCESS FULL | TAB3 | 1 | 39 | 2 |
|* 13 | INDEX UNIQUE SCAN | PK_TAB4 | 1 | 13 | |
| 14 | TABLE ACCESS FULL | TAB5 | 82 | 4264 | 2 |
| 15 | VIEW PUSHED PREDICATE | VIEW3 | 1 | 13 | |
| 16 | NESTED LOOPS | | 1 | 52 | 2 |
| 17 | NESTED LOOPS | | 1 | 39 | 2 |
|* 18 | INDEX UNIQUE SCAN | PK_TAB6 | 1 | 13 | 1 |
|* 19 | INDEX RANGE SCAN | PK_TAB7 | 1 | 26 | 1 |
|* 20 | INDEX UNIQUE SCAN | PK_TAB8 | 1 | 13 | |
| 21 | TABLE ACCESS BY INDEX ROWID| TAB9 | 3 | 78 | 1 |
|* 22 | INDEX UNIQUE SCAN | PK_TAB9 | 1 | | |
|* 23 | INDEX UNIQUE SCAN | PK_TAB10 | 1 | 13 | |
| 24 | BUFFER SORT | | 1 | 6 | 9 |
| 25 | TABLE ACCESS BY INDEX ROWID | TAB2 | 1 | 6 | 1 |
|* 26 | INDEX RANGE SCAN | TAB2_IDX2 | 1 | | |
|* 27 | TABLE ACCESS BY INDEX ROWID | TAB1 | 12 | 468 | 1 |
|* 28 | INDEX UNIQUE SCAN | PK_TAB1 | 1 | | |
Predicate Information (identified by operation id):
2 - filter(ROWNUM<=3)
10 - access("TAB5"."PATH_ID"="TAB4"."PATH_ID")
12 - filter("TAB3"."AUTH_GROUP_ID"<>(-1) AND ("TAB3"."AUTH_GROUP_ID"=0 OR "TAB3"."AUTH_GROUP_ID"=1))
13 - access("TAB3"."PATH_ID"="TAB4"."PATH_ID")
18 - access("TAB5"."DOMAIN_ID"="TAB6"."DOMAIN_ID")
19 - access("TAB6"."DOMAIN_ID"="TAB7"."DOMAIN_ID")
20 - access("TAB7"."RULE_ID"="TAB8"."RULE_ID")
22 - access("TAB9"."POLICY_ID"="TAB5"."POLICY_ID")
23 - access("TAB9"."ASSOCIATED_FORM_ID"="TAB10"."FORM_ID")
26 - access("TAB2"."CERT_SERIAL_NUM"=1022)
27 - filter("VIEW2"."POLICY_ID"="TAB1"."POLICY_ID")
28 - access("TAB1"."TRANSACTION_ID"="TAB2"."TRANSACTION_ID")
Note: cpu costing is off
52 rows selected.
Elapsed: 00:00:03.37
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 COLLECTION ITERATOR (PICKLER FETCH) OF 'DISPLAY'
Statistics
38 recursive calls
0 db block gets
287 consistent gets
0 physical reads
0 redo size
5006 bytes sent via SQL*Net to client
405 bytes received via SQL*Net from client
5 SQL*Net roundtrips to/from client
29 sorts (memory)
0 sorts (disk)
52 rows processed -
Sorting character column ( used in order by clause dynamically)
Hi,
I need help on sorting character-based numbers. Like say, I want to sort customers based on street numbers(which is a character string being used in the
order by clause) they live in.
The criteria are :
i. Numbers must take precedence.
This being a character string, 1000001 comes before 2. This shouldn't happen. And you cannot use to_number
since using it with a string having characters in it would raise an error.
ii. If only a single alphabet occurs as the last character, then treat the whole string as a number except the last character and then sort it
as if sorting a number. Something like : if you have 1000A, 200D, 200B, 1000X, the result would be 200B,200D,1000A,1000X.
iii. if a character occurs elsewhere in the string, then perform the search normally as if performing a character search.
The output of the following data :
100
A101
B100A
110C
C120B
120
100020
120C
C1100
100D
would be like :
100
100D
110C
120
120C
100020
A101
B100A
C120B
C1100
Please note that the sort is being done dynamically, so I could have access to the values of the street numbers only during run time.
Any help is really appreciated.
Thanks in advance.
Regards,
Anil.Create a function to test whether the column is numeric :
create FUNCTION is_numeric(v_number VARCHAR2)
RETURN INTEGER
IS
l_number NUMBER;
BEGIN
IF INSTR(UPPER(v_number),'E') > 0 THEN
RETURN 0;
END IF;
l_number := TO_NUMBER(v_number);
RETURN 1;
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END;
And try this query
Assume the table name is TEST with column STREET
select * from TEST
order by case is_numeric(STREET) when 1 then LPAD(STREET,20, ' ') else STREET end
Please make sure that 20 mentioned above is the column size for STREET.
Hope this helps.
-Nags -
Can I use an expression in order by clause
SELECT
PERIOD_NUM, period_num *12,
period_name,
--TO_DATE(SUBSTR(PERIOD_NAME,1,3)| |SUBSTR(PERIOD_NAME,INSTR(PERIOD_NAME,'-'),3),'MON-RR'),
TO_NUMBER(TO_CHAR(TO_DATE((SUBSTR(PERIOD_NAME,1,3)| |SUBSTR(PERIOD_NAME,INSTR(PERIOD_NAME,'-')+1,2)),'MONRR'),'RRRRMM'))
FROM GL.GL_PERIODS
order by 2--TO_NUMBER(TO_CHAR(TO_DATE(SUBSTR(PERIOD_NAME,1,3)| |SUBSTR(PERIOD_NAME,INSTR(PERIOD_NAME,'-')+1,2),'MONRR'),'RRRRMM'))
desc
gl_periods is a table, period_name consists of values like oct-01_fy-01 etc and oct01_01_fy-01
I want to sort on this column based on period_name, so I am trying to convert into number like for oct-01_fy-01, the query gives
200110 and so on. the recent period comes first, But my question is I can't use the same to_number(.....) in the order by clause. oracle gives an error
ORA-01843: not a valid month.
Let me know whether anyone come across with this kind of situationIf you are sure the expression is correct, since you are using it in the select clause, give an alias and use the alias in the order by clause.
null -
Is it possible to use a decode function in the order by clause, when you have two select statements with a union between? I seem to be getting an error-message telling me to type a number.
Can anybody help me?Tove,
Is this what you mean?
SQL> SELECT
2 *
3 FROM
4 (SELECT
5 empno no
6 , ename name
7 , job description
8 FROM emp
9 UNION
10 SELECT
11 deptno no
12 , dname name
13 , loc description
14 FROM dept
15 )
16 ORDER BY
17 DECODE(1
18 , 1,TO_CHAR(no)
19 , 2,name
20 , 3,description)
21 /
NO NAME DESCRIPTION
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
7369 SMITH CLERK
7499 ALLEN SALESMAN
7521 WARD SALESMAN
7566 JONES MANAGER
7654 MARTIN SALESMAN
7698 BLAKE MANAGER
7782 CLARK MANAGER
7788 SCOTT ANALYST
7839 KING PRESIDENT
7844 TURNER SALESMAN
7876 ADAMS CLERK
7900 JAMES CLERK
7902 FORD ANALYST
7934 MILLER CLERK
18 rows selected.Now, to order by the second column, I set the first parameter of the
DECODE to "2":
SQL> SELECT
2 *
3 FROM
4 (SELECT
5 empno no
6 , ename name
7 , job description
8 FROM emp
9 UNION
10 SELECT
11 deptno no
12 , dname name
13 , loc description
14 FROM dept
15 )
16 ORDER BY
17 DECODE(2
18 , 1,TO_CHAR(no)
19 , 2,name
20 , 3,description)
21 /
NO NAME DESCRIPTION
10 ACCOUNTING NEW YORK
7876 ADAMS CLERK
7499 ALLEN SALESMAN
7698 BLAKE MANAGER
7782 CLARK MANAGER
7902 FORD ANALYST
7900 JAMES CLERK
7566 JONES MANAGER
7839 KING PRESIDENT
7654 MARTIN SALESMAN
7934 MILLER CLERK
40 OPERATIONS BOSTON
20 RESEARCH DALLAS
30 SALES CHICAGO
7788 SCOTT ANALYST
7369 SMITH CLERK
7844 TURNER SALESMAN
7521 WARD SALESMAN
18 rows selected.
SQL> SELECT
2 *
3 FROM
4 (SELECT
5 empno no
6 , ename name
7 , job description
8 FROM emp
9 UNION
10 SELECT
11 deptno no
12 , dname name
13 , loc description
14 FROM dept
15 )
16 ORDER BY
17 DECODE(3
18 , 1,TO_CHAR(no)
19 , 2,name
20 , 3,description)
21 /
NO NAME DESCRIPTION
7788 SCOTT ANALYST
7902 FORD ANALYST
40 OPERATIONS BOSTON
30 SALES CHICAGO
7369 SMITH CLERK
7934 MILLER CLERK
7900 JAMES CLERK
7876 ADAMS CLERK
20 RESEARCH DALLAS
7566 JONES MANAGER
7698 BLAKE MANAGER
7782 CLARK MANAGER
10 ACCOUNTING NEW YORK
7839 KING PRESIDENT
7499 ALLEN SALESMAN
7844 TURNER SALESMAN
7654 MARTIN SALESMAN
7521 WARD SALESMAN
18 rows selected.I needed to put the TO_CHAR in the DECODE so all of the columns
by which I could potentially order are VARCHAR2's, else I was
getting
SQL> /
DECODE(1
ERROR at line 17:
ORA-01785: ORDER BY item must be the number of a SELECT-list expressionHTH
T. -
Planned orders are not taking into account the processing time
Hi,
Could you please tell me why planned orders quantities are not based on processing time from the material master.
Materials was set before with 1 day in house production , no matter of lot size.
Based on routings I updated material master with processing time depending on lot size.
What is bottering me now is that planned orders are not taking into account processing time. (e.g. based on my processing time 1000 pce are produced in 3 days, but planned orders show me that 1000 pce are still need in 1 day , but it can not be produced in 1 day ).
what did I miss?
Thanks a lot for any information!Hello Simona
The in house production time from tab MRP 2 is lot size independent.
However, on tab work scheduling you can define a lot size dependent times.
Please observe that, if you have entered an in house production time, the processing time will not be considered. The F1 help of the field provides the following explanation:
You can define work scheduling times in the material master record in one of two ways:
Either you enter the in-house production time. If required, you can get the system to update this value from the routing.
Or you enter the setup, teardown, processing, and interoperation times. If you maintain these values, the system determines the in-house production time on the basis of lot size.
Therefore, if you want to consider the processing time, you should remove the in house processing time.
BR
Caetano -
Materialized view and Order by clause
Hi all. I'd like to have some information regarding the order by clause used in a materialized view.
I'm using Oracle 9.2 and Win2003 server. I have a common view (my_common_view) on which a materialized view is based (my_materialized_view). Materialized view is built for fast refresh and is created in this way:
create materialized view my_materialized_view as
select * from my_common_view
order by 1,2,3;
My question is: if I query this materialized view without adding an order by clause in the statement, I will obtain always an ordered result set? So, is it useful to include the order by clause in the materialized view script or the order by has to be include in the statement used for quering the materialized view (for example like this: select * from my_materialized view order by ...)?
Thank you very much.
Ste.SQL> create table t
2 (x int)
3 /
Table created.
SQL> create view vw
2 as
3 select *
4 from t
5 order by x
6 /
View created.
SQL> select *
2 from v$version
3 /
BANNER
Oracle9i Enterprise Edition Release 9.2.0.7.0 - 64bit Production
PL/SQL Release 9.2.0.7.0 - Production
CORE 9.2.0.7.0 Production
TNS for IBM/AIX RISC System/6000: Version 9.2.0.7.0 - Production
NLSRTL Version 9.2.0.7.0 - Production
SQL> -
Select statement takes very long to run with order by clause.
Hi all,
I have a select statement which when I run without the order by clause takes arround 2 minutes to run. But with the order by clause it goes on for ever. I am trying to access the database server through a network which is not too fast.
The select statement is based on 9 views which are again based on some views. It also has inline views and outer joins. These views and inline views can not be done away with.
When selected without the order by clause it gives 3215 records.
Anything like 2 to 3 minutes will be Ok.
Thanks.
--MalayThe select statement is as follows :-
SELECT f.system_name,
a.signal_type,
f.sys_signal_name,
a.bus_desc,
b.vl_ident,
b.vl_name,
b.src_mac,
b.src_mac_addr,
b.dest_mac,
b.dest_mac_addr,
b.network,
bb.bufr_size_in_bytes,
bb.bag_in_ms,
bb.is_rma_used,
bb.is_ic_used,
bb.sub_vl_cnt,
bb.skew_max_in_ns,
cc.msg_name,
c.src_ip,
c.src_ip_addr,
c.src_port,
c.dest_ip,
c.dest_ip_addr,
c.dest_port,
cc.rate_in_ms,
cc.tx_mode,
cc.protocol,
cc.port_type,
cc.msg_length_in_bytes,
d.mnemonic,
d.start_addr32,
d.lsb,
d.end_addr32,
d.msb,
d.format,
d.init_value,
d.fs_mnemonic,
d.fs_afdx_data_id,
d.digital_data_id,
DECODE(
d.digital_datatype,
NULL, '',
'UNUSED', '',
api$util.concat_column_data(
'api_'
|| d.digital_datatype,
'digital_data_id',
d.digital_data_id,
bool.FALSE
) AS data_details,
f.connection_id
|| '_'
|| a.digital_bus_id
|| '_'
|| b.afdx_vl_id
|| '_'
|| bb.afdx_output_id
|| '_'
|| c.afdx_frame_id
|| '_'
|| cc.afdx_msg_id
|| '_'
|| d.afdx_data_id AS KEY
FROM api_afdx a,
api_afdx_vl b,
api_afdx_output bb,
api_afdx_frame c,
api_afdx_msg cc,
api_afdx_data d,
vf_signal e,
(SELECT DISTINCT aa.signal_id,
cc.system_name,
bb.connection_id,
bb.sys_signal_name
FROM vf_nodes aa,
vf_connections bb,
vf_system cc
WHERE aa.connection_id = bb.connection_id
AND bb.system_id = cc.system_id) f
WHERE e.signal_id = f.signal_id(+)
AND e.digital_bus_id = a.digital_bus_id
AND a.digital_bus_id = bb.digital_bus_id(+)
AND bb.afdx_output_id = b.afdx_output_id(+)
AND b.afdx_vl_id = c.afdx_vl_id(+)
AND bb.afdx_output_id = cc.afdx_output_id(+)
AND cc.afdx_msg_id = d.afdx_msg_id(+)
ORDER BY f.system_name,
a.signal_type,
f.sys_signal_name,
b.vl_name,
cc.msg_name,
d.start_addr32,
d.lsb;
Where api_afdx ,
api_afdx_vl ,
api_afdx_output ,
api_afdx_frame ,
api_afdx_msg ,
api_afdx_data ,
vf_signal ,
vf_nodes ,
vf_connections ,
vf_system
are all views. -
Using bind varaible for order by clause
Hello
Can some one suggest for the following scenario?
My order by clause will be constructed based on the selected fields in the form. And now the order by clause need to be passed as parameter to Excel report.
When I use Ref cursor and OPEN-FOR-USING clause, data is not sorted.
do we have any other alternate for this?
Please observe the example code for the same. Block below resembles the code for to select the data for excel report.
SQL> select * from t;
T
sdf
der
gdr
ghft
ytut
lkrt
rtrt
tyrt
SQL> declare
2 l_order_by VARCHAR2 (100);
3 l_test varchar2(10);
4 TYPE TEST IS REF CURSOR;
5 c_TEST test;
6 L_string VARCHAR2(2000):= 'select * from t order by :pi_order';
7 begin
8 l_order_by := ' t DEsc';
9 open c_test for l_string using l_order_By;
10 loop
11 fetch c_test into l_test;
12 exit when c_TEST%notfound;
13 dbms_output.put_line (l_test);
14 end loop;
15 close c_test;
16 end;
17 /
sdf
der
gdr
ghft
ytut
lkrt
rtrt
tyrt
PL/SQL procedure successfully completed.
Cheers
Ram KanalaMy order by clause will be constructed based on the selected fields in the formDoes this look like you need ?
SQL> var so number
SQL> exec :so := 1;
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> declare
2 rc sys_refcursor;
3 type emprec is table of emp%rowtype index by pls_integer;
4 erec emprec;
5 begin
6
7 open rc for 'select * from emp order by ' ||
8 'decode(:p,1,ename,2,deptno,3,sal,null),' ||
9 'decode(:p,4,ename,5,deptno,6,sal,null) desc' using :so,:so;
10 fetch rc bulk collect into erec;
11 close rc;
12
13 for i in 1..erec.count loop
14 dbms_output.put_line('Ename = ' || erec(i).ename || ', deptno = ' || erec(i).deptno || ', sal
= ' || erec(i).sal);
15 end loop;
16 end;
17 /
Ename = ADAMS, deptno = 20, sal = 1100
Ename = ALLEN, deptno = 30, sal = 1600
Ename = BLAKE, deptno = 30, sal = 2850
Ename = CLARK, deptno = 10, sal = 2450
Ename = FORD, deptno = 20, sal = 3000
Ename = JAMES, deptno = 30, sal = 950
Ename = JONES, deptno = 20, sal = 2975
Ename = KING, deptno = 10, sal = 5000
Ename = MARTIN, deptno = 30, sal = 1250
Ename = MILLER, deptno = 10, sal = 1300
Ename = SCOTT, deptno = 20, sal = 3000
Ename = SMITH, deptno = 20, sal = 800
Ename = TURNER, deptno = 30, sal = 1500
Ename = WARD, deptno = 30, sal = 1250
PL/SQL procedure successfully completed.
SQL> exec :so := 2
PL/SQL procedure successfully completed.
SQL> /
Ename = CLARK, deptno = 10, sal = 2450
Ename = KING, deptno = 10, sal = 5000
Ename = MILLER, deptno = 10, sal = 1300
Ename = JONES, deptno = 20, sal = 2975
Ename = FORD, deptno = 20, sal = 3000
Ename = ADAMS, deptno = 20, sal = 1100
Ename = SMITH, deptno = 20, sal = 800
Ename = SCOTT, deptno = 20, sal = 3000
Ename = WARD, deptno = 30, sal = 1250
Ename = TURNER, deptno = 30, sal = 1500
Ename = ALLEN, deptno = 30, sal = 1600
Ename = JAMES, deptno = 30, sal = 950
Ename = BLAKE, deptno = 30, sal = 2850
Ename = MARTIN, deptno = 30, sal = 1250
PL/SQL procedure successfully completed.
SQL> exec :so := 5;
PL/SQL procedure successfully completed.
SQL> /
Ename = BLAKE, deptno = 30, sal = 2850
Ename = TURNER, deptno = 30, sal = 1500
Ename = ALLEN, deptno = 30, sal = 1600
Ename = MARTIN, deptno = 30, sal = 1250
Ename = WARD, deptno = 30, sal = 1250
Ename = JAMES, deptno = 30, sal = 950
Ename = SCOTT, deptno = 20, sal = 3000
Ename = JONES, deptno = 20, sal = 2975
Ename = SMITH, deptno = 20, sal = 800
Ename = ADAMS, deptno = 20, sal = 1100
Ename = FORD, deptno = 20, sal = 3000
Ename = KING, deptno = 10, sal = 5000
Ename = MILLER, deptno = 10, sal = 1300
Ename = CLARK, deptno = 10, sal = 2450
PL/SQL procedure successfully completed.Rgds. -
Does anyone know how or where to enter the ORDER BY clause when using a SQL data source in a load rule? I would prefer to do the ordering in the load rule rather than creating a view on the DB side to do this if at all possible.
According to the documentation "The SQL Statement box in the Open SQL Data Sources dialog box provides Select, From, and Where text boxes that help you write SQL queries. You can specify multiple data sources, filter the display of records, and specify how records displayed in Data Prep Editor are ordered and grouped." but I haven't found anywhere where you can order or group data. I don't need to be able group data I just want to be able to order my data to make the load process as efficient as possible.
I do sort the data when inserting it into the load table but Oracle doesn't necessarily insert the records into the table based on their order when going in.
We are currently on Essbase 11.1.1.3 and Oracle 11g.
Any help is greatly apprecied.Well DUH! Everyone knows that! :-)
Not sure why I never tried that, but it works. They need to update the documentation to say this, but we all know that will never happen.
Thanks again! -
Designer BC4J Generator Extension creates wrong order by clause
Hi,
Just tried out the new Designer BC4J Generator Extension and it creates a wrong order by clause. I have a List of values in Designer with one table usage. There is a bound item based on a columnname CODE and has the name L_CODE. The order by sequence is 1 and ascending. After generating it in JDeveloper the generated query is:
SELECT PrjEntityUsage.CODE,
PrjEntityUsage.NAAM
FROM X_PROJECTEN PrjEntityUsage
ORDER BY LCODE
So there are two problems:
1) If the name property in designer is different from the column name the select in the SQL query in JDeveloper needs an alias with the name property from Designer.
2)If there is an _ in the name it is ignored in the order by clause in JDeveloper.
I do not know where to report the bug so i placed it here.
Regards,
Andre JochemsAndre,
Thanks for notifying us about these. There are indeed bugs which we have logged and scheduled for a fix.
Regards
Sue Harper -
Sorting technique used by oracle for "order by" clause.
Hi All,
it could be very help to me if you provide some information about sorting technique used by oracle engine for order by clause.
Issue i am facing :
Table : xx
Line Date
1 05-06-2013 00:00:00
2 05-06-2013 00:00:00
when we query above table using order by date, it is returning line 2 prior to line 1. we would like to know why it is returning line 2 first?
Regards,
Ram>
it could be very help to me if you provide some information about sorting technique used by oracle engine for order by clause.
>
Well ok - but be warned that many people wind up being sorry they ask that question. Hopefully Hemant's answer is what you really wanted.
See 'Linguistic Sorting and String Searching' in the Oracle® Database Globalization Support Guide
http://docs.oracle.com/cd/B28359_01/server.111/b28298/ch5lingsort.htm
Sorting will be controlled by the settings of NLS_LANGUAGE, NLS_SORT and NLS_COMP.
Here is the doc page for NLS_SORT
http://docs.oracle.com/cd/B28359_01/server.111/b28298/ch3globenv.htm#i1008393
>
NLS_SORT specifies the type of sort for character data. It overrides the default value that is derived from NLS_LANGUAGE.
NLS_SORT contains either of the following values:
NLS_SORT = BINARY | sort_name
BINARY specifies a binary sort. sort_name specifies a linguistic sort sequence.
>
And the one for NLS_COMP
http://docs.oracle.com/cd/B28359_01/server.111/b28298/ch3globenv.htm#i1008458
>
The value of NLS_COMP affects the comparison behavior of SQL operations.
You can use NLS_COMP to avoid the cumbersome process of using the NLSSORT function in SQL statements when you want to perform a linguistic comparison instead of a binary comparison. When NLS_COMP is set to LINGUISTIC, SQL operations perform a linguistic comparison based on the value of NLS_SORT. A setting of ANSI is for backward compatibility; in general, you should set NLS_COMP to LINGUISTIC when you want to perform a linguistic comparison. -
Cursor ORDER BY Clause Changing Row Count In BULK COLLECT ... FOR LOOP?
Oracle 10g Enterprise Edition Release 10.2.0.4.0 running on Windows Server 2003
Oracle Client 10.2.0.2.0 running on Windows 2000
I have some PL/SQL code that's intended to update a column in a table based on a lookup from another table. I started out by testing it with the UPDATE statement commented out, just visually inspecting the DBMS_OUTPUT results to see if it was sane. During this testing I added/changed the cursor ORDER BY clause to make it easier to read the output, and saw some strange results. I've run the code 3 times with:
1. no ORDER BY clause
2. ORDER BY with two columns (neither indexed)
3. ORDER BY with one column (not indexed)
and get three different "rows updated" counts - in fact, when using the ORDER BY clauses it appears that the code is processing more rows than without either ORDER BY clause. I'm wondering why adding / changing an ORDER BY <non-indexed column> clause in a cursor would affect the row count?
The code structure is:
TYPE my_Table_t IS TABLE OF table1%ROWTYPE ;
my_Table my_Table_t ;
CURSOR my_Cursor IS SELECT * FROM table1 ; -- initial case - no ORDER BY clause
-- ORDER BY table1.column1, table1.column2 ; -- neither column indexed
-- ORDER BY table1.column2 ; -- column not indexed
my_Loop_Count NUMBER := 0 ;
OPEN my_Cursor ;
LOOP
FETCH my_Cursor BULK COLLECT INTO my_Table LIMIT 100 ;
EXIT WHEN my_Table.COUNT = 0 ;
FOR i IN 1..my_Table.COUNT LOOP
my_New_Value := <call a pkg.funct to retrieve expected value from another table> ;
EXIT WHEN my_New_Value IS NULL ;
EXIT WHEN my_New_Value = <an undesirable value> ;
IF my_New_Value <> my_Table(i).column3 THEN
DBMS_OUTPUT.PUT_LINE( 'Changing ' || my_Table(i).column3 || ' to ' || my_New_Value ) ;
UPDATE table1 SET column3 = my_New_Value WHERE column_pk = my_Table(i).column_pk ;
my_Loop_Count := my_Loop_Count + 1 ;
END IF ;
END LOOP ;
COMMIT ;
END LOOP ;
CLOSE my_Cursor ;
DBMS_OUTPUT.PUT_LINE( 'Processed ' || my_Loop_Count || ' Rows ' ) ;Hello (and welcome),
Your handling the inner cursor exit control is suspect, which will result in (seemingly) erratic record counts.
Instead of:
LOOP
FETCH my_Cursor BULK COLLECT INTO my_Table LIMIT 100 ;
EXIT WHEN my_Table.COUNT = 0 ;
FOR i IN 1..my_Table.COUNT LOOP
my_New_Value := <call a pkg.funct to retrieve expected value from another table> ;
EXIT WHEN my_New_Value IS NULL ;
EXIT WHEN my_New_Value = <an undesirable value> ;
IF my_New_Value my_Table(i).column3 THEN
DBMS_OUTPUT.PUT_LINE( 'Changing ' || my_Table(i).column3 || ' to ' || my_New_Value ) ;
UPDATE table1 SET column3 = my_New_Value WHERE column_pk = my_Table(i).column_pk ;
my_Loop_Count := my_Loop_Count + 1 ;
END IF ;
END LOOP ;
COMMIT ;
END LOOP ;Try this:
LOOP
FETCH my_Cursor BULK COLLECT INTO my_Table LIMIT 100 ;
FOR i IN 1..my_Table.COUNT LOOP
my_New_Value := <call a pkg.funct to retrieve expected value from another table> ;
EXIT WHEN my_New_Value IS NULL ;
EXIT WHEN my_New_Value = <an undesirable value> ;
IF my_New_Value my_Table(i).column3 THEN
DBMS_OUTPUT.PUT_LINE( 'Changing ' || my_Table(i).column3 || ' to ' || my_New_Value ) ;
UPDATE table1 SET column3 = my_New_Value WHERE column_pk = my_Table(i).column_pk ;
my_Loop_Count := my_Loop_Count + 1 ;
END IF ;
EXIT WHEN my_Cursor%NOTFOUND;
END LOOP ;
END LOOP ;
COMMIT ;Which also takes the COMMIT outside of the LOOP -- try to never have a COMMIT inside of any LOOP.
Additionally, not too sure about these:
my_New_Value := <call a pkg.funct to retrieve expected value from another table> ;
EXIT WHEN my_New_Value IS NULL ;
EXIT WHEN my_New_Value = <an undesirable value> ;Any one of those EXITs will bypass your my_Loop_Count increment.
Edited by: SeánMacGC on Jul 9, 2009 8:37 AM
Had the cursor not found in the wrong place, now corrected.
Maybe you are looking for
-
Bought a new computer and want to recover old iTunes?
Our old computer we had blew its power supply, so we bought a new one. We have a Free Agent Go Flex Drive that saved all our old files on it, and I wanted to recover my old iTunes program as is. I am a statistician, and one of my projects I was hired
-
Handling of pictures changed? Item values not properly updated in Beta 3?
I had created an application which allows to upload an image for a row, basically based on what's done in the demo app for the products table. Today, I exported the app and then uninstalled Beta 2 and installed Beta 3. Installation finished without a
-
Hi guys, I have a question which I seems to me like it is most probably a bug. If you have a Grid (not a Matrix) and you set oGrid.CollapseLevel = 1 then the following problem occurs. When you use a1 = oGrid.DataTable.GetValue(4, 5).ToString(); oGrid
-
I have the new iPad and iPhone 4s. iMessage has already been set up syncing these two devices before, but I am now logged out on my iMessage on the iPad but still logged in on the iPhone. My question is, if I sign back into the iMessage on my iPad wi
-
Printing Blueprints (Tile)
i am having such difficulty trying to print some blueprints out using Preview. i haven't done this in quite a while but i know that it was at least at one point possible. i want to print a PDF that the actual size is 24" x 36". i want to "tile" the p