Sorting: ORDER BY DECODE Problem on Pagination Query

Hi,
I've been searching around the easiest way to perform a dynamic "ORDER BY" clause and the "DECODE()" clause solution seem to be exactly what I am looking for. Unfortunately, It seems the DECODE function is not returning a correct column name value (I think it is returning NULL) since I'm receive the result set as if there was no ORDER BY clause.
I need some help to get through this!
Here the version with DECODE (not working)
It is a Procedure with frstRow, nbRows and var_order as parameters
The output returns the rows as if no ORDER BY was used
SELECT c1, c2
FROM
SELECT ROWNUM rn, arv.*
FROM A_AWA_AR arv
WHERE ROWNUM < (frstRow + nbRows - 1) -- show only some rows determined by procedure
ORDER BY DECODE(var_order, 1, c1, 2, c2, c1) -- sort by var_order
WHERE rn BETWEEN frstRow AND (frstRow + nbRows)
AND ROWNUM <= nbRows
Here the version without DECODE (working)
The output returns the rows as expected - ordered by c2 column
SELECT c1, c2
FROM
SELECT ROWNUM rn, arv.*
FROM A_AWA_AR arv
WHERE ROWNUM < (frstRow + nbRows - 1) -- show only some rows determined by procedure
ORDER BY c2 -- sort by var_order
WHERE rn BETWEEN frstRow AND (frstRow + nbRows)
AND ROWNUM <= nbRows
-----

Here are some results I've been getting... I will try the best I can to explain my problem.
And by the way the table A_AWA_AR is a VIEW maybe this can help.
My problem -- I think -- is that I don't understand how DECODE works. I should be a conditional IF-ELSE-THEN-like function but its behavior is not returning what I'm expecting.
I need a solution where I will be able to sort the first lets say 10 rows using ROWNUM to tag row position (in sub-query) then the main query should only show the rows from 3rd postion to 7th position in the main query (as an example) with RN BETWEEN 3 AND 7. My solution does not work when I use decode but works when i pass the column name directly.
Here is a simple query returning the values in the order they are found in the view:
SELECT ROWNUM rn, ROW_ID, SR_NUM
FROM A_AWA_AR
WHERE ROWNUM < 10 Results:
RN                                     ROW_ID          SR_NUM                                                         
1                                      1-100876        1-60476802                                                     
2                                      1-10087G        1-60476812                                                     
3                                      1-10087Q        1-60476822                                                     
4                                      1-10088A        1-60476842                                                     
5                                      1-10088K        1-60476852                                                     
6                                      1-10088U        1-60476862                                                     
7                                      1-100894        1-60476872                                                     
8                                      1-10089E        1-60476882                                                     
9                                      1-10089O        1-60476892                                                      Now this is the query & result I would like to obtain (this is done pretty fast since it returns as soon as it reaches 9 sorted rows):
SELECT ROWNUM rn, ROW_ID, SR_NUM
FROM A_AWA_AR
WHERE ROWNUM < 10
ORDER BY SR_NUMResults:
RN                                     ROW_ID          SR_NUM                                                         
1                                      1-1RV9J7        1-107274499                                                    
2                                      1-1RVXIF        1-107305575                                                    
3                                      1-1SHY65        1-108332861                                                    
4                                      1-22FOSR        1-125023563                                                    
5                                      1-236F3X        1-126270717                                                    
6                                      1-28P5EB        1-135542675                                                    
7                                      1-29P2VY        1-137219038                                                    
8                                      1-29ZNFH        1-137712221                                                    
9                                      1-2BLWQR        1-140430339                                                     But with decode even a simple pseudo decode:
SELECT ROWNUM rn, ROW_ID, SR_NUM
FROM A_AWA_AR
WHERE ROWNUM < 10
ORDER BY DECODE(1,1,SR_NUM)Results:
RN                                     ROW_ID          SR_NUM                                                         
1                                      1-100876        1-60476802                                                     
2                                      1-10087G        1-60476812                                                     
3                                      1-10087Q        1-60476822                                                     
4                                      1-10088A        1-60476842                                                     
5                                      1-10088K        1-60476852                                                     
6                                      1-10088U        1-60476862                                                     
7                                      1-100894        1-60476872                                                     
8                                      1-10089E        1-60476882                                                     
9                                      1-10089O        1-60476892                                                      Here is the structure I'm trying to get and works when passing a column name:
SELECT *
FROM
    SELECT ROWNUM rn, ROW_ID, SR_NUM
    FROM A_AWA_AR
    WHERE ROWNUM < 10
    ORDER BY SR_NUM
