Datawindow查询不遵守ORDER BY子句

时间:2015-05-18 08:27:12

标签: sql sql-order-by sybase powerbuilder datawindow

我正在开发表格,以便为采购目的订购订单。

我将Datawindow作为主窗体中的报表对象。此数据窗口(子报告)具有选择以检索该订单表单的订单行。除ORDER BY子句外,一切正常。我需要按订单行号(orderline.orderlineno)按降序对订单行进行排序。

我已经通过Sybase Central尝试了SQL语句> Interactive SQL,它以所需顺序显示记录,但在我的应用程序中使用表单时,它显示按orderline.forcompid排序的记录。这很奇怪

我的SQL语句或Power Builder中的其他内容是否存在任何错误?

数据窗口(子报告)SQL

( SELECT orderline.orderlineno,   
         orderline.name,   
         orderline.unitid,   
         orderline.quantity,   
         orderline.makerref,   
         orderline.price, 
orderline.discount , 
         orderline.currencycode,
         orderline.linecontent,
         orderline.workorderid,
         componenttype.compname,   
         componenttype.comptype,   
         componentunit.compno,   
         componentunit.serialno,   
         workorder.compjobid,
         workorder.title, 
            workorder.woorigin
,orderline.notes

    FROM orderline,   
         componenttype,   
         componentunit,   
         workorder  
   WHERE ( componentunit.comptypeid = componenttype.comptypeid ) and  
         ( componentunit.compid = orderline.forcompid ) and  
         ( orderline.workorderid = workorder.workorderid ) and 
         ( orderline.orderid = :ll_OrderID ) AND  
         ( orderline.status = 1 ) AND
         ( orderline.includeonform <> 0 ) 
union
  SELECT orderline.orderlineno,   
         orderline.name,   
         orderline.unitid,   
         orderline.quantity,   
         orderline.makerref,   
         orderline.price,   
orderline.discount ,
         orderline.currencycode,
         orderline.linecontent,
         orderline.workorderid,
         componenttype.compname,   
         componenttype.comptype,   
         componentunit.compno,   
         componentunit.serialno,    
         0,
         NULL,
         0 
,orderline.notes

    FROM orderline,   
         componenttype,   
         componentunit   
   WHERE orderline.workorderid IS NULL AND
         ( componentunit.comptypeid = componenttype.comptypeid ) and  
         ( componentunit.compid = orderline.forcompid ) and  
         ( orderline.orderid = :ll_OrderID ) AND  
         ( orderline.status = 1 ) AND
         ( orderline.includeonform <> 0 ) 

union
  SELECT orderline.orderlineno,   
         orderline.name,   
         orderline.unitid,   
         orderline.quantity,   
         orderline.makerref,   
         orderline.price,   
orderline.discount ,
         orderline.currencycode,
         orderline.linecontent,   
         orderline.workorderid,
         NULL,   
         NULL, 
         NULL, 
         NULL,  
         workorder.compjobid,
         workorder.title,
            workorder.woorigin 
,orderline.notes
    FROM orderline, 
         workorder  

    WHERE orderline.forcompid IS NULL AND 
         ( orderline.workorderid = workorder.workorderid ) and 
         ( orderline.orderid = :ll_OrderID ) AND  
         ( orderline.status = 1 ) AND 
         ( orderline.includeonform <> 0 ) 

union
  SELECT orderline.orderlineno,   
         orderline.name,   
         orderline.unitid,   
         orderline.quantity,   
         orderline.makerref,   
         orderline.price, 
orderline.discount ,  
         orderline.currencycode,
         orderline.linecontent, 
         orderline.workorderid,  
         NULL,   
         NULL, 
         NULL, 
         NULL, 
         0, 
         NULL, 
         0   
,orderline.notes
    FROM orderline

    WHERE orderline.forcompid IS NULL AND 
         ( orderline.workorderid IS NULL ) and 
         ( orderline.orderid = :ll_OrderID ) AND  
         ( orderline.status = 1 ) AND 
         ( orderline.includeonform <> 0 ) 
) 
ORDER BY 1 ASC

**编辑:修改SQL **

SELECT orderline.orderlineno,
 orderline.name,   
         orderline.unitid,   
         orderline.quantity,   
         orderline.makerref,   
         orderline.price, 
         orderline.discount, 
         orderline.currencycode,
         orderline.linecontent,
         orderline.workorderid,
         componenttype.compname,   
         componenttype.comptype,   
         componentunit.compno,   
         componentunit.serialno,   
         workorder.compjobid,
         workorder.title, 
     workorder.woorigin,
         orderline.notes

FROM

