找到 Microsoft Management Studio 中的前三个工作日。

huangapple go评论54阅读模式
英文:

find first 3 working days in microsoft managment studio

问题

我有一个名为'工作日'的列,它提供了是否是工作日的信息(1表示工作日,0表示周末)。任务是找出每个月的前3个工作日。

我尝试使用以下代码:

SELECT working_day, *
FROM table
WHERE tdate BETWEEN DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) AND DATEADD(dd, -1, DATEADD(mm, DATEDIFF(mm, -1, GETDATE()), 0))
AND working_day = 1
AND tdate = CAST(GETDATE() AS DATE);
英文:

I have a column 'working days' which give info is it working day or no (1 for woking day and 0 for weekend). And the task is to find first 3 working days in each month.

I try to use this code:

 SELECT working_day, *
  FROM table 
  WHERE tdate BETWEEN DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) AND DATEADD(dd, -1, DATEADD(mm, DATEDIFF(mm, -1, GETDATE()), 0))
  AND working_day = 1
  AND tdate = CAST(GETDATE() AS DATE);

答案1

得分: 0

尝试使用以下的 row_number 函数:

WITH CTE AS
(
  SELECT *,
    ROW_NUMBER() OVER (PARTITION BY YEAR(tdate), MONTH(tdate) ORDER BY tdate) RN
  FROM tbl_name
  WHERE working_day = 1
)
SELECT tdate, working_day
FROM CTE 
WHERE RN <= 3

演示

英文:

Try using the row_number function as the following:

WITH CTE AS
(
  SELECT *,
    ROW_NUMBER() OVER (PARTITION BY YEAR(tdate), MONTH(tdate) ORDER BY tdate) RN
  FROM tbl_name
WHERE working_day = 1
)
SELECT tdate, working_day
FROM CTE 
WHERE RN &lt;= 3

Demo

huangapple
  • 本文由 发表于 2023年2月6日 19:11:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定