Order of results
Given the following statement:
SELECT ID, NAME, DESC
FROM MYTABLE
WHERE ID IN (4321,1234,2314,3214)
I would like the resulting rows to be returned in the same order as they are in the IN statement e.g. 4321, 1234, 2314 and 3214 not what it gives me which is: 1234,2314,3214,4321.
Any ideas? Thanks in advance.
Peter
I don't think you can do this unless and otherwise you have certain logic or other data in the same table that helps you in getting that same sort order.
Thanks,
Dharmesh Patel
Similar Messages
-
I'm looking to do some custom ordering of results. Lets say I have a table:
CREATE TABLE test_ordering (
ID NUMBER PRIMARY KEY,
NAME VARCHAR2(40));With some records..
INSERT INTO test_ordering (id, name) VALUES (1,'Apartment 1');
INSERT INTO test_ordering (id, name) VALUES (2,'Apartment 5');
INSERT INTO test_ordering (id, name) VALUES (3,'Apartment 9');
INSERT INTO test_ordering (id, name) VALUES (4,'Apartment 10');
INSERT INTO test_ordering (id, name) VALUES (5,'Apartment 11');
INSERT INTO test_ordering (id, name) VALUES (6,'Apartment 13');
INSERT INTO test_ordering (id, name) VALUES (7,'Apartment 21');
INSERT INTO test_ordering (id, name) VALUES (8,'Unit 1');
INSERT INTO test_ordering (id, name) VALUES (9,'Unit 9');
INSERT INTO test_ordering (id, name) VALUES (10,'Unit 10');
INSERT INTO test_ordering (id, name) VALUES (11,'Unit 15');
INSERT INTO test_ordering (id, name) VALUES (12,'Unit 31');
COMMIT;So if I select and order by name I get
SQL> SELECT * FROM test_ordering ORDER BY name;
ID NAME
1 Apartment 1
4 Apartment 10
5 Apartment 11
6 Apartment 13
7 Apartment 21
2 Apartment 5
3 Apartment 9
8 Unit 1
10 Unit 10
11 Unit 15
12 Unit 31
9 Unit 9
17 rows selected.Now I would like the results in a slightly different order.
ID NAME
1 Apartment 1
2 Apartment 5
3 Apartment 9
4 Apartment 10
5 Apartment 11
6 Apartment 13
7 Apartment 21
8 Unit 1
9 Unit 9
10 Unit 10
11 Unit 15
12 Unit 31So if I want to order by the alpha characters and then by the numeric portion of the NAME.
Any ideas? I've been trying a few things using REGEXP_SUBSTR, but its not working out...
SELECT * FROM test_ordering ORDER BY REGEXP_SUBSTR(name,'[[:alpha:]]'), REGEXP_SUBSTR(name,'[[:digit:]]');Ok, actually I can think of a few more examples now that need a different solution.
Let say we have
DROP TABLE test_ordering;
CREATE TABLE test_ordering (
ID NUMBER PRIMARY KEY,
NAME VARCHAR2(40));
CREATE UNIQUE INDEX test_ordering_sanity_check ON test_ordering(name);
INSERT INTO test_ordering (id, name) VALUES (1,'Apartment 21');
INSERT INTO test_ordering (id, name) VALUES (2,'Apartment 5');
INSERT INTO test_ordering (id, name) VALUES (3,'Apartment 1');
INSERT INTO test_ordering (id, name) VALUES (4,'Apartment 11');
INSERT INTO test_ordering (id, name) VALUES (5,'Apartment 9');
INSERT INTO test_ordering (id, name) VALUES (6,'Apartment 10');
INSERT INTO test_ordering (id, name) VALUES (7,'Apartment 13');
INSERT INTO test_ordering (id, name) VALUES (8,'Unit 1');
INSERT INTO test_ordering (id, name) VALUES (9,'Unit 9');
INSERT INTO test_ordering (id, name) VALUES (10,'Unit 10');
INSERT INTO test_ordering (id, name) VALUES (11,'Unit 15');
INSERT INTO test_ordering (id, name) VALUES (12,'Unit 1a');
INSERT INTO test_ordering (id, name) VALUES (13,'Unit 1b');
INSERT INTO test_ordering (id, name) VALUES (14,'Unit 5');
INSERT INTO test_ordering (id, name) VALUES (15,'Unit 15d');
INSERT INTO test_ordering (id, name) VALUES (16,'Unit 11');
INSERT INTO test_ordering (id, name) VALUES (17,'Unit 11a');
INSERT INTO test_ordering (id, name) VALUES (18,'Unit 20');
INSERT INTO test_ordering (id, name) VALUES (19,'Unit 20a');
INSERT INTO test_ordering (id, name) VALUES (20,'Unit 20b');
INSERT INTO test_ordering (id, name) VALUES (21,'Unit 11b');
INSERT INTO test_ordering (id, name) VALUES (22,'Unit A');
INSERT INTO test_ordering (id, name) VALUES (23,'Unit B');
INSERT INTO test_ordering (id, name) VALUES (24,'Unit C');
COMMIT;And I'd like that ordered like this...
ID NAME
3 Apartment 1
2 Apartment 5
5 Apartment 9
6 Apartment 10
4 Apartment 11
7 Apartment 13
1 Apartment 21
8 Unit 1
10 Unit 10
16 Unit 11
17 Unit 11a
21 Unit 11b
11 Unit 15
15 Unit 15d
12 Unit 1a
13 Unit 1b
14 Unit 5
9 Unit 9
18 Unit 20
19 Unit 20a
20 Unit 20b
22 Unit A
23 Unit B
24 Unit CThanks. -
SQ02 - Issue on a infoset which doesn't order the results
Hello to everyone,
I'm experiencing some issues when creating a Infoset and implementing the relative query, as the results aren't ordered by the first field which is the Primary Key of the first table of the join which composes the Infoset. Here are the details:
I've created a query on the following JOIN:
DFKKKO >> ERDB >> ERCHC
Plus, i've implemented some code to mess the query so that it returns some additional field with custom values.
One of the target of this query is to have the output sorted by DFKKKO-OPBEL, which is the key of DFKKKO, but it seems that when I ask for a big amount of data, the output isn't sorted at all.
Is there a way to ask the infoset via SQ02 or the query via SQ01 or some coding within the additional fields, etc.. to sort the output by certain fields? In this particular case, I'd like to order for the first output field which is the key of the first table of the join.
Thank you very much in advance.
Best regards,
ClaudioHello Arbind,
thanks for your quick reply, i'll be start testing it immediately, but I've seen that apparently this method sorts the extraction after it is done. Even though it could appear strange, I should keep a progressive number while elaborating each row of the selection, so for example I'll count in this way:
record 1 - counter 1
record 2 - counter 2
record n - counter n.
Actually, my problem with sort was that record 1 has the key 1, record 2 has the key 2018, record 3 has the key 58, so the counter 2 points to a wrong record.
Your solution is really helpful, but do you know actually if I can put a sort or order by directly in the main selection by indicating something somewhere?
If it isn't possible, i'll start thinking of an alternative solution for the counter problem (which is a counter of the billing rows in a document, instead of a simple counter).
Thanks also to Christian for the answer!
Thank you very much.
Kind regards,
Claudio
Edited by: Claudio CPU on Feb 22, 2011 12:14 PM -
In what order are results from gather_database_stats 'LIST AUTO' returned?
11.2.0.1.0. The results from the following seem to have some order to them (groupings of objects returned alphabetically). Didn't see anywhere in the docs to indicate what's getting returned in what order (empty stats, stale stats). I looked at table stats for one at the top of the list and it looks like the table had stats gathered recently, so i'm confused. I ran this from sqlplus:
set serveroutput on size unlimited;
DECLARE
ObjList dbms_stats.ObjectTab;
BEGIN
dbms_stats.gather_database_stats(objlist=>ObjList, options=>'LIST AUTO');
FOR i in ObjList.FIRST..ObjList.LAST
LOOP
dbms_output.put_line(ObjList(i).ownname || '.' || ObjList(i).ObjName || ' ' || ObjList(i).ObjType || ' ' || ObjList(i).partname);
END LOOP;
END;
/Enlightened answer: "So bug off and work with what you get"
The report was run on a database that doesnt have any auto stat collection occurring. There are 4000 items returned in the list. One of the items looks ok when you look at dba_tables (row count correct and last_analyzed recent). Just trying to figure out why this table was included in the list. Might be the way stats collected for it isn't updating some mechanism that gather_database_stats is utilizing. From what I can tell the order is 1) tables with no stats, 2) indexes with no stats, 3) stale tables. -
Modification to Sales Office in Sales Order not resulting in Delta in BI
Hello,
I am experiencing a issue in delta update to 0SD_C03.
Any changes to the Sales Office in a Sales Order are not resulting in a delta update in 2LIS_11_VATIM OR 2LIS_11_VADHR.
This is resulting in erroneous reporting when looking at Sales Orders at Sales Office Level.
I would like to know any solution or work around for this issue.
Regards,
AshutoshHi,
have made a trace after select a sales area, when the ddlb of the sales office was shown.
it seems there is no authorization check related to sales office restriction in standard.
you might consider to add your logic to
Class CL_BP_SALES_CORPACCOUNTOR_CN03
method GET_V_SALES_OFFICE
by enhance the component
BP_SALES
view
CorpAccountOrgEF
under context node
CorpAccountOrgEF
Hope this could do help!
Hongyan -
Cross-company + third party SD-order item result analysis
Hello,
please, advice.
SD-Process - third party cross-company
1.Sales organisation belongs to company code A.
2.Plant belongs to company code B.
3.Third party - so there is no delivery, intercompany billing is executed on the header level (IVA invoice type).
After the SD-order position is created, a purchase order is created too automatically. There is still no invoice receipt, intercompany billing, or external invoice. But the status Final Billed (FNBL) is determined for the position within the SAPKKA12 right after the position is created (not only within SAPKKA12, but also within result analysis KKA3 too).
Could you please test the same within your system and provide some clarification?
Thanks a lot
Regards
PHi,
Go to http://help.sap.com/saphelp_nw04/helpdata/en/da/7a2f41b239a831e10000000a1550b0/content.htm
Thanks,
Raviteja -
Ordering search results in Finder
When I search for certain files, I often need to sort the results in some custom order. Like by "date created". Or, by "date created", then by "kind". Unfortunately, Finder offers sorting by either "name", "kind", "last opened" only. Cmd+J (view options) actually doesn't offer an options. Creating a smart folder doesn't help either.
Am I missing something, or should I just look for another file browser for this kind of task?Spotlight/Finder's Find only offers these three options: name, kind, and last opened. See http://www.pinkmutant.com/articles/Leopard/leospot.html
-
Change sort order search results?
When I search in Mountain Lion I get a list of results, for instance documents that contain the word "serial"
see the attached screenshot.
The searchresults are ordered by name.
Only problem is the last result (starting with a Z) comes first.
I want the results starting with an A to show first.
In Lion you could reverse the sort order by clicking in the top bar (which says "Name" or "Naam" in my screenshot) but here that does not work.
What am I missing?
ThanksChange the item arrangement to 'None'
And then you'll be able to reverse the sort order -
Oracle: strange ORDER BY result
We're loading some data with a select clause from an oracle 8.1.7 and sorting the data by adding an "ORDER BY F_NAME" to the query.
On our development app server this runs fine and returns the expected result, but on out test app server the order depends on the case of the field F_NAME. It returns big caps before small caps (i.e. Armani, Miller, aman, meyer).
Does anyone know what to do in this case? I can replace it with an ORDER BY UPPER(F_NAME) of course, but this behaviour is just so strange that I fail to accept that as a valid workaround.Interesting. The only points I'd add are:
1) I presume you're connecting to two different schemas (users) on the same database. I wonder if its possible to affect these settings for specific users?
2) Do you use the OCI or thin driver? (Or different driver on each platform?). OCI uses Oracle client functionality, so its possible it might be picking up environmental settings that alter its behavior.
3) You can also alter behavior of things like this for a single connection (with "alter session" SQL); assuming no-one has done this explicitly with a SQL statement it would seem unlikely that the app server is doing it when creating connections in the pool.
Seems like you need to consult with someone with specific expertise in these areas of Oracle.
T -
SQL Order - matching results to a list
I have a list which I query, matchin the list on the camp_uid. The problem is that I want to display the results in the same order as the list was originally compiled, which is not in numeric order. One option is to set up a loop on the original list values, then use a query over query to obtain the results that are appropriate to that camp_uid, but there must be a better way? I search the ORDER command, there does not seem to be anything in place to make it keep the order of the supplied list in the IN statement.
Anybody have any better ideas than my query over query? I am a lilttle concerned over the the performance being forced to run multiple queries over data that I already have, even if they are in a Query over Query
<CFQUERY name="GetCamp" DATASOURCE="#datasource#">
SELECT camp_uid,camp_title,camp_text,camp_image_type
FROM campaigns
WHERE camp_uid IN (
<cfqueryparam value="#session.camp_list#" cfsqltype="CF_SQL_INTEGER" list="true">
</CFQUERY>
Thanks
MarkYou might try adding a column to your query based on the order the camp ids appear in the list.
To items to consider:
1. This code is not tested.
2. You should validate that your session.camp_list variable contains only integers, else you are vulnerable to a SQL injection attack.
3. This code assume that your list does not contain any duplicate values.
<CFQUERY name="GetCamp" DATASOURCE="#datasource#">
SELECT camp_uid,camp_title,camp_text,camp_image_type,
CASE
<!--- loop over list and create an order value for each camp, this assumes that list does not contain any duplicates --->
<cfloop from="1" to="#ListLen(session.camp_list)#" index="listIdx">
WHEN camp_uid = #ListGetAt(session.camp_list ,listIdx)#
THEN #listIdx#
</cfloop>
END AS camp_order
FROM campaigns
WHERE camp_uid IN ( <cfqueryparam value="#session.camp_list#" cfsqltype="CF_SQL_INTEGER" list="true"> )
ORDER BY camp_order;
</CFQUERY>
Assume that your session.camp_list variable contains "23,17,5" the query qenerated should look like:
SELECT camp_uid,camp_title,camp_text,camp_image_type,
CASE
WHEN camp_uid = 23
THEN 1
WHEN camp_uid = 17
THEN 2
WHEN camp_uid = 5
THEN 3
END AS camp_order
FROM campaigns
WHERE camp_uid IN ( 23,17,5 )
ORDER BY camp_order;
Message was edited by: JR \"Bob\" Dobbs. -
Changing Order Of Results (Union)
Hi All,
This simple query;
SELECT DISTINCT client_id FROM order_header
UNION
SELECT '1Forza WIP' FROM dual
UNION
SELECT 'Total' FROM dualReturns this data;
CLIENT ID
1Forza WIP
DAIRNUNEAT
FORZCLECKH
GBI-FELIXS
GU-LONDON
NOM-TELFOR
PLUMALRESF
PURECORBY
RIVELONDON
TRADNORFOL
TSC-SCUNTH
Total
YOPLPARISIs there anything I can do with order by (or another function) to change the results to look like this;
CLIENT ID
1Forza WIP
DAIRNUNEAT
FORZCLECKH
GBI-FELIXS
GU-LONDON
NOM-TELFOR
PLUMALRESF
PURECORBY
RIVELONDON
TRADNORFOL
TSC-SCUNTH
YOPLPARIS
TotalNotice 'Total' is now displayed last.
Thanks in advance.
SM.Hi,
Given that you know the value that has to be last:
ORDER BY NULLIF (client_id, 'Total') NULLS LASTA more general solution is to add a column to be used for sorting:
SELECT DISTINCT client_id
, 0 AS sort_col
FROM order_header
UNION
SELECT '1Forza WIP' AS client_id
, 2 AS sort_col
FROM dual
UNION
SELECT 'Total' AS client_id
, 3 AS
FROM dual
ORDER BY sort_col
, client_id
;If you don't want to display the extra column, see if you can tell your front-end tool not to display it.
In SQL*Plus, for example, you could say
COLUMN sort_col NOPRINTIf you can't do that, then compute the sort key in a sub-query, and don't include it in the main SELECT clause:
WITH got_sort_col AS
SELECT DISTINCT client_id
, 0 AS sort_col
FROM order_header
UNION
SELECT '1Forza WIP' AS client_id
, 2 AS sort_col
FROM dual
UNION
SELECT 'Total' AS client_id
, 3 AS
FROM dual
SELECT client_id
FROM got_sort_col
ORDER BY sort_col
, client_id
;Edited by: Frank Kulash on Mar 16, 2010 10:21 AM -
[SXI_MONITOR] Order of result after refresh
Dear all,
the results of a query are ordered descending depending on fields date and time.
But after clicking of refresh (F5), the results become disordered.
There is no recognizable order.
How can I set up that the order after refresh is the same like before?
We are using 7.11 (Patch 4).
Thanks and regards
ChrisDear Hareenkumar,
thanks for your reply. Of course I can do this manually and forget to
mention that I will not do this manually.
I would like to know if there is a way to do this automatically.
The first result is even sort descending automatically.
thx
Chris -
Found Difference between 9i & 10g order by result
Hello Guru's
I have created following table in both 9i and 10g
test1 with Columns as below
A VARCHAR2(10)
B NUMBER
Inserted 2 records in 9i and 10g ( same data)
*9i Result:*
SELECT * FROM test1;
A B
2 1
2 2
*10g result:*
SELECT * FROM test1;
A B
2 1
2 2
But if I use order by clause getting different result.
*9i Result:*
SELECT * FROM test1 order by a;
A B
2 1
2 2
*10g result:*
SQL> select * from test1 order by a;
A B
2 2
2 1
Please help about this output mismatch for same query in 9i and 10g.
Thanks In Advance.
Sandeep.What is your exact oracle version?
It seems that your theory is true at least in these version - (Strange....)
In, 9i ->
satyaki9i>
satyaki9i>select * from v$version;
BANNER
Oracle9i Enterprise Edition Release 9.2.0.1.0 - 64bit Production
PL/SQL Release 9.2.0.1.0 - Production
CORE 9.2.0.1.0 Production
TNS for HPUX: Version 9.2.0.1.0 - Production
NLSRTL Version 9.2.0.1.0 - Production
Elapsed: 00:00:00.00
satyaki9i>
satyaki9i>
satyaki9i>create table strange
2 (
3 a number,
4 b number
5 );
Table created.
Elapsed: 00:00:00.00
satyaki9i>
satyaki9i>insert into strange values(&a,&b);
Enter value for a: 2
Enter value for b: 1
old 1: insert into strange values(&a,&b)
new 1: insert into strange values(2,1)
1 row created.
Elapsed: 00:00:00.00
satyaki9i>/
Enter value for a: 2
Enter value for b: 2
old 1: insert into strange values(&a,&b)
new 1: insert into strange values(2,2)
1 row created.
Elapsed: 00:00:00.00
satyaki9i>
satyaki9i>commit;
Commit complete.
Elapsed: 00:00:00.00
satyaki9i>
satyaki9i>SELECT * FROM strange;
A B
2 1
2 2
Elapsed: 00:00:00.00
satyaki9i>
satyaki9i>SELECT * FROM strange order by a;
A B
2 1
2 2
Elapsed: 00:00:00.00
satyaki9i>
satyaki9i>In 10g,
satyaki10g>
satyaki10g>select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
Elapsed: 00:00:00.03
satyaki10g>
satyaki10g>
satyaki10g>
satyaki10g>create table strange
2 (
3 a number,
4 b number
5 );
Table created.
Elapsed: 00:00:00.05
satyaki10g>
satyaki10g>insert into strange values(&a,&b);
Enter value for a: 2
Enter value for b: 1
old 1: insert into strange values(&a,&b)
new 1: insert into strange values(2,1)
1 row created.
Elapsed: 00:00:00.00
satyaki10g>
satyaki10g>/
Enter value for a: 2
Enter value for b: 2
old 1: insert into strange values(&a,&b)
new 1: insert into strange values(2,2)
1 row created.
Elapsed: 00:00:00.00
satyaki10g>
satyaki10g>commit;
Commit complete.
Elapsed: 00:00:00.00
satyaki10g>
satyaki10g>SELECT * FROM strange;
A B
2 1
2 2
Elapsed: 00:00:00.00
satyaki10g>
satyaki10g>SELECT * FROM strange order by a;
A B
2 2
2 1
Elapsed: 00:00:00.00
satyaki10g>
satyaki10g>Let me go through the docs.
Regards.
Satyaki De. -
Sort order of results in UOW Conformed Query ??
We would like to know what is the expected behavior when a query with ascending ordering or descending ordering is run with UOW conformed.
We know for sure that SQL generated and submit to the database with ORDER BY CLAUSES (which get the data in the database and sorted properly)
However, with a confirm query, the database result will in turn, combine selected objects in the UNIT OF WORK.
Will the combined collection of object be sorted in the prescribed ordering ?
For example:
If in UOW we have some uncommitted CAT object CAT-9, and CAT- 5
In Database we return CAT-1, CAT-3, CAT-7 sorted in ascending order
Will toplink return after the UOW conformed query, a collection in the following order ?
CAT-1, CAT-3, CAT-5, CAT-7, CAT-9 ???
Please advice.
Thank you in advance.No, any new objects that conform to the query will be added to the end of the result set, so may not match the query's sort order.
If you need the conformed results to maintain ordering you can either,
- Sort the results in-memory after executing the query through Collections.sort(List).
- Use a TreeSet as the query resultCollectionClass to sort the query results. -
When I start a new search in Mail, the results are always returned ordered according to their rating. I prefer date ordered so I click the date column and arrange by date. However, it is annoying to have to do this for every new search... is there a way to set the default sort method to "sort by date"
Hi riscy,
Welcome to Apple Discussions!
The "toggle it" part is highlighting on one of the sortable columns (From, Subject, Date Received, etc) and then click/toggle on the small triangle in the right corner of that column heading.
So, for example, if you are sorting by Subject and the triangle points up, you will be sorting in alphabetical order A-Z - if the triangle is down, it will be Z-A. Same on Date Received (or Sent if you are in your Sent folder)
Of course you must have messages to sort for the triangle to show.
Good Luck,
John
Maybe you are looking for
-
Error saving purchase order data decimal values in SAP R/3
sir, PO was created with currency japanese YEN using Tcode ME21N . The net price was showing 142502300 (in JPY). when checked in EKPO table , it saved with a value 1425023.00 instead of 142502300 . what can be done to correct this. In PO Pri
-
GarageBand and Garritan Libraries (GPO, GJBB)?
Hi all, I recently purchased Garritan Personal Orchestra and Garritan Jazz and Big Band along with Finale 2006, and am attempting to integrate them with GarageBand. The help section that covers this topic gave me pretty vague instructions, and I was
-
Must use Captivate v4.0 to capture playing video - Best practices & PC requirements?
Hi all, I like Captivate, but have had a lot of trouble capturing playing video (a big part of my client's product) in the past. Despite encouraging client to consider other tools, they have decided they want to continue using Captivate and output to
-
Exchange Server 2013 Management Pack - not reporting Operations Manager 2012
Hi, We've installed the Exchange 2013 Management Pack into SCOM2012 but nothing seems to be reporting back. There nothing in Organization Status or Server Health. The Agent is installed on the Exchange Server. There are Event ID's on the Exchange ser
-
Searching for hierarchical keywords
Hi, I'm a newcomer to Aperture, and I have trouble using the hierarchical keywords. For example, if I create the keywords Sport->Soccer and Sport->Hockey, and I assign the keywords Soccer and Hockey to some photos, how can I search for the photos car