Order by cluase
hi i would like to sory by alphabets defined in the order by clause
like for ex
select * from employee
order by ename ----(starting by letter L)
please help
If you want to order by some different sort sequence, one approach is to use a function, eg:
create function ROT13(varchar2 in p_text) return varchar2
is
l_result varchar2(100) := null;
begin
for i in 0..length(p_text)
l_result := l_result ||
case when p_text(i) <= chr(ascii('A') + 13) then chr(ascii(p_text(i)) + 13)
else chr(ascii(p_text(i) - 13)
end;
end loop;
return l_result
end;
select * from employee
order by ROT13(ename)
/This example shows that that names can be sorted based on whatever character sequence you define...
(NB you can calculate ROT13 using a regular expression if you prefer)
HTH
Regards Nigel
Similar Messages
-
Oracle order by cluase on null and identical column values
Can anybody please help me to understand the below things. Thanks in advance.
1. How will be the row ordering when the ORDER BY column contains null values for all the records
2. How will be the row ordering if all the records catains the same values (for the column which is used in order by clause)
3. Whether index is having any impact in the ordering - (mainly in the case of above scenarios)
4. Whether any other db objects will have impact on ordering
Thanks again.
Regards,
ShineShine Haridasan wrote:
1. How will be the row ordering when the ORDER BY column contains null values for all the recordsIt will have the same effect as not ordering
http://download.oracle.com/docs/cd/E11882_01/server.112/e26088/statements_10002.htm#i2171079
>
Without an order_by_clause, no guarantee exists that the same query executed more than once will retrieve rows in the same order.
>
2. How will be the row ordering if all the records catains the same values (for the column which is used in order by clause)Same answer as 1.
3. Whether index is having any impact in the ordering - (mainly in the case of above scenarios)It might.
4. Whether any other db objects will have impact on orderingThey might. -
Hello,
I'm using FORM 6i and writing the data to Excel Report
I'm passing the string to the Order By clause. But the data is not sorted.
Code is as follows:
procedure test (pi_order IN VARCHAR2)
is
type t_data IS REF CURSOR;
c_data t_data;
l_order_by VARCHAR2(200);
type data_rec IS RECORD (col1 NUMBER(2),
col2 VARCHAR2(100));
r_data data_rec;
BEGIN
IF (pi_order_by IS NULL) THEN
l_order_by := NULL;
ELSE
l_order_by := pi_order_by;
END IF;
OPEN c_data for SELECT COL1, COL2 , pkg1.p2(x, y) col3
FROM table_name
WHERE col1 = 1
ORDER BY l_order_by;
LOOP
FETCH c_data INTO r_data;
EXIT WHEN c_data%NOTFOUND;
---- OTHER CODE HERE--
EXCEPTION
END;
So, basically i'm using the REF CURSOR, and passing the order by string dynamically from the Oracle Form. The above procedure is part of PLL.
I have verified running the script at SQL*PLUS, to test if there is any dependency on Forms. But data is not sorted in SQL*PLUS environment also.
And also i'm using a packaged procedure as one column in the cursor query. So I can't use EXEC_SQL, which will parse the total statement once . To me known we can use EXEC_SQL, when we don't have any input parameters to the SQL statement.
Any suggestion on passing Dynamic Order by cluase in any appraoch?
Cheers
RamHi
I researched some and found this.
It looks like you are using 'REF CURSOR WITH STATIC SQL'.
You need REF CURSOR with DYNAMIC SQL ( put it in single quotes ).
Read this article here:
http://www.oracle.com/technology/oramag/oracle/04-may/o34asktom.html
Goto this section and read ---
Cursor or Ref Cursor
At an interview for an Oracle PL/SQL developer job, I was asked to describe the difference between cursor and ref cursor and when you would appropriately use each of these. Could you please tell me the answer?
Well, technically, under the covers, at the most basic level, they are the same. A typical PL/SQL cursor is static by definition. Ref cursors, on the other hand, can be dynamically opened (the query is not known until runtime) or opened by use of a set of static SQL statements, the choice of which is based on logic (an IF/THEN/ELSE block will open one or the other query). For example, the following block of code demonstrates a typical static PL/SQL cursor, cursor C. Also, it shows how you can use a ref cursor (L_CURSOR in this example) to open a query by using dynamic SQL or static SQL:
Declare
type rc is ref cursor;
cursor c is select * from dual;
l_cursor rc;
begin
if (to_char(sysdate,'dd') = 30)
then
-- ref cursor with dynamic sql
open l_cursor for
'select * from emp';
elsif (to_char(sysdate,'dd') = 29)
then
-- ref cursor with static sql
open l_cursor for
select * from dept;
else
-- with ref cursor with static sql
open l_cursor for
select * from dual;
end if;
-- the "normal" static cursor
open c;
end;
Message was edited by:
brian952 -
Using ORDER BY in Oralce 7.3.x
Hi,
I need to create a view using ORDER BY cluase in Oralce 7.3.x.
(I guess ORDER BY Clause is not supported in Oracle 7.3).
Please Let me know any workaround for this.
Thanks in advance.
-GopiHave you tried a simple select statement using ORDER BY? ORDER BY is definitely supported in 7.3.
I haven't written a VIEW using ORDER BY, but what I remember most about VIEWS is that you have to use COLUMN ALIASES when referencing them in other statements. Maybe you need to assign column aliases and then peform the order by.... I don't know this for sure though.
PS views are slow enough as it is. Why not just select the data through the view, and then when you run queries off of the view, then perform the ORDER BY? -
Create table Example as
select 124 Emp_Id, 350 Amt from dual
Union select 123 Emp_Id, 200 Amt from dual
Union select 124 Emp_Id, 350 Amt from dual
Union select 125 Emp_Id, 250 Amt from dual
Union select 126 Emp_Id, 250 Amt from dual
Union select 127 Emp_Id, 100 Amt from dual
I am having the dataet like
Emp_Id TotalAmt
123 200
124 350
125 250
126 250
127 100
with query
SELECT EMP_ID,Amt,
SUM (Amt) OVER ( ORDER BY Amt DESC) AS total
FROM Example
WHERE Amt > 0 ;
query return
Emp_Id Amt Total
124 350 350
*125 250 850*
*126 250 850*
123 200 1050
127 100 1150
in this second row total amount added with third row.
Excepted Output is
Emp_Id Amt Total
124 350 350
125 250 600
126 250 850
123 200 1050
127 100 1150Hi,
Add more expressions to the analytic ORDER BY cluase so that there are no ties in the ordering.
For example:
SELECT EMP_ID,Amt,
SUM (Amt) OVER ( ORDER BY Amt DESC
, emp_id -- Added
) AS total
FROM Example
WHERE Amt > 0 ;"SUM (x) OVER (ORDER BY y)" means you will get the same result for each value of y, and a different result only if y is different.
If there is nothing else unique about each row, you can use ROWNUM.
This behavior is a consequence of the default windowing clause "RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW". "RANGE" refers to the value(s) in the ORDER BY clause. SBH (above) gave another solution which explicitly changes the wondowing from RANGE to ROWS, meaning you can get a separate result for each row, regardless of the value(s) of y.
Edited by: Frank Kulash on Jul 12, 2010 5:40 AM -
Cant export worksheet when connected to MS SQL Server 2000
SQL Developer version: 1.5.1
Hello,
I have a connection to an SQL Server 2000 database. I can select data with an "order by" clause but I receive an error when I attempt to do an "Export --> xls" on the worksheet data. I dont think this was a problem prior to SQL Developer 1.5.
No export error occurs if the "order by" cluase is removed.
Here is an example of a select stmt that generates the error.
select LastName, FirstName, EmployeeID
from northwind.dbo.employees
order by LastName, FirstName
Here are the error details:
First dialog box error text: The ORDER BY clause is invalid in views, inline functions, derived tables, and subqueries, unless TOP is also specified.
Second dialog box error text (after clicking Details button):
java.lang.NullPointerException
at oracle.dbtools.raptor.dialogs.export.ColumnPanel.addColumnsToTree(ColumnPanel.java:85)
at oracle.dbtools.raptor.dialogs.export.ColumnPanel.showPanel(ColumnPanel.java:61)
at oracle.dbtools.raptor.dialogs.export.ColumnPanel.<init>(ColumnPanel.java:45)
at oracle.dbtools.raptor.dialogs.actions.TableExportAction.showPanels(TableExportAction.java:297)
at oracle.dbtools.raptor.dialogs.actions.TableExportAction.launchDlg(TableExportAction.java:197)
at oracle.dbtools.raptor.format.ui.ExportContextMenuListener$1.actionPerformed(ExportContextMenuListener.java:137)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.AbstractButton.doClick(AbstractButton.java:302)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1000)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1041)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
The table selected from is not a view, inline function, derived table, or subquery (as the error suggests). I can work around the problem by adding "TOP nnnn" to the select statement but this shouldn't be required.
Any suggestions?
Thanks.I confirmed that this is not a problem in SQL Developer 1.2.1.32.13. It started with 1.5. In 1.5.1.5540 I can successfully run the query and see the output in the Results tab. Only the Export has the error.
-
Sorting a varchar2 field in Report 10g
Dear all,
i have a table of salaries, which have a field RANK_NO VARACHAR2.
I create a report and order it by RANK_NO. but it is not ordering what i want to be. it is ordered like
1
10
11
2
3
4
5
6
7
8
9
then i use the order by cluase like this,
ORDER BY TO_NUMBER(REGEXP_SUBSTR(RANK_NO,'[0-9]+')),
REGEXP_SUBSTR(UPPER(RANK_NO),'[A-Z]+').
but the result is same .
how to sort a varchar2 field in reports?
Thanks & RegardsHi Muhammad,
Use the following Order By clause hope it helps you,
ORDER BY TO_NUMBER (REPLACE (RANK_NO, LTRIM (RANK_NO, '0123456789'))), RANK_NO
Best Regards
Arif Khadas -
Hi All,
I have a table with following structure initially without any data.
Name Null? Type
DTL_MSG_ID NOT NULL NUMBER(9)
MODULE_TYPE NOT NULL VARCHAR2(2 CHAR)
MSG_ID NOT NULL NUMBER(9)
WHSE NOT NULL VARCHAR2(3 CHAR)
MSG_TYPE VARCHAR2(10 CHAR)
MSG VARCHAR2(1000 CHAR)
MSG_OBJ BLOB
CREATE_DATE_TIME DATE
MOD_DATE_TIME DATE
MISC1 VARCHAR2(20 CHAR)
MISC2 VARCHAR2(20 CHAR)
VERSION_ID NOT NULL NUMBER(6)
Below is the list of index created on table columns.
SQL> select index_name,table_name,column_name,column_position from user_ind_columns where table_name='E_WMS_EVNT_MSG_DTL' order by 1,4;
INDEX_NAME TABLE_NAME COLUMN_NAME COLUMN_POSITION
E_WMS_EVNT_MSG_DTL_IND_1 E_WMS_EVNT_MSG_DTL MSG_ID 1
FK_E_WMS_EVNT_MSG_DTL E_WMS_EVNT_MSG_DTL MSG_ID 1
FK_E_WMS_EVNT_MSG_DTL E_WMS_EVNT_MSG_DTL MODULE_TYPE 2
FK_E_WMS_EVNT_MSG_DTL E_WMS_EVNT_MSG_DTL WHSE 3
FK_E_WMS_EVNT_MSG_DTL E_WMS_EVNT_MSG_DTL DTL_MSG_ID 4
PK_E_WMS_EVNT_MSG_DTL E_WMS_EVNT_MSG_DTL DTL_MSG_ID 1
PK_E_WMS_EVNT_MSG_DTL E_WMS_EVNT_MSG_DTL MODULE_TYPE 2
PK_E_WMS_EVNT_MSG_DTL E_WMS_EVNT_MSG_DTL WHSE 3
8 rows selected.
Generate Explain plan using order by cluase in query
SQL> explain plan for SELECT wmsevntmsg0_.module_type AS module2_107_, wmsevntmsg0_.dtl_msg_id AS dtl1_107_, wmsevntmsg0_.msg_type AS msg5_107_, wmsevntmsg0_.msg AS msg107_, wmsevntmsg0_.msg_id AS msg4_107_, wmsevntmsg0_.msg_obj AS msg7_107_, wmsevntmsg0_.create_date_time AS create8_107_, wmsevntmsg0_.mod_date_time AS mod9_107_, wmsevntmsg0_.whse AS whse107_, wmsevntmsg0_.misc1 AS misc10_107_, wmsevntmsg0_.misc2 AS misc11_107_
2 FROM e_wms_evnt_msg_dtl wmsevntmsg0_ WHERE wmsevntmsg0_.msg_id = :1 AND wmsevntmsg0_.module_type = :2 AND wmsevntmsg0_.whse = :3 ORDER BY wmsevntmsg0_.msg_id,wmsevntmsg0_.dtl_msg_id ASC
3 ;
Plan is using the PK index to fetch data.
Plan hash value: 2070795294
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 514 | 306K| 6425 (1)| 00:01:18 |
|* 1 | TABLE ACCESS BY INDEX ROWID| E_WMS_EVNT_MSG_DTL | 514 | 306K| 6425 (1)| 00:01:18 |
|* 2 | INDEX FULL SCAN | PK_E_WMS_EVNT_MSG_DTL | 253K| | 177 (2)| 00:00:03 |
Explain plan without using Order by in query.
SQL> explain plan for SELECT wmsevntmsg0_.module_type AS module2_107_, wmsevntmsg0_.dtl_msg_id AS dtl1_107_, wmsevntmsg0_.msg_type AS msg5_107_, wmsevntmsg0_.msg AS msg107_, wmsevntmsg0_.msg_id AS msg4_107_, wmsevntmsg0_.msg_obj AS msg7_107_, wmsevntmsg0_.create_date_time AS create8_107_, wmsevntmsg0_.mod_date_time AS mod9_107_, wmsevntmsg0_.whse AS whse107_, wmsevntmsg0_.misc1 AS misc10_107_, wmsevntmsg0_.misc2 AS misc11_107_
FROM e_wms_evnt_msg_dtl wmsevntmsg0_ WHERE wmsevntmsg0_.msg_id = 115952 AND wmsevntmsg0_.module_type = '10' AND wmsevntmsg0_.whse = '61' 2 ;
I believe its now using the correct index to fetch data.
Plan hash value: 461238093
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 611 | 1 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| E_WMS_EVNT_MSG_DTL | 1 | 611 | 1 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | FK_E_WMS_EVNT_MSG_DTL | 1 | | 1 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------------Please suggest, I am wondering why oracle using PK index in first scenario. Table has the updated stats.
Thanks in Advanced.
BhupinderHi Oscar,
Thanks a lot for your reply.
But the problem here is why Oracle is not using the index I created for all the columns i WHERE condition, instead it is looking for data from Primary Key index.
Regards,
Bhupinder -
Post Author: gsrk
CA Forum: General
I am creating a Pack Slip using a crystal repot and i am running in to a problem in the Print Order. I've created a simple report to show a carton items for a shipment and the report is grouped by carton ID (datatype uniqueidentifier) to keep all the carton items together in a report. New reports are created for each carton. I've another column(sortkey) in the report or for each carton and i should print the report in that order only.
How can i print the report in the sortkey order? I can't simply add the sortkey to the record sort expression becuase of group by clause. Group by cluase is automatically becoming the top level sort.
Is there a way to sort the report(based on the sortkey column on the report) just before printing.Post Author: V361
CA Forum: General
Group sort expert may help, give it a look, you will need to have a sum (which you can suppress) but if you did (Max item number) or something, you can then sort off the sum in Group sort expert..... -
Sql order in where clause...
i want to ask about
how oracle deal with the order of where cluase
as in select * from where tname = 'EMP'
and tabtype = 'TABLE'
oracle strat from down (tabtype) or from up (tname)
and there is difference between this order
if the where clause contains (and) or (or)
i always need to know this thing but no place i found say about it in strait way..
thanks..
nullI am sorry for the late reply.
I guess that ORDER doesn't matter in the WHERE clause. We should take 2 cases:
1) Say, in a WHERE clause, U have a limiting condition on one of the indexed field of the table, then definitely that will be executed first and the result set will be passed to the other conditions.
2) Say, in the WHERE clause, U don't have any indexed field then ORACLE will go for cost based execution. In this case, it will first go to the column, for which the SELECTIVITY is the highest ( ORACLE does so internally ) and then the result set is passed to the other conditions.
Hope this helps.
Correct me, if I am wrong. -
Open Sales Orders Backlog Report
Hi All: I have been asked to create Backlog reports for Open Sales Orders and the revenue amounts for the next 15days, one month, 2 months, 3 months 6 months etc etc. Does anyone know of a standard report that covers that and the business content and cues that go with it?
Thanks in advance
BobbyI haven't seen any standard content for it, with my client, we actually developed the open order report from Item extractor and status extractor. And compare the created on date to the system data to see how long it has been opened for and put it in different buckets.
Hopes that help.
thanks.
Wond. -
Need a report on open order aging. The open order means not delivered or partially delivered. Further with days range since its open or not fully delievered i.e. > 15 days or 15-30 days and so on.
The standard transactions Va05 and VL10c can provide the list but dont provide any aging info.
Kindly help in this.
thanks
anuDear Anu
1. First in VA05 you can use Variants and get the report as desired by you,create one variant and use Filter along with greater then, less then (all are in selection option icon)
This way you can create three variants.
But limitation is dates has to be manually changed in variants each time.
2. Try this Tcodes
S_ALR_87014387 Display Document Flow
S_ALR_87014392 Display Document Flow
This reports will give you document flow run report with ticking checkbox for sales order,delivery and goods isse then after getting the list expand all (Shift+F12)
you will get the quantities for sales order, and what is delivered and what is issued.
3. As such if you want exactly the report you can take help of ABAP to create the ALV
4. Report or create Queries in SQVI , or else create MCSI report
Regards
Jitesh -
Necessary Fields For Creation of Service PO of Order Type Relaese Order.
Dear Guru,
I have encountered an issue which i am trying to resolve...
My this requirment will seem little okward the way i am asking but i have no way...
The issue is I have to create a Service PO of Order type Release order (RO) using BAPI Function Module .BAPI_PO_CREATE1.
The service PO should be of multiple Item and services for particular line item should be multiple.
When I am creating this using ME21 or ME21N i am facing no issue.
But when i am using BAPI Function Module .BAPI_PO_CREATE1
i am getting following errors ;; The error which i am getting as below
T ID NUM MESSAGE
E BAPI 001 No instance of object type PurchaseOrder has been created. External reference:
E MEPO 000 Purchase order still contains faulty items
E SE 029 Please maintain services or limits
E SE 140 Service HIRING OF LCD: please specify unit of measure
But I am failing to findout in which field services or limits and unit of measurement have to maintain.
What are the necessary fields have to pass in Bapi import parameter and the table i am unable to findout.
Please show some way how to resolve this or give me some guideline to resolve this
Dear Moderator request your kind intervane to move this qurry into correct forum if i have asked this in wrong forum
Thanks and regards
saifur rahamanHi Saifur
Can you please elaborate how did you resolve the issue we are also facing same problem when we are trying to create the PO for service items through SRM it is throwing same error while creating the PO in SAP.
Email Id : [email protected]
Thank you in advance!!
Regards
Deepika -
How to restrict manual changing of free goods in sales order
Hi ,
Goodmorning ,
We have some requirement : In sales order free goods quantity determination by system should not be allowed to change manually , where can we do this ?
Looking for your inputs
Thanks and regards
VenkatAs per SAP Standard, when the main Item quantity is changed, the Free Goods are redetermined. In this case any manual changes to Free Goods Quantities are lost.
But your requirement is for restricting the Chages of the Quantity of Free Goods Correct?
I believe there is no SAP standard solution for this. You will have to apply a User Exit, which will check the Item category of each LIne item & if it is free goods (TANN) then changes are not permitted.
Hope this helps.
Thanks,
Jignesh Mehta -
Query help needed for Sales order panel user field query.
I have a user defined form field on sales order row level called = U_DEPFEEAMT
1, I would like this field to get the value from a field on this sales order row level multiplied by what is in point 2 below. The details of field in point 1 is :
Form=139, item=38, pane=1, column=10002117, and row=1
2. The contents in field 1 should be multiplied by a value coming from another user field linked to OITM master item.
The details of user field attached to OITM is :
OITM.U_DepositFeeON
Appreciate your help.
Thank you.Try this one:
SELECT T0.U_DepositFeeON*$[$38.10002117.number\]
FROM dbo.OITM T0
WHERE T0.ItemCode = $[$38.1.0\]
Thanks,
Gordon
Maybe you are looking for
-
Good Consignation Return in MM
Hi, Does anybody know how could I return goods in a vendor consigment stock? I know with type movement 122 is likely to do so, but I have to enter mandatory the material dpocument, I would like to count on a way to return all the goods in consignatio
-
Safari 5.0.3 keeps crashing - help please (STILL!)
Here is the crash report; I am still having problems with this issue. I followed the advice and found an updated afloat app. It happened again, so I re-followed advice from a few days ago on here and deleted afloat again. Unfortunately, it is still h
-
Wanna send integer value by reference in function
I wanna send integer value by reference thro' function. Here's the code going. class test basic b = new basic(); test() test2.Fuction(b.value); class basic int value; basic(){} //////here i want value to be affected by Function.I don't want it affect
-
Hi Guys and Girls, How can I disable a user in OID, currently the only OID access is available via the Oracle Directory Manager(ODM) or command line, apparently self service is unavailable . The Nearest thing I could find was the orclIsEnabled attrib
-
Does iphone support bluetooth MAP technology
Does iphone support bluetooth MAP technology? I bought a 2015 Jeep Grand Cherokee using Uconnect and it won't read my text message out loud.