ORA-01785

Hi,
Can anyone help me with this error - ORA-01785: ORDER BY item must be the number of a SELECT-list expression
Both queries are running fine individually so i am quite confused.
select oh.client_id,
       oh.customer_id,
       oh.name,
       to_char(trunc(oh.ship_by_date), 'DD-Mon-YYYY'),
       ol.order_id,
       sum(itl.update_qty) as cases_shipped,
       count(distinct itl.pallet_id) as no_pallets     
from order_header oh, order_line ol, inventory_transaction itl
where oh.order_id = ol.order_id
and ol.line_id = itl.line_id
and itl.reference_id = ol.order_id
and itl.sku_id = ol.sku_id
and oh.client_id = ol.client_id
and itl.client_id = oh.client_id
and itl.code = 'Shipment'
and trunc(oh.ship_by_date) >= '16-FEB-10'
and trunc(oh.ship_by_date) <= '20-FEB-10'
and oh.client_id = 'NOM-TELFOR'
group by oh.client_id, oh.customer_id, oh.name, trunc(oh.ship_by_date), ol.order_id
UNION ALL
select oh.client_id,
       oh.customer_id,
       oh.name,
       to_char(trunc(oh.ship_by_date), 'DD-Mon-YYYY'),
       ol.order_id,
       sum(itl.update_qty) as cases_shipped,
       count(distinct itl.pallet_id) as no_pallets   
from order_header oh, order_line ol, inventory_transaction_archive itl
where oh.order_id = ol.order_id
and ol.line_id = itl.line_id
and itl.reference_id = ol.order_id
and itl.sku_id = ol.sku_id
and oh.client_id = ol.client_id
and itl.client_id = oh.client_id
and itl.code = 'Shipment'
and trunc(oh.ship_by_date) >= '16-FEB-10'
and trunc(oh.ship_by_date) <= '20-FEB-10'
and oh.client_id = 'NOM-TELFOR'
group by oh.client_id, oh.customer_id, oh.name, trunc(oh.ship_by_date), ol.order_id
order by trunc(oh.ship_by_date) asc, oh.client_id, oh.customer_idThanks in advance.
SM.

In a UNION you have to order on columns/expressions you're extractiong in the select list.
And you have to use either the column numbers or the column aliases.
Since you're trying to order on an expression that is not in the select list you need to do something like:
select client_id,
       customer_id,
       name,
       to_char(ship_by_date, 'DD-Mon-YYYY'),
       order_id,
       cases_shipped,
       no_pallets  
from (
select oh.client_id,
       oh.customer_id,
       oh.name,
       trunc(oh.ship_by_date) ship_by_date,
       ol.order_id,
       sum(itl.update_qty) as cases_shipped,
       count(distinct itl.pallet_id) as no_pallets     
from order_header oh, order_line ol, inventory_transaction itl
where oh.order_id = ol.order_id
and ol.line_id = itl.line_id
and itl.reference_id = ol.order_id
and itl.sku_id = ol.sku_id
and oh.client_id = ol.client_id
and itl.client_id = oh.client_id
and itl.code = 'Shipment'
and trunc(oh.ship_by_date) >= '16-FEB-10'
and trunc(oh.ship_by_date) <= '20-FEB-10'
and oh.client_id = 'NOM-TELFOR'
group by oh.client_id, oh.customer_id, oh.name, trunc(oh.ship_by_date), ol.order_id
UNION ALL
select oh.client_id,
       oh.customer_id,
       oh.name,
       trunc(oh.ship_by_date),
       ol.order_id,
       sum(itl.update_qty) as cases_shipped,
       count(distinct itl.pallet_id) as no_pallets   
from order_header oh, order_line ol, inventory_transaction_archive itl
where oh.order_id = ol.order_id
and ol.line_id = itl.line_id
and itl.reference_id = ol.order_id
and itl.sku_id = ol.sku_id
and oh.client_id = ol.client_id
and itl.client_id = oh.client_id
and itl.code = 'Shipment'
and trunc(oh.ship_by_date) >= '16-FEB-10'
and trunc(oh.ship_by_date) <= '20-FEB-10'
and oh.client_id = 'NOM-TELFOR'
group by oh.client_id, oh.customer_id, oh.name, trunc(oh.ship_by_date), ol.order_id
order by 4,1,2
)Max
http://oracleitalia.wordpress.com
Edited by: Massimo Ruocchio on Mar 2, 2010 5:09 PM

