sql - 仅获取第一行或最高值 SQL
问题描述
我正在尝试获取第一行(也是此查询的最高值)它现在正在发布所有内容
例如,当我尝试添加 Limit/NumRow 以获取第一行(也是最高值)时,它会导致查询出错。查询是
select
title
, min(year)
, max(year)
, count(appointment) AS num_appointments
from appointments
group by title
ORDER BY title
ORDER BY count(title) desc;
解决方案
您可以尝试以下方法 - 限制适用于MySql/Postgres
select title, min(year), max(year),
count(appointment) AS num_appointments
from appointments group by title
ORDER BY count(appointment) desc
limit 1
如果您的数据库是 SQL Server - 您可以使用 TOP 如下所示 -
select TOP 1 title, min(year), max(year),
count(appointment) AS num_appointments
from appointments group by title
ORDER BY count(appointment) desc
推荐阅读
- scala - 用于更好地链接收集的功能模式
- angular - ng2-nouislider 格式范围负载角 9
- machine-learning - 使用 doc2vec 查询文档相似度
- ios - 参数类型不符合预期的类型 MKAnnotation
- reactjs - 在 react 中从 formik 向 Firestore 提交数据
- javascript - 在 Javascript 中创建一个唯一的数组
- highcharts - Highcharts - 如果条形图上没有足够的空间,则更改文本颜色
- python - 如何根据访问频率将回访用户分类到不同的时间段
- docker - 通过 docker 镜像安装 jfrog 容器注册表的问题
- azure-active-directory - 通过订阅获取 Azure 应用程序