order by in Minus Operator [message #283079] |
Mon, 26 November 2007 01:25  |
pragatimathur
Messages: 42 Registered: July 2006 Location: DELHI
|
Member |
|
|
I am using order by with minus operator in my query but its giving error: "SQL Command Not Properly Ended"
Without Order By, output is coming but i have to use it
so please help me out.
SELECT
idp055.ROWID,
IDP010.AYBLNO
FROM
IDP055 ,
IDP010 ,
IDP050
WHERE
IDP055.EYBLNO = IDP010.AYBLNO AND
IDP010.AYBLNO = IDP050.BYBLNO
GROUP BY IDP055.ROWID,IDP010.AYBLNO
ORDER BY 1
MINUS
SELECT
idp055.ROWID,
IDP010.AYBLNO
FROM
IDP055 ,
IDP010 ,
IDP050
WHERE
IDP055.EYBLNO = IDP010.AYBLNO AND
IDP010.AYBLNO = IDP050.BYBLNO AND
ROWNUM<=3
GROUP BY IDP055.ROWID, IDP010.AYBLNO
ORDER BY 1
|
|
|
|
Re: order by in Minus Operator [message #283086 is a reply to message #283079] |
Mon, 26 November 2007 01:32   |
|
I mean to say try this query:
(SELECT
idp055.ROWID,
IDP010.AYBLNO
FROM
IDP055 ,
IDP010 ,
IDP050
WHERE
IDP055.EYBLNO = IDP010.AYBLNO AND
IDP010.AYBLNO = IDP050.BYBLNO
GROUP BY IDP055.ROWID,IDP010.AYBLNO)
MINUS
(SELECT
idp055.ROWID,
IDP010.AYBLNO
FROM
IDP055 ,
IDP010 ,
IDP050
WHERE
IDP055.EYBLNO = IDP010.AYBLNO AND
IDP010.AYBLNO = IDP050.BYBLNO AND
ROWNUM<=3
GROUP BY IDP055.ROWID, IDP010.AYBLNO )
ORDER BY 1
Kiran
|
|
|
|
|
|
|
Re: order by in Minus Operator [message #283130 is a reply to message #283087] |
Mon, 26 November 2007 03:11   |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
rajavu1 wrote on Mon, 26 November 2007 07:33 |
1. No need to use GROUP BY IDP055.ROWID,IDP010.AYBLNO if you want ot select IDP055.ROWID IDP010.AYBLNO . rather Use DISTINCT Keyword
|
Why use distinct?
WITH t AS (SELECT 1 x FROM dual
UNION ALL
SELECT 1 x FROM dual
UNION ALL
SELECT 1 x FROM dual
UNION ALL
SELECT 2 x FROM dual
UNION ALL
SELECT 3 x FROM dual
UNION ALL
SELECT 4 x FROM dual)
,
y as (SELECT 2 b FROM dual
UNION ALL
SELECT 4 FROM dual)
SELECT x FROM t
MINUS
SELECT b FROM y;
X
----------------------
1
3
2 rows selected
All set operators (with the exception of UNION ALL) get rid of duplicates, therefore distinct is pointless.
|
|
|
|
|
Re: order by in Minus Operator [message #283155 is a reply to message #283151] |
Mon, 26 November 2007 03:55   |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
The only way to get an ordered list is to use the order by clause. End of story. This is well documented.
The only way to get an ordered list is to use the order by clause.
I do not care what your query shows
The only way to get an ordered list is to use the order by clause.
|
|
|
|
|
|
|
Re: order by in Minus Operator [message #283349 is a reply to message #283237] |
Mon, 26 November 2007 19:54   |
rleishman
Messages: 3728 Registered: October 2005 Location: Melbourne, Australia
|
Senior Member |
|
|
Just to round this one out:
MINUS currently employs a Binary Sort, whereas ORDER BY performs an NLS_SORT / NLS_LANGUAGE sort, which may be specified as Binary, or indistinguishable from binary for most data.
I'm not sure how Michel would replicate his excellent example with numbers - I thought NLS only controlled position and characters used for separators - and that binary values would correspond to absolute values.....
There is no guarantee that Oracle will continue to use a Binary Sort for MINUS (or INTERSECT or UNION) in the future.
10.2 is a good example of this, and sorts are no longer always used to resolve GROUP BY like they used to.
Even if you are happy with binary sorts and not planning to upgrade ever, parallel query may still bring you unstuck. I haven't checked in this case, but in many operations it partitions the data and performs the operation separately on each partition.
In the case of MINUS, that could cause the rows of each partition to return in sorted order, but the overall order of partitions would be random.
Ross Leishman
|
|
|
Re: order by in Minus Operator [message #283359 is a reply to message #283079] |
Mon, 26 November 2007 22:58   |
|
One more doubt, when we store numbers in VARCHAR2 field and sort it using ORDER BY cluase, then the sorted output returns how much true?
Somwhere i heard/read we better convert it into number(TO_NUMBER) and then should do sorting, ORDER BY.
What would be the difference here?
Regards,
Kiran.
|
|
|
|
Re: order by in Minus Operator [message #283367 is a reply to message #283359] |
Mon, 26 November 2007 23:24   |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
I would strengthen this advice: NEVER store numbers as VARCHAR2.
You shall enforce, it contains ONLY numeric characters (what about decimal point character, thousand separator, scientific notation?), otherwise using TO_NUMBER will end with an error.
This is a common principle, simple difference between alphabetic (for VARCHAR2) and numerical (for NUMBER) order.
|
|
|
Re: order by in Minus Operator [message #283370 is a reply to message #283079] |
Mon, 26 November 2007 23:33   |
|
There will be a difference.
SQL> CREATE TABLE test_ord (col VARCHAR2(10));
Table created.
SQL> INSERT INTO test_ord
2 (col
3 )
4 VALUES ('121'
5 );
1 row created.
SQL> INSERT INTO test_ord
2 (col
3 )
4 VALUES ('0123'
5 );
1 row created.
SQL> INSERT INTO test_ord
2 (col
3 )
4 VALUES ('0111'
5 );
1 row created.
SQL> INSERT INTO test_ord
2 (col
3 )
4 VALUES ('3'
5 );
1 row created.
SQL> INSERT INTO test_ord
2 (col
3 )
4 VALUES ('09'
5 );
1 row created.
SQL> COMMIT ;
Commit complete.
SQL> SELECT *
2 FROM test_ord
3 ORDER BY col;
COL
----------
0111
0123
09
121
3
SQL> SELECT *
2 FROM test_ord
3 ORDER BY TO_NUMBER (col);
COL
----------
3
09
0111
121
0123
Regards,
Kiran.
|
|
|
|
|
|
|
|
|
Re: order by in Minus Operator [message #283396 is a reply to message #283349] |
Tue, 27 November 2007 00:44  |
 |
Michel Cadot
Messages: 68770 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Ross,
It is possible to replace the example with numbers using partitioned tables and/or parallelism.
I'm pretty confident we can do it with clustered tables.
This is why I said it is not so easy to build an example with numbers.
Regards
Michel
|
|
|