sql - 使用棘手的分组查询
问题描述
请帮帮我,我有这样的桌子:
id1, id2, quantity
1 1 5
2 1 3
3 1 0
2 3 10
1 3 4
我只需要获取 id1 和 id2 值,其中数量最大按 id2 分组。请帮我。
第一个 id - 它是好 (id2) 所在的 id。Q - q 每个仓库。完美的结果是这样的:
id1, id2, quantity
1 1 5
2 3 10
或类似的东西。我需要知道具有 MAX 数量的产品 ID (id2) 和存储 ID (id1) 以及此类产品的其他存储。也很容易得到结果 MAX(q)。
解决方案
这是你想要的吗?
select t.*
from t
order by quantity desc
fetch first 1 row only;
这将获取数量最多的行。或这个?
select t.*
from t
where t.quantity = (select max(t2.quantity) from t t2 where t2.id2 = t.id2);
推荐阅读
- xslt - 根据条件将节点分组并使用 Schematron 进行验证
- android - 使用事件流在 Flutter 中获取下载进度
- python - 如何通过单击 tkinter 中的按钮来运行 python 文件?
- unix - awk 解析文本文件问题
- google-data-studio - 结合谷歌数据工作室的两个维度
- powershell - SolrCloud Configset API 上传返回 500 "KeeperErrorCode = NoNode"
- newline - 从/到 docx 转换时换行的 pandoc 处理
- electron - 电子图标
- git - 有没有办法使用 Git Gui 将更改从远程 Gitlab 存储库拉到本地副本?
- reactjs - react js如何获取最小值