我的表是这样的:
audit cons articulo date bodega amount lote
--- |----|----- |------------|--------|---------|------
11 | 2 | 10 | 04/03/2009 | BMP | 399 |454
23 | 4 | 11 | 03/03/2009 | BMO | 244 |787
31 | 2 | 10 | 04/03/2009 | BMP | 555 |454
45 | 5 | 12 | 03/03/2009 | BNO | 300 |786
23 | 7 | 11 | 03/03/2009 | BIM1 | 200 |123
61 | 4 | 10 | 04/03/2009 | BIM1 | 500 |783
75 | 5 | 13 | 24/01/2008 | BMP | 600 |567
75 | 1 | 13 | 24/01/2008 | BMP | 700 |777
我需要选择每个articulo,bodega和lote的最后日期数量
审计+ cons = pk表
示例:
audit cons articulo date bodega amount lote
--- |----|----- |------------|--------|--------- |
11 | 2 | 10 | 04/03/2009 | BMP | 399 | 454
23 | 4 | 11 | 03/03/2009 | BMO | 244 | 787
45 | 5 | 12 | 03/03/2009 | BNO | 300 | 786
23 | 7 | 11 | 03/03/2009 | BIM1 | 200 | 123
61 | 4 | 10 | 04/03/2009 | BIM1 | 500 | 783
75 | 5 | 13 | 01/01/2009 | BMP | 600 | 567
75 | 1 | 13 | 01/01/2009 | BMP | 700 | 777
问题在于,当我使用时,我无法获得金额:
select amount, bodega, articulo, max(date) ,lote
from table
group by amount, bodega, articulo,lote
这会重复很多行,任何帮助都会很棒
答案 0 :(得分:1)
Oracle 9i +支持ROW_NUMBER:
WITH example AS (
SELECT t.*,
ROW_NUMBER() OVER(PARTITION BY t.bodega, t.articulo
ORDER BY t.audit) AS rnk
FROM YOUR_TABLE t)
SELECT e.*
FROM example e
WHERE e.rnk = 1
否则,您可以对派生表进行连接(但如果有多个具有相同最高日期值的bodega / articulo,则会返回重复项):
SELECT a.*
FROM YOUR_TABLE a
JOIN (SELECT t.bodega, t.articulo, MAX(t.date) AS maxdate
FROM YOUR_TABLE t
GROUP BY t.bodega, t.articulo) b ON b.bodega = a.bodega
AND b.articulo = a.articulo
AND b.maxdate = a.date
答案 1 :(得分:1)
MIN/MAX using KEEP的组合可能会起作用
答案 2 :(得分:0)
SELECT
t1.amount, t1.bodega, t1.articulo, t1.date
FROM
table t1 join
(select
bodega, articulo, max(date) date
from
table
group by
amount, bodega, articulo
) on t1.bodega=t2.bodega and t1.articulo=t2.articulo and t1.date=t2.date
答案 3 :(得分:0)
试试这个......
select * from table
where (audit,cons,articulo ,date,bodega,Lote )
in (select min(audit),min(cons),articulo ,max(date),bodega,Lote
from table
group by articulo , bodega ,lote)