Sort by month query

All,
I'm trying to display "Mon" (i.e. Jan, Feb, Mar, ...), but sort by "MM" (i.e. Mar, Apr, May) but am having issues with the query. This is the query I'm trying to run:
<pre>
select null link, to_char(date_created, 'Mon'), count(applicant_id)
from applicant
group by null, to_char(date_created, 'Mon')
order by to_char(date_created,'MM')
</pre>
Looking for some advice on how to sort by the cardinal month value while displaying the month name value.
Thanks.

One solution would be:
select null link, createdate, cnt
from ( select to_char(date_created,'MM') sortdate, to_char(date_created, 'Mon') createdate, count(applicant_id) cnt
       from applicant
       group by to_char(date_created,'MM'), to_char(date_created, 'Mon')
order by sortdate

Similar Messages

  • How do I sort by month ? using the "Month" format

    Hello All,
    Is it possible to sort by month using the follwoing SQL.
    Select TO_CHAR (RCA_CLOSE_DATE, 'MON-YY') "Month",
    SUM (ADJUSTMENT_INCL_GST)
    From My_Table
    Group By TO_CHAR (RCA_CLOSE_DATE, 'MON-YY')
    Output
    Month Sum(Adjustment Incl Gst)
    APR-06 $6,539,983.08
    AUG-06 $23,122,705.31
    FEB-06 $14,365,106.52
    JUL-06 $19,776,122.04
    JUN-06 $20,161,278.12
    MAR-06 $8,248,955.87
    MAY-06 $18,498,683.04
    NOV-06 $24,120,095.49
    OCT-06 $12,781,721.23
    SEP-06 $15,632,604.21
    Hope someone can help
    Frank

    If the desired sort order is MM-YYYY, then how would...
    ORDER BY
        TO_CHAR(RCA_CLOSE_DATE, 'MM-YYYY') ...not achieve this?
    Perhaps the original poster could clarify what they mean by "sort by month". In numeric order by month then year, in numeric order by year then month, or in some other order?
    If you do not like TO_CHAR, another approach (again assuming "numeric order by month then year" is what is desired) might be:
    SELECT
        TO_CHAR(RCA_CLOSE_DATE, 'MON-YY') AS MONTH,
        SUM(ADJUSTMENT_INCL_GST) AS TOTAL
    FROM
        MY_TABLE
    GROUP BY
        EXTRACT(MONTH FROM RCA_CLOSE_DATE),
        EXTRACT(YEAR FROM RCA_CLOSE_DATE),
        TO_CHAR(RCA_CLOSE_DATE, 'MON-YY')
    ORDER BY
        EXTRACT(MONTH FROM RCA_CLOSE_DATE),
        EXTRACT(YEAR FROM RCA_CLOSE_DATE)In any event, in a query with GROUP BY clause:
    1. The expression(s) in ORDER BY clause do not need to appear in the SELECT clause.
    2. The expression(s) in ORDER BY clause do need to appear in the GROUP BY clause (unless you want to order on the result of an aggregate expression, like involving SUM, which does not appear to be the case here).
    3. Expressions in the SELECT clause do need to appear in the GROUP BY clause, unless they are aggregate expressions, like involving SUM.
    Hope this helps.
    P.S. If the desired order is "in numeric order by year then month", then you might use TRUNC like this:
    SELECT
        TO_CHAR(RCA_CLOSE_DATE, 'MON-YY') AS MONTH,
        SUM(ADJUSTMENT_INCL_GST) AS TOTAL
    FROM
        MY_TABLE
    GROUP BY
        TRUNC(RCA_CLOSE_DATE, 'MONTH'),
        TO_CHAR(RCA_CLOSE_DATE, 'MON-YY')
    ORDER BY
        TRUNC(RCA_CLOSE_DATE, 'MONTH')Doing the TRUNC in a subquery and formatting the result in the outer query as John has suggested may be more efficient.

  • Display Month verbiage on chart but sort by month number

    Hello,
    I have inserted a simple bar chart (values by month for a single year) and my goal is to get the data values to group by month.
    I found that I can achieve this by using a formula that extracts the month from my Date/TIme field MONTH({Command.OB_DELIVERY_DATE}).
    This works OK and displays months as 1, 2, 3,  etc. on my chart), but I want to display the month text instead (Jan, Feb, Mar)
    I tried a second variable:  ToText({Command.OB_DELIVERY_DATE},'MMMM')
    This works in displaying the verbiage of the months, however, the problem is that it groups the months on my chart alphabetically now (April is now my 1st month instead of January).
    What is the trick to displaying month verbiage on my chart, but still sorting these months in their proper numerical order?
    Many Thanks!

    Hi Dave,
    Try this please:
    1) Insert a Crosstab and place it on the Report Header. Use the Delivery Date field as the row and add the measure field as the Summary Field with the right Sum function
    2) Highlight the Delivery Date field > Group Options > set it to print 'For Each Mointh'
    3) While in the Crosstab Expert highlight the Delivery Date field > Click Group Options > Options  tab > check the option 'Customize Group Name field' > Select 'use formula as group name' and click the formula button beside. Use this code:
    ToText({Command.OB_DELIVERY_DATE},'MMMM')
    4) Right-click the Crosstab and select Insert Chart. Choose the right Chart Type
    5) Suppress the Section that holds the Crosstab
    Hope this helps.
    -Abhilash

  • Sort in a Query as a Webservice ?

    Hi everyone,
    I can't manage to apply a sort filter in a query as a webservice, is there a way to do it ? We are using BO XI R2 with webi.
    thank you.
    Sebastien

    Hi Alan,
    thank you for your reply. I can also do that in the Webi / infoview control panel, but I am talking of applying a sort in a query built with the "query as web service wizard".
    Do you say that in this wizard you can apply sorts ? I don't have any right click function on the objects in this part of the interface.
    thank you.
    Sebastien
    Edited by: Sebastien Anezo on Jan 7, 2009 4:28 PM

  • How to sum time values sorted by month/year?

    I would like to sum time values that are sorted by month/year. (Concept is based on a pilot's logbook)
    In Table 1, Column A is a date; Column B is the associated time value (duration).
    In Table 2, I would like to Sum the time value of Table 1 Column B by month an year.
    TABLE 1
    Date
    Time
    Header 3
    Jan 1, 2013
    2:30
    Jan 3, 2013
    4:15
    Jan 9, 2013
    3:55
    Feb 2, 2013
    5:01
    Feb 10, 2013
    1:33
    March 3, 2013
    5:55
    TABLE 2
    Month/YR
    Total Time
    Header 3
    Jan-13
    10:40
    Feb-13
    6:34
    Mar-13
    5:55
    Apr-13

    smash,
    Here's an example of what can be done:
    The formula in the Total column of Summary is:
    =SUMIFS(Log :: B, Log :: A, ">="&A, Log :: A, "<"&EOMONTH(A, 0)+1)
    The Month column of Summary is filled with a series of dates that can be formatted as Jan-13, etc, if you wish.
    I used the 0h 0m format for the Durations to distinguish it from times.
    Jerry

  • Chart axis sorting by Month

    I have two database fields, "Month" and "Year" which will be combined in a formula for the purpose of a chart.
    (eg: "{Table.Month}" and "{Table.YEAR}" = January 2008)
    My question is, if I use a line chart, how can I have Crystal sort the Month/Year properly instead of having it sorted alphabetically like it is already doing? 
    As of right now, I'm getting April 2007, April 2008, August 2007, August 2008, etc...

    Try to create a formula to return a perfect date format by combining both month and year. Also try to combine a day value as 1 so that you will get a complete date. Now use this formula in chart and click on order button and choose group by monthly.
    Hope this Helps!
    Raghavendra

  • Applying sort to a query returned by a stored procedure

    I am looking for some advice on the best approach for applying a dynamic sort to a query returned by a stored procedure.
    We have a stored procedure that has 3 inputs fields which are used to specify sort columns and it has an additional 3 fields to indicate if the corresponding input column is to be sorted in ascending or descending order. We presently accomplish this by using dynamic SQL in the procedure but this approach has some drawbacks. Ideally we would like these queries to compile just like any other cursor. We have tried using decodes but this does not seem practical or easy to maintain.
    This procedure is used by a web application that allows the user to click on a column header to specify their sort preference. The previous sort selection becomes the second sort field and the one before that the third.
    Your advice is much appreciated!

    I see, so you want to be able to sort by "name desc, age asc, salary asc", for example.
    there is no built in option. it's either dynamic sql, or decodes.
    the decodes could still work, with only 6 lines, but the problem is handling mixed data types. I'll stick with sort col of 1=name (char), 2=number_col, 3=date_col
    order by
      decode(sort_order1, 'A', decode (sort_col1, 1,name_col, 2,to_char(number_col,'9999999999.00000'), 3,to_char(date_col,'yyyymmddhh24miss'), 'X'),'X'),
      decode(sort_order1, 'D', decode (sort_col1, 1,name_col, 2,to_char(number_col,'9999999999.00000'), 3,to_char(date_col,'yyyymmddhh24miss'), 'X'),'X') DESC,
      decode(sort_order2, 'A', decode (sort_col2, 1,name_col, 2,to_char(number_col,'9999999999.00000'), 3,to_char(date_col,'yyyymmddhh24miss'), 'X'),'X'),
      decode(sort_order2, 'D', decode (sort_col2, 1,name_col, 2,to_char(number_col,'9999999999.00000'), 3,to_char(date_col,'yyyymmddhh24miss'), 'X'),'X') DESC,
      decode(sort_order3, 'A', decode (sort_col3, 1,name_col, 2,to_char(number_col,'9999999999.00000'), 3,to_char(date_col,'yyyymmddhh24miss'), 'X'),'X'),
      decode(sort_order3, 'D', decode (sort_col3, 1,name_col, 2,to_char(number_col,'9999999999.00000'), 3,to_char(date_col,'yyyymmddhh24miss'), 'X'),'X') DESC,Message was edited by:
    shoblock
    forgot to make 3 asc/desc variables

  • Sort the month name column based on month id in rpd itself only

    sort the month name column based on month id in rpd itself only without creating the logical column.

    Hi,
    sort the month name column based on month id in rpd itself only without creating the logical column. Can you be bit specific?
    Is this what you want..http://www.biconsultinggroup.com/knowledgebase.asp?CategoryID=198&SubCategoryID=368
    Regards,
    Srikanth
    http://bintelligencegroup.wordpress.com

  • Sort By Month Name in Crosstab report

    Hi,
    I have one cross tab report in which in Datapoint I have one column which shows Month Details. When I run the reports the heading of Column comes as Month Name (ie Aug, April, Dec,.....) . Can you please help me to understand how can it be done it displayed in sorted month according to Month Name (ie. Jan, Feb, Mar, Apr.......). As per one of the Thread I tried to use to_date(Column_name) but it does not work as Column name is char column and cannot be converted to date.

    Hi ,
    What if you use the following ...in bold?????
    SQL> select ename,hiredate from emp;
    ENAME      HIREDATE
    SMITH      18/12/1980
    ALLEN      20/02/1981
    WARD       22/02/1981
    JONES      02/04/1981
    MARTIN     28/09/1981
    BLAKE      01/05/1981
    CLARK      09/06/1981
    SCOTT      18/04/1987
    KING       17/11/1981
    TURNER     08/09/1981
    ADAMS      21/05/1987
    JAMES      03/12/1981
    FORD       03/12/1981
    MILLER     23/01/1982
    14 rows selected
    SQL> select hiredate , to_number(to_char(hiredate,'MM')) from emp order by 2;
    HIREDATE    TO_NUMBER(TO_CHAR(HIREDATE,'MM
    23/01/1982                               1
    20/02/1981                               2
    22/02/1981                               2
    02/04/1981                               4
    18/04/1987                               4
    21/05/1987                               5
    01/05/1981                               5
    09/06/1981                               6
    08/09/1981                               9
    28/09/1981                               9
    17/11/1981                              11
    18/12/1980                              12
    03/12/1981                              12
    03/12/1981                              12
    14 rows selectedOR ....
    EVEN BETTER AS YOU NEED THE MONTH NAME....
    SQL> ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';
    Session altered
    SQL> select hiredate , to_char(hiredate,'MON') , to_number(to_char(hiredate,'MM')) from emp order by 3;
    HIREDATE    TO_CHAR(HIREDATE,'MON') TO_NUMBER(TO_CHAR(HIREDATE,'MM
    23/01/1982  JAN                                                  1
    20/02/1981  FEB                                                  2
    22/02/1981  FEB                                                  2
    02/04/1981  APR                                                  4
    18/04/1987  APR                                                  4
    21/05/1987  MAY                                                  5
    01/05/1981  MAY                                                  5
    09/06/1981  JUN                                                  6
    08/09/1981  SEP                                                  9
    28/09/1981  SEP                                                  9
    17/11/1981  NOV                                                 11
    18/12/1980  DEC                                                 12
    03/12/1981  DEC                                                 12
    03/12/1981  DEC                                                 12
    14 rows selectedRegards,
    Simon
    Message was edited by:
    sgalaxy

  • Implementing Interactive Reports Sorts in Report Query

    My users love how the Interactive Reports work, especially being able to choose their sort fields. I have an Report Query that can use multiple sorts and many sort fields (chose from APEX page). Is it possible to implement this type of thing in the Report Query (without using decodes or dynamic SQL).
    I looked at the code being executed by Interactive Reports and can see the order by changing
    select
    null as apxws_row_pk,
    "CODE",
    "CITY_NAME",
    "STATE_CODE",
    "PK_ID",
    count(*) over () as apxws_row_cnt
    from (
    select * from (
    select "PK_ID",
    "CODE",
    "CITY_NAME",
    "STATE_CODE"
    from "#OWNER#"."ZIP"
    ) r
    ) r where rownum <= to_number(:APXWS_MAX_ROW_CNT)
    order by "CODE"

    Ben, Not sure I follow.
    I'm passing in the column names of the order by's from my APEX page to the DB function. Right now I'm doing a huge case statement for each one.
    FUNCTION get_student_demographic (l_user in varchar2,
    l_owner in varchar2,
    l_report_order1 in varchar2,
    l_report_order2 in varchar2,
    l_report_order3 in varchar2,
    l_report_order4 in varchar2,
    SELECT (CASE l_report_order1
    WHEN '%null%' THEN ''
    WHEN 'LAST_NAME' THEN A.LAST_NAME
    WHEN 'FIRST_NAME' THEN A.FIRST_NAME
    ..... many more
    ELSE ''
    END ) report_order1,

  • Sorting in BEx query

    Hi,
    While generating a BEx report, we applied sorting to the column.
    When I run query in BEx analyzer the sequence of the columns is fine, but when I run the same query through the portal, the sorting applied to the query is not working.
    Do you have any idea why such a thing happens?
    Thanks

    Hi,
    The note (1256102) I sent you in my last response is valid for the java runtime. Have you already checked whether this note solves this issue? Please also let us know which Support Package you are on and which patchlevel you are using for the bi java components BI-BASE-S and BIWEBAPP.
    Best regards,
    Janine

  • Sorting using SQL query

    Hello,
    Can someone write an sql query using fieldname and table to sort.
    I am using the Database Connectivity Toolset and I believe I am able to query using sql but I am not sure about the syntax for sorting.
    I looked it up, but I am coming up with an error when implementating it in my vi.
    Thanks.

    Thanks for helping. I appreciate your input and time.
    I have attached what I have seen and it does not show the sql query although I have done as you suggested.
    I have also pasted a little of the table: jenny and the fields that I am sorting in ascending and descending order by sql.
    Message Edited by roboticstudent on 03-09-2006 10:05 AM
    Attachments:
    Jenny.doc ‏279 KB

  • Sort in the query (7.0)?

    Hi Experts,
    In the query it is sorting by Document number. I changed the properties of the individual characteristic to sort by posting date. But it is not working any idea?
    Thanks in advance.
    Sharat.

    Can anybody answer this question?
    Thanks.

  • Sales By Month Query

    Hello -
    Any way to adjust the below query to also include Credit Memo / Returned Items?
    Also, does it include freight amt?
    SELECT T0.State1 AS 'Bill-to State', (SELECT SUM(T1.DocTotal) FROM OINV T1 with (NOLOCK) INNER JOIN OCRD T2 ON T2.CardCode=T1.CardCode
    WHERE MONTH(T1.DOCDATE) = 1 AND T2.State1 = T0.State1
    AND YEAR(T1.DOCDATE) = YEAR(GETDATE())) AS 'JAN Amt'
    FROM dbo.OCRD T0 LEFT JOIN dbo.OINV T1 ON T1.CardCode = T0.CardCode
    GROUP BY T0.State1
    ORDER BY T0.State1
    Thanks,
    Mike

    Hi Mike,
    That might be too long for a year.  Try this to see:
    SELECT T0.State1 AS 'Bill-to State', (SELECT SUM(T1.DocTotal) FROM OINV T1 with (NOLOCK) INNER JOIN OCRD T2 ON T2.CardCode=T1.CardCode
    WHERE MONTH(T1.DOCDATE) = 1 AND T2.State1 = T0.State1
    AND YEAR(T1.DOCDATE) = YEAR(GETDATE())) - ISNULL((SELECT SUM(T1.DocTotal) FROM ORIN T1 with (NOLOCK) INNER JOIN OCRD T2 ON T2.CardCode=T1.CardCode
    WHERE MONTH(T1.DOCDATE) = 1 AND T2.State1 = T0.State1
    AND YEAR(T1.DOCDATE) = YEAR(GETDATE())),0) AS 'Jan Amt'
    FROM dbo.OCRD T0 LEFT JOIN dbo.OINV T1 ON T1.CardCode = T0.CardCode
    LEFT JOIN dbo.ORIN T3 ON T3.CardCode = T0.CardCode
    GROUP BY T0.State1
    ORDER BY T0.State1
    Thanks,
    Gordon

  • Average Sales per month query

    Experts,
    I worked on this query and was able to get the average Sales for 2009.
    SELECT T0.CardCode, T0.CardName, (SUM(T1.Debit) - sum(T1.Credit)) AS '2009', ((SUM(T1.Debit) - sum(T1.Credit))/12)
    FROM  OCRD T0
    LEFT JOIN JDT1 T1 ON T1.ShortName = T0.CardCode LEFT JOIN JDT1 T2 ON T2.ShortName = T0.CardCode WHERE T0.CardType = 'C' AND Year(T1.Duedate) = 2009 Group By T0.CardCode, T0.CardName
    How can I also include the average sales for 2010?
    Thanks,
    Marli

    Hi Marli,
    Try this:
    SELECT T0.CardCode, T0.CardName, (SUM(T1.Debit) - sum(T1.Credit)) AS '2009',
    ((SUM(T1.Debit) - sum(T1.Credit))/12) AS '2009 Avg',(SUM(T2.Debit) - sum(T2.Credit)) AS '2010',
    ((SUM(T2.Debit) - sum(T2.Credit))/Month(GetDate()))
    FROM  OCRD T0
    LEFT JOIN JDT1 T1 ON T1.ShortName = T0.CardCode AND Year(T1.Duedate) = 2009
    LEFT JOIN JDT1 T2 ON T2.ShortName = T0.CardCode AND Year(T2.Duedate) = 2010
    WHERE T0.CardType = 'C'
    Group By T0.CardCode, T0.CardName
    Thanks,
    Gordon

Maybe you are looking for

  • Filesystem mounts as read only after boot

    Hi! I have checked the wiki, the forum posts and poked around on the web but haven't found anything helpful. I have just installed arch on my laptop, and after boot the file system is read only. Where do I change it so that it is mounted as read writ

  • Trigger Firing in Form6i

    Hi all ill explain detail my situation In my forms my menu contain icons like new(+),save,e.t.c. when I click on new(+) icon one additional record comes for inserting But i don't want it. I tried to know the trigger fired for adding record to disabli

  • Install problem: began but no disc 2

    I began a wipe and install on my Mac with OSX I thought I had the second disc but I don't and now I can't do anything.  I tried installing from the disc by hold C. Now the disc one is on the computer and won't come out.  Clicking quit install does no

  • HT5312 I don't know how to change my questions for my Apple ID

    I don't know how to change my questions for my Apple ID

  • How to make af:inputText bind  to a specific format?

    Hi, I want to make af:inputText as format specific i.e., Where I can specify such that it will accept only numeric/alphanumeric/alphabetic value? Regards, Lokesh.