此查询的目的是恢复销售产品的产品及其价格,价格应该从最接近但不等于传递日期的日期开始,基本上是最近可用的价格.每天都没有价格记录.在where子句中使用聚合select语句有点不对劲.有一个更好的方法吗?也许在加入标准?
select
p.ProductName,
pp.Price,
pp.Date,
from product p
inner join productprice pp on p.productid = pp.productid
where
pp.evaluationdate = (select max(Date) from productprice
where productid = p.productid
and date < @DateIn)
and p.producttype = 'OnSale'
Run Code Online (Sandbox Code Playgroud)
实际的查询有点复杂,但这基本上是问题所在.感谢您的输入.
编辑 将返回多个产品
编辑 我正在试验@Remus Rusanu和@ km的建议(虽然@Remus Rusanu删除了他的)三个,包括我原来的,在性能方面似乎差不多.我试图决定一个人是否以其他一些无形的方式提供优惠,即维护,自我记录等,因为这将由其他人维持.再次感谢.