码迷,mamicode.com
首页 > 其他好文 > 详细

distinct与order by

时间:2014-11-17 15:46:12      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   sp   div   on   log   cti   

不知为啥,当我得查询中出现distinct时,order by 中必须包含要查询的列,否则报错。

SELECT DISTINCT
    a.DetailId,
    a.OrderId,
    a.ProductId,
    pi2.BarCode,
    pi1.ProductName,
    pi1.productCode,
    pi1.spec,
    a.UnitId,
    ui.UnitName,
    SNUM(a.DetailId) as Quantity,
    a.Discount,
    a.UnitPrice,
    a.TaxRate,
    a.ProductMoney,
    a.TaxMoney,
    a.PayableMoney,
    a.IsGiveAway,
    a.DetailId AS ColGuid,
    a.ParentGuid,
    a.DetailId AS OrderDetailId,
    a.OrderId AS RelevanceOrder,
    a.Memo,
    pi2.SalePrice AS InitUnitPrice,
    a.SEQ,      ---必须包含这个,否则报错
    a.Quantity - SNUM(a.DetailId) AS OutQuantity
FROM
    SaleOrderDetail a
LEFT JOIN ProductInfo pi1 ON a.ProductId = pi1.ProductId
LEFT JOIN ProductPrice pi2 ON a.ProductId = pi2.ProductId
AND pi2.unitid = a.unitid
LEFT JOIN UnitInfo ui ON a.UnitId = ui.UnitId
WHERE
    a.DetailId IN (SELECT COLUMN_VALUE FROM TABLE(SPLITSTR(IDS, ,)))
ORDER BY
    a.SEQ ASC

 

distinct与order by

标签:style   blog   color   ar   sp   div   on   log   cti   

原文地址:http://www.cnblogs.com/wpcnblog/p/4103616.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!