SQL Server 2008 group by和order by获取最多付费到最低付费的排序列表

时间:2015-05-07 09:22:43

标签: sql-server

以下是我的查询,我得到客户支付最多,付费最少的数据。

但问题是假设客户A支付最多,并且如果有很多客户,它会与客户A重复3到4条记录。

我需要客户付款最少到最低付款,之后客户支付最多的付款。

需要包括客户无法做到的分组。

select  
    a.custcode, a.dono, b.dolnno, b.item, b.itemdesc,
    isnull(a.ordertype,'') ordertype,
    a.custname, a.CollectionDate,
    CAST((SUBSTRING(a.CollectionDate, 7, 4) + SUBSTRING(a.CollectionDate, 4, 2) + SUBSTRING(a.CollectionDate, 1, 2)) AS date) AS trandate,
    isnull(a.jobnum,'') jobnum, 
    isnull(a.Remark1,'') as remarks,
    b.lnstat, isnull(b.qtyor,0) qtyor, 
    isnull(b.qtypick, 0) qtypick, isnull(b.qtyis, 0) qty, 
    isnull(b.unitprice, 0) unitprice, b.crat, 
    ISNULL(qtyor * b.unitprice, 0) ordPrice, 
    ISNULL(qtyis * b.unitprice, 0) issPrice,
    isnull(deliverydate, '') deliverydate,
    isnull(timeslots, '') deliverytime,
    isnull(c.prd_brand_id, '') as prd_brand_id,
    isnull(c.prd_cls_id, '') as prd_cls_id,
    isnull(c.itemtype, '') as itemtype,
    isnull(a.status_id, '') as status_id,
    isnull(a.OUTBOUND_GST, 0) as Tax
from 
    [test_dohdr] a,[test_dodet] b,[test_ITEMMST]c 
where 
    a.dono = b.dono 
    and b.ITEM = c.item   
    and ISNULL(CollectionDate, '') <> '' 
    AND CAST((SUBSTRING(a.CollectionDate, 7, 4) + '-' + SUBSTRING(a.CollectionDate, 4, 2) + '-' + SUBSTRING(a.CollectionDate, 1, 2)) AS date) >= '2014-05-01'   
    and ISNULL(CollectionDate, '') <> ''
    AND CAST((SUBSTRING(a.CollectionDate, 7, 4) + '-' + SUBSTRING(a.CollectionDate, 4, 2) + '-' + SUBSTRING(a.CollectionDate, 1, 2)) AS date) <= '2015-05-07'  
order by 
    (qtyis * b.unitprice) DESC

0 个答案:

没有答案