Order by clause being ignored in region definition?

Hi-
I've created an interactive report on a database table. In this report, i'd like my data to display in a certain order initially, but allow it to be changed, if necessary. My query looks like:
select action_year, action_month, action_count , (and so on)
from my_table
order by action_year, action_month, action_count desc
When the report is displayed, the data does not display sorted in the order specified in my SQL (for the first row returned, i'll see '2007' when i have '2008' data in the table). I'm wondering if the "order by" clause is ignored by the APEX query parser and if so, how can i get my data to display in the manner that i am programmatically after. As far as i know, i've not specified any additional sorts on my report data.
cheers,
jim.

If you create a view like this:
CREATE VIEW emp_vu
AS
   SELECT   ename, empno, job, sal, deptno
       FROM emp
   ORDER BY deptno, enameand use a select in your interactive report
SELECT *
   FROM emp_vuthe data will show initialy as it is supposed to. You are
able to sort on it as you need. The only thing you need to
disable (exclude) is the single link column. Apparently, if this
option is enabled, apex sorts on that link value.
Denes Kubicek
http://deneskubicek.blogspot.com/
http://www.opal-consulting.de/training
http://apex.oracle.com/pls/otn/f?p=31517:1
-------------------------------------------------------------------

Similar Messages

  • Order By clause is ignoring LowerCase field name

    I am trying to use a QoQ, but am running into a problem when
    using the order by clause...
    All the fieldvalues in the order by field start with
    uppercase except for one, and this field name gets thrown to the
    end of the list
    Anyone got any ideas???
    thanks
    Sujit

    Before you try the following, please note that you can clean
    this up in your original DB queries -- which is almost always
    better.
    Anyway, suppose your QofQ was:
    SELECT MixcasedColumn
    FROM SomeQuerySet
    ORDER BY MixcasedColumn
    Then you would add a dummy column to use for case-insensitive
    sort:
    SELECT
    MixcasedColumn, UPPER (MixcasedColumn) AS SortColumn
    FROM
    SomeQuerySet
    ORDER BY
    SortColumn <!--- Note that upper() is not allowed here
    but derived columns are! --->

  • Being ignored

    My name is David and I've been a Verizon Wireless customer for the past few years. A couple of months ago, I began having issues with a brand new jetpack device. The device was using absurd amounts of data and I had no clue why. I contacted customer service and eventually got in touch with (removed) Smith, a supervisor. We talked about three times over the phone, on separate occasions, trying to figure out what was causing the problem. We reset the device, changed the password, etc., but nothing was fixing the problem. In the meantime, I had taken multiple steps to make sure nothing was using the data on my end. I set up a metered connection, which tracked exactly how much data went through my computer and it was not even close to what the jetpack was saying, despite being the only device connected to it. Ms Smith gave me her email address to stay in closer contact with her while we were trying all of the steps to fix the issue, so I could let her know exactly what I was seeing. The email address she gave me was (removed) Our contact first started in November and she sent me the first email on 12/1/13. The device began using even more data, now at a very alarming rate. In one night, it used 12.5 GBs of data. I spoke with her again on 12/14/13 when I first noticed how much data it had been using. She eventually sent me a new device, stating that the one I had was faulty and she wanted me to let her know when I got the new one so she could monitor the activity and see if it was following the same pattern or if it was acting normal. It ended up working 100% fine and not like the faulty device I had sent back in. She had told me over the phone that she would fix all of the overage charges from when I first got the device since it was messed up. She gave me credit for the total of overage from the first 3 months with the device, but she hadn't fixed the bill that had used 55 GBs of data, which was the last cycle in which I had that particular device.
    The last time I heard from Ms. Smith was on 12/31/13 when she had told me she was going to fix the charges. In the meantime, I was left waiting to hear that the fourth and final month had been taken care of, so the $460 worth of overages was still sitting on my bill. On 12/31/13, she emailed me asking for my number to reach me on so she could call me that day. I responded with my number, but never received a call. A week later on 1/6/14, I emailed her again with my number, telling her that I wasn't sure if she had received my email or not, but I was still waiting. As of 1/21/14, I had still not received a call, so I emailed her again telling her I needed to talk to her ASAP so we could get everything resolved and I could pay my bill. I also mentioned that I needed to have my data plan changed back to normal (16 GBs) rather than the 30 GB plan because it was too much. I had been bumped up to 30 GBs during that big month of overages, to try and prevent extra charges. A little less than a week later on 1/26/14, I emailed Ms. Smith once again, telling her I was worried about my service getting cut off and I needed to speak with her ASAP. I mentioned that I did not want to have to start from scratch with someone else.
    On 2/6/14, I woke up to discover that my service had indeed been turned off. I called and spoke with someone in customer service, explaining what had been going on. I was told by that representative that her supervisor would call me between 8-10 PM that night, but in the meantime my service would be temporarily restored. I never received a call. Around 10:30, I called back to try and speak with someone else. I was put through to another representative, once again having to explain what had been going on. This representative told me that I would have to wait for a call back from his supervisor, because he would be leaving at 11:00 PM. I explained to the representative that I was already promised a call back that night and didn't receive a call, so I had to have it taken care of because my service was shut off. His supervisor took my call and told me that there wasn't much they could do, because Ms. Smith had already started my case. He said they would send her an email, telling her she needs to contact me. He told me that he would have someone from his team call me back within 24-48 hours to see if she got in touch with me and if not, I would start from scratch with them.
    In the meantime, I sent Ms. Smith an email the next day on 2/7/14, telling her my service had been shut off and I tried calling the night before to speak with someone else, but they couldn't do much for me because she had already been working with me. Again, I never did get a response.
    Monday 2/10/14 had come along and once again my service was turned back off. I never did receive a call from the supervisor I spoke with on 2/6/14, who had promised me his team would call me back no later than 48 hours. So, again, I called to try and speak with someone else. I eventually got through to another supervisor, but after I explained to him what had been going on, the call was disconnected. I waited for about 30 minutes to see if he'd call back, but he didn't. Once again, I called to try and speak with someone else. I was directed towards the financial department and I explained to them that I was just talking with someone and was disconnected, so I was hoping to get back to them. I was told that there was no way to trace me back to the man I was speaking with. I then explained to that representative what was going on and she told me that if I paid the actual monthly access portion of my outstanding bill, my service would be restored and she would promise to put a note on my account to not turn it off again, because I was working to have the charges fixed. I paid the monthly access and she then transferred me to technical support to have someone in that department help me further. I then explained my story to the technical representative and she told me that her supervisor was not on-duty at the time, but she was preparing an email to send to her and she would definitely get back to me by the end of the week. That was two weeks ago from today and I've yet to receive a call from anyone.
    Once again, I woke up today to find that my service had again been shut off. I called in and spoke with financial services and explained to them what has been going on and she said if I went ahead and paid my bill that had just come due for last month, she could turn it on. So, that's what I did, but I'm still left in the dark as for the future.
    I am so beyond frustrated with this entire situation, that I have no clue what to do. I've told this story to at least ten people and I've gotten no where. To say that I feel like I'm being ignored, is an understatement. I can send y'all the entire transcript of emails with myself and Ms. Smith, proving that I've been trying to contact her for the past month and a half. This whole situation has gone further than the simple problem of the overage charges. It has now risen to a ridiculous level that I am being ignored by what seems like the entire company as a whole. How many times do I have to call to where I can finally work with one person who will help fix the problem? How many times can one customer be told that he will be contacted and he never receives a call? At this point, I am more than prepared to go over to Sprint and cancel my service with Verizon. The $460 that I am being raked trough the coals for will be nothing compared to not having my business for years to come. I am also prepared to contact and file complaints with the FCC or BBB for the fact that I have done absolutely everything in my power, including going in to a local store, to speak with someone about my issues. Like I said, I have the emails proving that I've been ignored and I can show them to y'all or to them. I don't know what to do anymore and I'm tired of calling and starting over with a new representative then being transferred around in loops where I have to re-tell the entire story again, so this is my last resort. If I don't get help by the end of the day, I'll be somewhere else tomorrow.
    Private info removed as required by the Terms of Service.
    Message was edited by: Admin Moderator

    Hi Barry,
    I am sorry about the mix up with the order, could you send me a PM of your contact details? Please include the email address you used to contact support.

  • Break Order overriding my ORDER BY clause

    Hi Experts,
    I have created a report in Oracle 6i where I am getting the ORDER BY clause at runtime. And in the datamodel query, I am using a lexical parameter to say, order by the column selected in the parameter.
    The problem am facing is, at runtime, the ORDER BY clause is being ignored by the report and it takes the column mentioned with break order property as 'Ascending' and sorts by that column.
    I am not able to set the break order property to None to all of the columns as the reports builder is erroring out asking me to have atleast one column set to Asc or Desc!
    Have anyone faced this problem before ? Is there anyway to get rid of this and make the report use by columns mentioned in the ORDER BY clause.
    Please note: I tried an explicit ORDER BY <columnname> in the data model query but still the report uses the break order value mentioned and sorts by that columns.
    Appreciate your help on the same.
    Thanks!

    Hi,
    there's a simple rule how this works. For every query you have one or more (hierarchic) groups in the data model. For the lowest group the ORDER BY is choosen for sorting and for the groups, not beeing the lowest group, the BREAK ORDER is choosen (and vice versa ignored).
    Regards
    Rainer

  • 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,
    Girish

    You add a rownumber to your DB view and use that to 'sort' your report.
    regards
    John
    http://obiee101.blogspot.com/

  • I have replied to you three times now without reply. I DID NOT authorise a payment of £9.99 for NowTV. What is the solution to getting my money refunded. I think you assume I will go away after being Ignored. No I will not go away. I find now that you hav

    I have replied to you three times now without reply. I DID NOT authorise a payment of £9.99 for NowTV. What is the solution to getting my money refunded. I think you assume I will go away after being Ignored. No I will not go away. I find now that you have DISABLED MY APPLE ID. Is this a punishment you use to anyone who takes up a stand against charges being made unauthorised on their account?. Please tell me why you have disabled my account. When will my account be restored? If not when will you refund ALL my purchases made to APPLE/ITUNES? I can then move on to another brand and hopefully better customer service. After reading articles regarding APPLE ID DISABLED it appears common practice from your company to DISABLE anyone's account if they question unknown charges. Please reply with your intentions regarding  the unauthorised payment and the DISABLING of my account.
    David Forrester.
    Sent from my iPad
    On 18 Mar 2014, at 09:20, iTunes Store <[email protected]> wrote:
    Follow-Up: 319042795
    Hello again,
    I wanted to send a quick note to see if you are still experiencing any difficulties with the iTunes Store. Resolving your issue is important to me, so please don't hesitate to reply if you need any further assistance.
    Sincerely,
    iTunes Store Customer Support
    http://www.apple.com/support/itunes/ww/
    Dear David,
    Welcome to iTunes Store Customer support.
    I understand that you have been charged an additional 9.99 GBP for a purchase that not authorized. I know how eager you are to know more about this purchase and I am happy to look into this for you.
    David, the purchase worth 9.99 GBP was for a day pass from "NOW TV for Apple TV." To review your iTunes Store account's purchase history, please follow the steps in this article:
    iTunes Store &amp; Mac App Store: Seeing your purchase history and order numbers
    http://support.apple.com/kb/HT2727
    Please reply to this email and let me know if this purchase was unauthorized.
    Thank you for being an iTunes Store customer. Have a great day!
    Sincerely,
    iTunes Store Customer Support
    http://www.apple.com/support/itunes/ww/
    Lang_Country: en_gb
    User Storefront: UK
    Concern Type: Problem Not Listed
    Web Order #:
    Content Title: NOW TV Day Pass
    Provider Name: BSkyB
    Track IDs: []
    Purchase Date : 2014-03-16 12:33:48 Etc/GMT
    Purchase Device : Apple TV
    Comments : I am being charged for what is listed plus another _9.99 for Apple TV pass. Did not authorise this _9.99 charge and not sure what it is exactly.
    <Personal Information Edited by Host>

    This is a user-supported board. You are not addressing Apple here. Nor is it a good idea to post your private information to a public forum. You should edit your post immediately.
    Unfortunately no one here can access your support history. You must respond to the emails directly.

  • 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.

  • Slow SQL output when table alias is NOT used in order by clause

    Hi guys,
    My query is based on Oracle 9208
    I have a table TAB1 with 68000 records with transaction_id as the primary key in this table (unique index).
    I have another TAB2 with the same number of records again with transaction_id in this table (foreign key to above).
    I have the below query that gets executed via an application:-
    SELECT TO_CHAR(V1.TRANSACTION_ID), V1.POLICY_ID, V1.REQUEST_TYPE
    FROM <VIEW> V1 WHERE V1.CERT_SERIAL_NUM=56192 AND
    (V1.AUTH_GROUP_ID=0 OR V1.AUTH_GROUP_ID=1) AND ROWNUM <= 3 ORDER
    BY TRANSACTION_ID ASC
    The above view V1 is created as below:-
    CREATE OR REPLACE FORCE VIEW "V1"
    ("TRANSACTION_ID",
    "PARENT_TRANSACTION_ID",
    "CA_DN_ID",
    "AUTH_GROUP_ID",
    "POLICY_ID",
    "REQUEST_TYPE",
    "REQUEST_STATUS",
    "EE_DN_HASH",
    "EE_DN",
    "EE_EMAIL_HASH",
    "EE_EMAIL",
    "KEY_USAGE",
    "SMART_CARD_SERIAL",
    "CERT_TYPE",
    "CERT_SERIAL_NUM",
    "CERT_INDEX",
    "RENEWAL_FLAG",
    "ARCHIVE_FLAG",
    "TIME_RECEIVED",
    "DOWNLOADED",
    "REQUEST_DATA",
    "ACTION",
    "STEP_NUM")
    AS
    SELECT
    T1.transaction_id,
    T1.parent_transaction_id,
    T1.ca_dn_id,
    V2.auth_group_id,
    V2.policy_id,
    T1.request_type,
    T1.request_status,
    T2.ee_dn_hash,
    T2.ee_dn,
    T2.ee_email_hash,
    T2.ee_email,
    T2.key_usage,
    T2.smart_card_serial,
    T2.cert_type,
    T2.cert_serial_num,
    T2.cert_index,
    T2.renewal_flag,
    T2.archive_flag,
    T1.time_received,
    T1.downloaded,
    T1.request_data,
    V2.action,
    V2.step_num
    FROM TAB1
    <ANOTHER VIEW> V2,
    TAB2 T1,
    TAB2 T2
    WHERE
    T1.transaction_id = T2.transaction_id
    AND
    V2.policy_id = T1.policy_id
    order by transaction_id;
    The query at the top runs within milliseconds if the VIEW is created as :-
    order by t2.transaction_id
    But without the alias "t2" in the order by, the query takes about 1 1/2 minutes
    Can you tell me why? I thought if you ordering by primary key (lesser number of values compared to foreign key values), the query should be faster..no?
    Thanks in advance

    Thanks for keeping up with this issue Hemant.
    Here are the plans for each case.
    I would be very interested in how you'd recognize which plan is the best and what are the derivatives.
    I don't much (or rather anything!) what is 'card' values, 'cost' values etc which I believe are used to decide the best plan of the lot.
    Thanks again
    Note TAB1 and TAB2 are from view definition posted initially
    1) Execution Plan for VIEW1 <<-- With ORDER BY" clause but no table ailas (order by transaction_id)
    SQL> EXPLAIN PLAN FOR SELECT TO_CHAR(QT.TRANSACTION_ID), QT.POLICY_ID, QT.REQUEST_TYPE
    2 FROM <VIEW1> QT WHERE QT.CERT_SERIAL_NUM=24293 AND
    3 (QT.AUTH_GROUP_ID=0 OR QT.AUTH_GROUP_ID=1) AND ROWNUM <= 3 ORDER
    4 BY TRANSACTION_ID ASC
    5 /
    Explained.
    Elapsed: 00:00:01.00
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    | Id | Operation | Name | Rows | Bytes | Cost |
    | 0 | SELECT STATEMENT | | 3 | 195 | 17 |
    |* 1 | COUNT STOPKEY | | | | |
    | 2 | VIEW | VIEW1 | 17 | 1105 | 17 |
    |* 3 | SORT ORDER BY STOPKEY | | 17 | 38573 | 17 |
    | 4 | NESTED LOOPS | | 17 | 38573 | 10 |
    | 5 | MERGE JOIN CARTESIAN | | 1 | 167 | 9 |
    | 6 | VIEW | VIEW2 | 1 | 52 | 8 |
    | 7 | SORT UNIQUE | | 1 | 156 | 8 |
    | 8 | NESTED LOOPS | | 1 | 156 | 6 |
    | 9 | NESTED LOOPS | | 1 | 143 | 6 |
    | 10 | NESTED LOOPS OUTER | | 1 | 117 | 5 |
    |* 11 | HASH JOIN | | 1 | 104 | 5 |
    | 12 | NESTED LOOPS | | 1 | 52 | 2 |
    |* 13 | TABLE ACCESS FULL | TAB3 | 1 | 39 | 2 |
    |* 14 | INDEX UNIQUE SCAN | (PK_TAB4) | 1 | 13 | |
    | 15 | TABLE ACCESS FULL | TAB5 | 82 | 4264 | 2 |
    | 16 | VIEW PUSHED PREDICATE | View3 | 1 | 13 | |
    | 17 | NESTED LOOPS | | 1 | 52 | 2 |
    | 18 | NESTED LOOPS | | 1 | 39 | 2 |
    |* 19 | INDEX UNIQUE SCAN | (PK_TAB6) | 1 | 13 | 1 |
    |* 20 | INDEX RANGE SCAN | (PK_TAB7) | 1 | 26 | 1 |
    |* 21 | INDEX UNIQUE SCAN | (PK_TAB8) | 1 | 13 | |
    | 22 | TABLE ACCESS BY INDEX ROWID| TAB9 | 3 | 78 | 1 |
    |* 23 | INDEX UNIQUE SCAN | (PK_TAB9) | 1 | | |
    |* 24 | INDEX UNIQUE SCAN | (PK_TAB10)| 1 | 13 | |
    | 25 | BUFFER SORT | | 1 | 115 | 9 |
    | 26 | TABLE ACCESS BY INDEX ROWID | TAB2 | 1 | 115 | 1 |
    |* 27 | INDEX RANGE SCAN | (TAB2_IDX2)| 1 | | |
    |* 28 | TABLE ACCESS BY INDEX ROWID | TAB1 | 12 | 25224 | 1 |
    |* 29 | INDEX UNIQUE SCAN | (PK_TAB1) | 1 | | |
    Predicate Information (identified by operation id):
    1 - filter(ROWNUM<=3)
    3 - filter(ROWNUM<=3)
    11 - access("TAB5"."PATH_ID"="TAB4"."PATH_ID")
    13 - filter("TAB3"."AUTH_GROUP_ID"<>(-1) AND ("TAB3"."AUTH_GROUP_ID"=0 OR "TAB3"."AUTH_GROUP_ID"=1))
    14 - access("TAB3"."PATH_ID"="TAB4"."PATH_ID")
    19 - access("TAB5"."DOMAIN_ID"="TAB6"."DOMAIN_ID")
    20 - access("TAB6"."DOMAIN_ID"="TAB7"."DOMAIN_ID")
    21 - access("TAB7"."RULE_ID"="TAB8"."RULE_ID")
    23 - access("TAB9"."POLICY_ID"="TAB5"."POLICY_ID")
    24 - access("TAB9"."ASSOCIATED_FORM_ID"="TAB10"."FORM_ID")
    27 - access("TAB2"."CERT_SERIAL_NUM"=24293)
    28 - filter("View2"."POLICY_ID"="TAB1"."POLICY_ID")
    29 - access("TAB1"."TRANSACTION_ID"="TAB2"."TRANSACTION_ID")
    Note: cpu costing is off
    54 rows selected.
    Elapsed: 00:00:01.81
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE
    1 0 COLLECTION ITERATOR (PICKLER FETCH) OF 'DISPLAY'
    Statistics
    39 recursive calls
    0 db block gets
    629 consistent gets
    0 physical reads
    104 redo size
    5169 bytes sent via SQL*Net to client
    405 bytes received via SQL*Net from client
    5 SQL*Net roundtrips to/from client
    31 sorts (memory)
    0 sorts (disk)
    54 rows processed
    2) Execution Plan for VIEW1 <<-- With ORDER BY" clause and table alias (order by TAB2.transaction_id)
    SQL> explain plan for SELECT TO_CHAR(QT.TRANSACTION_ID), QT.POLICY_ID, QT.REQUEST_TYPE
    2 FROM <VIEW1> QT WHERE QT.CERT_SERIAL_NUM=30003 AND
    3 (QT.AUTH_GROUP_ID=0 OR QT.AUTH_GROUP_ID=1) AND ROWNUM <= 3 ORDER
    4 BY TRANSACTION_ID ASC
    5 /
    Explained.
    Elapsed: 00:00:10.20
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    | Id | Operation | Name | Rows | Bytes | Cost |
    | 0 | SELECT STATEMENT | | 3 | 195 | 14 |
    | 1 | SORT ORDER BY | | 3 | 195 | 14 |
    |* 2 | COUNT STOPKEY | | | | |
    | 3 | VIEW | VIEW1 | 17 | 1105 | 13 |
    | 4 | NESTED LOOPS | | 17 | 38573 | 13 |
    | 5 | MERGE JOIN CARTESIAN | | 1 | 167 | 12 |
    |* 6 | TABLE ACCESS BY INDEX ROWID | TAB2 | 1 | 115 | 4 |
    | 7 | INDEX FULL SCAN | (TAB2_IDX) | 94 | | 1 |
    | 8 | BUFFER SORT | | 1 | 52 | 8 |
    | 9 | VIEW | VIEW2 | 1 | 52 | 8 |
    | 10 | SORT UNIQUE | | 1 | 156 | 8 |
    | 11 | NESTED LOOPS | | 1 | 156 | 6 |
    | 12 | NESTED LOOPS | | 1 | 143 | 6 |
    | 13 | NESTED LOOPS OUTER | | 1 | 117 | 5 |
    |* 14 | HASH JOIN | | 1 | 104 | 5 |
    | 15 | NESTED LOOPS | | 1 | 52 | 2 |
    |* 16 | TABLE ACCESS FULL | TAB3 | 1 | 39 | 2 |
    |* 17 | INDEX UNIQUE SCAN | (PK_TAB4) | 1 | 13 | |
    | 18 | TABLE ACCESS FULL | TAB5 | 82 | 4264 | 2 |
    | 19 | VIEW PUSHED PREDICATE | View3 | 1 | 13 | |
    | 20 | NESTED LOOPS | | 1 | 52 | 2 |
    | 21 | NESTED LOOPS | | 1 | 39 | 2 |
    |* 22 | INDEX UNIQUE SCAN | (PK_TAB6) | 1 | 13 | 1 |
    |* 23 | INDEX RANGE SCAN | (PK_TAB7) | 1 | 26 | 1 |
    |* 24 | INDEX UNIQUE SCAN | (PK_TAB8) | 1 | 13 | |
    | 25 | TABLE ACCESS BY INDEX ROWID| TAB9 | 3 | 78 | 1 |
    |* 26 | INDEX UNIQUE SCAN | (PK_TAB9) | 1 | | |
    |* 27 | INDEX UNIQUE SCAN | (PK_TAB10) | 1 | 13 | |
    |* 28 | TABLE ACCESS BY INDEX ROWID | TAB1 | 12 | 25224 | 1 |
    |* 29 | INDEX UNIQUE SCAN | (PK_TAB1) | 1 | | |
    Predicate Information (identified by operation id):
    2 - filter(ROWNUM<=3)
    6 - filter("TAB2"."CERT_SERIAL_NUM"=30003)
    14 - access("TAB5"."PATH_ID"="TAB4"."PATH_ID")
    16 - filter("TAB3"."AUTH_GROUP_ID"<>(-1) AND ("TAB3"."AUTH_GROUP_ID"=0 OR "TAB3"."AUTH_GROUP_ID"=1))
    17 - access("TAB3"."PATH_ID"="TAB4"."PATH_ID")
    22 - access("TAB5"."DOMAIN_ID"="TAB6"."DOMAIN_ID")
    23 - access("TAB6"."DOMAIN_ID"="TAB7"."DOMAIN_ID")
    24 - access("TAB7"."RULE_ID"="TAB8"."RULE_ID")
    26 - access("TAB9"."POLICY_ID"="TAB5"."POLICY_ID")
    27 - access("TAB9"."ASSOCIATED_FORM_ID"="TAB10"."FORM_ID")
    28 - filter("VIEW2"."POLICY_ID"="TAB1"."POLICY_ID")
    29 - access("TAB1"."TRANSACTION_ID"="TAB2"."TRANSACTION_ID")
    Note: cpu costing is off
    53 rows selected.
    Elapsed: 00:00:08.29
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE
    1 0 COLLECTION ITERATOR (PICKLER FETCH) OF 'DISPLAY'
    Statistics
    1079 recursive calls
    0 db block gets
    597 consistent gets
    21 physical reads
    0 redo size
    5177 bytes sent via SQL*Net to client
    405 bytes received via SQL*Net from client
    5 SQL*Net roundtrips to/from client
    63 sorts (memory)
    0 sorts (disk)
    53 rows processed
    3) Execution Plan for VIEW1 <<-- Without any "ORDER BY" clause
    SQL> explain plan for SELECT TO_CHAR(QT.TRANSACTION_ID), QT.POLICY_ID, QT.REQUEST_TYPE
    2 FROM <VIEW1> QT WHERE QT.CERT_SERIAL_NUM=30003 AND
    3 (QT.AUTH_GROUP_ID=0 OR QT.AUTH_GROUP_ID=1) AND ROWNUM <= 3 ORDER
    4 BY TRANSACTION_ID ASC
    5 /
    Explained.
    Elapsed: 00:00:10.20
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    | Id | Operation | Name | Rows | Bytes | Cost |
    | 0 | SELECT STATEMENT | | 3 | 213 | 11 |
    | 1 | SORT ORDER BY | | 3 | 213 | 11 |
    |* 2 | COUNT STOPKEY | | | | |
    | 3 | NESTED LOOPS | | 17 | 1207 | 10 |
    | 4 | MERGE JOIN CARTESIAN | | 1 | 32 | 9 |
    | 5 | VIEW | VIEW2 | 1 | 26 | 8 |
    | 6 | SORT UNIQUE | | 1 | 156 | 8 |
    | 7 | NESTED LOOPS | | 1 | 156 | 6 |
    | 8 | NESTED LOOPS | | 1 | 143 | 6 |
    | 9 | NESTED LOOPS OUTER | | 1 | 117 | 5 |
    |* 10 | HASH JOIN | | 1 | 104 | 5 |
    | 11 | NESTED LOOPS | | 1 | 52 | 2 |
    |* 12 | TABLE ACCESS FULL | TAB3 | 1 | 39 | 2 |
    |* 13 | INDEX UNIQUE SCAN | PK_TAB4 | 1 | 13 | |
    | 14 | TABLE ACCESS FULL | TAB5 | 82 | 4264 | 2 |
    | 15 | VIEW PUSHED PREDICATE | VIEW3 | 1 | 13 | |
    | 16 | NESTED LOOPS | | 1 | 52 | 2 |
    | 17 | NESTED LOOPS | | 1 | 39 | 2 |
    |* 18 | INDEX UNIQUE SCAN | PK_TAB6 | 1 | 13 | 1 |
    |* 19 | INDEX RANGE SCAN | PK_TAB7 | 1 | 26 | 1 |
    |* 20 | INDEX UNIQUE SCAN | PK_TAB8 | 1 | 13 | |
    | 21 | TABLE ACCESS BY INDEX ROWID| TAB9 | 3 | 78 | 1 |
    |* 22 | INDEX UNIQUE SCAN | PK_TAB9 | 1 | | |
    |* 23 | INDEX UNIQUE SCAN | PK_TAB10 | 1 | 13 | |
    | 24 | BUFFER SORT | | 1 | 6 | 9 |
    | 25 | TABLE ACCESS BY INDEX ROWID | TAB2 | 1 | 6 | 1 |
    |* 26 | INDEX RANGE SCAN | TAB2_IDX2 | 1 | | |
    |* 27 | TABLE ACCESS BY INDEX ROWID | TAB1 | 12 | 468 | 1 |
    |* 28 | INDEX UNIQUE SCAN | PK_TAB1 | 1 | | |
    Predicate Information (identified by operation id):
    2 - filter(ROWNUM<=3)
    10 - access("TAB5"."PATH_ID"="TAB4"."PATH_ID")
    12 - filter("TAB3"."AUTH_GROUP_ID"<>(-1) AND ("TAB3"."AUTH_GROUP_ID"=0 OR "TAB3"."AUTH_GROUP_ID"=1))
    13 - access("TAB3"."PATH_ID"="TAB4"."PATH_ID")
    18 - access("TAB5"."DOMAIN_ID"="TAB6"."DOMAIN_ID")
    19 - access("TAB6"."DOMAIN_ID"="TAB7"."DOMAIN_ID")
    20 - access("TAB7"."RULE_ID"="TAB8"."RULE_ID")
    22 - access("TAB9"."POLICY_ID"="TAB5"."POLICY_ID")
    23 - access("TAB9"."ASSOCIATED_FORM_ID"="TAB10"."FORM_ID")
    26 - access("TAB2"."CERT_SERIAL_NUM"=1022)
    27 - filter("VIEW2"."POLICY_ID"="TAB1"."POLICY_ID")
    28 - access("TAB1"."TRANSACTION_ID"="TAB2"."TRANSACTION_ID")
    Note: cpu costing is off
    52 rows selected.
    Elapsed: 00:00:03.37
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE
    1 0 COLLECTION ITERATOR (PICKLER FETCH) OF 'DISPLAY'
    Statistics
    38 recursive calls
    0 db block gets
    287 consistent gets
    0 physical reads
    0 redo size
    5006 bytes sent via SQL*Net to client
    405 bytes received via SQL*Net from client
    5 SQL*Net roundtrips to/from client
    29 sorts (memory)
    0 sorts (disk)
    52 rows processed

  • 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

  • Passing parameters in order by clause of cursor

    Hi friends,
    I am facing a strange problem
    I have a cursor with a order by clause.
    OPEN cursor_nomrpt FOR
    SELECT DISTINCT
    CAST(plano.desc3 AS VARCHAR2(50)) category
    ,plano.name plan_name
    ,nvl(CAST(plano.desc6 AS VARCHAR2(50)),'not applicable') pcrr
    ,nvl(trunc(plano.value18,2),0) nominal_slm
    ,nvl(trunc(plano.value19,2),0) blm
    ,plano.dbdateeffectivefrom date_live
    plano.dbkey pog_id
    ,case when plano.value47 < 0 THEN 0 when plano.value47 IS NULL THEN 0 ELSE plano.value47 END AS parent_id
    FROM
    dplapro1.ix_spc_planogram plano
    INNER JOIN dplapro1.ix_spc_performance perf ON plano.dbkey = perf.dbparentplanogramkey
    INNER JOIN dplapro1.ix_spc_product product ON perf.dbparentproductkey = product.dbkey
    INNER JOIN dplapro1.ix_spc_product_key prodkey ON product.dbkey2 = prodkey.dbkey
    AND prodkey.keylevel = 2
    WHERE
    plano.value50 = 0
    AND plano.dbkey4 = p_subcatkey
    ORDER BY ltrim(rtrim(p_orderby)) ASC;
    p_orderby is the parameter being passed. But the output is not sorted. But when I run the cursor by hardcoding the parameter value it works fine...
    Need your help on this

    Hi,
    When you use a local variable in a cursor, it's as if you had hard-coded a literal in its place, so you can't use a variable for a column name.
    If you know all the possible values of p_orderby, you can do something like this:
    ORDER BY  TRIM ( CASE  p_orderby
                   WHEN  'DESC'     THEN  plano.desc
                   WHEN  'VALUE'     THEN  plano.value
               END
                ) ASC;If you don't know all the possible values, you could use dynamic SQL.
    By the way,
    TRIM (x)returns the same results as
    LTRIM ( RTRIM (x))

  • Is parameter in ORDER BY clause possible?

    I'm using a function to return a ref cursor and currently pass a parameter without any problems. I would like to change the sort on the fly by passing a parameter to the order by clause, but Oracle ignores it.
    CREATE OR REPLACE PACKAGE pkg_agent_appt_status AS
    TYPE rcur IS REF CURSOR;
    FUNCTION f_agent_appt_status (ssn IN VARCHAR2, sort_str IN VARCHAR2) RETURN rcur;
    END pkg_agent_appt_status;
    CREATE OR REPLACE PACKAGE BODY pkg_agent_appt_status AS
    FUNCTION f_agent_appt_status (ssn IN VARCHAR2, sort_str IN VARCHAR2)
    RETURN rcur
    IS
    retval rcur;
    BEGIN
    OPEN retval FOR
    SELECT agncy.CORPORATE_NAME "Agency Name",
    agnt_state.APPT_STATE "State",
    agnt.AGENT_STATUS "Appt Status",
    TO_CHAR(agnt_state.APPT_STATE_EFF_DT,'mm/dd/yyyy') "Effective Date",
    agnt.AGENT_NUMBER "Agent ID",
    agnt.AGENT_STATUS "Agent Status",
    STATE.STATE_NAME
    FROM AGNT_APPT_STAT_PRDCR_WRK agnt,
    AGNT_APPT_STAT_WRK agncy,
    AGNT_APPT_STATE_STAT_PRDCR_WRK agnt_state,
    STATE
    WHERE agnt.AGENT_TAX_ID = ssn
    AND agnt.COMPANY_CODE = agncy.COMPANY_CODE
    AND agnt.PARENT_AGENT_AGENCY_ID = agncy.AGENT_NUMBER
    AND agnt.COMPANY_CODE = agnt_state.COMPANY_CODE
    AND agnt.AGENT_AGENCY_ID = agnt_state.AGENT_AGENCY_ID
    AND agnt.AGENT_NUMBER = agnt_state.AGENT_NUMBER
    AND agnt_state.APPT_STATE = STATE.STATE_CODE
    ORDER BY
    sort_str;                     
    RETURN retval;
    END f_agent_appt_status;
    END pkg_agent_appt_status;

    If you want to do this, you'd have to use dynamic SQL (execute immediate or DBMS_SQL). For the easier 'execute immediate' approach, you'd do something like
    create or replace someProc( someArg varchar2 )
    as
      strSQL varchar2(4000)
    begin
      strSQL := <<string containing your SQL statement up to the order by clause>>
      strSQL := strSQL || 'ORDER BY ' || someArg
      execute immediate strSQL;
    endJustin

  • 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.S

    Try 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.

  • Decode in order by clause with desc

    I want to user order by clause with decode + desc order.
    somthing like
    ORDER BY DECODE ('SALE', e.sale, 'SALE DESC' ????)
    ????-> How to use desc order with decode
    Thanks in advance

    I thought smart people in this OTN community will understand that I am trying to order by the thisdate column that is timestamp datatype:). My apologize for not being that specific.
    The query I gave is a simple version of the stored procedure I am using. The point is I need to order by - depending on one of the parameters that is passed to the procedure. In simplest decode statements, its something like
    order by decode(p_in_var,'ABC','thisdate asc','DEF','thisdate desc',thisdate asc)
    Here p_in_var is varchar input parameter to the stored procedure.
    Please let me know if there is any more information needed.
    Thx!

  • [10g] Complex Order By clause...

    I can manually sort my data, and can provide the logic I use to do that, but I need help actually putting it into an order by clause.
    I am working in XE, 10g...
    Here's some sample data to start with:
    CREATE TABLE inventory_history
    (     time_stamp     CHAR(20)
    ,     part_nbr     CHAR(25)
    ,     act_date     DATE
    ,     ord_nbr          CHAR(10)
    ,     ln_nbr          CHAR(3)
    ,     code_pre     CHAR(3)
    ,     code_sfx     CHAR(3)
    ,     user_id          CHAR(36)
    ,     qty          NUMBER(15,4)
    ,     adj_qty          NUMBER(15,4)
    ,     cnt_qty          NUMBER(15,4)
    INSERT INTO inventory_history
    VALUES ('20080716100442488094','N174X53A',TO_DATE('07/16/2008','mm/dd/yyyy'),NULL,NULL,'MTL','INS','user2',0,0,31);
    INSERT INTO inventory_history
    VALUES ('20080717135714597592','N174X53A',TO_DATE('07/17/2008','mm/dd/yyyy'),NULL,NULL,'MTL','INS','user2',31,0,0);
    INSERT INTO inventory_history
    VALUES ('20100401145927954563','N174X53A',TO_DATE('04/01/2010','mm/dd/yyyy'),NULL,NULL,'MTL','INS','user1',0,0,31);
    INSERT INTO inventory_history
    VALUES ('20100917085814607499','N174X53A',TO_DATE('09/17/2010','mm/dd/yyyy'),NULL,NULL,'MTL','INS','user1',30,0,0);
    INSERT INTO inventory_history
    VALUES ('20080715093021657035','N174X53A',TO_DATE('07/15/2008','mm/dd/yyyy'),'P000012345','001','POR','LIN','user5',31,0,0);
    INSERT INTO inventory_history
    VALUES ('20080716100406218646','N174X53A',TO_DATE('07/16/2008','mm/dd/yyyy'),'P000012345','001','MTL','INS','user2',31,0,0);
    INSERT INTO inventory_history
    VALUES ('20080819111303812239','N174X53A',TO_DATE('08/19/2008','mm/dd/yyyy'),'P000012345','002','POR','LIN','user5',31,0,0);
    INSERT INTO inventory_history
    VALUES ('20100504083427334677','N174X53A',TO_DATE('05/04/2010','mm/dd/yyyy'),'P000012345','002','POR','RTV','user3',31,0,0);
    INSERT INTO inventory_history
    VALUES ('20100512095750285578','N174X53A',TO_DATE('05/12/2010','mm/dd/yyyy'),'P000012345','002','POR','LIN','user6',30,0,0);
    INSERT INTO inventory_history
    VALUES ('20100614130143734676','N174X53A',TO_DATE('06/14/2010','mm/dd/yyyy'),'P000012345','002','MTL','INS','user1',0,0,30);
    INSERT INTO inventory_history
    VALUES ('20100412101824624708','N174X53A',TO_DATE('04/12/2010','mm/dd/yyyy'),'P000012345','004','POR','LIN','user4',31,0,0);
    INSERT INTO inventory_history
    VALUES ('20100422150057876187','N174X53A',TO_DATE('04/22/2010','mm/dd/yyyy'),'P000012345','004','MTL','INS','user2',0,0,31);
    INSERT INTO inventory_history
    VALUES ('20100504081750020711','N174X53A',TO_DATE('05/04/2010','mm/dd/yyyy'),'P000012345','004','POR','RTV','user3',31,0,0);
    INSERT INTO inventory_history
    VALUES ('20100512095303832580','N174X53A',TO_DATE('05/12/2010','mm/dd/yyyy'),'P000012345','004','POR','LIN','user6',31,0,0);
    INSERT INTO inventory_history
    VALUES ('20100712133833381506','N174X53A',TO_DATE('07/12/2010','mm/dd/yyyy'),'P000012345','004','MTL','INS','user1',31,0,0);
    INSERT INTO inventory_history
    VALUES ('20091124130834762244','015-279A',TO_DATE('11/24/2009','mm/dd/yyyy'),'P000045557','002','POR','LIN','user12',10,0,0);
    INSERT INTO inventory_history
    VALUES ('20091124131110241744','015-279A',TO_DATE('11/24/2009','mm/dd/yyyy'),'P000045557','002','POR','ADJ','user12',0,10,0);
    INSERT INTO inventory_history
    VALUES ('20091124131850641554','015-279A',TO_DATE('11/24/2009','mm/dd/yyyy'),'P000045557','002','POR','ADJ','user12',0,-20,0);
    INSERT INTO inventory_history
    VALUES ('20091124131916249134','015-279A',TO_DATE('11/24/2009','mm/dd/yyyy'),'P000045557','002','POR','LIN','user12',10,0,0);
    INSERT INTO inventory_history
    VALUES ('20091124135601215291','015-279A',TO_DATE('11/24/2009','mm/dd/yyyy'),'P000045557','002','POR','ADJ','user12',0,10,0);
    INSERT INTO inventory_history
    VALUES ('20091124140318980761','015-279A',TO_DATE('11/24/2009','mm/dd/yyyy'),NULL,NULL,'MTL','INS','user13',10,0,0);
    INSERT INTO inventory_history
    VALUES ('20091124142344235001','015-279A',TO_DATE('11/24/2009','mm/dd/yyyy'),'P000045557','002','POR','ADJ','user12',0,-10,0);
    INSERT INTO inventory_history
    VALUES ('20100701123833300000','test',TO_DATE('07/01/2010','mm/dd/yyyy'),'P000011111','001','POR','LIN','user12',5,0,0);
    INSERT INTO inventory_history
    VALUES ('20100705964833300000','test',TO_DATE('07/05/2010','mm/dd/yyyy'),'P000011111','001','MTL','INS','user2',5,0,0);
    INSERT INTO inventory_history
    VALUES ('20100702123456700000','test',TO_DATE('07/02/2010','mm/dd/yyyy'),'P000011111','002','POR','LIN','user6',5,0,0);
    INSERT INTO inventory_history
    VALUES ('20100704987654300000','test',TO_DATE('07/04/2010','mm/dd/yyyy'),NULL,NULL,'MTL','INS','user1',5,0,0);And this is what I'm looking to do ... this is how I want the above data sorted:
    TIME_STAMP          PART_NBR     ACT_DATE     ORD_NBR          LN_NBR     CODE_PRE     CODE_SFX     USER_ID     QTY     ADJ_QTY     CNT_QTY
    20091124130834762244     015-279A     11/24/2009     P000045557     002     POR          LIN          user12     10     0     0
    20091124131110241744     015-279A     11/24/2009     P000045557     002     POR          ADJ          user12     0     10     0
    20091124131850641554     015-279A     11/24/2009     P000045557     002     POR          ADJ          user12     0     -20     0
    20091124131916249134     015-279A     11/24/2009     P000045557     002     POR          LIN          user12     10     0     0
    20091124135601215291     015-279A     11/24/2009     P000045557     002     POR          ADJ          user12     0     10     0
    20091124140318980761     015-279A     11/24/2009                    MTL          INS          user13     10     0     0
    20091124142344235001     015-279A     11/24/2009     P000045557     002     POR          ADJ          user12     0     -10     0
    20080715093021657035     N174X53A     7/15/2008     P000012345     001     POR          LIN          user5     31     0     0
    20080716100406218646     N174X53A     7/16/2008     P000012345     001     MTL          INS          user2     31     0     0
    20080716100442488094     N174X53A     7/16/2008                    MTL          INS          user2     0     0     31
    20080717135714597592     N174X53A     7/17/2008                    MTL          INS          user2     31     0     0
    20080819111303812239     N174X53A     8/19/2008     P000012345     002     POR          LIN          user5     31     0     0
    20100401145927954563     N174X53A     4/1/2010                    MTL          INS          user1     0     0     31
    20100512095750285578     N174X53A     5/12/2010     P000012345     002     POR          LIN          user6     30     0     0
    20100614130143734676     N174X53A     6/14/2010     P000012345     002     MTL          INS          user1     0     0     30
    20100412101824624708     N174X53A     4/12/2010     P000012345     004     POR          LIN          user4     31     0     0
    20100422150057876187     N174X53A     4/22/2010     P000012345     004     MTL          INS          user2     0     0     31
    20100512095303832580     N174X53A     5/12/2010     P000012345     004     POR          LIN          user6     31     0     0
    20100712133833381506     N174X53A     7/12/2010     P000012345     004     MTL          INS          user1     31     0     0
    20100917085814607499     N174X53A     9/17/2010                    MTL          INS          user1     30     0     0
    20100701123833300000     test          7/1/2010     P000011111     001     POR          LIN          user12     5     0     0
    20100705964833300000     test          7/5/2010     P000011111     001     MTL          INS          user2     5     0     0
    20100702123456700000     test          7/2/2010     P000011111     002     POR          LIN          user6     5     0     0
    20100704987654300000     test          7/4/2010                    MTL          INS          user1     5     0     0This is the logic I use to sort the data manually:
    If the record has ord_nbr/ln_nbr (they aren't NULL), then sort by part_nbr, ord_nbr, ln_nbr, and time_stamp, in that order.
    And here's the complicated part...
    If the record does not have ord_nbr/ln_nbr (they are NULL, and this can only happen on MTL-INS transactions), then it needs to be placed into the sort I just described via this logic:
    It must be placed with the other records with the same part_nbr, after the closest previous POR LIN transaction by time_stamp (for that part_nbr), but before the next chronological MTL-INS transaction by time_stamp (for that part_nbr), and between those 2 constraints, fit in by time_stamp.
    Any suggestions??
    Edited by: user11033437 on Sep 23, 2010 3:13 PM (Fixed order of sample data, so part_nbr was ascending, though really the overall sort doesn't matter, just the sorting within each part_nbr group.)

    Hi,
    user11033437 wrote:
    ... And this is what I'm looking to do ... this is how I want the above data sorted:
    TIME_STAMP          PART_NBR     ACT_DATE     ORD_NBR          LN_NBR     CODE_PRE     CODE_SFX     USER_ID     QTY     ADJ_QTY     CNT_QTY
    20091124130834762244     015-279A     11/24/2009     P000045557     002     POR          LIN          user12     10     0     0
    20091124131110241744     015-279A     11/24/2009     P000045557     002     POR          ADJ          user12     0     10     0
    20091124131850641554     015-279A     11/24/2009     P000045557     002     POR          ADJ          user12     0     -20     0
    20091124131916249134     015-279A     11/24/2009     P000045557     002     POR          LIN          user12     10     0     0
    20091124135601215291     015-279A     11/24/2009     P000045557     002     POR          ADJ          user12     0     10     0
    20091124140318980761     015-279A     11/24/2009                    MTL          INS          user13     10     0     0
    20091124142344235001     015-279A     11/24/2009     P000045557     002     POR          ADJ          user12     0     -10     0
    20080715093021657035     N174X53A     7/15/2008     P000012345     001     POR          LIN          user5     31     0     0
    20080716100406218646     N174X53A     7/16/2008     P000012345     001     MTL          INS          user2     31     0     0
    20080716100442488094     N174X53A     7/16/2008                    MTL          INS          user2     0     0     31
    20080717135714597592     N174X53A     7/17/2008                    MTL          INS          user2     31     0     0
    20080819111303812239     N174X53A     8/19/2008     P000012345     002     POR          LIN          user5     31     0     0
    20100401145927954563     N174X53A     4/1/2010                    MTL          INS          user1     0     0     31
    20100512095750285578     N174X53A     5/12/2010     P000012345     002     POR          LIN          user6     30     0     0
    20100614130143734676     N174X53A     6/14/2010     P000012345     002     MTL          INS          user1     0     0     30
    20100412101824624708     N174X53A     4/12/2010     P000012345     004     POR          LIN          user4     31     0     0
    20100422150057876187     N174X53A     4/22/2010     P000012345     004     MTL          INS          user2     0     0     31
    20100512095303832580     N174X53A     5/12/2010     P000012345     004     POR          LIN          user6     31     0     0
    20100712133833381506     N174X53A     7/12/2010     P000012345     004     MTL          INS          user1     31     0     0
    20100917085814607499     N174X53A     9/17/2010                    MTL          INS          user1     30     0     0
    20100701123833300000     test          7/1/2010     P000011111     001     POR          LIN          user12     5     0     0
    20100705964833300000     test          7/5/2010     P000011111     001     MTL          INS          user2     5     0     0
    20100702123456700000     test          7/2/2010     P000011111     002     POR          LIN          user6     5     0     0
    20100704987654300000     test          7/4/2010                    MTL          INS          user1     5     0     0This is the logic I use to sort the data manually:
    If the record has ord_nbr/ln_nbr (they aren't NULL), then sort by part_nbr, ord_nbr, ln_nbr, and time_stamp, in that order.
    And here's the complicated part...
    If the record does not have ord_nbr/ln_nbr (they are NULL, and this can only happen on MTL-INS transactions), then it needs to be placed into the sort I just described via this logic:
    It must be placed with the other records with the same part_nbr, after the closest previous POR LIN transaction by time_stamp (for that part_nbr), but before the next chronological MTL-INS transaction by time_stamp (for that part_nbr), and between those 2 constraints, fit in by time_stamp.Sorry, I don't see how you get those results from that data.
    In normal string stort order '015-279A' comes before 'N174X53A', which comes before 'test'. How do you get the order you want?
    Then within each part_nbr, your explanation seems to say you want the rows with NULL ord_nbr to come right after the preceding POR-LIN row (where "preceding" means according to time_stamp), which would result in this order:
    TIME_STAMP           PART_NBR     ACT_DATE   ORD_NBR    LN_ COD
    20091124130834762244 015-279A     11/24/2009 P000045557 002 POR
    20091124131110241744 015-279A     11/24/2009 P000045557 002 POR
    20091124131850641554 015-279A     11/24/2009 P000045557 002 POR
    20091124131916249134 015-279A     11/24/2009 P000045557 002 POR
    20091124140318980761 015-279A     11/24/2009                MTL
    20091124135601215291 015-279A     11/24/2009 P000045557 002 POR
    20091124142344235001 015-279A     11/24/2009 P000045557 002 POR
    20080715093021657035 N174X53A     7/15/2008  P000012345 001 POR
    20080716100442488094 N174X53A     7/16/2008                 MTL
    20080717135714597592 N174X53A     7/17/2008                 MTL
    20080716100406218646 N174X53A     7/16/2008  P000012345 001 MTL
    20080819111303812239 N174X53A     8/19/2008  P000012345 002 POR
    20100401145927954563 N174X53A     4/1/2010                  MTL
    20100504083427334677 N174X53A     5/4/2010   P000012345 002 POR
    20100512095750285578 N174X53A     5/12/2010  P000012345 002 POR
    20100917085814607499 N174X53A     9/17/2010                 MTL
    20100614130143734676 N174X53A     6/14/2010  P000012345 002 MTL
    20100412101824624708 N174X53A     4/12/2010  P000012345 004 POR
    20100422150057876187 N174X53A     4/22/2010  P000012345 004 MTL
    20100504081750020711 N174X53A     5/4/2010   P000012345 004 POR
    20100512095303832580 N174X53A     5/12/2010  P000012345 004 POR
    20100712133833381506 N174X53A     7/12/2010  P000012345 004 MTL
    20100701123833300000 test         7/1/2010   P000011111 001 POR
    20100705964833300000 test         7/5/2010   P000011111 001 MTL
    20100702123456700000 test         7/2/2010   P000011111 002 POR
    20100704987654300000 test         7/4/2010                  MTLCan you explain the sorting rules agian, using different words.
    Explain in particular why the rows that are in the wrong place above should be changed.
    The code I used to get the results above was:
    WITH     got_r_num   AS
         SELECT    time_stamp
         ,       part_nbr
         ,       act_date
         ,       ord_nbr
         ,       ln_nbr
         ,       code_pre
         ,       code_sfx
         ,       ROW_NUMBER () OVER ( PARTITION BY  part_nbr
                                      ORDER BY          ord_nbr
                               ,               ln_nbr
                               ,          time_stamp
                             ) AS r_num
         FROM       inventory_history
    SELECT    time_stamp
    ,       part_nbr
    ,       act_date
    ,       ord_nbr
    ,       ln_nbr
    ,       code_pre
    FROM       got_r_num
    ORDER BY  part_nbr
    ,            CASE
              WHEN  ord_nbr  IS NOT NULL
              THEN  r_num
              ELSE  LAST_VALUE ( CASE
                                     WHEN  code_pre = 'POR'
                               AND   code_sfx = 'LIN'
                               THEN  r_num
                                 END
                           IGNORE NULLS
                               ) OVER ( PARTITION BY  part_nbr
                                  ORDER BY       time_stamp
           END
    ,         r_num
    ;It's a good thing that you're using Oracle 10; earlier versions didn't have the IGNORE NULLS feature in LAST_VALUE. That can be worked around, but it's complicated, and this is complicated enough already.
    By the way, if you want to experiment with this kind of ORDER BY expression, I suggest you iput it in the SELECT clause, like this:
    WITH     got_r_num   AS
         SELECT    time_stamp
         ,       part_nbr
         ,       act_date
         ,       ord_nbr
         ,       ln_nbr
         ,       code_pre
         ,       code_sfx
         ,       ROW_NUMBER () OVER ( PARTITION BY  part_nbr
                                      ORDER BY          ord_nbr
                               ,               ln_nbr
                               ,          time_stamp
                             ) AS r_num
         FROM       inventory_history
    SELECT    time_stamp
    ,       part_nbr
    ,       act_date
    ,       ord_nbr
    ,       ln_nbr
    ,       code_pre
    ,       code_sfx
    ,            CASE
              WHEN  ord_nbr  IS NOT NULL
              THEN  r_num
              ELSE  LAST_VALUE ( CASE
                                     WHEN  code_pre = 'POR'
                               AND   code_sfx = 'LIN'
                               THEN  r_num
                                 END
                           IGNORE NULLS
                               ) OVER ( PARTITION BY  part_nbr
                                  ORDER BY       time_stamp
           END
              as O_NUM
    ,       r_num
    FROM       got_r_num
    ORDER BY  part_nbr
    ,            o_num
    ,         r_num
    ;Edited by: Frank Kulash on Sep 23, 2010 5:39 PM
    Sorry, I left off some columns of output. I'll fix it when I have time, which is not now.

  • PreparedStatement and order by clause

    Hi all,
    I'm having a strange problem, when I try to use a preparedStatement which has a parameter in the order by clause the query ignores the order by clause and returns the data in the default table order.
    I'm trying to allow the enduser the ability to sort the results in any way they like.
    a sample of code is:
    javax.sql.DataSource dataSource;
    java.sql.Connection myConnection = null;
    String sort_order = null;
    sort_order = request.getParameter("sort_order");
    if(sort_order == null) {
         sort_order = "start_date, hostname";
    String sql = (String)Tokens.getInstance().getSQLProps().get(Tokens.FIND_SCHED);
    try {
    dataSource = getDataSource(request);
    myConnection = dataSource.getConnection();
    PreparedStatement pstmt = myConnection.prepareStatement(sql);
    pstmt.setString(1, (Tokens.getInstance().getProps().getProperty(Tokens.CURRENT)));
    pstmt.setString(2, sort_order);
    ResultSet rset = pstmt.executeQuery();
    thanks for any help you can provide

    You cannot use bind variables for column names. In this case, what you are telling Oracle to do is to sort by the literal string 'start_date, hostname', which (since the literal string is the same for every row) has no effect. If you want Oracle to sort by the values in those columns, you would have to have different versions of the query depending on the ORDER BY clause. In this case, you would probably want to append the ORDER BY clause to your base SQL statement rather than trying to pass the ORDER BY clause in as a bind variable.
    Justin
    Distributed Database Consulting, Inc.
    http://www.ddbcinc.com/askDDBC

Maybe you are looking for