sql - 如何从两个不同表的两个不同列中获取 MAX
问题描述
我有表 A、表 B、表 C,并且想从 B 和 MAX(date) 中获取 MAX(OrderId) 和 group by,并从 C 中获取 group by
表 A:
SNO
AccNo
表 B:
SNO(FK)
OrderId
表 C:
AccNo(FK)
date
我写了这样的东西:
select b.max(OrderId), other columns
from a, b, c
where a.sno = b.sno
and a.accno = c.accno
and (b.orderid, c.date) IN (select b.orderid, max(c.date)
from a, b,c
where a.sno = b.sno
and a.accno = c.accno
group by b.orderid)
group by other columns
不知何故,这运行良好,但没有给出预期的结果。将不胜感激一些帮助。
解决方案
You can do:
select a.AccNo, max(b.orderId) as maxOrderId, max(myDate) as maxDate
from tableA a
inner join tableB b on a.SNO = b.SNO
inner join tableC c on a.AccNo = c.AccNo
group by a.AccNo
Is this what you wanted?
推荐阅读
- sql-server - 从用户定义的函数返回可以为空的日期
- php - 尝试在 Laravel 视图上使用 React 组件时出错
- debugging - Amyuni“打印机未激活”错误是什么意思?
- reactjs - 如何启动和打开电子邮件客户端 ReactJS
- pandas - 带有缺失值的 Pandas 转换为 Int64
- javascript - TypeError:使用 spotify 的 api 时无法读取未定义的属性
- javascript - 试图从环境文件中检索值在反应 js 中未定义
- mysql - mysql - 从多个表中获取相关行而不使用所有组合
- jax-rs - JAX-RS Response.getEntity() 始终为空
- c# - C# 不能采用 Windows InstallDate (fe CIM_OperatingSystem)