Order by clause
Hello
I want to know what it means when I use the "order by" clause with more than one column in both cases (sort order, it's the same, and when the sort order is different), for example
What should happen when I do this
1 Select * from table
order by 1, 2; -both are a CSA
2 - Select * from table
order by 1 asc, desc 2;
in both cases if it sorts the column with the first column without worrying, it is asc or desc, order another column order, because he should get
related data in the first column, then another sort column does nothing?
Am I wrong?
Thank you
SQL > select * from t by col1, col2 CSA CSA;
COL1 COL2
-------------------- --------------------
1 1
1 2
1 3
2 1
2 2
SQL > select * from t by CSA col1, col2 desc;
COL1 COL2
-------------------- --------------------
1 3
1 2
1 1
2 2
2 1
Tags: Database
Similar Questions
-
How to remove the Order By clause
How can I get rid of the Order By clause in the underlying SQL query? I created a simple hierarchy, but when I use the hierarchy in the analysis and then come down, the results show multiple occurrences of each child. For some reason, I was able to reduce the number of occurrences of each child by adding a saved filter.
I was expecting this:
Instead, I got this:
The generated physical query looks like this:
SET VARIABLE QUERY_SRC_CD = "report"; SELECT s_0, s_1, s_2, s_3, s_4 s_5, (FROM s_6)
SELECT
s_0 0,.
CAST (NULL AS VARCHAR s_1 (1)).
"Registration - College". «Dimensions of the inscription '.» "' Exercise ' s_2,.
'All the colleges' s_3,.
IDOF ("registration - College". «Hierarchies of the inscription '.» "College". ". ("' S_4 all Colleges").
MOUNT s_5 (NULL AS DOUBLE),
"Registration - College". "" Counties of schooling. "" Full-time student "s_6
"REGISTRATION - College".
UNION ALL
SELECT
1 s_0
"Registration - College". «Dimensions of the inscription '.» "" Name of College "s_1,.
"Registration - College". «Dimensions of the inscription '.» "' Exercise ' s_2,.
'All the colleges' s_3,.
IDOF ("registration - College". «Hierarchies of the inscription '.» "College". ". ("' S_4 all Colleges").
IDOF ("registration - College". «Hierarchies of the inscription '.» "College". ". ("' S_5 College ').
"Registration - College". "" Counties of schooling. "" Full-time student "s_6
"REGISTRATION - College".
WHERE
("Registration - College". «Dimensions of the inscription '.» ("' Exercise ' IN ('201213')) AND (IDOF ("registration - College". «Hierarchies of the inscription '.» "College". ". (("' Colleges ') (1))
) djm ORDER OF 1, 3 ASC NULLS LAST, 4 ASC NULLS FIRST, 5 ASC NULLS FIRST, 2 ASC NULLS FIRST, NULLS FIRST CSA 6
EXTRACT FIRST 10000000 LINES ONLY
HI mbengue,.
Check the report you are running any column presentation sorted in the user interface (in which case the order by clause will creep into generated sqls Physics). Also, I think that its worth it to check the settings of the hierarchy you have configured and if the installation program for the calculation of the measured column (number of students in this case), has been done correctly.
Kind regards
Arko
-
Cannot wrap a select with order by clause in a file.
TOGETHER SET TERMOUT OFF FEEDBACK OFF PAGESIZE 0 400 LINESIZE ECHO OFF OFF POSITION
coil & 1;
Select "EMPID, EMP_NAME, REGION, FROM, TO, MONTH, ACTIVITY_TYPE, PROJECT_PIN, MILESTONE_NAME, PRACTICE_ID, RESOURCE_CATEGORY, HOURS, COMMENT, percent ALLOCATION, INVOICE_TO, the STATE of the double"
Union of all the
select empid,resource_name,region,week_of,(week_of+7),core_month,bill_rate,project_name,project_type,milestone_name,practice_id,resource_category,total_hours_submitted,project_type,"%ALLOCATION",invoice_to,status from master_emp_allocation order by week_of;
spool off;
"exit";
/
the above block gives result below
select empid,resource_name,region,week_of,(week_of+7),core_month,bill_rate,project_name,project_type,milestone_name,practice_id,resource_category,total_hours_submitted,project_type,"%ALLOCATION",invoice_to,status from master_emp_allocation order by week_of
*
ERROR at line 3:
ORA-00904: "WEEK_OF": invalid identifier
---------------------------------------------------------------------------
Without the order by clause it wraps the file passed as an argument.
check
Combining the UNION AGENDA and BY
Columns in the ORDER BY list must be a subset of the columns in the selection list on the left side of the union.
All the columns in the ORDER BY list should be sorted in ascending order, and they must be a prefix in the order of the columns in the target of the left side of the UNION list.
-
How does the order by clause in oracle 10G?
Hi This is my table called Master.I want to order by sno. so I run the below mentioned query.
SNO DESC STIME, ETIME CCODE ADD SID
11 11 11 1 10 s11
1 1 s1 09:00 12:00 10
2 2 s2 06:00 07:00 9
3 3 S3 9:00 23:00 0
SELECT * FROM MASTER BY SNO
SNO DESC STIME, ETIME CCODE ADD SID
1 1 s1 09:00 12:00 10
11 11 s11 11 1 10
2 2 s2 6:00 7:00 9
3 3 S3 09:00 23:00 0
but I had bad results. Help kindly me.i can't understand how the order by clause is fetching the record.
Hello
Most probably because that SNO is not digital, but a tank.
If it contains a number onlly, try
SELECT * FROM MASTER BY TO_NUMBER (SNO);
-
I have a very basic SQL statement that I am running on a simple table of three columns. I just want to choose two of the three columns basing selection on a column, then rank each other. I use version 9.2.0.1.0 and I tried to use sqlplus and toad 10.6 and the results are the same in both. My problem is that even if I use an Order By clause the results not be sorted. I have countless statements written and never had a problem with the Order By clause before. I'm puzzled.
Executed SQL statement:
Select COL3 From Table1 where COL2 = 1 Order By COL3;
Result:
DXXXXX
NXXX Mxxx/Cxxxx CXXV
Nzzzz Ezzzz
Oxxxxxxxxxx Exxxx
Rxxxxxxxxx Exxxx
Sxxxxxxx Exxxx
Sxxxxxx Hxxx
gxxxxxxxYou should know that I replaced everything except the first letter of each letters to alter woth Word. There's nothing remarkable about the table:
Executed SQL statement:
DESC Table1;
Result:
Table1 TABLE:
Name Null? Type
----------------------------------------- -------- ----------------------------
COL1 NUMBER
COL2 NUMBER
COL3 VARCHAR2 (255)Hello
Use this...
Select COL3 From Table1 where COL2 = 1 Order By LOWER (COL3);
It should work once you order by the case-based.
When you view, its up to you, if you want to use all the conversions of case or not
-
ORDER BY clause with an expression
As the column of names and table name is valid, what happening internally when the query is executed?
SQL > SELECT STU_ID, STU_NAME FROM to STUDENT BY 2 + 3, STU_ID;
What I discovered, was that the lines get sorted by ascending order of STU_ID. It is obvious that he does not know the expression 2 + 3, so my doubts are:
(a) is he adding a pseudo-column to the table with 2 + 3 get discounted at 5?
Select (b) if there is a 5th column among students of the table and specify us it in the select statement of the list (5 column names) then why not consider as 5 and sort the data according to that?
(c) it is true that, any expression that we write is getting evaluated to null? Then - what is ORDER BY NULL refers to any order and if the Analyzer search name column or next position (if specified)?
(d) If neither of the two, so what is the reason of ignorance?Hello
Welcome to the forum!
987236 wrote:
As the column of names and table name is valid, what happening internally when the query is executed?SQL > SELECT STU_ID, STU_NAME FROM TO STUDENT BY 2 + 3, STU_ID;
What I discovered, was that the lines get sorted by ascending order of STU_ID. It is obvious that he does not know the expression 2 + 3, so my doubts are:
b is a pseudo-column adding to the table with 2 + 3 get discounted at 5
There is no column pseduo involved. (Pseudo-colonnes are something that Oracle provides to you, without requiring you to specify what you want. Include ROWID and LEVEL pseudo-colonnes. In this example, 2 + 3 is an Expression . Like the pseudo-colonnes, expressions can usually appear in the ORDER BY clause, even if they are not actually stored in the table.)
It is the first sort by the NUMBER 5 (= 2 + 3), not the 5th column. Since the NUMBER 5 has the same value on every row, every row relates to the forefront. The result of sorting by any constant is the same; the results are the same that besides as exrpression in the ORDER BY clause. This is the clause ORDER BY, that you have published is equivalent toORDER BY 2 + 3 , 'foo' , SYSDATE , NULL , stu_id
and this is also equivalent to
ORDER BY stu_id
None of the expressions except stu_id vary from one line to the other, then none of them affect sorting.
Select (b) if there is a 5th column among students of the table and specify us it in the select statement of the list (5 column names) then why not consider as 5 and sort the data according to that?
As Solomon replied only literal numbers are taken to refer to a column. 5 is a literal number, then
ORDER BY 5
means "Sort by the 5th column", but 2 + 3 is not a literal (that is an expression that happens to include a couple of literals), so
ORDER BY 2 + 3
does not refer to the 5th colunmn.
(c) it is true that, any expression that we write is getting evaluated to null? Then - what is ORDER BY NULL refers to any order and if the Analyzer search name column or next position (if specified)?
No, when you way "ORDER BY x", the rows with values of x which come first, followed by the lines with higher values of x. lines having the same value of x will be together, in no particular order with respect to the other (unless there is an expression of failure later in the clause ORDER BY.
This is exactly what happens when you say "ORDER BY NULL". NULL has the same value (in fact, the same lack of any value) on all lines, so all rows have the same value of x, and they will appear in no particular order.(d) If neither of the two, so what is the reason of ignorance?
-
Does anyone know how or where to enter in the ORDER BY clause when you use a SQL data source in a load rule? I prefer to do the classification in the State of charge rather than create a view on the side of the DB for this if possible.
According to the documentation "box of the SQL statement in Open SQL Data Sources dialog box provides Select, From, and where areas of text that helps you write SQL queries." You can specify multiple data sources, filter the display of folders and specify how the records displayed in the data editor Prep are classified and grouped. "but I have not found anywhere where you can order or group data. I don't need to be able to group data I want to just be able to control my data to make the most effective possible charging process.
I sort the data by inserting into the table of load, but Oracle is not necessarily insert the records in the table according to their order when you go.
We're currently on 11.1.1.3 Essbase and Oracle 11 g.
Any help is greatly apprecied.Just nail it on your WHERE clause in the area WHERE the. I think that you can even stick the entire statement (under the keyword "SELECT") within the area of SELECTION.
-
sys_context and order by clause
Hi all
IM using Oracle 11 g R2.
Is it somehow possible to use the context variable (to store the column name) in the ORDER BY clause? My ultimate goal is to build by Order at run time, without using the concatenation.
for example, I created context as my_ctx for some pkg and its variable as v_ctx with a value defined as 'TABLE_NAME '.
So it is possible to use it as
Select * from all_tables
sys_context order ('my_ctx', 'v_ctx');
If not, does perform a work around?
Thank you
VivekHi, Vivek,
Vivek says:
Hi allIM using Oracle 11 g R2.
Is it somehow possible to use the context variable (to store the column name) in the ORDER BY clause? My ultimate goal is to build by Order at run time, without using the concatenation.
Of course, you can use SYS_CONTEXT in an ORDER BY clause, but it is unclear what you are trying to do, so I don't know if SYS_CONTEXT will help you.
for example, I created context as my_ctx for some pkg and its variable as v_ctx with a value defined as 'TABLE_NAME '.
So it is possible to use it asSelect * from all_tables
sys_context order ('my_ctx', 'v_ctx');If not, does perform a work around?
Post a full test script Hat people can run to recreate the problem and test their ideas. Show the output you want from the parameters a data you give.
Consider using a CASE expression in the ORDER BY clause. You can use SYS_CONTEXT in the expression BOX, if you wish.
-
Hai all,
I have problem with order by clause,
My query is
"Select number from table1 order number CSA.
and the output as
1
10
12
13
15
17
19
2
20
21
22
.
........
But if we give the order that it should display below the only good?
1
2
10
12
13
15
17
19
20
21
22 ........
Help me please why it is not displayed as she. and how to make the statement to display as in the second case. Thanks in advance.
Kind regards
UrajaIn your table, the column is VARCHAR2 data type.
It is a string, even if it is to store numeric values, it will get condemn as strings
Try the below
select your_column from table1 order by to_number(your_column) asc
Published by: JAC on November 19, 2012 16:35
-
order by clause union in date column
Hi gems... good evening...
Oracle version: 11.2.0.2 EL6 Linux server
I have a query as below:
Select col1, col2, col3 col4
tab1, tab2, tab3
where the conditions
Union
Select col1, col2, col3 col4
tab1, tab2, tab3
where the conditions
Union
Select col1, col2, col3 col4
tab1, tab2, tab3
where the conditions;
Now the col4 is a date column, and I need to order by on that entire result sets. I know that I can do (order of col4) (order by 4) or at the end of all of the query.
But the problem is that the output is coming to Mon-dd-yyyy (i.e. 31-Dec-2012).
I want each output in the format dd/mm/yyyy, so I need to use the function to_char.
But in this case, I can't use the order by clause, because in this case it's get organized by character IE by 1,2,3,4,5 like that.
How can I solve my problem... Please help... Thanks in advance.
Published by: gogol on 3 November 2012 08:05Maybe - when you order by date column, she did it with a date internal representation so on exit you can use any date format you think appropriate
select distinct col1,col2,col3,col4 from (select col1,col2,col3,col4 from tab1,tab2,tab3 where conditions_1 or conditions_2 or conditions_3 )
Concerning
Etbin
-
Purpose of the ORDER BY clause in the analytic function Min Max
I was always using analytical functions like Min Max without ORDER BY clause. But today I used with the ORDER BY clause. The results are very different. I would like to know the purpose of the ORDER BY clause in Min, Max and analogues of analytical functions.user10566312 wrote:
I was always using analytical functions like Min Max without ORDER BY clause. But today I used with the ORDER BY clause. The results are very different. I would like to know the purpose of the ORDER BY clause in Min, Max and analogues of analytical functions.It is a good point that many developers are not so aware. As far as I understand it the way it works.
Some analytical functions do not need an order by or windowing clause (SUM, COUNT, MIN, etc.). If there is no specified window, then the full score is the window.
As soon as you add a command also add you a windowing clause. This window has the default value of 'rank ofrowsbetween unbounded preceding and current_row. So as soon as you add an order by clause, you get a sliding window.Documentation: http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions001.htm
windowing_clause
...
You cannot specify this clause unless you specified the order_by_clause. Window limits defined by the clause RANGE you can not specify only a single expression to the > order_by_clause. Please refer to 'Restrictions on the ORDER BY Clause'.example of
with testdata as (select 10 numval, level lv from dual connect by level < 10) select lv, numval, sum(numval) over () sum1, sum(numval) over (order by lv) sum2 from testdata; LV NUMVAL SUM1 SUM2 -- ------ ---- ---- 1 10 90 10 2 10 90 20 3 10 90 30 4 10 90 40 5 10 90 50 6 10 90 60 7 10 90 70 8 10 90 80 9 10 90 90
Published by: Sven w. on 25 Sep 2012 16:57 - default behavior has been corrected. Thanks to Chris
-
Problem in the SQL because of the order by clause
Hi all
I am facing a problem in a sql query. I use Oracle 10 g 2.
Query is given below:
The query Plan is:SELECT t1.ename FROM T1, T2 WHERE T1.EMPNO = 1234 AND T1.ACCOUNTNO = T2.ACCOUNTNO AND T1.SEQ = T2.SEQ ORDER BY T2.SEQ
Now, I want to reduce the time of this request..---------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 2 | 218 | 11716 (1)| 00:00:41 | |* 1 | TABLE ACCESS BY INDEX ROWID | T1 | 1 | 89 | 1 (0)| 00:00:01 | | 2 | NESTED LOOPS | | 2 | 218 | 11716 (1)| 00:00:41 | |* 3 | TABLE ACCESS BY INDEX ROWID| T2 | 2 | 40 | 11715 (1)| 00:00:41 | | 4 | INDEX FULL SCAN | PK_T2_SEQ | 58752 | | 122 (5)| 00:00:01 | |* 5 | INDEX RANGE SCAN | FK_ACCOUNTNO | 3 | | 0 (0)| 00:00:01 | ----------------------------------------------------------------------------------------------------
If I remove order by clause query that the query performance is quite perfect, but with order by clause its take time.
I already set SORT_AREA_SIZE, but still nothing improves.
Welcome and thank you for your suggestions.
Thank you
Published by: BluShadow on June 23, 2011 07:55
addition of {noformat}{noformat} tags and formatted explain plan to make it readable. Please see {message:id=9360002} for details on how to post code and data
Hey, a quick test, try:
select ename from ( SELECT t1.ename, T2.SEQ FROM T1, T2 WHERE T1.EMPNO = 1234 AND T1.ACCOUNTNO = T2.ACCOUNTNO AND T1.SEQ = T2.SEQ and rownum > 0) ORDER BY SEQ
Amiel Davis
-
internal problem in a query order by clause
Hello...
I have an update statement with a select clause simple present form of inner query...
(select col1 from table1 where col2 = 'abc' and rownum = 1 order by col3 desc)
Since it is an internal application, this is why I can't delete the square brackets.
COL3 can be 0 or 1 (1 can only happen once... 0 can be several times)
My goal is to retrieve the record if the col3 is 1
If is not 1, then it will get the first album with col3 = 0... that's why I put order by col3.
Furthermore, I don't want that one folder, that's why I put rownum = 1.
but it fails with 'missing right parenthesis.
Help, please...Hello
Don't forget that the ORDER BY clause is applied last, when the WHERE clause is complete, then when you way
WHERE ROWNUM = 1 ORDER BY col3 DESC
in the same auxiliary request, you are choosing a line (arbitrarily) and then "sorting" only one line.
Here's a way to get the desired results:
( SELECT col1 FROM ( SELECT col1 , ROW_NUMBER () OVER (ORDER BY col3 DESC) AS r_num FROM table1 WHERE col2 = 'abc' AND col3 IN (0, 1) ) WHERE r_num = 1 )
Depending on how it is used in your query completed, there may be better ways to achieve the same results.
-
Evaluate the Expression in the order by Clause
Hello
I'm struggling to run after JPQL.
Select r.firstname, r.lastname, (e.distance/e.duration) from Runner r Inner Join r.event e Order BY (e.distance/e.duration) Desc;
Error suggests that ' (' character is unacceptable in the Order By Clause.) Here is the error.
javax.servlet.ServletException: java.lang.IllegalArgumentException: an exception occurred while creating a query in EntityManager:
Description of the exception: syntax error parsing the query [Select r.firstname, r.lastname, (e.distance/e.duration) of Runner r Inner Join r.event e Order BY Desc (e.distance/e.duration);], line 1, column 101: unexpected token [()].
Inner exception: NoViableAltException (81! = [1328:1: orderByItem returns [object node]: (n = stateFieldPathExpression (a = ASC | DESC = d |) | I = IDENT (a CSA = | d = DESC |)) ;])])
Can someone show me please how to fix it.
Thank you
AdamWhat version do you use? It should work in EclipseLink > 2.1.
---
James: http://www.eclipselink.org -
Need help with order by clause row_number() Fn
Desired outputwith t as ( select 123 id, 'brwr' lstatus from dual union all select 123 id, 'ca' lstatus from dual union all select 123 id, 'fac' lstatus from dual union all select 345 id, 'ca' lstatus from dual union all select 345 id, 'brwr' lstatus from dual union all select 567 id, 'brwr' lstatus from dual union all select 789 id, 'brwr' lstatus from dual union all select 1011 id, 'fac' lstatus from dual union all select 1011 id, 'xyz' lstatus from dual )select id,lstatus,row_number() over(partition by id order by lstatus)rw from t;
That is, my first priority is to 'ca', then 'College '.id lstatus rn 123 brwr 3 123 ca 1 123 fac 2 345 ca 1 345 brwr 2 567 brwr 1 789 brwr 1 1011 fac 1 1011 xyz 2
I am aware that this can be done through the use of CASE within the order by clause
but could not find the logic of the apt.
Please help me.A solution just for data given to samples...
with t as ( select 123 id, 'brwr' lstatus from dual union all select 123 id, 'ca' lstatus from dual union all select 123 id, 'fac' lstatus from dual union all select 345 id, 'ca' lstatus from dual union all select 345 id, 'brwr' lstatus from dual union all select 567 id, 'brwr' lstatus from dual union all select 789 id, 'brwr' lstatus from dual union all select 1011 id, 'fac' lstatus from dual union all select 1011 id, 'xyz' lstatus from dual )select id,lstatus,row_number() over(partition by id order by (case when lstatus <> 'brwr' THEN UPPER(lstatus) else lstatus end)) rw from t;
Basically, it shows how to use the CASE statement. You can change based on your actual data.
Concerning
Arun
Maybe you are looking for
-
Building from scratch pad...
I'm a student working on my final project (just passed my first exam DRESSED!) I'm trying to use a Boolean array to generate a 4-digit password. So far, I am able to get the correct number appears, but I want to eliminate the value by defect-1 for no
-
Hello Roundness can be defined using different methods. So far, I found MRS. or MZC, MIC, MCC, and LSC.Full description in the VI as comment So far I managed to calculate the ICL because LV so provide a VI Can anyone help me please with those others?
-
I use windows 7. my windows firewall was disabled. When I click on turn on the firewall, that a message "Action Center can't turn on the firewall".when I try to turn on manually I get an error report that says error code 80070422 0 x ' windows firewa
-
See 5.3 and 8.1 of Windows domain join fails
Hi allI just upgraded view server and view composer 5.2 to 5.3 connections. Always running vSphere/vCenter 5.0, but these are listed as supported in the support matrix.My related stocks work fine (XP and Windows 7). I tried to add a new Pool of Windo
-
Determine which user added an ESX host to vCenter
I'm trying to figure out if there is anyway to determine which user has added a vCenter ESX host using the api. I don't see this information as any sort of property of the host object. Any help would be appreciated!Paul