比较难SQL 语句

dannyplus 2007-08-12 12:42:29
一个订单表有orderno,productid,price,flag,date,找出flag=0中,price大于flag=1中,目前productid的最新price的订单.也就是查出产品的价格大于已确认的最近的订单中的价格.真得很难讲清楚.简单的说就是一个订单中的产品价格不能比最近一次已入库的产品价格高.
问题的难点在于productid,在不同的单子中有同样的产品,怎样把这个产品找出来再用它来找出最近入库的价格.
帮帮吧
...全文
247 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
dannyplus 2007-08-14
  • 打赏
  • 举报
回复
搞定了,多谢两位
dannyplus 2007-08-13
  • 打赏
  • 举报
回复
很急,有人再帮一下吗?
Limpire 2007-08-12
  • 打赏
  • 举报
回复
1:orderno是不是唯一连续?
2:flag=1代表确认?
3:已入库是不是代表确认——flag=1?
dannyplus 2007-08-12
  • 打赏
  • 举报
回复
谢谢chuwenqing() 和mantisXF(枫の叶) 两位的热心帮助,但是按两位讲的都有问题不能得到正确的结果.我考虑用存储过程嵌套试一下
mantisXF 2007-08-12
  • 打赏
  • 举报
回复
select o.orderno, o.productid, o.price, o.date
from order o,
(select f0.productid, max(f0.price)
from (select orderno, productid, price, date
from order where flag = 0) f0,
(select orderno, productid, price, date
from order where flag = 1) f1
where f0.orderno = f1.orderno
and f0.productid = f1.productid
and f0.price > f1.price
group by f0.productid)
where tt o.productid = tt.productid
and o.price = tt.price;
chuwenqing 2007-08-12
  • 打赏
  • 举报
回复

也就是说一个新进来的订单(这个单子里有多个产品),在验收时要验证每个产品(productid)的价格不能比最近入库的单子里产品的价格高.现在要把产品价格高的单子找出来.
表A orderno,productid,price,flag,date
select distinct orderno from A where flag=0 and price>(select top 1 price from A where productid=productid and flag=1 order by date desc)
现在的问题是如何处理productid,因为这个productid在同一个表里怎样区分要查的单子里的productid和原来已确认过的单子里的productid

第一个from A 改为 A as B 后面的 where 后面的句子改为 B.productid = A.productid
你再看看
dannyplus 2007-08-12
  • 打赏
  • 举报
回复
问题就在于productid=productid这里,如何表示
dannyplus 2007-08-12
  • 打赏
  • 举报
回复
也就是说一个新进来的订单(这个单子里有多个产品),在验收时要验证每个产品(productid)的价格不能比最近入库的单子里产品的价格高.现在要把产品价格高的单子找出来.
表A orderno,productid,price,flag,date
select distinct orderno from A where flag=0 and price>(select top 1 price from A where productid=productid and flag=1 order by date desc)
现在的问题是如何处理productid,因为这个productid在同一个表里怎样区分要查的单子里的productid和原来已确认过的单子里的productid
Limpire 2007-08-12
  • 打赏
  • 举报
回复
目前 productid的最新price的订单
-------------------------------
这句还是不明白
dannyplus 2007-08-12
  • 打赏
  • 举报
回复
谢谢 Limpire(昨夜小楼)

1:orderno不是唯一连续,它是订单号里面有多个产品
2:flag=1代表确认已入库
3:已入库标识flag=1

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