Home » SQL & PL/SQL » SQL & PL/SQL » Order by clause (Oracle 10g)
Order by clause [message #629071] Wed, 03 December 2014 02:21 Go to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Hi All,

The order of the query should not change at any point of time if so it will result a complete error in the report.
For that whether I have to add any order by cluse at the last or this is enough . Please suggest.

WITH temp AS
 (SELECT ROWNUM RN,particular,hiredate,sal,comm,
         SUM(balance + sal - comm) OVER(ORDER BY hiredate NULLS FIRST) balance
    FROM (SELECT 'OPEN' particular,to_date('') hiredate,0 sal,0 comm,344 balance
            FROM dual
          UNION ALL
          SELECT ename,hiredate,NVL(sal, 0) sal,NVL(comm, 0) comm,0 balance
            FROM emp2
           ORDER BY hiredate nulls FIRST))
SELECT *
  FROM temp
UNION ALL
SELECT MAX(rn) + 1 rn,'TOTAL' particular,to_date('') hiredate,
       SUM(BALANCE) sal,SUM(COMM) comm,0 balance
  FROM temp
UNION ALL
SELECT MAX(rn) + 2 rn,'FINAL' particular,to_date('') hiredate,
       0 sal,0 comm,min (balance) balance
  FROM temp 
 WHERE rn=(select max(rn) from temp);
 -- order by rn


Regards,
Nathan

[Updated on: Wed, 03 December 2014 02:24]

Report message to a moderator

Re: Order by clause [message #629072 is a reply to message #629071] Wed, 03 December 2014 02:29 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
If you want to be absolutely sure then you need an order by at the end.
Re: Order by clause [message #629074 is a reply to message #629071] Wed, 03 December 2014 02:31 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
In my opinion:

Quote:

whether I have to add any order by cluse at the last

Yes.

Quote:

or this is enough

It is not.
Re: Order by clause [message #629078 is a reply to message #629074] Wed, 03 December 2014 02:44 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Thank you very much. I will definitely add that. But for the curiosity I have added already at two places once in analytical, second in query.Is it correct that how many times we will select from an inlive view those many times we need order by clause.And any other way how we can improve the query as well.

[Updated on: Wed, 03 December 2014 02:47]

Report message to a moderator

Re: Order by clause [message #629083 is a reply to message #629078] Wed, 03 December 2014 03:03 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Remove the ORDER BY in the most inner query (in "temp" subquery) and use ROW_NUMBER instead of ROWNUM (which does not change you have to use ORDER BY at the end of the whole query).

Re: Order by clause [message #629086 is a reply to message #629083] Wed, 03 December 2014 03:17 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Thanks Michel,You mean this
WITH temp AS
 (SELECT ROW_NUMBER()OVER(ORDER BY hiredate nulls FIRST) RN,particular,hiredate,sal,comm,
         SUM(balance + sal - comm) OVER(ORDER BY hiredate NULLS FIRST) balance
    FROM (SELECT 'OPEN' particular,to_date('') hiredate,0 sal,0 comm,344 balance
            FROM dual
          UNION ALL
          SELECT ename,hiredate,NVL(sal, 0) sal,NVL(comm, 0) comm,0 balance
            FROM emp2
           ))
SELECT *
  FROM temp
UNION ALL
SELECT MAX(rn) + 1 rn,'TOTAL' particular,to_date('') hiredate,
       SUM(BALANCE) sal,SUM(COMM) comm,0 balance
  FROM temp
UNION ALL
SELECT MAX(rn) + 2 rn,'FINAL' particular,to_date('') hiredate,
       0 sal,0 comm,min (balance) balance
  FROM temp 
 WHERE rn=(select max(rn) from temp)
  order by rn;
Re: Order by clause [message #629089 is a reply to message #629086] Wed, 03 December 2014 03:24 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

max(rn) is count(*) over(), so add this in "temp" subquery then you just have to use cnt and test that rn=cnt in your last query.
Re: Order by clause [message #629090 is a reply to message #629086] Wed, 03 December 2014 03:25 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
The max and min in the final union all aren't needed - you're selecting a single row there.
Re: Order by clause [message #629092 is a reply to message #629090] Wed, 03 December 2014 03:44 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

The max and min in the final union all we have used for not to use group by again. And the last order by removed and count is added.Hence the query is like

WITH temp AS
 (SELECT count(*)OVER(ORDER BY 1) cnt,
         ROW_NUMBER()OVER(ORDER BY hiredate nulls FIRST) RN,particular,hiredate,sal,comm,
         SUM(balance + sal - comm) OVER(ORDER BY hiredate NULLS FIRST) balance
    FROM (SELECT 'OPEN' particular,to_date('') hiredate,0 sal,0 comm,344 balance
            FROM dual
          UNION ALL
          SELECT ename,hiredate,NVL(sal, 0) sal,NVL(comm, 0) comm,0 balance
            FROM emp2
           ))
SELECT  RN,particular,hiredate,sal,comm,balance
  FROM temp
UNION ALL
SELECT max(rn)+1 rn ,'TOTAL' particular,to_date('') hiredate,
       SUM(BALANCE) sal,SUM(COMM) comm,0 balance
  FROM temp
UNION ALL
SELECT MAX(rn) + 2 rn,'FINAL' particular,to_date('') hiredate,
       0 sal,0 comm,min(balance) balance
  FROM temp 
 WHERE rn=cnt;  
Re: Order by clause [message #629093 is a reply to message #629092] Wed, 03 December 2014 03:51 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Remove ORDER BY 1 in COUNT, the OVER clause should contain nothing (as I posted).

"SELECT max(rn)" is "SELECT cnt".
Re: Order by clause [message #629094 is a reply to message #629093] Wed, 03 December 2014 03:59 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

WITH temp AS
 (SELECT count(*)OVER() cnt,
         ROW_NUMBER()OVER(ORDER BY hiredate nulls FIRST) RN,particular,hiredate,sal,comm,
         SUM(balance + sal - comm) OVER(ORDER BY hiredate NULLS FIRST) balance
    FROM (SELECT 'OPEN' particular,to_date('') hiredate,0 sal,0 comm,344 balance
            FROM dual
          UNION ALL
          SELECT ename,hiredate,NVL(sal, 0) sal,NVL(comm, 0) comm,0 balance
            FROM emp2
           ))
SELECT  RN,particular,hiredate,sal,comm,balance
  FROM temp
UNION ALL
SELECT cnt+1 rn ,'TOTAL' particular,to_date('') hiredate,
       SUM(BALANCE) sal,SUM(COMM) comm,0 balance
  FROM temp group by cnt
UNION ALL
SELECT cnt + 2 rn,'FINAL' particular,to_date('') hiredate,
       0 sal,0 comm,min(balance) balance
  FROM temp 
 WHERE rn=cnt group by cnt;  

[Updated on: Wed, 03 December 2014 03:59]

Report message to a moderator

Re: Order by clause [message #629095 is a reply to message #629092] Wed, 03 December 2014 04:01 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
sss111ind wrote on Wed, 03 December 2014 09:44
The max and min in the final union all we have used for not to use group by again.

That makes no sense. You do not need aggregate functions or group by on a select that only ever selects one row.
Re: Order by clause [message #629096 is a reply to message #629092] Wed, 03 December 2014 04:02 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
sss111ind wrote on Wed, 03 December 2014 09:44
And the last order by removed

You always need order by if you want the results in a particular order.
Re: Order by clause [message #629099 is a reply to message #629095] Wed, 03 December 2014 04:15 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Hi CookieMonster,

For this to get the serialnumber max function has been used if we remove this then we have to add group by in the query. I am not getting the exact point what you are referring to.

SELECT max(rn)+1 rn ,'TOTAL' particular,to_date('') hiredate,
       SUM(BALANCE) sal,SUM(COMM) comm,0 balance
  FROM temp



Re: Order by clause [message #629100 is a reply to message #629099] Wed, 03 December 2014 04:36 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
That one needs max.
The final one doesn't need max.
Removing max never causes you to have to add group by - that makes no sense.
Re: Order by clause [message #629101 is a reply to message #629100] Wed, 03 December 2014 04:42 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

But for the final one I have to generate the serial number as Max(rownum)+2 means 17. How I can get that value.
Re: Order by clause [message #629102 is a reply to message #629101] Wed, 03 December 2014 04:47 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

It's My mistake I got it finally that already I am using there as means by default I will get 15th row if I add 2 to rn then automatically i will get the seraial number.
sELECT MAX(rn) + 2 rn,'FINAL' particular,to_date('') hiredate,
       0 sal,0 comm,min (balance) balance
  FROM temp 
 WHERE rn=(select max(rn) from temp);


Now my query will look like
WITH temp AS
 (SELECT ROWNUM RN,particular,hiredate,sal,comm,
         COUNT(*) OVER() cnt,
         SUM(balance + sal - comm) OVER(ORDER BY hiredate NULLS FIRST) balance
    FROM (SELECT 'OPEN' particular,to_date('') hiredate,0 sal,0 comm,344 balance
            FROM dual
          UNION ALL
          SELECT ename,hiredate,NVL(sal, 0) sal,NVL(comm, 0) comm,0 balance
            FROM emp2
           ORDER BY hiredate NULLS FIRST))
SELECT RN,particular,hiredate,sal,comm,balance
  FROM temp
UNION ALL
SELECT MAX(rn) + 1 rn,'TOTAL' particular,to_date('') hiredate,
       SUM(sal) sal,SUM(COMM) comm,0 balance
  FROM temp
UNION ALL
SELECT rn + 2 rn,'FINAL' particular,to_date('') hiredate,
       0 sal,0 comm,balance balance
  FROM temp 
 WHERE rn=cnt
order by rn;



Thank you all.

[Updated on: Wed, 03 December 2014 04:49]

Report message to a moderator

Re: Order by clause [message #629103 is a reply to message #629102] Wed, 03 December 2014 04:51 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
You've already posted code that gets the rn for the final row that doesn't use max at all, so why are you re-adding it?
Re: Order by clause [message #629104 is a reply to message #629103] Wed, 03 December 2014 04:51 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
And that last update is what I was talking about.
Re: Order by clause [message #629105 is a reply to message #629104] Wed, 03 December 2014 04:52 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Remove the order by in the with clause - it's not needed.
Re: Order by clause [message #629106 is a reply to message #629105] Wed, 03 December 2014 04:57 Go to previous message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

As you suggested I have removed the order by from with clause it is mismatching (even wrong output) with the query result containing the order by inside the
with clause.
Previous Topic: Job Scheduing
Next Topic: Procedure for counting rows in a table
Goto Forum:
  


Current Time: Fri Apr 26 05:43:29 CDT 2024