DBAsupport.com Forums - Powered by vBulletin
Results 1 to 6 of 6

Thread: Help in order by

  1. #1
    Join Date
    Mar 2006
    Posts
    5

    Question Help in order by

    Hi,

    I have a query to select months from date in a table which matches certain conditions, but result set contains month in sorted order not in the original order.

    Help me to get months in original order, my query is like this,

    SELECT SUBSTR(TO_CHAR(d.DOWNLOADDATE),4,6), COUNT(*) FROM DOWNLOAD d
    WHERE d.DOWNLOADDATE BETWEEN TO_DATE('07/01/2005','mm/dd/yyyy') AND TO_DATE(TO_CHAR(SYSDATE,'mm/dd/yyyy'),'mm/dd/yyyy')
    GROUP BY SUBSTR(TO_CHAR(d.DOWNLOADDATE),4,6)

  2. #2
    Join Date
    Aug 2002
    Location
    Colorado Springs
    Posts
    5,253
    What do you define as the original order, because you know that there is no row ordering inherent in a relational database, right?
    David Aldridge,
    "The Oracle Sponge"

    Senior Manager, Business Intelligence Development
    XM Satellite Radio
    Washington, DC

    Oracle ACE

  3. #3
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,684
    Hi.

    This is not a clever way to pull out the month from a date. It is better to use the TO_CHAR(date, 'MM') format mask, that way you are not dependent on implicit conversions to turn the date into a string.

    When you convert the month to a string, it ceases to be a number, so it is sorted as ASCII text. As a result, 10 comes before 2. In order to prevent this you need to convert the resulting string back to a number:

    Code:
    SELECT TO_NUMBER(TO_CHAR(d.DOWNLOADDATE, 'MM')) AS month, COUNT(*)
    FROM   DOWNLOAD d
    WHERE  d.DOWNLOADDATE BETWEEN TO_DATE('07/01/2005','mm/dd/yyyy') AND TO_DATE(TO_CHAR(SYSDATE,'mm/dd/yyyy'),'mm/dd/yyyy')
    GROUP BY TO_NUMBER(TO_CHAR(d.DOWNLOADDATE, 'MM'))
    ORDER BY 1;
    Of course, if the time period extends over more than a single year, you will be combining months from separate years, so you might want to include the year, to make this distinction:

    Code:
    SELECT TO_NUMBER(TO_CHAR(d.DOWNLOADDATE, 'YYYY')) AS year,
           TO_NUMBER(TO_CHAR(d.DOWNLOADDATE, 'MM')) AS month,
           COUNT(*)
    FROM   DOWNLOAD d
    WHERE  d.DOWNLOADDATE BETWEEN TO_DATE('07/01/2005','mm/dd/yyyy') AND TO_DATE(TO_CHAR(SYSDATE,'mm/dd/yyyy'),'mm/dd/yyyy')
    GROUP BY TO_NUMBER(TO_CHAR(d.DOWNLOADDATE, 'YYYY')), TO_NUMBER(TO_CHAR(d.DOWNLOADDATE, 'MM'))
    ORDER BY 1, 2;
    Cheers

    Tim...
    Last edited by TimHall; 03-14-2006 at 11:39 AM.
    Tim...
    OCP DBA 7.3, 8, 8i, 9i, 10g, 11g
    OCA PL/SQL Developer
    Oracle ACE Director
    My website: oracle-base.com
    My blog: oracle-base.com/blog

  4. #4
    Join Date
    Aug 2002
    Location
    Colorado Springs
    Posts
    5,253
    TO_DATE(TO_CHAR(SYSDATE,'mm/dd/yyyy'),'mm/dd/yyyy') is the same as SYSDATE, no?
    David Aldridge,
    "The Oracle Sponge"

    Senior Manager, Business Intelligence Development
    XM Satellite Radio
    Washington, DC

    Oracle ACE

  5. #5
    Join Date
    Feb 2005
    Posts
    158
    "TO_DATE(TO_CHAR(SYSDATE,'mm/dd/yyyy'),'mm/dd/yyyy') is the same as SYSDATE, no?"
    Same as TRUNC(SYSDATE)

    Try the following SQL :
    SELECT SUBSTR(TO_CHAR(TRUNC(d.DOWNLOADDATE,'MM')),4,6), COUNT(*) FROM DOWNLOAD d
    WHERE d.DOWNLOADDATE BETWEEN TO_DATE('07/01/2005','mm/dd/yyyy') AND TO_DATE(TO_CHAR(SYSDATE,'mm/dd/yyyy'),'mm/dd/yyyy')
    GROUP BY TRUNC(d.DOWNLOADDATE,'MM')
    order by TRUNC(d.DOWNLOADDATE,'MM')

  6. #6
    Join Date
    Mar 2006
    Posts
    5
    HI,

    Ur query gave me the result i wanted, thanks a lot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width