( SELECT 
         A.orderlineno AS LineNo,   
         A.name as LineName ,   
         A.unitid AS Unit,   
         A.quantity As Qty,   
         A.makerref AS Maker,   
         A.price AS Price, 
         A.discount As Dsc, 
         A.currencycode As Curr,
         A.linecontent As content,
         A.workorderid AS WOID,
         B.compname AS CName,   
         B.comptype As CType,   
         C.compno AS CNo,   
         C.serialno As Serial,   
         D.compjobid AS CJob,
         D.title As Tit, 
         D.woorigin AS WOor,
         A.notes As Nots

    FROM orderline A,   
         componenttype B,   
         componentunit C,   
         workorder D 
   WHERE ( C.comptypeid = B.comptypeid ) and  
         ( C.compid = A.forcompid ) and  
         ( A.workorderid = D.workorderid ) and 
         ( A.orderid = 40003774 ) AND  
         ( A.status = 1 ) AND
         ( A.includeonform <> 0 ) 
union
  SELECT 
         A1.orderlineno AS LineNo,   
         A1.name as LineName ,   
         A1.unitid AS Unit,   
         A1.quantity As Qty,   
         A1.makerref AS Maker,   
         A1.price AS Price, 
         A1.discount As Dsc, 
         A1.currencycode As Curr,
         A1.linecontent As content,
         A1.workorderid AS WOID,
         B1.compname AS CName,   
         B1.comptype As CType,   
         C1.compno AS CNo,   
         C1.serialno As Serial,   
         0,
         NULL, 
         0,
        A1.notes As Nots

    FROM orderline A1,   
         componenttype B1,   
         componentunit C1  
   WHERE A1.workorderid IS NULL AND
         ( C1.comptypeid = B1.comptypeid ) and  
         ( C1.compid = A1.forcompid ) and  
         ( A1.orderid = 40003774 ) AND  
         ( A1.status = 1 ) AND
         ( A1.includeonform <> 0 ) 

union
  SELECT 

         A2.orderlineno AS LineNo,   
         A2.name as LineName ,   
         A2.unitid AS Unit,   
         A2.quantity As Qty,   
         A2.makerref AS Maker,   
         A2.price AS Price, 
         A2.discount As Dsc, 
         A2.currencycode As Curr,
         A2.linecontent As content,
         A2.workorderid AS WOID,
         NULL,  
         NULL,  
         NULL,   
         NULL,   
         B2.compjobid AS CJob,
         B2.title As Tit, 
         B2.woorigin AS WOor,
         A2.notes As Nots


    FROM orderline A2, 
         workorder B2  

    WHERE A2.forcompid IS NULL AND 
         ( A2.workorderid = B2.workorderid ) and 
         ( A2.orderid = 40003774 ) AND  
         ( A2.status = 1 ) AND 
         ( A2.includeonform <> 0 ) 

union
  SELECT 

         A3.orderlineno AS LineNo,   
         A3.name as LineName ,   
         A3.unitid AS Unit,   
         A3.quantity As Qty,   
         A3.makerref AS Maker,   
         A3.price AS Price, 
         A3.discount As Dsc, 
         A3.currencycode As Curr,
         A3.linecontent As content,
         A3.workorderid AS WOID,
         NULL,   
         NULL,   
         NULL,   
         NULL,  
         0,
         NULL, 
         0,
         A3.notes As Nots

    FROM orderline A3

    WHERE A3.forcompid IS NULL AND 
         ( A3.workorderid IS NULL ) and 
         ( A3.orderid = 40003774 ) AND  
         ( A3.status = 1 ) AND 
         ( A3.includeonform <> 0 ) 

)
         orderline,   
         componenttype,   
         componentunit,   
         workorder

ORDER BY 1 ASC

2 个答案:

答案 0 :(得分:1)

按照步骤..

  1. 首先为select语句中的列提供别名。因为它的联合别名应该在所有查询中匹配。

  2. 使用别名命名外部选择,并使用所需列的别名来执行orderby。

  3. **请记住,order by会影响执行时间..

    看下面的示例

    SELECT  LineNo,
            LineName,   
            Unit,   
            QTY,   
             .......
    
    FROM
    
    ( SELECT 
             A.orderlineno AS LineNo,   
             A.name as LineName ,   
             A.unitid AS Unit,   
             A.quantity As Qty,   
             ...........
    
        FROM orderline A,   
             componenttype B,   
             componentunit C,   
             workorder D 
       WHERE ( C.comptypeid = B.comptypeid ) and  
             ( C.compid = A.forcompid ) and  
             ( A.workorderid = D.workorderid ) and 
             ( A.orderid = 40003774 ) AND  
             ( A.status = 1 ) AND
             ( A.includeonform <> 0 ) 
    union
      SELECT 
             A1.orderlineno AS LineNo,   
             A1.name as LineName ,   
             A1.unitid AS Unit,   
             A1.quantity As Qty,   
            .........
    
        FROM orderline A1,   
             componenttype B1,   
             componentunit C1  
       WHERE A1.workorderid IS NULL AND
             ( C1.comptypeid = B1.comptypeid ) and  
             ( C1.compid = A1.forcompid ) and  
             ( A1.orderid = 40003774 ) AND  
             ( A1.status = 1 ) AND
             ( A1.includeonform <> 0 ) 
    
    union
      ........
    ) AS t1
    
    ORDER BY LineNo ASC
    

答案 1 :(得分:0)

你的SQL很好。不妨在工会上取消订单。 在数据窗口中设置排序顺序。拖动列想要排序到右侧并取消选中“升序”框。这总是覆盖SQL排序。