SQL语句,输出多条不同物料号,最大日期对应的价格。SQL语句怎么写。

2025-04-05 23:12:50
推荐回答(2个)
回答1:

select table.物料编码,table.日期,table.价格 from table,
(select 物料编码,max(最大日期) b from table group by 物料编码) a where table.物料编码=a.物料编码 and table.日期=a.b
上面是比较一般的连立写法,你也可以尝试将select 物料编码,max(最大日期) b from table放入子查询,不过需要将,物料编码,max(最大日期) 二者连立oracle中可以
物料编码||max(最大日期) ,其他数据库这么直接连立未必可行。
oracle子查询写法:日期转换部分我就不写了,当做是字符型的
select table.物料编码,table.日期,table.价格 from table where 物料编码||日期 in (select 物料编码||max(最大日期) b from table group by 物料编码)

回答2:

select 1,2,3 from table order by 1,2 desc;