用SQL语句进行查询的两个问题

springding 2001-10-13 09:29:11
-----------------------------------
date on out onhand
-----------------------------------
9/12 6 5 1
-----------------------------------
9/13 8 3 6
-----------------------------------
9/14 9 2 13
-----------------------------------

onhand=前一天的onhand + 今天的in - 今天的out

如何用SQL语句实现如上的查询功能?

还有如何得到按A字段GROUP,B字段ORDER后,每个GROUP中B字段最大的那条记录的C字段内容?

非常感谢!
...全文
74 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
springding 2001-10-15
  • 打赏
  • 举报
回复
弱水三千:我试了一下你写的
select mydate, qty_in, qty_out,
(select sum(qty_in) - sum(qty_out)
from mytable as x
where x.mydate < = y.mydate) as onhand
from mytable as y;
在编译时会报x.mydate < = y.mydate这个表达式有错误,是我错了还是你错了???
springding 2001-10-14
  • 打赏
  • 举报
回复
小弟是个初学者,水平有限,所以还想请楼上的兄弟解释一下这里的x和y表示什么?谢谢啦!
KingSunSha 2001-10-14
  • 打赏
  • 举报
回复
x,y是别名,当你引用一个很长的表名的时候,用别名能够比较方便的写代码。
同时,当引用同一个表的时候,用不同的别名能使数据库分辨是从哪个表里拿数据。
select x.field1, y.field2
from mytable1 as x, mytable2 as y
....
等价于
select mytable1.field1, mytable2.filed2
from mytable1,mytable2
KingSunSha 2001-10-13
  • 打赏
  • 举报
回复
注:不要用date,in这种sql关键字做字段名!!!!!
1、onhand = sum(in)-sum(out) where date <= a specified date
select mydate, qty_in, qty_out,
(select sum(qty_in) - sum(qty_out)
from mytable as x
where x.mydate < = y.mydate) as onhand
from mytable as y;
2、select a,b,c
from mytable as x
where (a,b) in (select a, max(b)
from mytable as y
where y.a = x.a);
AFIHA 2001-10-13
  • 打赏
  • 举报
回复
up

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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