Regarding order by clause
the values of a column are:
1 - 5 hours
20 mins - 1hr
greater than 5 hours
less than20 mins
I want the output in the order as:
less than20 mins
20 mins - 1hr
1 - 5 hours
greater than 5 hours
This is neither ascending nor descending order.
Is there any way to get the query output like this?
- Pallavi
Pallavi,
Your data format is not neat. In one place you are referring hours and another place you are referring it as hrs. Try to maintain one approach.
Now, tell me why do you need this?
Why don't you use another column of integer which holds the priority number during insertion so that you can fetch the value based on that comparison.
Kindly explain your scenario.
Regards.
Satyaki De.
Similar Messages
-
Hi,
I have a doubt regarding Order By Clause in Views.
As per my knowledge, we can't put an order by clause in the subquery that defines view. But when i created a view in Oracle 9i with the order by clause, view got created.
Please see the my view code below :
create or replace view testview
as select * from employees
order by 1,2;
Could anyone please confirm that we can have order by clause in Views in oracle 9i?
Thanks,
TandraAccording to the SQL Reference doc, there is no such restriction for a non-updatable view:
The view subquery cannot select the CURRVAL or NEXTVAL pseudocolumns.
If the view subquery selects the ROWID, ROWNUM, or LEVEL pseudocolumns, those columns must have aliases in the view subquery.
If the view subquery uses an asterisk (*) to select all columns of a table, and you later add new columns to the table, the view will not contain those columns until you re-create the view by issuing a CREATE OR REPLACE VIEW statement.
For object views, the number of elements in the view subquery select list must be the same as the number of top-level attributes for the object type. The datatype of each of the selecting elements must be the same as the corresponding top-level attribute.
You cannot specify the SAMPLE clause.
This restriction exists only for updatable views:
If you want the view to be inherently updatable, it must not contain any of the following constructs:
A set operator
A DISTINCT operator
An aggregate or analytic function
A GROUP BY, ORDER BY, CONNECT BY, or START WITH clause
A collection expression in a SELECT list
A subquery in a SELECT list
Joins (with some exceptions as described in the paragraphs that follow). -
Doubt regarding multiple criteria in order by clause
Hi, I don't understand the effect of multiple elements inside the order by clause. I have the following example table:
factor_x | factor_y | price
=====================
1 | 5 | 1000
2 | 4 | 6970
3 | 3 | 3688
4 | 2 | 9087
5 | 1 | 10000
=====================
So I tried: select price from pricetable order by factor_x; results as follows:
1000
6970
3688
9087
10000
Then: select price from pricetable order by factor_y; results as follows:
10000
9087
3688
6970
1000
Then: select price from pricetable order by factor_x, factor_y; results as follows:
1000
6970
3688
9087
10000
which is same as using order by factor_x. Can anyone tells me what is the effect of adding a 2nd, 3rd..... criterion in the order by clause? Because in this example I cannot see the difference. Many thanks.Hi,
I did a little change in your data. Hope it will help you to understand.
SQL> WITH T AS (SELECT 1 X , 1 Y , 1000 PRICE FROM DUAL UNION A
2 SELECT 2 , 4 , 6970 FROM DUAL UNION ALL
3 SELECT 4 , 3 , 3688 FROM DUAL UNION ALL
4 SELECT 4 , 2 , 9087 FROM DUAL UNION ALL
5 SELECT 4 , 5 , 10000 FROM DUAL)
6 SELECT PRICE,X,Y FROM T ORDER BY X;
PRICE X Y
1000 1 1
6970 2 4
9087 4 2
10000 4 5
3688 4 3
SQL> WITH T AS (SELECT 1 X , 1 Y , 1000 PRICE FROM DUAL UNION A
2 SELECT 2 , 4 , 6970 FROM DUAL UNION ALL
3 SELECT 4 , 3 , 3688 FROM DUAL UNION ALL
4 SELECT 4 , 2 , 9087 FROM DUAL UNION ALL
5 SELECT 4 , 5 , 10000 FROM DUAL)
6 SELECT PRICE,X,Y FROM T ORDER BY X,Y;
PRICE X Y
1000 1 1
6970 2 4
9087 4 2
3688 4 3
10000 4 5
SQL>Regards
Avinash -
Hi, everyone,
I am trying to use the following insert statement to insert records in asc order. if i use the select statement alone the order by clause works fine, but when i use the same sql and add a insert into table statment the order by clause does not work. I am not getting the records in ascending order. could anybody help me in this regard?
INSERT INTO cat_sales_stg
select b.SSC,
ltrim(rtrim(a.CAT_DESC)) cat_desc,
SUM(a.AMOUNT) AMOUNT
FROM Trans a, SSC b
WHERE a.ACCOUNT_NUMBER = TO_CHAR(b.ACCOUNT_NUMBER)
AND a.TMONTH >= 200905 AND a.TMONTH <= 200910
AND a.FORMAT_NAME = 'ABC'
AND b.BMONTH = 200910
AND b.SAMPLE = 3
AND b.BANNER_NAME = 'ABC'
AND b.MODEL_NAME = 'XYZ'
group by b.SSC, ltrim(rtrim(a.CAT_DESC))
order by ssc,cat_desc
Thanks in advanceuser10636796 wrote:
Hi, everyone,
I am trying to use the following insert statement to insert records in asc orderWhat Toon, William, and others have said is that you DON'T insert rows in a specific order. That is completely outside the way relational databases are designed. You insert rows as unordered and use an ORDER BY clause in a SELECT when reading them. ORDER BY is for SELECT statements, not INSERT.
In particular Toon poined out that we can't control where individual rows get stored.
There is a databas object called a varray that can store data in sorted order. I have never seen them used because selecting the data back out again is more work; using an ordinary table and an ORDER BY clause is much easier. -
Suppress "Order By" clause in Answers Query
Hello,
Is it possible to Suppress "Order By" clause in Answers Query.
I'm using a database view as data source. In the view definition, "order by" clause is already specified. Is it possible to get the same order in the OBIEE report??
I do not want to use Sort Order column in the repository.
Thanks,
GirishYou add a rownumber to your DB view and use that to 'sort' your report.
regards
John
http://obiee101.blogspot.com/ -
In Report,order by clause is not working
Hi All,
For the report i prepared the query in this one am using the order by clause,i run the query in the sql plus working fine am getting the data based on the order by clause.when i use the same query for the report am getting the data but not in the order(i.e order by clause in not working).
Please help me how to do this one.
Thanks in Advance.
Regards,
Leelakrishna.GHi Dora,
Data is coming in the proper sequence,but in the first group data is not coming(In first group data first line only displaying,remaining data is not comming).
My req is:
The report is executing based on the creation date(i.e.,from creation date to today).
In this so many Purchase requisitions will be there for this one different approval in different times will be there.
For Ex:
we will take 2 PR number only(for the first one 3 approvals,second one 4 levels of approval).
1. one supplier 1person raised by 1PR submit
No Action
Approve
2. 2Supplier 2personraised by 2PR Submit
NO Action
Forword
Approve
with ur suggestion am trying data is gettin fine but first group first line is coming remaining data is not coing.
EX:
1. one supplier 1person raised by 1PR submit
No Action
Approve
2PR Submit
NO Action
Forword
Approve
Note:"2. 2Supplier 2personraised by " this kind of lines data is missing.
Can you pls check and suggest me how i can do this.
Thanks in Advance.
Regards,
Leelakrishna.G -
Order By clause in Receiver JDBC adapter
Hello All,
Is it possible to construct a select statement that uses an Order by clause in JDBC receiver adapter?
ex : I would like the JDBC adapter to from a query as follows:
<b>Select * from EMP where sal = 10000 order by empid</b>
We are able to construct the select statement without order by clause but not with it.
Can any one suggest me how to do it. This is bit urgent for us.
Thanks
Abinashhi,
did you try with
SQL_DML
http://help.sap.com/saphelp_nw04/helpdata/en/2e/96fd3f2d14e869e10000000a155106/content.htm
Regards,
michal -
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
); -
ORDER BY Clause in XMLAGG is throwing errors.
Hi,
I'm getting errors when generating the XML using the ORDER BY clause in the XMLAgg function. Although, running the SQL directly in SQL Plus doesn't have any problem.
Here is the PL/SQL sample.
DECLARE
vX_XML SYS.XMLTYPE;
vCL_XML CLOB;
BEGIN
SELECT XMLElement("GROUP",
XMLAttributes(Group_Id AS "ID", Group_Name AS "NAME", Group_Description AS "DESC"),
XMLAgg(XMLElement("FIELD",
XMLAttributes(Field_Id AS "ID", Field_Description AS "DESC",
Table_Name AS "TAB_NM", Rule_Name AS "RULE_NM",
Field_Hierarchy_Depth AS "DOL_VAR_CALC")) ORDER BY Field_Description))
INTO vX_XML
FROM (SELECT g.Group_Id, g.Group_Name, g.Group_Description, f.Field_Id, f.Field_Description,
DECODE(f.Table_Name, 'ESTIMATES', 'E', 'LINE_ITEMS', 'L', NULL) Table_Name,
rt.Rule_Name,
CASE NVL(f.Field_Hierarchy_Depth, 0) WHEN 0 THEN 'N' ELSE 'Y' END Field_Hierarchy_Depth
FROM GROUPS g,
Rules r,
Rule_Types rt,
Fields f
WHERE g.Group_Id = 3087
AND r.Group_Id_Fk = g.Group_Id
AND rt.Rule_Type_Id = r.Rule_Type_Id_Fk
AND f.Field_Id = r.Field_Id_Fk) tab
GROUP BY Group_Id, Group_Name, Group_Description;
vCL_XML := vX_XML.GetCLOBVal();
END;
And, here are the errors.
ERROR at line 8:
ORA-06550: line 8, column 23:
PLS-00306: wrong number or types of arguments in call to 'XMLAGG'
ORA-06550: line 8, column 23:
PL/SQL: ORA-00904: "XMLAGG": invalid identifier
ORA-06550: line 6, column 5:
PL/SQL: SQL Statement ignored
Thanks in Advance.
BTW, we are using Oracle 9.2.0.4.This is bug 2785463 with no ETA for a fix
regards
Coby D. Adams Jr. -
Order By Clause with Empty Field values !
Hello,
In Oracle , the order by clause does not return the expected query result, if any of the field value in the order by clause has an empty string.
Oracle treats the empty string as null value and ORDER BY gives a result with the empty string field values listed at last.
For example :
test is a sample table containing "name" field.
Query: "select name from test order by name"
In SQL Server/Access
Result (1)
NAME
(blank)
(blank)
User1
User2
User3
In Oracle
Result (2)
NAME
User1
User2
User3
(blank)
(blank)
I know some of the Work arounds for this as listed below :
1) To use NVL in Order By Clause.
i.e., the modified query
"select name from test order by nvl(name,0)"
gives the result same as Result (1).
2) To have single blank space in the field value if it is empty.
I dont want to use either of these two options b'se it would lead to a mass change in my existing code.
Is there any way i can do a
collation order settings in the Oracle databases to get the results as in MS SQL/Access.
Can Any help me out in Solving this?
Thanks
Devi Shankar
nullBharath,
I am moving this question to the SQL forum.
Regards,
Geoff -
IN clause with ORDER BY clause in sub-queries
Hello,
We generate dynamic queries with the following statement pattern (could be many union/intersect sub-queries):
select my_col
from my_table
where my_col IN
select table_2.my_col , x_col from table_2 where x_col > 10
UNION
select table_3.my_col , y_col from table_3 where y_col > 20
INTERSECT
select table_4.my_col , z_col from table_4 where z_col is between 30 and 50
I know that I can do just the sub-queries w/ an ORDER BY clause as follows (as long as the 2nd parameter in the select stmts are of the same type):
select table_2.my_col , x_col from table_2 where x_col > 10
UNION
select table_3.my_col , y_col from table_3 where y_col > 20
INTERSECT
select table_4.my_col , z_col from table_4 where z_col is between 30 and 50
order by 2 desc
But my questions are:
1. What is (if there is) the syntax that will ensure that the result set order will be that of the ordering of the sub-queries?
Or does my SQL stmt have to have syntactically (but not semantically) change to achieve this?
Thanks,
JimRandolf Geist wrote:
just a minor doubt - I think it is not officially supported to have separate ORDER BYs in a compound query with set operators (e.g. UNION / UNION ALL subsets). Of course one could use inline views with NO_MERGE + NO_ELIMINATE_OBY hints, but I think the only officially supported approach is to use a single, final ORDER BY (that needs to use positional notation as far as I remember).
Randolf,
You're right, of course, about the separate "order by" clauses.
Interestingly the following type of thing does work though (in 10.2.0.3, at least):
with v1 as (
select * from t1 where col1 = 'ABC' order by col2
v2 as (
select * from t1 where col1 = 'DEF' order by col2
select * from v1
union all
select * from v2
;A quick check the execution plan suggsts that Oracle appears to be convering this to the following - even though its technically not acceptable in normal circumstances:
select * from t1 where col1 = 'ABC' order by col2
union all
select * from t1 where col1 = 'DEF' order by col2
;Regards
Jonathan Lewis
http://jonathanlewis.wordpress.com
http://www.jlcomp.demon.co.uk
To post code, statspack/AWR report, execution plans or trace files, start and end the section with the tag {noformat}{noformat} (lowercase, curly brackets, no spaces) so that the text appears in fixed format.
"Science is more than a body of knowledge; it is a way of thinking"
Carl Sagan -
Different order of subgroups groups per configuration in order by clause
Hello,
I have a question regarding Order by and group by of SQL. I have three tables, Groupconfig and ItemConfig and Items.
Group has a extra field which will specify the sorting order of the items when read out from the database.
I have the choice of Sorting by alpa description of the item or a sorting order defined on the item table.
But when I write some SQL to retrieve all the items in the items table and join to the item config table, to get the description of the item and the sorting number,
then join again to the groupconfig table to get the sorting of each group.
Problem is that when I run the query the data is not quite returned in the correct order in each group as defined in the group config for each group.
I know there are differences with the NLS_SORT variable in Oracle but I have a feeling that this is more to do with my SQL can someone help?
I require the overall groups to be retuned by group ID, and within each group a different ordering.
SELECT * FROM
SELECT
GC.GROUPID GP_GROUPID,
GC.DESCRIP GP_DESCRIP,
GC.SORTING_ORDER GP_SORTINGORDER,
IC.SORTING_ORDER IC_SORDER,
IC.DESCR IC_DESCRIPTION,
IC.ITEMID IC_ITEMID,
RID.UNIT I_UNIT,
IC.SORTING_ORDER
FROM
RF_ITEMDETAILS RID,
RF_ITEMCONFIG IC,
RF_GROUPCONFIG GC
WHERE
IC.ITEMID = RID.ITEMID
AND GC.GROUPID = IC.GROUPID
ORDER BY
CASE WHEN GC.SORTING_TYPE = 'N' THEN IC.SORTING_ORDER END,
CASE WHEN GC.SORTING_TYPE = 'A' THEN IC.DESCR END
) GP
ORDER BY GP.GP_SORTINGORDER
But when I run this second group is correct and ordered by Numeric sorting but the first group by the sorting number field is never correct.
Doe this SQL look correct or does anyone know if a good performat way to do this?maaher cant beleive I actually asked that question. thanks for your reply.
I have changed my SQL to build up the Orber by clause using the case statements like so....( and added Product Group table in also!)
SELECT
PC.PRODUCTID PC_ID,
PC.DESCR PC_DESC,
PC.SORTING_ORDER PC_SORT,
GC.GROUPID GC_GROUPID,
GC.DESCRIP GC_DESCRIP,
GC.SORTING_ORDER GC_SORTINGORDER,
IC.ITEMID IC_ITEMID,
IC.DESCR IC_DESCRIPTION,
IC.SORTING_ORDER IC_SORDER,
RID.UNIT ID_UNIT
FROM
RF_ITEMDETAILS RID,
RF_ITEMCONFIG IC,
RF_GROUPCONFIG GC,
RF_PRODUCTCONFIG PC
WHERE
-- Item Configuration
IC.ITEMID = RID.ITEMID
AND IC.VISIBLE = 'Y'
-- Group Configuration
AND GC.GROUPID = IC.GROUPID
AND GC.VISIBLE = 'Y'
-- Product Configuration
AND PC.PRODUCTID = GC.PRODUCTID
AND PC.VISIBLE = 'Y'
-- Order by
ORDER BY
PC.SORTING_ORDER,
CASE WHEN PC.SORTING_TYPE = 'N' THEN GC.SORTING_ORDER END,
CASE WHEN PC.SORTING_TYPE = 'A' THEN GC.DESCRIP END,
CASE WHEN GC.SORTING_TYPE = 'N' THEN IC.SORTING_ORDER END,
CASE WHEN GC.SORTING_TYPE = 'A' THEN IC.DESCR END -
Sorting character column ( used in order by clause dynamically)
Hi,
I need help on sorting character-based numbers. Like say, I want to sort customers based on street numbers(which is a character string being used in the
order by clause) they live in.
The criteria are :
i. Numbers must take precedence.
This being a character string, 1000001 comes before 2. This shouldn't happen. And you cannot use to_number
since using it with a string having characters in it would raise an error.
ii. If only a single alphabet occurs as the last character, then treat the whole string as a number except the last character and then sort it
as if sorting a number. Something like : if you have 1000A, 200D, 200B, 1000X, the result would be 200B,200D,1000A,1000X.
iii. if a character occurs elsewhere in the string, then perform the search normally as if performing a character search.
The output of the following data :
100
A101
B100A
110C
C120B
120
100020
120C
C1100
100D
would be like :
100
100D
110C
120
120C
100020
A101
B100A
C120B
C1100
Please note that the sort is being done dynamically, so I could have access to the values of the street numbers only during run time.
Any help is really appreciated.
Thanks in advance.
Regards,
Anil.Create a function to test whether the column is numeric :
create FUNCTION is_numeric(v_number VARCHAR2)
RETURN INTEGER
IS
l_number NUMBER;
BEGIN
IF INSTR(UPPER(v_number),'E') > 0 THEN
RETURN 0;
END IF;
l_number := TO_NUMBER(v_number);
RETURN 1;
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END;
And try this query
Assume the table name is TEST with column STREET
select * from TEST
order by case is_numeric(STREET) when 1 then LPAD(STREET,20, ' ') else STREET end
Please make sure that 20 mentioned above is the column size for STREET.
Hope this helps.
-Nags -
Case statement in order by clause
Hi,
I have written the below query which is having CASE statement in ORDER BY clause. Please let me know what mistake i have done in the query because am getting "Missing Keyword" Error.
SELECT opn_quest_id, seq_nbr
FROM opinion_question
order by case when :p=1 then
opn_quest_id,seq_nbr
else
opn_quest_id
end;
Thanks,
Santhosh.STry Ignore the following solution.
SELECT opn_quest_id, seq_nbr
FROM opinion_question
ORDER BY CASE
WHEN : p = 1 THEN opn_quest_id || seq_nbr
ELSE opn_quest_id
END;
What are the data type of the corresponding columns used in the CASE Statement? I have assumed it to be strings.
!http://www.mysmiley.net/imgs/smile/sad/sad0049.gif! My Apologies....
Regards,
Jo
Edited by: Joice John on Jul 13, 2009 3:07 AM
Wrong Solution. Corrected by Sven. -
Evaluate Expression in Order By Clause
Hi,
I'm having trouble to execute following 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 Order By Clause. Here is the error.
javax.servlet.ServletException: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing the query [Select r.firstname, r.lastname, (e.distance/e.duration) from Runner r Inner Join r.event e Order BY (e.distance/e.duration) Desc;], line 1, column 101: unexpected token [(].
Internal Exception: NoViableAltException(81!=[1328:1: orderByItem returns [Object node] : (n= stateFieldPathExpression (a= ASC | d= DESC | ) | i= IDENT (a= ASC | d= DESC | ) );])
Can someone please show me how to fix it.
Thanks,
AdamHello Adam
You cannot use "(e.distance/e.duration)" as an order by clause. The correct query would be:
"Select r.firstname, r.lastname, (e.distance/e.duration) as speed from Runner r Inner Join r.event e Order BY speed Desc"
but will not work until bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=331575 is fixed. Please vote for it.
Best Regards,
Chris
Maybe you are looking for
-
How do I download photos from Facebook to iPhoto
How do I download photos from facebook to iPhoto? Ihave osx and iPhoto 11 9.2.3.
-
I just purchased CS5 for my Windows 7 computer and am printing for the first time to my Epson RX595 printer. I go into the print settings in InDesign and go to the printer settings to select Best Photo. When I say OK, go out of the printer settings
-
My iphone 4 plays music outloud but has no other sound
My iphone 4 just recently started to not ring or use keyboard sounds. ive tried everything and i dont know how to fix it. the one thing that still works is the music. how should i fix it?
-
Itunes quits when i try to open a tvshow i purchased
working on windows vista with the service packs applied and all the updates... the shows i've downloaded from itunes will not play. well, once i got one to play, but other than that nothing. i get the "itunes has stopped working" dialog... windows pr
-
I want to buy a camcorder that works with fcp x. I looked at the supported camera, but those supported expensive for me. I can get a camera between $ 500- 700.00. Is there anybody that know a camcorder that works for me? I appreciate the help, Talla