Which clause is overridden by the ORDER By clause
1. group by
2. having
3. select
4. where
5. from
It implies that distinct, group by guarantees a
sorted order. I have seen Tom Kyte disprove this lots
of time....and 10.2 (in particular the hash group by) is going to screw up a lot of people who have mistakenly relied on this.
SQL> with t as (
2 select mod(rownum,10) as r
3 from user_objects
4 where rownum <= 100
5 )
6 select r
7 from t
8 group by r;
R
6
7
5
8
1
2
3
4
9
0
10 rows selected.
SQL> with t as (
2 select mod(rownum,10) as r
3 from user_objects
4 where rownum <= 100
5 )
6 select r
7 from t
8 group by r
9 order by r;
R
0
1
2
3
4
5
6
7
8
9
10 rows selected.
Similar Messages
-
Error : The ORDER BY clause is invalid in views, inline functions, derived
Hi All,
I am on 11g 6.2, Windows Server 2008, my db SQL server 2008, I am facing the error for the reports in which I am trying to edit one the column formula and do something like 'abc/sum(abc)*100'.
10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 43113] Message returned from OBIS. [nQSError: 43119] Query Failed: [nQSError: 16001] ODBC error state: 37000 code: 8180 message: [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared.. [nQSError: 16001] ODBC error state: 37000 code: 1033 message: [Microsoft][ODBC SQL Server Driver][SQL Server]The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.. [nQSError: 16002] Cannot obtain number of columns for the query result. (HY000)
One of the solutions to this which I have found is to edit the EXPRESSION_IN_ORDERBY_SUPPORTED feature in the db properties.
I want to know what does EXPRESSION_IN_ORDERBY_SUPPORTED means?
When I create a calculations in 11g like abc/sum(abc) in the column formula for a column then i get this error.
What does this error mean? Does OBIEE 11g doesn't support using these expressions in the report and the fact that it applies the order by clause to the reports, the report fail?
Could anybody please explain the issue. There is very limited information on this over the web.
Thanks in advance.
RonnyThanks svee for the quick response, actually i had resolved the issue by unchecking the EXPRESSION_IN_ORDERBY_SUPPORTED option in the database. I want to understand how does that makes the difference?
What does EXPRESSION_IN_ORDERBY_SUPPORTED mean? Does it mean that if I give any expression in my answers report and since obiee uses a order by for all the queries, the expression won't be supported?
Please explain. -
How to change the order by clause of a standard VO in OAF
Hi All,
I need to change only the order by clause of a standard VO (Which is attached to a messageChoice item).
Steps followed:
1)Created a custom VO by extending the standard VO .
2)VO Impl class files are not generated as seeded VO has no such class files.
3)Created a substitution
4)Imported it
5)When I try access the specific page, getting Sys Admin error without any clue about the error.
Even I tried to create the custom VO without extending the std VO ,
Still getting the same error .
Please provide your suggestions on this.
Thanks,
ThavamWhen I set the FND:Diagnostics profile option , I could see what was the exact error . It was SQL Wrapper exception error.
The VO which is extended , has a dynamic where clause with bind variables. The binding style of the extended VO does not match with seeded VO. It works when the binding style is changed.
Thanks,
Thavam -
State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 43113] Message returned from OBIS. [nQSError: 43119] Query Failed: [nQSError: 16001] ODBC error state: 37000 code: 8180 message: [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared.. [nQSError: 16001] ODBC error state: 37000 code: 1033 message: [Microsoft][ODBC SQL Server Driver][SQL Server]The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.. [nQSError: 16002] Cannot obtain number of columns for the query result. (HY000)
I have already tried to follow this thread below, but no change.
HI
In this specific report ran above the offending sql uses a CTE.
I am on 11.1.1.6.1, Windows Server 2008
Currently in testing phase in migration from 10g.
I know what the error means, just not how to resolve it or what setting may be causing this.In Physical layer, go to specific database(Physical layer's) properties, database features tab -> Un check the EXPRESSION_IN_ORDERBY_SUPPORTED.
For that failed report, go to Answers-> advance tab-> Look for 'Advanced SQL Clauses'
Check this box to issue an explicit Select Distinct
Let me know updates
If helps Pls mark correct or helpful -
In which transaction can i find the Order acknowledgement field?
Hi All,
In which transaction Can i get the Order acknowledgement field ? i tried in VL10B and VL10D but i could not get it.
And also tell me what is the field (order acknowledgement) and in which table can i find it?
Thanks in Advance.
PallaviEKES-ERDAT
Tcode - ME92F -
Help Urgent! RowIterator retrieval doesn't respect the order by clause.
Hi,
Jdev 10.2.2
I'm using the RowIterator (source iterator) through a defined association to create rows in a destination RowIterator.
The creation of those rows must respect the order clause defined for the ViewObject => source Iterator.
I noticed that when I retrieve the rows through the RowIterator, the order clause is not respected?
Example of implementation:
Pre-defined weight levels for a postal tariff:
Product A:
From weight To weight
0 - 10
10 - 20
The user will have enter a price for each level.
I pre-create those rows so that only the price will have to be entered.
As you see the order of display/creation is important.
I there a way to make the RowIterator respect the Order Clause defined for the ViewObject?
I am a bit worried, please tell it's not the default behaviour!
Thanks
Frederic
For info my code:
RowIterator deliveryWeightIt = weightGroupImpl.getDeliveryWeightAssoc(); // source rows
if (deliveryWeightIt == null)
throw new JboException(getClass().getName()+".setWeightGroupId Detail row generation,. System error. DeliveryWeightAssoc is null.");
deliveryWeightIt.first();
Row currentDeliveryRow = deliveryWeightIt.getCurrentRow();
Row newPostalDetail = null;
try
while (currentDeliveryRow != null)
newPostalDetail = postalDetailIt.createRow();
SequenceImpl s = new SequenceImpl("SITE_POSTAL_REL_SEQ", getDBTransaction());
Number newSeq = s.getSequenceNumber();
System.out.println("numberOfGeneratedRows: "+numberOfGeneratedRows+", newSeq: "+newSeq+", FROMWEIGHT: "+currentDeliveryRow.getAttribute(DeliveryWeightImpl.FROMWEIGHT)); newPostalDetail.setAttribute(PostalDetailImpl.FROMWEIGHT,currentDeliveryRow.getAttribute(DeliveryWeightImpl.FROMWEIGHT));
newPostalDetail.setAttribute(PostalDetailImpl.TOWEIGHT,currentDeliveryRow.getAttribute(DeliveryWeightImpl.TOWEIGHT));
postalDetailIt.insertRow(newPostalDetail);
numberOfGeneratedRows++;
currentDeliveryRow = deliveryWeightIt.next();Ok I think I found my mystake.
I was using the entity association instead of using a view link.
I avoid using links because I encountered Class Cast exception => similar to thread 10.1.2: view Link accessors in entity object return ViewRow ??????
Will there be patch available soon?
Regards
Frederic -
Using ROWID in the ORDER BY clause
Hi,
I can consider myself as a PL/SQL beginner. I need to query some existing tables created for audit scope.
These tables have more duplicate rows respect to the same audit date: the precision is at "hour and minute and second" level.
But I need to understand which is the more recent row for the same audit date. So I think to use in the SELECT statement
the clause ORDER BY in this manner:
SELECT ... FROM Audit_Table ORDER BY Audit_Date desc, ROWID desc.
I'd like to use this ORDER BY also for the ROW_NUMBER function.
Any suggests to me about this topic? Many thanksI don't need to provide an example, I can do better by pointing you to the documentation:
http://docs.oracle.com/database/121/SQLRF/pseudocolumns008.htm#SQLRF00254
"If you delete a row, then Oracle may reassign its rowid to a new row inserted later" - for example
I can point you to asktom: https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:912210644860
"ROWIDs are NOT sortable" - for example
Your example just happened to work, but you are using the rowid semantically incorrect. -
PreparedStatement and order by clause
Hi all,
I'm having a strange problem, when I try to use a preparedStatement which has a parameter in the order by clause the query ignores the order by clause and returns the data in the default table order.
I'm trying to allow the enduser the ability to sort the results in any way they like.
a sample of code is:
javax.sql.DataSource dataSource;
java.sql.Connection myConnection = null;
String sort_order = null;
sort_order = request.getParameter("sort_order");
if(sort_order == null) {
sort_order = "start_date, hostname";
String sql = (String)Tokens.getInstance().getSQLProps().get(Tokens.FIND_SCHED);
try {
dataSource = getDataSource(request);
myConnection = dataSource.getConnection();
PreparedStatement pstmt = myConnection.prepareStatement(sql);
pstmt.setString(1, (Tokens.getInstance().getProps().getProperty(Tokens.CURRENT)));
pstmt.setString(2, sort_order);
ResultSet rset = pstmt.executeQuery();
thanks for any help you can provideYou cannot use bind variables for column names. In this case, what you are telling Oracle to do is to sort by the literal string 'start_date, hostname', which (since the literal string is the same for every row) has no effect. If you want Oracle to sort by the values in those columns, you would have to have different versions of the query depending on the ORDER BY clause. In this case, you would probably want to append the ORDER BY clause to your base SQL statement rather than trying to pass the ORDER BY clause in as a bind variable.
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
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. -
ORDER BY clause with expression
Considering that all the column names and table name is valid, what happens internally when the following query is run?
SQL> SELECT STU_ID, STU_NAME FROM STUDENT ORDER BY 2+3, STU_ID;
What i found out was the rows get ordered in ascending order of STU_ID. It's quite clear that it is ignoring the expressing 2+3, so my doubts are:
(a) Is it appending any pseudo-column to the table with 2+3 getting evaluated to 5?
(b) If there exist a 5th column in the table student and we specify it in the select statement's select list (5 column names) then why its not considering it as 5 and sorting the data according that?
(c) Is it true that, any expression we write is getting evaluated to NULL? Then is it that ORDER BY NULL evaluates to no ordering and the parser searches for next column-name or position (if specified) ?
(d) if neither then, what's the reason of the ignorance?Hi,
Welcome to the forum!
987236 wrote:
Considering that all the column names and table name is valid, what happens internally when the following query is run?
SQL> SELECT STU_ID, STU_NAME FROM STUDENT ORDER BY 2+3, STU_ID;
What i found out was the rows get ordered in ascending order of STU_ID. It's quite clear that it is ignoring the expressing 2+3, so my doubts are:
(a) Is it appending any pseudo-column to the table with 2+3 getting evaluated to 5There's no pseduo-column involved. (Pseudo-columns are something that Oracle provides for you, without you having to spell out what you want. ROWID and LEVEL are examples of pseudo-columns. In this example, 2+3 is an Expression . Like pseudo-columns, expressions can usually appear in the ORDER BY clause, even though they are not actually stored in the table.)
It's sorting first by the NUMBER 5 (= 2 + 3), not the 5th column. Since the NUMBER 5 has the same value on every row, every row ties for first place. The result of sorting by any constant is the same; the results are the same as not including that exrpression in the ORDER BY clause. That is the ORDER BY clause you posted is equivalent to
ORDER BY 2 + 3
, 'foo'
, SYSDATE
, NULL
, stu_idand it's also equivalent to
ORDER BY stu_idNone of the expressions except stu_id vary from row to row, so none of them affect the sorting.
(b) If there exist a 5th column in the table student and we specify it in the select statement's select list (5 column names) then why its not considering it as 5 and sorting the data according that?As Solomon said, only number literals are taken to mean a column. 5 is a number literal, so
ORDER BY 5means "sort by the 5th column", but 2 + 3 is not a literal (it's an expression that happens to include a couple of literals), so
ORDER BY 2 + 3does not refer to the 5th colunmn.
(c) Is it true that, any expression we write is getting evaluated to NULL? Then is it that ORDER BY NULL evaluates to no ordering and the parser searches for next column-name or position (if specified) ?No, when you way "ORDER BY x", the rows with lower values of x come first, followed by rows with higher values of x. Rows with the same value of x will be together, in no particular order with respect to each other (unless there is a tie-breaker expression later in the ORDER BY clause.
That's exactly what happens when you say "ORDER BY NULL". NULL has the same value (actually, the same lack of any value) on all rows, so all the rows have the same value of x, and they will appear in no particular order.
(d) if neither then, what's the reason of the ignorance? -
[JPA/TOPLINK] is the function "lower" supported in "order by" clause?
In EJB-QL
I can use lower() or upper() in where clause.
But there is always a parse exception thrown when i tried to use it in main clause or order by clause.
works:
select s from Student s where lower(s.name) like 'm%'
exception thrown:
select s from Student s where s.name like 'm%' order by lower(s.name)
OR --------------
Why i am asking this is that the resultset returned from database is not alphabetical sorted but ascii sorted, which means any characters with upper case is always prior to the ones with lower case.
If EJB-QL doesn't support using lower() in order by clause, do I have any other options to avoid this problem?
BTW, it is the Oracle 10g we are using as DB
many thanks,
Xuphey
Edited by: Xuphey on Nov 29, 2007 1:33 AMIf you want to do this, you'd have to use dynamic SQL (execute immediate or DBMS_SQL). For the easier 'execute immediate' approach, you'd do something like
create or replace someProc( someArg varchar2 )
as
strSQL varchar2(4000)
begin
strSQL := <<string containing your SQL statement up to the order by clause>>
strSQL := strSQL || 'ORDER BY ' || someArg
execute immediate strSQL;
endJustin -
Read only view which has an ORDER BY clause
Aim to create a read only view which has an ORDER BY clause
Sample script
CREATE TABLE tab1 (
col1 VARCHAR2(3),
col2 NUMBER
INSERT INTO tab1(col1, col2) VALUES ('ccc', 30);
INSERT INTO tab1(col1, col2) VALUES ('bbb', 20);
INSERT INTO tab1(col1, col2) VALUES ('ddd', 40);
INSERT INTO tab1(col1, col2) VALUES ('aaa', 10);
COMMIT;
-- To creat View
CREATE OR REPLACE VIEW v_tab1
AS
SELECT
col1,
col2
FROM
tab1
ORDER BY
col1
WITH READ ONLY;
I get the following error
WITH READ ONLY
ERROR at line 10:
ORA-00933: SQL command not properly ended
When I comment out the ORDER BY clause, the view is created
Pls let me know how I can create a read only view with ORDER BY clauseHi,
take a look at the documentation for CREATE VIEW, there is a restriction:
"You cannot specify the ORDER BY clause in the subquery if you also specify the subquery_restriction_clause."
Wolfgang -
What is the point in having multiple columns in ORDER BY clause?
DB version:10gR2
When using ORDER BY clause, the rows are always sorted according to the first column in the ORDER BY clause. So, what is point in having multiple columns in the ORDER BY clause(i always see this in production codes)?
For the below SQLs' from SCOTT schema, the result sets are always ordered according the first column ename. When i added job asc and job desc, the result set doesn't change.
SQL> select * from emp order by ename;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7839 KING PRESIDENT 17-NOV-81 5000 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7934 MILLER CLERK 7782 23-JAN-82 1300 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7369 SMITH CLERK 7902 17-DEC-80 800 20
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
14 rows selected.
SQL> select * from emp order by ename, job;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7839 KING PRESIDENT 17-NOV-81 5000 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7934 MILLER CLERK 7782 23-JAN-82 1300 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7369 SMITH CLERK 7902 17-DEC-80 800 20
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
14 rows selected.
SQL> select * from emp order by ename, job desc;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7839 KING PRESIDENT 17-NOV-81 5000 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7934 MILLER CLERK 7782 23-JAN-82 1300 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7369 SMITH CLERK 7902 17-DEC-80 800 20
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
14 rows selected.Because there is only one employee with the name SCOTT,FORD ...etc in the emp table and your first column in the order by list is ename
you spot the difference now
SQL> select * from emp order by job;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7788 SCOTT ANALYST 7566 19-APR-87 00:00:00 3000 20
7902 FORD ANALYST 7566 03-DEC-81 00:00:00 3000 20
7934 MILLER CLERK 7782 23-JAN-82 00:00:00 1300 10
7900 JAMES CLERK 7698 03-DEC-81 00:00:00 950 30
7369 SMITH CLERK 7902 17-DEC-80 00:00:00 800 20
7876 ADAMS CLERK 7788 23-MAY-87 00:00:00 1100 20
7698 BLAKE MANAGER 7839 01-MAY-81 00:00:00 2850 30
7566 JONES MANAGER 7839 02-APR-81 00:00:00 2975 20
7782 CLARK MANAGER 7839 09-JUN-81 00:00:00 2450 10
7839 KING PRESIDENT 17-NOV-81 00:00:00 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 00:00:00 1500 0 30
7654 MARTIN SALESMAN 7698 28-SEP-81 00:00:00 1250 1400 30
7521 WARD SALESMAN 7698 22-FEB-81 00:00:00 1250 500 30
7499 ALLEN SALESMAN 7698 20-FEB-81 00:00:00 1600 300 30
14 rows selected.
Elapsed: 00:00:00.00
SQL> select * from emp order by job, deptno asc;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7902 FORD ANALYST 7566 03-DEC-81 00:00:00 3000 20
7788 SCOTT ANALYST 7566 19-APR-87 00:00:00 3000 20
7934 MILLER CLERK 7782 23-JAN-82 00:00:00 1300 10
7369 SMITH CLERK 7902 17-DEC-80 00:00:00 800 20
7876 ADAMS CLERK 7788 23-MAY-87 00:00:00 1100 20
7900 JAMES CLERK 7698 03-DEC-81 00:00:00 950 30
7782 CLARK MANAGER 7839 09-JUN-81 00:00:00 2450 10
7566 JONES MANAGER 7839 02-APR-81 00:00:00 2975 20
7698 BLAKE MANAGER 7839 01-MAY-81 00:00:00 2850 30
7839 KING PRESIDENT 17-NOV-81 00:00:00 5000 10
7654 MARTIN SALESMAN 7698 28-SEP-81 00:00:00 1250 1400 30
7844 TURNER SALESMAN 7698 08-SEP-81 00:00:00 1500 0 30
7521 WARD SALESMAN 7698 22-FEB-81 00:00:00 1250 500 30
7499 ALLEN SALESMAN 7698 20-FEB-81 00:00:00 1600 300 30
14 rows selected.
Elapsed: 00:00:00.01
SQL> select * from emp order by job,deptno desc;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7902 FORD ANALYST 7566 03-DEC-81 00:00:00 3000 20
7788 SCOTT ANALYST 7566 19-APR-87 00:00:00 3000 20
7900 JAMES CLERK 7698 03-DEC-81 00:00:00 950 30
7369 SMITH CLERK 7902 17-DEC-80 00:00:00 800 20
7876 ADAMS CLERK 7788 23-MAY-87 00:00:00 1100 20
7934 MILLER CLERK 7782 23-JAN-82 00:00:00 1300 10
7698 BLAKE MANAGER 7839 01-MAY-81 00:00:00 2850 30
7566 JONES MANAGER 7839 02-APR-81 00:00:00 2975 20
7782 CLARK MANAGER 7839 09-JUN-81 00:00:00 2450 10
7839 KING PRESIDENT 17-NOV-81 00:00:00 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 00:00:00 1500 0 30
7654 MARTIN SALESMAN 7698 28-SEP-81 00:00:00 1250 1400 30
7521 WARD SALESMAN 7698 22-FEB-81 00:00:00 1250 500 30
7499 ALLEN SALESMAN 7698 20-FEB-81 00:00:00 1600 300 30
14 rows selected.
Elapsed: 00:00:00.01
SQL> -
How to ORDER BY Different column than in the group by clause.
Hello, I have this sql statement
SELECT COUNT([Tags]) AS CNT, [Tags]
FROM [Tags], [Images]
WHERE
([Images].[Tags] LIKE '%' + [Tags].[LongTag] + '%') AND
([Tags].AllowTagPost = 'True' )
GROUP BY [Images].[Tags]
HAVING COUNT([Tags]) > 30
ORDER BY [Tags].[LastTagPost] DESC
It returns the tags, the number of occurances sorted by count. However, I want to sort it by a column in the Tags table named LastTagPost (DATETIME) The idea is to get the tag that hasn't been used in the longest time and also has more occurances
than 30 in the image table. When I try to order by this column i get an error that i'm sure most of you are all to familer with.
Error:
Column "Tags.LastTagPost" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
Any help would be greatly appreciated.Couldn't you just include the LastTagPost as a MIN?
DECLARE @tags TABLE (LastTagPost DATETIME, LongTag VARCHAR(100), allowTagPost CHAR(5))
DECLARE @images TABLE (tags VARCHAR(10))
SELECT COUNT(i.Tags) AS CNT, i.Tags, MIN(t.LastTagPost) AS lastTagPost
FROM @Tags t
INNER JOIN @Images i
ON i.Tags LIKE '%' + t.LongTag + '%'
WHERE t.AllowTagPost = 'True'
GROUP BY i.Tags
HAVING COUNT(i.Tags) > 30
ORDER BY LastTagPost DESC
Don't forget to mark helpful posts, and answers. It helps others to find relevant posts to the same question. -
Hi,
Can we use order by clause in Sub query?
While using the order by clause, I am getting the "missing expression error" . If I remove order by clause query executing fine.
Here is my query:
select *
from emp_mstr
where emp_no in(select
emp_no
from emp_mstr
order by branch_no);
Thanks & Regards,
MahiMay be you miss some required spaces also, other than wrong use of ORDER BY
select *
from emp_mstr
where emp_no in
( select e2.emp_no
from emp_mstr e2
-- order by e2.branch_no
);Why do you want to ORDER BY in the subquery, which you use with IN clause? That will not make any difference in the result..Means the result you get with ORDER BY will be same as without that.. And in this case, ORDER by is a unncessary overhead.. And Ordering is very costly..
And why do you want to have the IN clause at all in your query? You are referring the same tables in the main query and sub query..
The below will give the same result
select *
from emp_mstr
where emp_no is not nullIf you want to use another table in the subquery, always use aliasess...
select *
from emp_mstr
where emp_no in
( select e2.emp_no
from emp_mstr2 e2
-- order by e2.branch_no
);
Maybe you are looking for
-
How to read multiple segments with same name in IDOC
I want to read segments in IDOC say e1edk02.I am using orders05 idoc. BUT If I use the statement CONSTANTS : c_segnam1 type char19 value 'E1EDK02'. read table idoc_data into wa_idocdata with key segnam = c_segnam1. It will read only
-
It would save me a lot of desk space if I could use my MacBook as the monitor for my G4. I connected the two by firewire but I couldn't run protected programs like Photoshop.
-
Extra Paragraph in Table of Contents
I have a TOC set up in a three-column text frame, and have it set to balance columns. When I generate the TOC, it gives me an extra paragraph return at the end, which messes up the column balance. ny way of fixing this?
-
Can anyone explain how to post the manual entries in BCS?
Hi All, Can anyone explain how to post the manual entries in BCS?
-
Email Actions using Smartforms - To and Cc fields
Hi All, I have a requirement to send emails to all partners in the order using actions and smartforms. However I need a few of them (Based on partner function) in the To list and the rest in the Cc List. Is there a possible way to do this? Thanks