如何使用Rank()从分区查询中获取最大值

时间:2014-08-11 14:20:01

标签: tsql

Rank

我希望结果在使用rank函数进行分区时返回最大Rank。 我使用以下查询。

SELECT DISTINCT dbo.pomst.co_num
    ,dbo.pomst.wh_num
    ,dbo.pomst.po_number
    ,dbo.pomst.po_suffix
    ,dbo.pomst.vendor_id
    ,dbo.item.uom
    ,dbo.item.upc_num
    ,dbo.item.item_desc
    ,RIGHT(dbo.auditlog.pallet_id, 8) AS pallet_id
    ,dbo.auditlog.abs_num
    ,dbo.auditlog.item_qty
    ,dbo.auditlog.lot
    ,dbo.auditlog.packer
    ,auditlog.comments
    ,auditlog.date_time
    ,rank() OVER (
        PARTITION BY auditlog.comments ORDER BY auditlog.date_time ASC
        ) AS CorrectTrans
FROM dbo.auditlog
INNER JOIN dbo.pomst ON dbo.auditlog.co_num = dbo.pomst.co_num
    AND dbo.auditlog.wh_num = dbo.pomst.wh_num
    AND dbo.auditlog.po_number = dbo.pomst.po_number
    AND dbo.auditlog.po_suffix = dbo.pomst.po_suffix
INNER JOIN dbo.item ON dbo.auditlog.co_num = dbo.item.co_num
    AND dbo.auditlog.wh_num = dbo.item.wh_num
    AND dbo.auditlog.abs_num = dbo.item.abs_num
WHERE (dbo.pomst.co_num = 'AC01')
    AND (dbo.pomst.wh_num = 'KU22')
    AND (dbo.pomst.row_status = 'C')
    AND (dbo.auditlog.trans_type = 're')
    AND item_qty NOT LIKE '-%'

2 个答案:

答案 0 :(得分:0)

我明白了!我试图获得排名的最大结果,但是如果我将排名顺序从asc翻转到desc并使用CTE我可以选择总是有1作为排名而不是试图获得Max的结果。我仍然想知道如何获得Max等级,但这个解决方案适合我的需要。

;with cte as
(SELECT DISTINCT dbo.pomst.co_num
    ,dbo.pomst.wh_num
    ,dbo.pomst.po_number
    ,dbo.pomst.po_suffix
    ,dbo.pomst.vendor_id
    ,dbo.item.uom
    ,dbo.item.upc_num
    ,dbo.item.item_desc
    ,RIGHT(dbo.auditlog.pallet_id, 8) AS pallet_id
    ,dbo.auditlog.abs_num
    ,dbo.auditlog.item_qty
    ,dbo.auditlog.lot
    ,dbo.auditlog.packer
    ,auditlog.comments
    ,auditlog.date_time
    ,rank() OVER (
        PARTITION BY auditlog.comments ORDER BY auditlog.date_time desc
        ) AS CorrectTrans
FROM dbo.auditlog
INNER JOIN dbo.pomst ON dbo.auditlog.co_num = dbo.pomst.co_num
    AND dbo.auditlog.wh_num = dbo.pomst.wh_num
    AND dbo.auditlog.po_number = dbo.pomst.po_number
    AND dbo.auditlog.po_suffix = dbo.pomst.po_suffix
INNER JOIN dbo.item ON dbo.auditlog.co_num = dbo.item.co_num
    AND dbo.auditlog.wh_num = dbo.item.wh_num
    AND dbo.auditlog.abs_num = dbo.item.abs_num
WHERE (dbo.pomst.co_num = 'AC01')
    AND (dbo.pomst.wh_num = 'KU22')
    AND (dbo.pomst.row_status = 'C')
    AND (dbo.auditlog.trans_type = 're')
    AND item_qty NOT LIKE '-%'
)
Select * from cte
where CorrectTrans = 1

答案 1 :(得分:0)

添加select和group by,并将现有查询用作子查询。

试试..

    select max([CorrectTrans]), Vendor_Id, Item_qty, Lot, Pallet_id
from (
-- Your existing query --
SELECT DISTINCT dbo.pomst.co_num
    ,dbo.pomst.wh_num
    ,dbo.pomst.po_number
    ,dbo.pomst.po_suffix
    ,dbo.pomst.vendor_id
    ,dbo.item.uom
    ,dbo.item.upc_num
    ,dbo.item.item_desc
    ,RIGHT(dbo.auditlog.pallet_id, 8) AS pallet_id
    ,dbo.auditlog.abs_num
    ,dbo.auditlog.item_qty
    ,dbo.auditlog.lot
    ,dbo.auditlog.packer
    ,auditlog.comments
    ,auditlog.date_time
    ,rank() OVER (
        PARTITION BY auditlog.comments ORDER BY auditlog.date_time ASC
        ) AS CorrectTrans
    FROM dbo.auditlog
    INNER JOIN dbo.pomst ON dbo.auditlog.co_num = dbo.pomst.co_num
    AND dbo.auditlog.wh_num = dbo.pomst.wh_num
    AND dbo.auditlog.po_number = dbo.pomst.po_number
    AND dbo.auditlog.po_suffix = dbo.pomst.po_suffix
    INNER JOIN dbo.item ON dbo.auditlog.co_num = dbo.item.co_num
    AND dbo.auditlog.wh_num = dbo.item.wh_num
    AND dbo.auditlog.abs_num = dbo.item.abs_num
    WHERE (dbo.pomst.co_num = 'AC01')
    AND (dbo.pomst.wh_num = 'KU22')
    AND (dbo.pomst.row_status = 'C')
    AND (dbo.auditlog.trans_type = 're')
    AND item_qty NOT LIKE '-%'
    -- =======================================
) x
group by Vendor_id, Item_qty, Lot, Pallet_id