我想要这个数字' 1000'仅出现一次,然后' 0'对于剩下的记录,直到下个月出现,也可以是一个案例类型语句?

发布于 2025-02-07 17:25:10 字数 683 浏览 2 评论 0原文

我正在使用SQL,我希望这个数字“ 1000”每月出现一次。我有一个记录集,每个月的第一个出现多次。我希望剩下的记录仅出现一次“ 1000”,然后出现“ 0”,直到下个月出现为止。我想要以下请求 - 也许是一个案例类型语句/订单选择?我正在使用SQL Server 2018 @@ SQLServer。请参阅下面的表,以了解我希望数据出现的方式。

非常感谢:)

日期金额
01/01/20221000
01/01/20220
01/01/20220
01/02/20221000
01/02/20220
01/03/202201/02/2022 0
01/01 /1000
01/01/ 01/01/01/01/ 03/20220

I am using SQL and I would like this number '1000' to appear once per month. I have a record set which has the first of every month appearing multiple times. I would like the number '1000' to appear once only and then '0' for the remaining records until the next month appears. I would like the below please- maybe a case type statement/order parition by? I am using SQL Server 2018 @@SQLSERVER. Please see table below of how i would like the data to appear.

Many Thanks :)

DateAmount
01/01/20221000
01/01/20220
01/01/20220
01/02/20221000
01/02/20220
01/02/20220
01/03/20221000
01/03/20220

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撑一把青伞 2025-02-14 17:25:10

解决问题的解决方案:

WITH CT1 AS
(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY CONCAT(MONTH([Date]),YEAR([Date])) ORDER BY [Date]) as rn
FROM your_table
)
SELECT [Date],
CASE WHEN rn = 1 THEN 1000 ELSE 0 END AS Amount
FROM CT1;

工作示例:

Solution for your problem:

WITH CT1 AS
(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY CONCAT(MONTH([Date]),YEAR([Date])) ORDER BY [Date]) as rn
FROM your_table
)
SELECT [Date],
CASE WHEN rn = 1 THEN 1000 ELSE 0 END AS Amount
FROM CT1;

Working Example: DB<>Fiddle Link

鹿! 2025-02-14 17:25:10

只有您可以使用 row_number 的日期列表和条件表达式任意分配每个月的一排值1000

select *, 
  Iif(Row_Number() over(partition by Month(date) order by (select null)) = 1, 1000, 0) Amount
from t
order by [date], Amount desc;

Given just a list of dates you could use row_number and a conditional expression to arbitrarily assign one row of each month a value of 1000

select *, 
  Iif(Row_Number() over(partition by Month(date) order by (select null)) = 1, 1000, 0) Amount
from t
order by [date], Amount desc;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文