sql - 根据条件获取每个月表中的第一行
问题描述
我有table
很多records
,columns
像这样:
under_symbol|quote_date|percent|type
under_symbol is a string,
quote_date is a datetime2,
percent is a float,
type is a char that can be 0 or 1
我需要得到每个给定月份的record
第一个,其中> .19 和< .21 和= 1table
under_symbol
percent
percent
type
这将为 each 返回所有这些under_symbol
,但我只需要每个under_symbol
匹配它的月份的第一条记录:
SELECT *
[under_symbol]
,[quote_date]
,[type]
,[percent]
FROM [Underlying].[dbo].[Table_EOD]
where [percent] > .18 and [percent] < .22 and type = '1'
问题是我得到了给定月份的很多记录。对于每个月,我只需要与这些条件匹配的每个唯一符号在该月中最早的那个。
解决方案
你可以试试这样的
with month_cte as (
select *, row_number() over (partition by eomonth(quote_date) order by quote_date) rn
from [Underlying].[dbo].[Table_EOD]
where [percent] > .18
and [percent] < .22
and type = '1')
SELECT [under_symbol]
,[quote_date]
,[type]
,[percent]
FROM month_cte
where rn=1;
推荐阅读
- indexing - 如何使用 INDEX 和 MATCH 来基于单元格?
- mysql - 在 sql 数据库创建表上出现语法错误
- c# - 解析值时遇到意外字符:{。大摇大摆的路径
- javascript - Web JavaScript 应用程序无法找到资源
- python - 如何从缩短的 tiktok URL 中提取用户帐户名称和视频 ID?
- python - 如何使用python openpyxl删除excel中的row = 2
- ios - 将项目从 Xcode 10.3 迁移到 Xcode 11.5 时出错
- javascript - 在 html 中“获取”多个 xml 文件
- powershell - 在powershell中用某些文本字符串替换更多文本字符串
- jenkins - 将构建参数导出到共享库