2014-06-19 29 views
0

我正在使用一個查詢並嘗試修改它,以便它只返回不同的「lodnum」,其中有超過2個所述「lodnum」條目。DISTINCT到只有一列

我查看了ROW_NUMBER()OVER/PARTITIONS。我似乎無法讓它做我想做的事。

查詢是:(抱歉格式)

SELECT i.lodnum, 
     i.prtnum, 
     i.lotnum, 
     sum(i.untqty), 
     i.ftpcod, 
     i.invsts 
     FROM inventory_view i, 
       locmst m 
     WHERE i.stoloc = m.stoloc 
     AND m.arecod = 'PART-HSY' 
     AND i.prtnum NOT IN (SELECT i2.prtnum 
             FROM inventory_view i2, 
               locmst m2 
             WHERE i2.stoloc = m2.stoloc 
             AND m2.arecod = 'PART-HSY' 
             AND i2.lotnum = i.lotnum 
             AND i2.invsts = i.invsts 
             GROUP BY i2.prtnum 
             HAVING COUNT(*) = 1) 
     AND i.lodnum IN (SELECT i3.lodnum FROM inventory_view i3, locmst m3 
             WHERE i3.stoloc = m3.stoloc 
             AND m3.arecod = 'PART-HSY' 
             AND i3.lotnum = i.lotnum 
             AND i3.invsts = i.invsts 
             GROUP BY i3.lodnum 
             HAVING COUNT(*) > 1) 

GROUP BY 
    i.lodnum, 
    i.stoloc, 
    i.prtnum, 
    i.lotnum, 
    i.ftpcod, 
    i.invsts 
ORDER BY 
    i.prtnum, 
    i.lotnum, 
    i.invsts 

回答

0

它看起來像:

with t as (
     YOUR QUERY HERE WITHOUT ORDER BY 
    ) 
select t.* 
from (select t.*, row_number() over (partition by lodnum order by lodnum) as seqnum 
     from t 
    ) t 
where seqnum = 1 
order by prtnum, lotnum, invsts; 
+0

嘿戈登得到了側一些任務跟蹤和忘記迴應。非常感謝你的幫助。這工作原樣與我的查詢粘貼到您定義的區域。 +1! – user3642066