sql - 查询给出了期望的结果,但希望使用此逻辑在案例表达式中应用窗口函数
问题描述
寻找更好的解决方案,我写的这个查询对我有用,我想知道是否有更好的方法或相同的逻辑可以在案例表达式中使用。
我已经编写了一个查询,该查询使用窗口函数给出了各个季度的每个 id 的最大计数,然后拉取 1 的 seqnum 并给出结果。
我想知道是否可以在具有相似逻辑的案例表达式中使用相同的查询。
select id,Quarter_yr, country
from (select id,Quarter_yr, country, count(*) as cnt,
row_number() over (partition by id, Quarter_yr order by count(*) desc) as seqnum
from table
group by id,Quarter_yr,country
) t
where seqnum = 1;
解决方案
也许这个查询就是你要找的
select top(1) id, Quarter_yr, country, count(*) as cnt
from table
group by id, Quarter_yr, country
order by count(*) desc;
推荐阅读
- python - 使用 requests_html 进行网页抓取
- python - 在 Windows x64 上使用 Visual Studio 命令提示符构建 z3
- linux-kernel - 如果有任何内容写入特定的内存地址,如何实现 Linux 中断处理程序来处理中断?
- python - 如何在 python 中剪切 pdf 并创建照片库
- android - 底部的锚点视图 - ConstraintLayout
- fonts - Woff 2 字体,里面有字体集合
- html - 调整元素宽度的问题
- 和
- 和
- 和
- go - 扫掠 AABB 与 2D 线段
- angular - 如何在 ionic 5 中使用电容器应用程序实现谷歌标签管理器
- javascript - 如何使用 Javascript 更改/定义构建 CLI Node.js 工具的终端大小?