sql-server - SQL : 当 period 格式为 yyyymm 时,从 SQL 服务器中提取最近 12 个月的数据
问题描述
我有一个看起来像这样的 SQL 服务器表
一个标题 | 另一个标题 |
---|---|
202010 | 一个 |
202011 | 乙 |
202012 | C |
202101 | D |
202102 | 乙 |
202103 | F |
202104 | G |
202105 | H |
202106 | 我 |
202107 | Ĵ |
202108 | ķ |
202109 | 大号 |
202110 | 米 |
202111 | ñ |
monthid 是格式为 yyyymm 且具有整数数据类型的列。
我需要提取过去 12 个月的数据。
有人可以建议我怎么做吗
解决方案
将您的数据转换为Pandas DataFrame
. 您可以在此处查看如何操作。
# Convert date column to datetime
df['A header'] = pd.to_datetime(df['A header'], format='%Y%m')
# Sort and select last 12 months
df.sort_values(by='A header',ascending=True).set_index("A header").last("12M").reset_index()
| A header | Another header |
|:--------------------|:-----------------|
| 2020-12-01 00:00:00 | C |
| 2021-01-01 00:00:00 | D |
| 2021-02-01 00:00:00 | E |
| 2021-03-01 00:00:00 | F |
| 2021-04-01 00:00:00 | G |
| 2021-05-01 00:00:00 | H |
| 2021-06-01 00:00:00 | I |
| 2021-07-01 00:00:00 | J |
| 2021-08-01 00:00:00 | K |
| 2021-09-01 00:00:00 | L |
| 2021-10-01 00:00:00 | M |
| 2021-11-01 00:00:00 | N |
推荐阅读
- javascript - 使用 switch case 语句状态机在 javascript 中绘制形状
- huggingface-transformers - 如何使用 GPT-J 进行释义
- java - 如何在 flink 中模拟异步操作与可完成的未来
- amazon-web-services - aws ecs 控制台运行任务时出现错误“无法运行任务”,如何调试/查找真正的错误
- javascript - Firebase:名为“[DEFAULT]”的 Firebase 应用程序已经存在,具有不同的选项或配置(应用程序/重复应用程序)
- r - 根据每组的最后一次观察删除一个子组
- authentication - AddOpenIdConnect() 和 DefaultPolicy 作为 FallbackPolicy 导致 signin-oidc 循环
- flask - Heroku Flask 部署 - 应用程序错误
- xml - XPath:比较所有属性的第一个和最后一个并返回唯一值
- spring-boot - 如何从 application.yml 中读取 Long 类型的值