WHERE rn BETWEEN 3 AND 7Results:
RN                                     ROW_ID          SR_NUM                                                         
3                                      1-1SHY65        1-108332861                                                    
4                                      1-22FOSR        1-125023563                                                    
5                                      1-236F3X        1-126270717                                                    
6                                      1-28P5EB        1-135542675                                                    
7                                      1-29P2VY        1-137219038                                                     Now with decode (not working):
SELECT *
FROM
    SELECT ROWNUM rn, ROW_ID, SR_NUM
    FROM A_AWA_AR
    WHERE ROWNUM < 10
    ORDER BY DECODE(1,1,SR_NUM)
WHERE rn BETWEEN 3 AND 7Results:
RN                                     ROW_ID          SR_NUM                                                         
3                                      1-10087Q        1-60476822                                                     
4                                      1-10088A        1-60476842                                                     
5                                      1-10088K        1-60476852                                                     
6                                      1-10088U        1-60476862                                                     
7                                      1-100894        1-60476872                                                      Thanks for the support!

Similar Messages

  • Change Sorting order in F4 Help in BEx Query

    Hello All,
    My users want to change the sorting order of variable (e.g. Calmonth) in the selection screen (while executing a query).
    Currently, by default,the sorting order is ascending on key.
    I need to change it to descending on key.
    How to change the sorting order for F4 ONLY?
    Thanks,
    Mainak

    Hi,
    You can sort the Char.Values by doing settings in Query Designer :
    Say for 0calmonth :
    In its Display properties tab,You select appropriate options in
    Sort Char -> select the name
    sort by -> Key/Text
    Sort Direction -> Ascending/Descending.
    You will then have them in required order.
    ALso if you want it only for F4 help:
    In F4 help screen,you will see two arrow buttons next to Char. name. they are useful for sorting the values to either descending or ascending.
    Hope it helps:)
    Edited by: Sriman on Nov 12, 2008 5:17 PM

  • Sort order of results in UOW Conformed Query ??

    We would like to know what is the expected behavior when a query with ascending ordering or descending ordering is run with UOW conformed.
    We know for sure that SQL generated and submit to the database with ORDER BY CLAUSES (which get the data in the database and sorted properly)
    However, with a confirm query, the database result will in turn, combine selected objects in the UNIT OF WORK.
    Will the combined collection of object be sorted in the prescribed ordering ?
    For example:
    If in UOW we have some uncommitted CAT object CAT-9, and CAT- 5
    In Database we return CAT-1, CAT-3, CAT-7 sorted in ascending order
    Will toplink return after the UOW conformed query, a collection in the following order ?
    CAT-1, CAT-3, CAT-5, CAT-7, CAT-9 ???
    Please advice.
    Thank you in advance.

    No, any new objects that conform to the query will be added to the end of the result set, so may not match the query's sort order.
    If you need the conformed results to maintain ordering you can either,
    - Sort the results in-memory after executing the query through Collections.sort(List).
    - Use a TreeSet as the query resultCollectionClass to sort the query results.

  • Sort problem on dynamic query !!

    Hi,
    I have a dynamic query written in pl/sql, when I check "Sort" for each field in Report Attribute, error message raised up as "ORA-01785: ORDER BY item must be the number of a SELECT-list expression".
    If I do not check Sort, it works fine. In my apps I need all fields sorted by user, how to fix this problem?
    My query as below:
    declare
    query varchar2(2000):='select';
    s_class varchar2(1000);
    cursor c1 is select * from demo_preference;
    begin
    for c1_val in c1 loop
    if c1_val.login is not null then
    query := query ||' ' || 'login' || ',';
    end if;
    if c1_val.id is not null then
    query := query ||' ' || 'id' || ',';
    end if;
    end loop;
    query := SUBSTR(query, 1, length(query)-1);
    s_class := '(NVL(:P2_class, ''%'' || ''null%'') = ''%'' || ''null%'' OR
    EXISTS (SELECT 1 FROM apex_collections WHERE collection_name = ''P2CLASSCOL'' AND c001 = class))';
    query := query ||' ' || 'from ming.reg_report_view1 where'
    || ' ' || s_class;
    return(query);
    end;

    Maybe the internally mapped column used when you clicked on sort is not shown in the report. Try using aliases when you construct the query string, it could help apex internally in identifying a column even if its order changes for a different user. After all the column order in the code is dynamic and I guess even the no of columns displayed might vary both of which could make sorting on a column identified with a number, invalid.
    How about displaying the report query somewhere, so that you know what is the exact query being processed, it might give you better information about the problem.
    If the problem persists, then use a collection that is fetched those record using the same query string and change the report to refer the collection and then set column sorting on. This way apex would get confused on what columns are being sorted and it would just sort on a c001..c050 column as though it was a string(yes problems with number columns sorting when you do this).

  • Any way to set the sort order other than in the query?

    The report has an OrderBy parameter so the user can select which field is used for sorting. The query has an OrderBy clause referencing the parameter. The problem is that it doesn't always pay any attention to the parameter. (Since it appears to be intermittent my suspicion is that it really never pays any attention to it but that sometimes whatever actually is determining the order gives the same results.)
    The problem is that putting an OrderBy clause in a query is the only way I know of to determine the output order of a report. Is there any other way?
    Thanks.

    Unfortunately Break Order doesn't seem to be controlling the sort order. I found that the value was set on numerous fields but I've changed them all to None & it still isn't displaying in the order specified by the OrderBy parameter.
    Note that this is a 'form layout' report with one page/record so it doesn't really have columns-but the pages are supposed to be printed in the order chosen by the user from a list of values. It doesn't seem to matter what's selected from that list though, the output appears in the same order as if no Order By clause is specified.
    Can you think of anything else that would cause the report to ignore the order by clause?
    thanks.

  • Artist sort order problem

    Hi.
    I have music sync'd via iTunes Match from my laptop to my iPad and to my iPhone. The names and order appear correct on both the laptop and on the iPad but on the iPhone several artists are either shown in all lower case or the last name will be used by the sort, rather than the first.
    I've reset the artist, album artist and sort artist tags to different names (and they show up correctly then on the iPhone) and then set them back, but the problem remains.
    I'm thinking that the iPhone has a corrupt file somewhere - but how can I reset that without losing everything on the phone?
    A factory reset and restore from iCloud results in the same problem.
    Any ideas?
    Thanks guys.
    --Cynan.

    I also have an Artist sort order problem. While the circumstances are quite different from yours, I wonder if it's due to a similar corruption problem. I don't use iTunes Match. I found Kenny G listed under letter T.

  • Sort order problem in reports

    Dear members,
    I have report with a field name sort_order but it don't show data in asending order, but first sort another column item_name in asending order.
    the sort_order is number
    item_name is char
    order by property of number is asending and item_name is none.
    both in same group.
    where I am wrong?
    Regards:

    hi O.Developer,
    here is my query:
    select MILL_ORDER, mill
    from tran_master
    group by MILL_ORDER, mills
    order by Mill_order, mills
    Mill Sort Order
    MILL1 1
    MILL2 2
    MILL3 3
    MILL4 6
    MILL5 7
    MILL6 8
    MILL7 10
    KP1 11
    MILL9 12
    DBL 13
    FAT 14
    BUT the above query gives the following out put:
    Mill Sort Order
    MILL1 1
    MILL2 2
    MILL3 3
    MILL4 6
    MILL5 7
    MILL6 8
    MILL7 10
    MILL9 12
    DBL 13
    FAT 14
    KP1 11
    regards:

  • Query Builder - sort order causing ORA-00979 error

    Hello,
    I'm having an issue with the "sort order" clause in the query builder tool when using a function on the same column.
    I've build a simple query in the gui and the following statement is displayed in the SQL Window
    select     "DEMO_ORDER_ITEMS"."ORDER_ID" as "ORDER_ID",
         sum(DEMO_ORDER_ITEMS.QUANTITY) as "QUANTITY"
    from     "DEMO_ORDER_ITEMS" "DEMO_ORDER_ITEMS"
    group by DEMO_ORDER_ITEMS.ORDER_ID
    and if I add a sort order of "1" on the quantity field, the following query is changed to
    select     "DEMO_ORDER_ITEMS"."ORDER_ID" as "ORDER_ID",
         sum(DEMO_ORDER_ITEMS.QUANTITY) as "QUANTITY"
    from     "DEMO_ORDER_ITEMS" "DEMO_ORDER_ITEMS"
    group by DEMO_ORDER_ITEMS.ORDER_ID
    order by DEMO_ORDER_ITEMS.QUANTITY DESC
    When running this query I get an error
    failed to parse SQL query:
    ORA-00979: not a GROUP BY expression
    It is quite obvious what is wrong with the query but I don't know how to get the query builder tool to generate the correct statement. The order by clause should be order by sum(DEMO_ORDER_ITEMS.QUANTITY) DESC.
    I've tried to do some searching and haven't found much documentation on this issue. Any assistance is appreciated.
    Thanks,
    Tony

    While this statement is generally certainly true and should also be remembered, it is not really helpful for the specific case.
    I think the behaviour described is a bug. However this might be one a little difficult to solve. The user unfortunatly can't influence much of the query builder results directly.
    Either put 1 into the order by clause or use the column alias name without table alias name ("QUANTITY" only).
    My personal solution would be NOT TO use any sort criteia in the query builder. This makes sense in so far as the generated SQL is often basis for some report. If that is an interactive report you won't add a sort order anyway. This is a layout thing and layout will be handled by the end user itself. he can choose whatever sorting he wants.
    Edited by: Sven W. on Jul 22, 2009 11:43 AM

  • Month Sort Order Problem in Time Dimension(Essbase with Obiee)?

    Hi All,
    I am facing issue with month order in answers.
    First I created BSO cube using Essbase Studio. When i exposed cube in answers, months were sorted in alphabetical order. So i used RANK function with evaluate to arrange month members in proper order. But if i use RANK function it pulls all missing records and it is effecting performance.
    Then i created Time dimension(dimension loading) in essbase from FLAT file,initially it was coming in proper order. But after some time again it sorted in alphabetical order!!!
    I have seen in internate few people saying they didn't face this problem. I
    Please advice...
    Essbase: 11.1.1.3
    OBIEE: 10.1.3.4.1
    RS

    Hi user10300020,
    That's a fairly common problem with months that happens regardless of database source.
    In the logical layer of OBIEE you can specify the sort order on the month name column. Just double click on the month name column and look for the option to set sort order based on a different column. I typically sort the month name based on the month number.
    Give that a shot and tell me if it works.
    -Joe

  • MDO select query: how to control sort order using parameter

    Hi experts, is there a way of controling the sort order of an MDO select query using some parameter?
    I was thinking about using some statement like CASE [Param.1] WHEN 'abc' THEN [ORDER_NO]...END in sort section of the query but it is not working.
    Of course I colud go for various select queries...but I am wondering if it can be done using only one?
    Any ideas?
    Thanks for any help

    Hi Marco,
    Yes this can be achieved dynamically using SortExpr under dynamic link assignment in MDOAction block if you are using a transaction to call it.
    Please check below thread:
    Re: MDO Query Action Block In MII Transaction
    Or else, this [Param.1] thing should work as well provided it doesn't get the logic part in it, just pass the columns names separated by comma. Haven't tried it though, will check it for you.
    Best Regards,
    Swaroop

  • Query result list sort order in the Service Manager Portal (2012).

    Hi there,
    I have a setup a user prompt for a request offering in which the values are based on a query results list.  When the user prompt is displayed in the portal the order of the items presented based on my query results list is in reverse
    alphabetical order (Z to A) instead of traditional A to Z.  I can clicked the column header to toggle the sort order, however having to do this is slightly annoying.
    My query results list is based on returning the Department field of a specific criteria of template AD user accounts (which are imported into the CMDB via the AD Connector).
    Where and how is the sort order defined?
    Thanks
    Bryan

    Hi Bryan,
    After a quick test I can see the query results is in a descending order based on the first display column of the query results configuration. In the Request offering wizard I don't see an option for sorting. I don't think it is possible to configure this
    out of the box. 
    But maybe it is possible from a Self Service Portal rendering point of view. Maybe there is a key for it in the settings.xml just like the maximum of query results:
    http://blogs.technet.com/b/servicemanager/archive/2011/11/08/advanced-query-results-customization-for-request-offerings.aspx
    If this is possible I'm also curious to know how! :)
    - Dennis

  • "Date Added" sort order problem when importing folders

    I sort by "Date Added" to view my iTunes library, and use the middle "view" button to view tracks grouped as albums with artwork. This works mighty well except when I'm importing folders containing the files in an album, such as those I get from eMusic--the tracks are displayed out of order when I do this. The only solution I've found so far is to import one track at a time. Any ideas how I can get those tracks in there in the right order when importing a folder? Any scripts hanging around that will do it right? Thanks to all....

    What sort order do you have selecting in the sort drop-down?  There is no "correct" sort order.  You can sort on any metadata you can show in the Browser.  The sort drop-down is located in the top border of the Browser, near the left corner.  Every container in Aperture retains the sort order it last had.  When you manually move an Image in the Browser, the sort order is changed to "Manual".

  • ATV 4.4.2 photo sort order problem

    I have just updated my Apple TV (I believe it is now 4.4.2)
    When I view my photo albums, the sort order is not the same as that in my iPhoto.
    It was before the update.
    I resorted the albums again in iPhoto, they look just right on my MBP.  They also look exactly the way I wanted it on my iPad2 and my iPod.  But on Apple TV, it's all wrong again.
    I have invited friends over this weekend for a presentation that I want to show them slides on the Apple TV.  I am dead meat now.  Please advise.
    Thank you in advance.
    Eddie

    I did some additional testing using my Apple TV (1st generation) and my Windows 7 computer, and this is what I found.
    (This likely applies to iPad/iPod/iPhone too).
    Apple TV displays photos in order of their "Date Created" file attribute.
    If the "Date Created" file attribute of all photos in an album is identical, then Apple TV displays the photos in alphabetical order.
    This I what I do to make sure that Apple TV displays photos in the order I want them to be in:
    First, I arrange photos in the order I want them to be in. For this, I use Google Picasa, but one could also use FastStone Image Viewer, or any similar tool.
    Then, I bulk rename all the photos, so that their file names are in alphabetical order. I do this from within Picasa, but there are many other freeware file rename tools one could use for this.
    Next, I bulk change the "Date Created" file attribute of the photo files, so that they all have an identical one. For this, I use BatchTouch, freeware available for Windows and Mac.
    Finally, I open iTunes, and sync the photos to my Apple TV.
    Note: if they photos were already synced to your Apple TV before you perform the steps above, you may need to this beforehand:
    Open iTunes on your PC, and make sure that the correct photo folder is selected for syncing to your device.
    Using Windows Explorer, navigate to this folder.
    Within it, locate a subfolder called “iPod Photo Cache” ("iPad Photo Cache for iPad syncing) and delete it.

  • Sort Order problem in a cursor

    Hi,
    I have a cursor where the sort order needs to be specified dynamic. I have declared variables (say x and y) which contains the numbers. When I use them in the order by clause, though no compilation error, but the sorting does not occur.
    Eg. Cursor C1 is
    Select col1,col2
    from tab1
    where ...
    ORDER BY x (the value of x is 2).
    Your help is greatly appreciated. (This cursor is in Oracle Forms)
    Thanks

    How come this works:
    select samp1,samp2
    from   (select 'abc' samp1, 123 samp2 from dual union all
            select 'mno' samp1, 456 samp2 from dual union all
            select 'def' samp1, 3 samp2 from dual union all
            select 'pqr' samp1, 12 samp2 from dual union all
            select 'jkl' samp1, 64 samp2 from dual union all
            select 'stu' samp1, 10 samp2 from dual union all
            select 'vwx' samp1, 88 samp2 from dual union all
            select 'ghi' samp1, 7 samp2 from dual union all
            select 'yzz' samp1, 1 samp2 from dual
            ) testdata
    order by &x
    Enter value for x: 2
    old  12: order by &x
    new  12: order by 2
    SAM      SAMP2
    yzz          1
    def          3
    ghi          7
    stu         10
    pqr         12
    jkl         64
    vwx         88
    abc        123
    mno        456
    9 rows selected.
    select samp1,samp2
    from   (select 'abc' samp1, 123 samp2 from dual union all
            select 'mno' samp1, 456 samp2 from dual union all
            select 'def' samp1, 3 samp2 from dual union all
            select 'pqr' samp1, 12 samp2 from dual union all
            select 'jkl' samp1, 64 samp2 from dual union all
            select 'stu' samp1, 10 samp2 from dual union all
            select 'vwx' samp1, 88 samp2 from dual union all
            select 'ghi' samp1, 7 samp2 from dual union all
            select 'yzz' samp1, 1 samp2 from dual
            ) testdata
    order by &x
    Enter value for x: 1
    old  12: order by &x
    new  12: order by 1
    SAM      SAMP2
    abc        123
    def          3
    ghi          7
    jkl         64
    mno        456
    pqr         12
    stu         10
    vwx         88
    yzz          1
    9 rows selected.Isn't this what is wanted?
    -Marilyn

  • Request/Service Offering Sort Order Problems

    I am having a difficult time finding a way to sort the results of a MP Enumeration List prompt type.  The sort order on the portal user prompt is random even though I have arranged them in teh Service Manager console list to be alphabetical.  This
    does not apply to the prompt the end user sees in the portal.   It is not alphabetical.  Anyone have any ideas? 
    The MP Enumeration prompt is mapped to the Service Request Source list.   Version is 2012 R2 with update rollup 2 installed on all SCSM servers.

    I'm experiencing the same issue, have you found a solution to this?

Maybe you are looking for

  • Display number of unread and read messages in mailboxes?

    Hiya, Can I get Mail to list the total messages in a mailbox in the column on the left? I have a smart-mailbox for "flagged" items and I'd like to be able to see how many things are in it, while I'm sorting things in my inbox. There's an unanswered p

  • JLayeredPane

    Hi, I'm having a problem with JLayeredPane. My background image won't show up. Any suggestions? Thanks in advance           JFrame frame = new JFrame();           Toolkit t = frame.getToolkit();           Dimension d = t.getScreenSize();           in

  • Where SSL Support Enable

    Hello, in which file and where can I enable / disable SSL Support for sftp and ssh an Solaris 10 x86? Thank you for helpful inforamations. Kind regards

  • Server no response when I up-grade firmware

    My PC shows 'server no response' when I use tftp.exe to up-grade the firmware on BEFW11S4 V4.  Also, I have tried web approach at 198.162.1.1 to upgrade firmware but it does't get through - the web page does't come out as server not found.  I have at

  • Process using up system memory

    I'm running 10.6.3 on a Late 2008 Aluminum Macbook. Lately I've been experiencing an issue with a process called "fldirpatcher" using up system memory for about one minute after bringing the device out of sleep. Has anyone else experienced this or kn