Similar Messages

  • ORA-01785: ORDER BY item must be the number of a SELECT-list expression

    I have a dynamic SQL on a page. The SQL is dynamic because there are 3 LOV fields from which the user may make selections, and those field values are used in the WHERE clause. Also, the 2nd LOV field is populated with AJAX based on the 1st LOV's selection. And, the SQL joins to another table. The report has 7 sortable columns. The main table the SQL runs over has about 1,000 rows in it.
    The 2nd table has about 500 rows. (Sorry, I'm not sure what information you might need to help me, so I listed several details.)
    The application, and page, have been working flawlessly for several weeks. Then, yesterday the page was being used and the user received the "ORA-01785: ORDER BY item must be the number of a SELECT-list expression" error. The error just appeared out of the blue, so to speak. After weeks of of usage this is the first time this error has appeared. The application had not been changed, but they had been entering data into the main table and the table joined to in the SQL. The application is in production, so I can't put the application into DEBUG, etc. to see what SQL is being built.
    Anyway, I was trying to debug the page today and was doing selections and seeing what would display, then as suddenly as the page quit working it started working correctly. No code changes made, no server changes made (to my knowledge), and no data changes made.
    I'm new to both Oracle and Apex, and am not a SQL, Oracle or Apex expert. So, I am very perplexed and don't have any idea of what could be happening.
    I don't know if it's an issue with the SQL, AJAX not loading the select fields properly, a corrupted table, a server load issue, or something else all together.
    Any suggestions?
    Also, I'm not familiar with Oracle error messages. Can they be red herrings sometimes? Because, I don't programmaticly code the ORDER BY of the SQL. Apex appends that part. So, how could that aspect quit working. And, then suddenly start working.
    Sorry, but I'm extremely confused. :-) Any suggestions or help will be greatly appreciated.
    Thanks, Tony

    Hi Dan,
    Thanks for the feedback. I'm not going to be dogmatic about possible cause, but it has myself and another Oracle developer (who writes SQL and PL/SQL in his sleep) stumped. He doesn't think it's the SQL. Your suggestion was one of the first things we thought of considering the error message we got, but from Scott's reply that it could be a red herring, and after additional testing we've ruled the SQL out for now.
    I understand what you're saying, but the SQL selects all the columns (about 40 columns are selected) that are sortable columns on the report. I've sorted by all the columns by clicking on them and that aspect works without error.
    The SQL is created dynamically and is quite large. I've included below just for information purposes.
    Thanks, Tony
    = = = = = = =
    DECLARE
    q VARCHAR2 (4000);
    BEGIN
    q := 'select ';
    q := q || 'hd."ID", hd."MODEL", hd."YEAR", hd."HIN", hd."BOAT_STATUS", hd."SPECIAL_ACTIVITY", hd."PARTIAL_KIT_VALUE",
    hd.OPTION_NUMBER || '' - '' || e.DESCRIPTION OPTION_NUMBER,
    hd."OPTIONS_HOURS", hd."OPTIONS_MATERIAL", hd."STATION", hd."ACTIVITY_COMMENT", hd."BI_EQUIV_MATERIAL",
    hd."BI_EQUIV_LABOR", hd."EI_EQUIV_MATERIAL", hd."EI_EQUIV_LABOR", hd."BASE_BOAT", hd."SA_ENGINES",
    hd."SA_OPTIONS", hd."DY_STANDARD_MATERIAL", hd."DY_STANDARD_LABOR", hd."DY_STANDARD_OVERHEAD",
    hd."DY_STANDARD_ENGINE", hd."DY_ENGINE_ENGINEUP", hd."DY_ENGINE_MATERIAL", hd."DY_ENGINE_LABOR",
    hd."DY_ENGINE_OVERHEAD", hd."DY_OPTIONS_MATERIAL", hd."DY_OPTIONS_LABOR", hd."DY_OPTIONS_OVERHEAD", hd."EI_STANDARD_UNITS", hd."EI_STANDARD_MATERIAL", hd."EI_STANDARD_LABOR", hd."EI_STANDARD_OVERHEAD", hd."EI_STANDARD_ENGINE", hd."EI_ENGINE_ENGINEUP", hd."EI_ENGINE_MATERIAL", hd."EI_ENGINE_LABOR", hd."EI_ENGINE_OVERHEAD", hd."EI_OPTIONS_MATERIAL", hd."EI_OPTIONS_LABOR", hd."EI_OPTIONS_OVERHEAD",
    htmldb_item.hidden(50,hd.ID) idalias ';
    q := q || 'from "PLP_HIN_DETAIL" hd , "PLP_ENGINES" e ';
    IF
    :P10_SEARCH_MODEL IS NOT NULL
    AND :P10_SEARCH_MODEL <> '%null%'
    AND :P10_SEARCH_YEAR IS NOT NULL
    AND :P10_SEARCH_YEAR <> '%null%'
    AND :P10_SEARCH_STATUS IS NOT NULL
    AND :P10_SEARCH_STATUS <> '%null%'
    THEN
    q := q || 'WHERE
    (instr(upper(hd.MODEL),upper(nvl(:P10_SEARCH_MODEL,hd.MODEL))) > 0
    AND instr(upper(hd.YEAR),upper(nvl(:P10_SEARCH_YEAR,hd.YEAR))) > 0
    AND instr(upper(hd.BOAT_STATUS),upper(nvl(:P10_SEARCH_STATUS,hd.BOAT_STATUS))) > 0)
    AND e.OPTION_NUMBER (+) = hd.OPTION_NUMBER and e.YEAR (+) = hd.YEAR AND e.MODEL (+) = hd.MODEL AND e.PLP_ID (+) = hd.PLP_ID';
    ELSE
    IF
    :P10_SEARCH_MODEL IS NOT NULL
    OR :P10_SEARCH_YEAR IS NOT NULL
    OR :P10_SEARCH_STATUS IS NOT NULL
    THEN
    q := q || 'WHERE
    (instr(upper(hd.MODEL),upper(nvl(:P10_SEARCH_MODEL,hd.MODEL))) > 0
    AND instr(upper(hd.YEAR),upper(nvl(:P10_SEARCH_YEAR,hd.YEAR))) > 0
    or instr(upper(hd.BOAT_STATUS),upper(nvl(:P10_SEARCH_STATUS,hd.BOAT_STATUS))) > 0)
    AND e.OPTION_NUMBER (+) = hd.OPTION_NUMBER and e.YEAR (+) = hd.YEAR AND e.MODEL (+) = hd.MODEL AND e.PLP_ID (+) = hd.PLP_ID';
    else
    q := q || 'WHERE e.option_number = hd.option_number and e.year = hd.year and e.model = hd.model and e.plp_id = hd.plp_id';
    END IF;
    END IF;
    q := q || ' AND :P0_PLP_ID = hd.PLP_ID';
    return q;
    END;

  • Error ORA-01785, while viewing Application Express views in Utilities tab.

    Hi,
    I am unable to see data in Application Express Views in Utilities tab. I am using APEX 3.1.2.
    I am getting the following error,
    failed to parse SQL query:
    ORA-01785: ORDER BY item must be the number of a SELECT-list expression.
    There is no sort by in the SQL query. select WORKSPACE, APPLICATION_ID, APPLICATION_NAME
    from APEX_APPLICATION_BC_ENTRIES .
    Can anyone throw some light on this?
    Thanks
    Nimmi

    No, It is not that view I get the same error whenever I am in the Utilities Tab of APEX and trying to query Application Express views.
    Thanks

  • Order_by in set_block_property returns ORA-01785

    Hello.
    We have a problem with using function in set_block_property with order by when using union sentence.
    Our code looks like:
    set_block_property('block', default_where, 'id = '||:item||'
    UNION ALL
    SELECT ROWID,column1,column2,column3,... FROM table WHERE
    id = '||:item);
    set_block_property('block', order_by, 'function(column1), column2');
    Form returns error: ORA -01785 : ORDER BY item must be the number of a SELECT-list expression
    We know, that with union we should use numbers in order by instead of column names. But it doesn't work.
    If we do not use UNION, then order_by with function works.
    How can we solve this problem?
    Thanks.
    Edited by: DejanH on Sep 10, 2008 8:03 AM

    Not sure you can ever get Forms to handle that complicated a query using default block processing.
    Since it looks like your block is display-only, I would create a record group with the query and order by, populate it, and then step through the record group row-by-row moving the values to the block (which would then be a control-block rather than a base-table block). Of course, if your query selects more than a few hundred rows, this is not a good solution.

  • ORA-01785 error when dynamically resetting report source

    When programmatically changing the region source on a 'function body returning SQL query' report, i get the following error:
    ORA-01785: ORDER BY item must be the number of a SELECT-list expression
    if the new SQL statement has fewer columns than the one it is replacing, and the user had previously selected a sort column for a column# higher than the highest column number in the new query
    for instance if the query was SELECT * FROM EMP and the user had clicked the COMM header to sort on COMM (column number 7), then changing the region source to SELECT * FROM DEPT results in the ORA-01785 -- evidently because Apex is submitting SELECT * FROM DEPT ORDER BY 7
    is there a way to programmatically reset Apex's ORDER BY column# setting? clear cache and reset pagination don't seem to do it

    that put me on the right track
    i really didn't want to reset all user preferences, as i am making extensive use of them in this application
    however, i did find HTMLDB_UTIL.REMOVE_SORT_PREFERENCES -- which, unfortunately, removes all sort preferences, not for a specific page or region.
    i was able to find the correct preference in WWV_FLOW_PREFERENCES$, so I can user HTML_DB_UTIL.SET_PREFERENCE to reset it, but the preference_name includes the region#, which changes on import -- so i'll need to extract if at runtime using javascript, or figure out some other runtime technique for finding the correct preference_name
    hey, apex developers, how about enhancing HTMLDB_UTIL.REMOVE_SORT_PREFERENCES to allow setting/removing the sort preference for a specific page (by page number or alias) and region (by position on the page or some other reliable identifier?

  • Help on ORA-01785: ORDER BY item must be the number of a SELECT-list expres

    Hi
    Please find the SQL below . I am unable to place ORDER bY upper(column) due to error ORA-01785: ORDER BY item must be the number of a SELECT-list expression. Please help
    SELECT engn_cnslt_list.cnslt_hcp_id "hcp_id",
    hcp.first_nm "first_nm", hcp.last_nm "last_nm",
    FROM cnslt_engn, engn_tier_dtl, engn_cnslt_list, hcp
    WHERE cnslt_engn.cnslt_engn_id = engn_cnslt_list.cnslt_engn_id
    AND engn_cnslt_list.cnslt_hcp_id = hcp.hcp_id
    AND engn_tier_dtl.cnslt_engn_id = cnslt_engn.cnslt_engn_id
    AND engn_tier_dtl.engn_tier_dtl_id =
    engn_cnslt_list.engn_tier_dtl_id
    AND cnslt_engn.cnslt_engn_id = 100643
    AND engn_cnslt_list.cnslt_hcp_id NOT IN (
    SELECT hcp_id
    FROM mtg_cnslt_list
    WHERE cnslt_mtg_id IN (SELECT cnslt_mtg_id
    FROM cnslt_mtg
    WHERE cnslt_engn_id = 1000643)
    AND hcp_id IS NOT NULL)
    UNION
    SELECT mtg_cnslt_list.hcp_id "hcp_id", hcp.first_nm "first_nm",
    hcp.last_nm "last_nm",
    FROM cnslt_mtg,
    cnslt_engn,
    mtg_cnslt_list,
    engn_tier_dtl,
    engn_cnslt_list,
    hcp
    WHERE cnslt_engn.cnslt_engn_id = cnslt_mtg.cnslt_engn_id
    AND cnslt_engn.cnslt_engn_id = engn_cnslt_list.cnslt_engn_id
    AND engn_cnslt_list.cnslt_hcp_id = mtg_cnslt_list.hcp_id
    AND engn_cnslt_list.cnslt_hcp_id = hcp.hcp_id
    AND cnslt_mtg.cnslt_mtg_id = mtg_cnslt_list.cnslt_mtg_id
    AND engn_tier_dtl.cnslt_engn_id = cnslt_engn.cnslt_engn_id
    AND mtg_cnslt_list.engn_tier_dtl_id =
    engn_tier_dtl.engn_tier_dtl_id
    AND cnslt_engn.cnslt_engn_id = 1000643
    order by upper(last_nm),upper(first_nm)

    try:
    select * from (SELECT engn_cnslt_list.cnslt_hcp_id "hcp_id",
    hcp.first_nm "first_nm", hcp.last_nm "last_nm",
    FROM cnslt_engn, engn_tier_dtl, engn_cnslt_list, hcp
    WHERE cnslt_engn.cnslt_engn_id = engn_cnslt_list.cnslt_engn_id
    AND engn_cnslt_list.cnslt_hcp_id = hcp.hcp_id
    AND engn_tier_dtl.cnslt_engn_id = cnslt_engn.cnslt_engn_id
    AND engn_tier_dtl.engn_tier_dtl_id =
    engn_cnslt_list.engn_tier_dtl_id
    AND cnslt_engn.cnslt_engn_id = 100643
    AND engn_cnslt_list.cnslt_hcp_id NOT IN (
    SELECT hcp_id
    FROM mtg_cnslt_list
    WHERE cnslt_mtg_id IN (SELECT cnslt_mtg_id
    FROM cnslt_mtg
    WHERE cnslt_engn_id = 1000643)
    AND hcp_id IS NOT NULL)
    UNION
    SELECT mtg_cnslt_list.hcp_id "hcp_id", hcp.first_nm "first_nm",
    hcp.last_nm "last_nm",
    FROM cnslt_mtg,
    cnslt_engn,
    mtg_cnslt_list,
    engn_tier_dtl,
    engn_cnslt_list,
    hcp
    WHERE cnslt_engn.cnslt_engn_id = cnslt_mtg.cnslt_engn_id
    AND cnslt_engn.cnslt_engn_id = engn_cnslt_list.cnslt_engn_id
    AND engn_cnslt_list.cnslt_hcp_id = mtg_cnslt_list.hcp_id
    AND engn_cnslt_list.cnslt_hcp_id = hcp.hcp_id
    AND cnslt_mtg.cnslt_mtg_id = mtg_cnslt_list.cnslt_mtg_id
    AND engn_tier_dtl.cnslt_engn_id = cnslt_engn.cnslt_engn_id
    AND mtg_cnslt_list.engn_tier_dtl_id =
    engn_tier_dtl.engn_tier_dtl_id
    AND cnslt_engn.cnslt_engn_id = 1000643
    order by upper(last_nm),upper(first_nm)

  • ORA-01785 error :ORDER BY item must be the number of a SELECT-list expressi

    When i'm sorting on a calculated field i'm getting the following error:
    ORDER BY item must be the number of a SELECT-list expression
    Please any help?
    Thanks

    Here is the query, but its generated by the disco plus:
    WITH
    DISC_TMP_090921083714 as (
    SELECT /*+ NOREWRITE */DECODE(( DECODE(( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') ),NULL,( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') )||o787901.DISTRICT,( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') )) ),'D10','Crawfordsville','D20','Fort Wayne','D30','Greenfield','D40','LaPorte','D50','Seymour','D60','Vincennes') as C_1,o787901.INVOICE_DT as E787916,SUM(o787901.USED_AMOUNT) as E787915_SUM,( SUM(o787901.PS_QTY) )-( ( SUM(o787901.PO_PAYMNTS) )/( CASE WHEN ( SUM(o787901.UNIT_COST) ) = 0 THEN ( ( SUM(o787901.ORIGINAL_AMOUNT) )/( SUM(o787901.PS_QTY) ) ) ELSE ( SUM(o787901.UNIT_COST) ) END ) ) as C_4,( 0.8*( SUM(o787901.PS_QTY) ) )-( ( SUM(o787901.PO_PAYMNTS) )/( CASE WHEN ( SUM(o787901.UNIT_COST) ) = 0 THEN ( ( SUM(o787901.ORIGINAL_AMOUNT) )/( SUM(o787901.PS_QTY) ) ) ELSE ( SUM(o787901.UNIT_COST) ) END ) ) as C_5,( SUM(o787901.PO_PAYMNTS) )/( CASE WHEN ( SUM(o787901.UNIT_COST) ) = 0 THEN ( ( SUM(o787901.ORIGINAL_AMOUNT) )/( SUM(o787901.PS_QTY) ) ) ELSE ( SUM(o787901.UNIT_COST) ) END ) as C_3,0.8*( SUM(o787901.PS_QTY) ) as C_2,SUM(o787901.PS_QTY) as E787907_SUM,( ( SUM(o787901.ENCUMB_AMNT) )/( SUM(o787901.ORIGINAL_AMOUNT) ) ) as C_6,SUM(o787901.ORIGINAL_AMOUNT) as E787909_SUM,( SUM(o787901.ENCUMB_AMNT) )-( SUM(o787901.PO_PAYMNTS) ) as C_7,SUM(o787901.PO_PAYMNTS) as E787905_SUM,SUM(o787901.ENCUMB_AMNT) as E787908_SUM
    FROM ( select sum(t1.AMOUNT) po_paymnts,
    (t2.ORIGINAL_AMOUNT/t2.PS_QTY) unit_cost_1,
    t1.PO_NO,
    t2.PS_QTY,
    t2.AMOUNT encumb_amnt,
    t2.ORIGINAL_AMOUNT,
    T2.class district,
    0 owner_id,
    0 stock_id,
    0 received_amount,
    0 curr_amount,
    0used_amount,
    to_char(add_months(t1.INVOICE_DT,6),'YYYY') invoice_dt,
    0 unit_cost
    from dwtarget.dw_ps_all_payments_fact t1,dwtarget.dw_ps_ap_po_rq_fact t2
    where t1.PO_NO=t2.PS_NO
    and t1.LINE_NO=t2.PS_LINE
    and t2.OBJECT in ('545000','545001')
    group by t1.PO_NO,T2.class,t2.PS_QTY,t2.AMOUNT,t2.ORIGINAL_AMOUNT,t2.class,to_char(add_months(t1.INVOICE_DT,6),'YYYY')
    union all
    select 0 unit_cost_1,0 po_paymnts,to_char(0) po_no,0 ps_qty,0 encumb_amnt,0 original_amount,to_char(t2.owner_cat) district, t1.owner_id, stock_id,
    sum(received_amount) received_amount,
    sum(curr_amount) curr_amount,
    sum(used_amount) used_amount,
         date_work,
    unit_cost
         from
    (select owner_id_to owner_id,stock_id_to stock_id,sum(full_amount) received_amount,0 curr_amount,0 used_amount,null date_work,unit_cost
    from dwtarget.dw_wms_mat_transfer
    where master_code_id in ('8664','8665')
    and unit_id='724'
    group by owner_id_to,stock_id_to,unit_cost
    union all
    select owner_id,stock_id,0 received_amount,sum(curr_amount) curr_amount,0 used_amount,null date_work,0 unit_cost
    from dwtarget.dw_wms_mat_inv
    where master_code_id in ('8664','8665')
    group by owner_id,stock_id
    union all
    select t2.owner_id, t1.stock_id, 0 received_amt, 0 curr_amt, sum(amount) used_amt,to_char(add_months(date_work,6),'YYYY') date_work,0 unit_cost
    from dwtarget.dw_wms_wo_mat_dc t1,
    (select owner_id,stock_id
    from dwtarget.dw_wms_mat_inv
    where master_code_id in ('8664','8665')
    group by owner_id,stock_id)t2
    where t1.stock_id = t2.stock_id
    and unit_id='724'
    group by t2.owner_id, t1.stock_id,to_char(add_months(date_work,6),'YYYY')) t1,
    dwtarget.dw_wms_setup_owner t2
    where t1.owner_id = t2.owner_id
    group by t2.owner_cat,t1.owner_id,stock_id,date_work,unit_cost
    ) o787901
    GROUP BY DECODE(( DECODE(( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') ),NULL,( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') )||o787901.DISTRICT,( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') )) ),'D10','Crawfordsville','D20','Fort Wayne','D30','Greenfield','D40','LaPorte','D50','Seymour','D60','Vincennes'),o787901.INVOICE_DT
    HAVING (( SUM(o787901.PS_QTY) ) != 0))
    SELECT C_1 as C_1,E787916 as E787916,E787915_SUM as E787915_SUM,C_4 as C_4,C_5 as C_5,C_3 as C_3,C_2 as C_2,E787907_SUM as E787907_SUM,C_6 as C_6,E787909_SUM as E787909_SUM,C_7 as C_7,E787905_SUM as E787905_SUM,E787908_SUM as E787908_SUM, 0
    FROM DISC_TMP_090921083714
    UNION ALL
    SELECT /*+ NOREWRITE */C_1 as C_1,E787916 as E787916,E787915_SUM as E787915_SUM,C_4 as C_4,C_5 as C_5,C_3 as C_3,C_2 as C_2,E787907_SUM as E787907_SUM,C_6 as C_6,E787909_SUM as E787909_SUM,C_7 as C_7,E787905_SUM as E787905_SUM,E787908_SUM as E787908_SUM, GID as GID FROM (SELECT DECODE(( DECODE(( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') ),NULL,( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') )||o787901.DISTRICT,( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') )) ),'D10','Crawfordsville','D20','Fort Wayne','D30','Greenfield','D40','LaPorte','D50','Seymour','D60','Vincennes') as C_1,o787901.INVOICE_DT as E787916,SUM(o787901.USED_AMOUNT) as E787915_SUM,( SUM(o787901.PS_QTY) )-( ( SUM(o787901.PO_PAYMNTS) )/( CASE WHEN ( SUM(o787901.UNIT_COST) ) = 0 THEN ( ( SUM(o787901.ORIGINAL_AMOUNT) )/( SUM(o787901.PS_QTY) ) ) ELSE ( SUM(o787901.UNIT_COST) ) END ) ) as C_4,( 0.8*( SUM(o787901.PS_QTY) ) )-( ( SUM(o787901.PO_PAYMNTS) )/( CASE WHEN ( SUM(o787901.UNIT_COST) ) = 0 THEN ( ( SUM(o787901.ORIGINAL_AMOUNT) )/( SUM(o787901.PS_QTY) ) ) ELSE ( SUM(o787901.UNIT_COST) ) END ) ) as C_5,( SUM(o787901.PO_PAYMNTS) )/( CASE WHEN ( SUM(o787901.UNIT_COST) ) = 0 THEN ( ( SUM(o787901.ORIGINAL_AMOUNT) )/( SUM(o787901.PS_QTY) ) ) ELSE ( SUM(o787901.UNIT_COST) ) END ) as C_3,0.8*( SUM(o787901.PS_QTY) ) as C_2,SUM(o787901.PS_QTY) as E787907_SUM,( ( SUM(o787901.ENCUMB_AMNT) )/( SUM(o787901.ORIGINAL_AMOUNT) ) ) as C_6,SUM(o787901.ORIGINAL_AMOUNT) as E787909_SUM,( SUM(o787901.ENCUMB_AMNT) )-( SUM(o787901.PO_PAYMNTS) ) as C_7,SUM(o787901.PO_PAYMNTS) as E787905_SUM,SUM(o787901.ENCUMB_AMNT) as E787908_SUM,GROUPING_ID(( DECODE(( DECODE(( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') ),NULL,( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') )||o787901.DISTRICT,( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') )) ),'D10','Crawfordsville','D20','Fort Wayne','D30','Greenfield','D40','LaPorte','D50','Seymour','D60','Vincennes') ),o787901.INVOICE_DT) as GID
    FROM ( select sum(t1.AMOUNT) po_paymnts,
    (t2.ORIGINAL_AMOUNT/t2.PS_QTY) unit_cost_1,
    t1.PO_NO,
    t2.PS_QTY,
    t2.AMOUNT encumb_amnt,
    t2.ORIGINAL_AMOUNT,
    T2.class district,
    0 owner_id,
    0 stock_id,
    0 received_amount,
    0 curr_amount,
    0used_amount,
    to_char(add_months(t1.INVOICE_DT,6),'YYYY') invoice_dt,
    0 unit_cost
    from dwtarget.dw_ps_all_payments_fact t1,dwtarget.dw_ps_ap_po_rq_fact t2
    where t1.PO_NO=t2.PS_NO
    and t1.LINE_NO=t2.PS_LINE
    and t2.OBJECT in ('545000','545001')
    group by t1.PO_NO,T2.class,t2.PS_QTY,t2.AMOUNT,t2.ORIGINAL_AMOUNT,t2.class,to_char(add_months(t1.INVOICE_DT,6),'YYYY')
    union all
    select 0 unit_cost_1,0 po_paymnts,to_char(0) po_no,0 ps_qty,0 encumb_amnt,0 original_amount,to_char(t2.owner_cat) district, t1.owner_id, stock_id,
    sum(received_amount) received_amount,
    sum(curr_amount) curr_amount,
    sum(used_amount) used_amount,
         date_work,
    unit_cost
         from
    (select owner_id_to owner_id,stock_id_to stock_id,sum(full_amount) received_amount,0 curr_amount,0 used_amount,null date_work,unit_cost
    from dwtarget.dw_wms_mat_transfer
    where master_code_id in ('8664','8665')
    and unit_id='724'
    group by owner_id_to,stock_id_to,unit_cost
    union all
    select owner_id,stock_id,0 received_amount,sum(curr_amount) curr_amount,0 used_amount,null date_work,0 unit_cost
    from dwtarget.dw_wms_mat_inv
    where master_code_id in ('8664','8665')
    group by owner_id,stock_id
    union all
    select t2.owner_id, t1.stock_id, 0 received_amt, 0 curr_amt, sum(amount) used_amt,to_char(add_months(date_work,6),'YYYY') date_work,0 unit_cost
    from dwtarget.dw_wms_wo_mat_dc t1,
    (select owner_id,stock_id
    from dwtarget.dw_wms_mat_inv
    where master_code_id in ('8664','8665')
    group by owner_id,stock_id)t2
    where t1.stock_id = t2.stock_id
    and unit_id='724'
    group by t2.owner_id, t1.stock_id,to_char(add_months(date_work,6),'YYYY')) t1,
    dwtarget.dw_wms_setup_owner t2
    where t1.owner_id = t2.owner_id
    group by t2.owner_cat,t1.owner_id,stock_id,date_work,unit_cost
    ) o787901
    WHERE ( SYS_OP_MAP_NONNULL(DECODE(( DECODE(( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') ),NULL,( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') )||o787901.DISTRICT,( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') )) ),'D10','Crawfordsville','D20','Fort Wayne','D30','Greenfield','D40','LaPorte','D50','Seymour','D60','Vincennes')) , SYS_OP_MAP_NONNULL(o787901.INVOICE_DT) ) IN ( SELECT SYS_OP_MAP_NONNULL(C_1) , SYS_OP_MAP_NONNULL(E787916) FROM DISC_TMP_090921083714)
    GROUP BY GROUPING SETS(( ( DECODE(( DECODE(( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') ),NULL,( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') )||o787901.DISTRICT,( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') )) ),'D10','Crawfordsville','D20','Fort Wayne','D30','Greenfield','D40','LaPorte','D50','Seymour','D60','Vincennes') ),o787901.INVOICE_DT ),( ( DECODE(( DECODE(( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') ),NULL,( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') )||o787901.DISTRICT,( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') )) ),'D10','Crawfordsville','D20','Fort Wayne','D30','Greenfield','D40','LaPorte','D50','Seymour','D60','Vincennes') ) ))
    HAVING GROUP_ID()=0
    AND GROUPING_ID(( DECODE(( DECODE(( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') ),NULL,( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') )||o787901.DISTRICT,( DECODE(o787901.DISTRICT,'1000','D10','2000','D20','3000','D30','4000','D40','5000','D50','6000','D60') )) ),'D10','Crawfordsville','D20','Fort Wayne','D30','Greenfield','D40','LaPorte','D50','Seymour','D60','Vincennes') ),o787901.INVOICE_DT) != 0)
    ORDER BY 14 DESC;
    Thanks

  • ORA-01460: unimplemented or unreasonable error & OPEN-FOR statement ...

    Hi,
    I have a procedure that opens a cursor which returns a result set based on a dynamic SELECT statement. The IN clause
    in most cases needs to handle more than 1000 expressions. So to avoid the ORA-01785 error, I use a function to
    convert the comma separated list of ids (which are unknown) into a collection which can then be used in the sub query
    to process each expression or id. I assumed that the maximum string length I could use for these list of ids was
    32767, i.e. VARCHAR2. But f I attempt to open the cursor with a list of ids where the string length is greater than 4000 bytes , the cursor is invalid
    and it seems to throw the following Oracle error:
    ORA-01460: unimplemented or unreasonable conversion ...
    Note that anything less than 4000 bytes is fine. I have attached some of the code below and would appreciate if anyone
    could tell me what im doing wrong! For example, can a varchar2 variable greater than 4000 bytes not be used when
    executing dynamic SQL in the context of the OPEN-FOR statement?
    -- Create type to hold collection of identifiers.
    CREATE OR REPLACE TYPE IDList IS TABLE OF NUMBER;
    -- Function which converts a string of comma separated list of identifiers
    -- into a collection.
    CREATE OR REPLACE FUNCTION fnConvertIDListToCollection(
         varList IN VARCHAR2,
         varDelimiter IN VARCHAR2 DEFAULT ',')
    RETURN IDList
    IS
         varString long := varList || varDelimiter;
         varPos pls_integer;
         varData IDList := IDList();
    BEGIN
         LOOP
              varPos := instr(varString, varDelimiter);
              EXIT WHEN (nvl(varPos, 0) = 0);
              varData.extend;
              varData(varData.count) := trim(substr(varString, 1, varPos - 1));
              varString := substr(varString, varPos + 1);
         END LOOP;
         RETURN (varData);
    END;
    CREATE OR REPLACE PROCEDURE MyTestProc
    myCursor OUT SYS_REFCURSOR
    AS
    varListOfIds VARCHAR2(32767);
    BEGIN
         -- Hard coding this for now but this will be an incoming parameter containing a list
         -- of unknown ids, separated with commas.
         varListOfIds := '1,2 .. , 5000';
         OPEN myCursor FOR
         'SELECT     DISTINCT val1, val2, val3
         FROM TABLEA
         WHERE     val1 IN (select * from table(cast(fnConvertIDListToCollection(:ListOfIds) as IDList)))' USING varListOfIds;
    END;
    /

    APC,
    Many thanks for the suggestion and yes I could possibly implement an alternative solution, certainly for some cases but I need to investigate further for others. I'm migrating some SQL Server logic over to Oracle and that was simply the approach taken on that platform.
    Could I trouble you with one further question as a newbie to all of this. I hinted in my last response that I was somewhat confused over the limits with the use of varchar2 variables in PL/SQL. If I were building up a piece of dynamic SQL (e.g. SELECT statement including a WHERE clause) using an incoming VARCHAR2 parameter for the WHERE clause, can this parameter contain more than 4000 bytes if necessary. I assumed it could be as big as 32767 bytes but an earlier response suggested a maximum of 4000 bytes. Really sorry for probably a fairly basic Oracle question but it would be very appreciated if you could explain this to me.
    Again, many thanks.

  • Decode in order by clause

    Is it possible to use a decode function in the order by clause, when you have two select statements with a union between? I seem to be getting an error-message telling me to type a number.
    Can anybody help me?

    Tove,
    Is this what you mean?
    SQL> SELECT
      2  *
      3  FROM
      4  (SELECT
      5      empno     no
      6  ,   ename     name
      7  ,   job       description
      8  FROM          emp
      9  UNION
    10  SELECT
    11      deptno    no
    12  ,   dname     name
    13  ,   loc       description
    14  FROM          dept
    15  )
    16  ORDER BY
    17  DECODE(1
    18  ,      1,TO_CHAR(no)
    19  ,      2,name
    20  ,      3,description)
    21  /
            NO NAME           DESCRIPTION
            10 ACCOUNTING     NEW YORK
            20 RESEARCH       DALLAS
            30 SALES          CHICAGO
            40 OPERATIONS     BOSTON
          7369 SMITH          CLERK
          7499 ALLEN          SALESMAN
          7521 WARD           SALESMAN
          7566 JONES          MANAGER
          7654 MARTIN         SALESMAN
          7698 BLAKE          MANAGER
          7782 CLARK          MANAGER
          7788 SCOTT          ANALYST
          7839 KING           PRESIDENT
          7844 TURNER         SALESMAN
          7876 ADAMS          CLERK
          7900 JAMES          CLERK
          7902 FORD           ANALYST
          7934 MILLER         CLERK
    18 rows selected.Now, to order by the second column, I set the first parameter of the
    DECODE to "2":
    SQL> SELECT
      2  *
      3  FROM
      4  (SELECT
      5      empno     no
      6  ,   ename     name
      7  ,   job       description
      8  FROM          emp
      9  UNION
    10  SELECT
    11      deptno    no
    12  ,   dname     name
    13  ,   loc       description
    14  FROM          dept
    15  )
    16  ORDER BY
    17  DECODE(2
    18  ,      1,TO_CHAR(no)
    19  ,      2,name
    20  ,      3,description)
    21  /
            NO NAME           DESCRIPTION
            10 ACCOUNTING     NEW YORK
          7876 ADAMS          CLERK
          7499 ALLEN          SALESMAN
          7698 BLAKE          MANAGER
          7782 CLARK          MANAGER
          7902 FORD           ANALYST
          7900 JAMES          CLERK
          7566 JONES          MANAGER
          7839 KING           PRESIDENT
          7654 MARTIN         SALESMAN
          7934 MILLER         CLERK
            40 OPERATIONS     BOSTON
            20 RESEARCH       DALLAS
            30 SALES          CHICAGO
          7788 SCOTT          ANALYST
          7369 SMITH          CLERK
          7844 TURNER         SALESMAN
          7521 WARD           SALESMAN
    18 rows selected.
    SQL> SELECT
      2  *
      3  FROM
      4  (SELECT
      5      empno     no
      6  ,   ename     name
      7  ,   job       description
      8  FROM          emp
      9  UNION
    10  SELECT
    11      deptno    no
    12  ,   dname     name
    13  ,   loc       description
    14  FROM          dept
    15  )
    16  ORDER BY
    17  DECODE(3
    18  ,      1,TO_CHAR(no)
    19  ,      2,name
    20  ,      3,description)
    21  /
            NO NAME           DESCRIPTION
          7788 SCOTT          ANALYST
          7902 FORD           ANALYST
            40 OPERATIONS     BOSTON
            30 SALES          CHICAGO
          7369 SMITH          CLERK
          7934 MILLER         CLERK
          7900 JAMES          CLERK
          7876 ADAMS          CLERK
            20 RESEARCH       DALLAS
          7566 JONES          MANAGER
          7698 BLAKE          MANAGER
          7782 CLARK          MANAGER
            10 ACCOUNTING     NEW YORK
          7839 KING           PRESIDENT
          7499 ALLEN          SALESMAN
          7844 TURNER         SALESMAN
          7654 MARTIN         SALESMAN
          7521 WARD           SALESMAN
    18 rows selected.I needed to put the TO_CHAR in the DECODE so all of the columns
    by which I could potentially order are VARCHAR2's, else I was
    getting
    SQL> /
    DECODE(1
    ERROR at line 17:
    ORA-01785: ORDER BY item must be the number of a SELECT-list expressionHTH
    T.

  • Custom Sort

    I have a wierd problem that has been causing me quite a few headaches and i think if there is a way to do a custom order by, it might get rid of this annoynce.
    I have an existing script that is five scripts unioned together. When i run the script on it's own, i get the desired result in the desired order, but when i run it through a data window in powerbuilder it is bringing back a different order and i can't figure out why. It's the exact same script.
    Anyways, i'm wondering if i can create a custom sort order by, so that i can specify what the first and second fields are and then let the rest fall as they are supposed to:
    Here is a look at my Current results:
    Equip_# - Read_Desc - Date ----------- Reading
    12345 - Consumption - 10/05/2008 - 1101.88
    12345 - Consumption - 10/05/2008 - 0000.00
    12345 - Summer Peak- 10/05/2008 - 0000.00
    12345 - Winter Peak - 10/05/2008 - 0000.00
    12345 - Radio Device - 10/05/2008 - 0000.00
    12345 - Demand ------- 10/05/2008 - 0000.00
    I need the first two fields of the table to sorted as follows and then the rest can just fall into place:
    Equip_# - Read_Desc - Date ----------- Reading
    12345 - Consumption - 10/05/2008 - 1101.88
    12345 - Demand ------- 10/05/2008 - 0000.00
    ...Doesn't matter how it sorts as long as they two come first
    Hope someone can help, cause this really just doens't make a lot of sense and i'm now just trying to get this done and get it behind me.
    thanks in advance

    Justin Cave wrote:
    Can you post the actual SQL statement you are using (or at least the skeleton structure)? You want the ORDER BY to apply to the entire result, not to one individual subquery that is getting unioned with a number of other subqueries.
    Is PowerBuilder trying to order the data before displaying it? Obviously, the GUI is free to disregard the order Oracle happens to return results if you've instructed it to sort the data differently.
    JustinI don't believe powerbuilder is trying to order the data before displaying it. If it is, it's in a place not easily seen.
    Here is my code:
    SELECT equipment.equipment_number,
    read_class.read_class_desc,
    meter_read.effective_date,
    meter_read.meter_read,' ',' ',' ',''seal_broken,'1'
    FROM equipment,
    meter_read,
    meter_read_class,
                   equipment_type,
                   equipment_class,
                   read_class
    WHERE meter_read.meter_read_class_id = meter_read_class.meter_read_class_id and
    meter_read.equipment_id = equipment.equipment_id and
                   equipment.equipment_type_id = equipment_type.equipment_type_id and
                   equipment_type.equipment_class= equipment_class. equipment_class and
                   meter_read_class.read_class =read_class.read_class and     
    equipment_class.equipment_category = 'EMETER' AND
    equipment.current_service_id = 32399 and
                   meter_read_class.read_class = 'ECONSUME' and
                   meter_read.effective_date=
                   (select max(mr.effective_date)
                        from equipment e,
    meter_read mr,
    meter_read_class mrc,
                   equipment_type et,
                   equipment_class ec
    WHERE mr.meter_read_class_id = mrc.meter_read_class_id and
    mr.equipment_id = e.equipment_id and
                   e.equipment_type_id = et.equipment_type_id and
                   et.equipment_class= ec. equipment_class and
    ec.equipment_category = 'EMETER' AND
    e.current_service_id = 32399 and
                   mrc.read_class = 'ECONSUME')
    union SELECT equipment.equipment_number,
    read_class.read_class_desc,
    meter_read.effective_date,
    meter_read.meter_read,' ',' ',' ',''seal_broken,'2'
    FROM equipment,
    meter_read,
    meter_read_class,
                   equipment_type,
                   equipment_class,
                   read_class
    WHERE meter_read.meter_read_class_id = meter_read_class.meter_read_class_id and
    meter_read.equipment_id = equipment.equipment_id and
                   equipment.equipment_type_id = equipment_type.equipment_type_id and
                   equipment_type.equipment_class= equipment_class.equipment_class and
    equipment_class.equipment_category = 'EMETER' AND
    equipment.current_service_id = 32399 and
                   meter_read_class.read_class = 'DEMAND' and
    --               read_class.read_class_desc not in ('Electrical Consumption', 'Electrical ERT Device') and
                   meter_read.effective_date=
                   (select max(mr.effective_date)
                        from equipment e,
    meter_read mr,
    meter_read_class mrc,
                   equipment_type et,
                   equipment_class ec
    WHERE mr.meter_read_class_id = mrc.meter_read_class_id and
    mr.equipment_id = e.equipment_id and
                   e.equipment_type_id = et.equipment_type_id and
                   et.equipment_class= ec. equipment_class and
    ec.equipment_category = 'EMETER' AND
    e.current_service_id = 32399 and
                   mrc.read_class = 'DEMAND' )
    order by meter_read, decode(read_class_desc,'Electrical Consumption',1,'Electrical Demand',2), read_class_desc
    I've only recently tried entering in the decode in the order by, as someone else suggested and it works fine for individual queries, but when i use it against the whole query (including the Union's), i get the following error:
    ORA-01785: ORDER BY item must be the number of a SELECT-list expression

  • Order By Parameter UNION Issue!!

    Hi All,
    i have a query in which i have to order by a parameter.
    but my problem is order by works only for non-union query if i add union to the query it gives error.
    please see following..
    SQL> SELECT   H.BOL_NUMBER BOL_NUMBER,
      2           H.PICKUP_NUMBER,
      3     to_char(H.PICKUP_DATE,'DD-MON-YYYY') PICKUP_DATE,
      4           RAC.CUSTOMER_NAME CUSTOMER_NAME,
      5           L.SALES_ORDER,
      6    to_char(MAX(PH.DATE_CONFIRMED),'DD-MON-YYYY HH24:MI:SS') DATE_CONFIRMED
      7  FROM     APPS.SO_PICKING_HEADERS_ALL PH,
      8    SMCOE.SMCOE_BOL_HEADERS H,
      9           SMCOE.SMCOE_BOL_LINES L,
    10           SO_HEADERS_ALL SHA,
    11           SO_ORDER_TYPES_ALL SOTA,
    12           APPS.RA_CUSTOMERS RAC,
    13    APPS.SO_PICKING_LINES_ALL PL,
    14    APPS.SO_PICKING_LINE_DETAILS PLD
    15  WHERE    H.BOL_HEADER_ID = L.BOL_HEADER_ID
    16           AND L.SO_HEADER_ID = SHA.HEADER_ID
    17           AND SHA.ORDER_TYPE_ID = SOTA.ORDER_TYPE_ID
    18           AND L.CUSTOMER_ID = RAC.CUSTOMER_ID
    19           AND H.PICKUP_DATE >= TO_DATE('3-NOV-2006 00:00:00','DD-MON-YYYY HH24:MI:SS')
    20           AND H.PICKUP_DATE < TO_DATE('4-NOV-2006 00:00:00','DD-MON-YYYY HH24:MI:SS')
    21           AND DECODE(NULL,NULL,'X',
    22                                 RAC.CUSTOMER_NAME) = DECODE(NULL,NULL,'X',
    23                                                                        NULL)
    24           AND DECODE(634307,NULL,1,
    25                            H.BOL_NUMBER) = DECODE(634307,NULL,1,
    26                                                         634307)
    27    AND decode('Only SO','Only SO','Z',RAC.CUSTOMER_NAME)=decode('Only SO','Only SO','Z','XXX')
    28    AND PH.DATE_CONFIRMED IS NOT NULL
    29    AND PH.PICKING_HEADER_ID=PL.PICKING_HEADER_ID
    30    AND PL.PICKING_LINE_ID=PLD.PICKING_LINE_ID
    31    AND PLD.PICKING_LINE_DETAIL_ID=L.PICKING_LINE_DETAIL_ID
    32    AND NOT EXISTS (SELECT 1
    33         FROM SO_PICKING_LINE_DETAILS SPLD,WSH_DELIVERIES WD
    34         WHERE SPLD.DELIVERY_ID=WD.DELIVERY_ID
    35         AND SPLD.PICKING_LINE_DETAIL_ID =L.PICKING_LINE_DETAIL_ID
    36         AND WD.STATUS_CODE='OP'
    37        )
    38   GROUP BY H.BOL_NUMBER ,
    39           H.PICKUP_NUMBER,
    40           H.PICKUP_DATE ,
    41           RAC.CUSTOMER_NAME ,
    42           L.SALES_ORDER,
    43           L.LINE_NUMBER,
    44           L.GROSS_WEIGHT,
    45           L.NET_WEIGHT,
    46           L.TARE_WEIGHT
    47  ORDER BY decode('CUSTOMER','DATE',PICKUP_DATE,'CUSTOMER',CUSTOMER_NAME,'PN',PICKUP_NUMBER,SALES_ORDER);
    BOL_NUMBER PICKUP_N PICKUP_DATE CUSTOMER_NAME                                                      
    SALES_ORDER                                                                                        
    DATE_CONFIRMED                                                                                     
        634307 SC22862  03-NOV-2006 A M CASTLE & CO                                                    
    300014511                                                                                          
    03-NOV-2006 19:05:01                                                                               
        634307 SC22862  03-NOV-2006 A M CASTLE & CO                                                    
    300014542                                                                                          
    03-NOV-2006 19:04:55                                                                               
        634307 SC22862  03-NOV-2006 A M CASTLE & CO                                                    
    300014599                                                                                          
    03-NOV-2006 19:04:56                                                                               
        634307 SC22862  03-NOV-2006 A M CASTLE & CO                                                    
    300014637                                                                                          
    03-NOV-2006 19:05:04                                                                               
        634307 SC22862  03-NOV-2006 CORROSION MATERIALS INC                                            
    14 rows selected.
    SQL> ed
    Wrote file afiedt.buf
    line 72 truncated.
      1  SELECT   H.BOL_NUMBER BOL_NUMBER,
      2           H.PICKUP_NUMBER,
      3             to_char(H.PICKUP_DATE,'DD-MON-YYYY') PICKUP_DATE,
      4           RAC.CUSTOMER_NAME CUSTOMER_NAME,
      5           L.SALES_ORDER,
      6              ' ' DATE_CONFIRMED
      7  FROM     SMCOE.SMCOE_BOL_HEADERS H,
      8           SMCOE.SMCOE_BOL_LINES L,
      9           APPS.RA_CUSTOMERS RAC
    10  WHERE    H.BOL_HEADER_ID = L.BOL_HEADER_ID
    11           AND L.CUSTOMER_ID = RAC.CUSTOMER_ID
    12           AND H.PICKUP_DATE >= TO_DATE('3-NOV-2006 00:00:00','DD-MON-YYYY HH24:MI:SS')
    13           AND H.PICKUP_DATE < TO_DATE('4-NOV-2006 00:00:00','DD-MON-YYYY HH24:MI:SS')
    14           AND DECODE(NULL,NULL,'X',
    15                                 RAC.CUSTOMER_NAME) = DECODE(NULL,NULL,'X',
    16                                                                        NULL)
    17           AND DECODE(NULL,NULL,1,
    18                            H.BOL_NUMBER) = DECODE(NULL,NULL,1,
    19                                                        NULL)
    20           AND SALES_ORDER NOT LIKE '4%'
    21           AND SALES_ORDER NOT LIKE 'PO%'
    22           AND SALES_ORDER <> '1'
    23           AND L.CUSTOMER_NAME NOT LIKE 'REC%'
    24        AND decode('Only SO','Product','Y',RAC.CUSTOMER_NAME)=decode('Only SO','Product','Y','XXX')
    25  UNION
    26  SELECT   H.BOL_NUMBER BOL_NUMBER,
    27           H.PICKUP_NUMBER,
    28             to_char(H.PICKUP_DATE,'DD-MON-YYYY') PICKUP_DATE,
    29           RAC.CUSTOMER_NAME CUSTOMER_NAME,
    30           L.SALES_ORDER,
    31        to_char(MAX(PH.DATE_CONFIRMED),'DD-MON-YYYY HH24:MI:SS') DATE_CONFIRMED
    32  FROM     APPS.SO_PICKING_HEADERS_ALL PH,
    33        SMCOE.SMCOE_BOL_HEADERS H,
    34           SMCOE.SMCOE_BOL_LINES L,
    35           SO_HEADERS_ALL SHA,
    36           SO_ORDER_TYPES_ALL SOTA,
    37           APPS.RA_CUSTOMERS RAC,
    38        APPS.SO_PICKING_LINES_ALL PL,
    39        APPS.SO_PICKING_LINE_DETAILS PLD
    40  WHERE    H.BOL_HEADER_ID = L.BOL_HEADER_ID
    41           AND L.SO_HEADER_ID = SHA.HEADER_ID
    42           AND SHA.ORDER_TYPE_ID = SOTA.ORDER_TYPE_ID
    43           AND L.CUSTOMER_ID = RAC.CUSTOMER_ID
    44           AND H.PICKUP_DATE >= TO_DATE('3-NOV-2006 00:00:00','DD-MON-YYYY HH24:MI:SS')
    45           AND H.PICKUP_DATE < TO_DATE('4-NOV-2006 00:00:00','DD-MON-YYYY HH24:MI:SS')
    46           AND DECODE(NULL,NULL,'X',
    47                                 RAC.CUSTOMER_NAME) = DECODE(NULL,NULL,'X',
    48                                                                        NULL)
    49           AND DECODE(634307,NULL,1,
    50                            H.BOL_NUMBER) = DECODE(634307,NULL,1,
    51                                                         634307)
    52        AND decode('Only SO','Only SO','Z',RAC.CUSTOMER_NAME)=decode('Only SO','Only SO','Z','XXX')
    53        AND PH.DATE_CONFIRMED IS NOT NULL
    54        AND PH.PICKING_HEADER_ID=PL.PICKING_HEADER_ID
    55        AND PL.PICKING_LINE_ID=PLD.PICKING_LINE_ID
    56        AND PLD.PICKING_LINE_DETAIL_ID=L.PICKING_LINE_DETAIL_ID
    57        AND NOT EXISTS (SELECT 1
    58                 FROM SO_PICKING_LINE_DETAILS SPLD,WSH_DELIVERIES WD
    59                 WHERE SPLD.DELIVERY_ID=WD.DELIVERY_ID
    60                 AND SPLD.PICKING_LINE_DETAIL_ID =L.PICKING_LINE_DETAIL_ID     
    61                 AND WD.STATUS_CODE='OP'
    62                )
    63       GROUP BY H.BOL_NUMBER ,
    64           H.PICKUP_NUMBER,
    65           H.PICKUP_DATE ,
    66           RAC.CUSTOMER_NAME ,
    67           L.SALES_ORDER,
    68           L.LINE_NUMBER,
    69           L.GROSS_WEIGHT,
    70           L.NET_WEIGHT,
    71           L.TARE_WEIGHT           
    72* ORDER BY decode('CUSTOMER','DATE',PICKUP_DATE,'CUSTOMER',CUSTOMER_NAME,'PN',PICKUP_NUMBER,SALES_ORDER)
    SQL> /
    OERROR at line 72:
    ORA-01785: ORDER BY item must be the number of a SELECT-list expression
    SQL> spool off;

    Maybe you could try this:
    select ...
    from
       select ...
       from ..
       UNION
       select ...
       from ...
    order by ...                                                                                                                                                                                                                                                                                   

  • Error on union with order by clause

    here is my sql.
    i want sort this sql by start_timestamp.
    select name,
    enumber,
    to_char(new_time(start_TimeStamp,'gmt','edt'),'MM/DD/YY HH24:MI:SS'),
    ssn
    from emp
    where
    ename = 'joh%'
    and
    start_TimeStamp9 > TO_DATE('2007-01-15 00:00:01','YYYY-MM-DD HH24:MI:SS')
    and end_TimeStamp11 < TO_DATE('2007-01-18 22:59:59','YYYY-MM-DD HH24:MI:SS')
    union
    select name,
    enumber,
    to_char(new_time(start_TimeStamp,'gmt','edt'),'MM/DD/YY HH24:MI:SS'),
    ssn
    from emp
    where
    ename = 'joh%'
    and
    start_TimeStamp9 > TO_DATE('2007-01-15 00:00:01','YYYY-MM-DD HH24:MI:SS')
    and end_TimeStamp11 < TO_DATE('2007-01-18 22:59:59','YYYY-MM-DD HH24:MI:SS')
    union
    select name,
    enumber,
    to_char(new_time(start_TimeStamp,'gmt','edt'),'MM/DD/YY HH24:MI:SS'),
    ssn
    from emp
    where
    ename = 'kris%'
    and
    start_TimeStamp9 > TO_DATE('2007-01-15 00:00:01','YYYY-MM-DD HH24:MI:SS')
    and end_TimeStamp11 < TO_DATE('2007-01-18 22:59:59','YYYY-MM-DD HH24:MI:SS')
    union
    select name,
    enumber,
    to_char(new_time(start_TimeStamp,'gmt','edt'),'MM/DD/YY HH24:MI:SS'),
    ssn
    from emp
    where
    ename = 'wal%'
    and
    start_TimeStamp9 > TO_DATE('2007-01-15 00:00:01','YYYY-MM-DD HH24:MI:SS')
    and end_TimeStamp11 < TO_DATE('2007-01-18 22:59:59','YYYY-MM-DD HH24:MI:SS')
    order by 3;
    i got this error.
    ORA-01785: ORDER BY item must be the number of a SELECT-list expression
    start_timestamp is the 3rd column of the sql.
    why i am getting this error.
    i tried this also.
    order by to_char(new_time(start_TimeStamp,'gmt','edt'),'MM/DD/YY HH24:MI:SS');
    still i got error.

    Also, be ready for a few surprises when you try to sort dates as strings (by using TO_CHAR):
    test@XE> alter session set nls_date_format = 'mm/dd/yyyy hh24:mi:ss';
    Session altered.
    test@XE>
    test@XE> --
    test@XE> -- incorrect order
    test@XE> --
    test@XE> select num, dt
      2  from (
      3    select 1 as num, to_char(new_time(to_date('10/12/01 07:20:33',
      4                                              'mm/dd/yy hh24:mi:ss'),'gmt','edt')) as dt from dual
      5    union
      6    select 2, to_char(new_time(to_date('07/22/07 08:10:23',
      7                                       'mm/dd/yy hh24:mi:ss'),'gmt','edt')) as dt from dual
      8    union
      9    select 3, to_char(new_time(to_date('11/29/99 15:34:49',
    10                                       'mm/dd/yy hh24:mi:ss'),'gmt','edt')) from dual
    11  )
    12  order by dt;
           NUM DT
             2 07/22/2007 04:10:23
             1 10/12/2001 03:20:33
             3 11/29/2099 11:34:49
    test@XE>
    test@XE> --
    test@XE> -- correct order
    test@XE> --
    test@XE> select num, dt
      2  from (
      3    select 1 as num, new_time(to_date('10/12/01 07:20:33',
      4                                      'mm/dd/yy hh24:mi:ss'),'gmt','edt') as dt from dual
      5    union
      6    select 2, new_time(to_date('07/22/07 08:10:23',
      7                               'mm/dd/yy hh24:mi:ss'),'gmt','edt') from dual
      8    union
      9    select 3, new_time(to_date('11/29/99 15:34:49',
    10                               'mm/dd/yy hh24:mi:ss'),'gmt','edt') from dual
    11  )
    12  order by dt;
           NUM DT
             1 10/12/2001 03:20:33
             2 07/22/2007 04:10:23
             3 11/29/2099 11:34:49
    test@XE>
    test@XE>I'd say it's best to sort dates as dates always, and never as strings.
    isotope

  • Influence of condition type "never" on the rest of the page

    Hi all,
    I have a question concerning the 'never'-Condition Type of a process.
    I thought when setting this attribute, this process doesn't influence my page. But it isn't that way (as described below). Is this a bug, or when does it influence the page?
    Here's the whole process of creating the page and the - for me - unexpected behavior:
    I created page 1000 with a tabular form with table DEMO_CUSTOMERS, using all available columns and allowed just update as operation.
    The Primary key_type is sequence!
    For getting a second tabular form on my page I created an additional page (page 12), but here I decided just to select 4 columns (PRODUCT_ID, PRODUCT_NAME, PRODUCT_DESCRIPTION and PRODUCT_AVAIL) from table DEMO_PRODUCT_INFO - the rest as done before.
    Now I copied the just created region from page 12 to page 1000 (nothing else). On my original page 1000 I set the ApplyMRU-Process on Condition Type 'never'.
    Running this page I get the error:
    failed to parse SQL query:
    ORA-01785: ORDER BY item must be the number of a SELECT-list expression
    If I delete the ApplyMRU-Process (which is set on Condition Type 'never') the two tabular forms are displayed one below the other. I've done so on page 2000 in my test-workspace (after copying page 1000).
    If you'd like to have a look, I recreated the whole thing as described above on http://apex.oracle.com/pls/apex (WORKSPACE: ub_test USER: test PWD: test) Application 47200.
    Besides: I know that there shouldn't be two tabular forms on one page and that I can't expect the automatically generated processes to work, but ...
    Regards Uli

    Jari,
    i still think this is a bug, obviously the condition of the Process isn't used.
    brgds,
    Peter
    get Syntax Highlighting for the Application Builder: http://apex.oracle.com/pls/apex/f?p=APEX_DEVELOPER_ADDON:ABOUT:0:::::
    Blog: http://www.oracle-and-apex.com
    ApexLib: http://apexlib.oracleapex.info
    BuilderPlugin: http://builderplugin.oracleapex.info
    Work: http://www.click-click.at and http://www.wirsindapex.at

  • Union Order by causing issue in SetWhereClause

    Hi,
    I am extending a CO and adding new query to the VO query.
    I added where clause to the VO query. Also added Union. The Union is the same VO query with another where condition. VO query default had a Order by clause.
    Now the total query looks like this.
    select * from
    (select a, b
    from x, y) QRSLT
    where ...
    UNION
    select * from
    (select a, b
    from x, y) QRSLT1
    where ...
    order by upper(a)
    "Order by" is getting added since it is there in the original VO query.
    Now I am getting the error as "java.sql.SQLException: ORA-01785: ORDER BY item must be the number of a SELECT-list expression"
    The above query without the order by is working good. What is the best way to fix this?
    Thanks,
    HC

    The problem is that you're using a UNION. The ORDER BY clause has to refer to a column in the query as a whole rather than just the second part of the UNION. In that situation, Oracle expects either a column position or the name of one of the selected columns.
    Thanks
    --Anil
    http://oracleanil.blogspot.com/

  • Order by absolute value

    Hi,
    I am trying to write a SQL query which returns the % difference between 2 columns in a table, in sorted descending order, based on the absolute value.
    The statement goes like this:
    SELECT ((col1 - col2)/col2)*100
    FROM table1
    WHERE col2 > 0
    UNION
    SELECT 100
    FROM table1
    WHERE col2 = 0
    ORDER BY 1 DESC
    What I really want is the ORDER BY clause to be something like this:
    ORDER BY ABS(1) DESC
    However, that results in the following error:
    ORA-01785: ORDER BY item must be the number of a SELECT-list expression
    Does anyone have a nice, efficient way for fixing the SQL query?
    Thanks!

    Add another column to your select, and order by that:
    SELECT ABS(((col1 - col2)/col2)*100),
    ((col1 - col2)/col2)*100
    FROM table1
    WHERE col2 > 0
    UNION
    SELECT 100, 100
    FROM table1
    WHERE col2 = 0
    ORDER BY 1 DESC
    null

Maybe you are looking for

  • Error executing a stored procedure from SSIS using the MERGE statement between databases

    Good morning, I'm trying to execute from SSIS a stored procedure that compares the content of two tables on different databases in the same server and updates one of them. To perform this action, I've created a stored procedure in the destination dat

  • Facing problems when trying to access sites

    Hi I am able to open the page when I tried in web browser but when I am trying to access the site using the below code it is failed import java.net.*; import java.io.*; public class URLParser { public static void main(String[] args) throws Exception

  • My "Choose Application" list of apps is replaced with iTunes list

    I have a problem with something being corrupted in the "Choose Applications" dialog.  Instead of a list of apps, there is a dropdown menu called "iTunes" and there is a list of songs where the application list should be.  Has anyone seen this error,

  • Building an array to a specified format..

    I'm trying to build an array that follows a specified output format. The user inputs columns & rows e.g. C x R...amd the the output should read C1 C2 C3 C4..... R1 V V V V.... R2 V V V V ... Where each V is not a constant, but a user inputed value. S

  • BADI & USER EXITS

    Hi experts , i want add two feild for street in TCODE FPP1(Business Partner creation) in SAP-ISU. can anyone tell me step by step procedure for it. Pls help ASAP Thanx & Rgds Pradeep Sharma