【问题标题】:How do I set a date range for 2 recurring days each week over a 2 or 3 month period in SQL?如何在 SQL 中为 2 个月或 3 个月期间每周 2 天重复设置日期范围?
【发布时间】:2010-11-01 20:04:27
【问题描述】:

我在 Microsoft SQL Server 2005 中工作,需要对周二有资格参加我们 50+ 锦标赛的球员进行分析 --> 要获得资格,他们需要在周一或周二下午 2 点之前的那一周获得 5+ 分,并且积分不是累积的(必须在周一或周二下午 2 点之前获得至少 5 分)。我使用的代码如下,但到目前为止只包含特定星期一和星期二的日期范围。我需要 2 个月内的所有星期一和星期二。我对此很陌生(有点像作为一项额外的职责扔在我的腿上,所以我必须在飞行中学习)......

到目前为止,正如我所说,我已经能够靠自己成功获得所需的所有信息,但它仅针对 1 周。到目前为止,我在网上找到的所有内容都比我能解释的更加混乱,没有人引用我提供的代码,而是给了我无法使用的全新代码......

SELECT     
     dbo.CombinedPts.Account, dbo.CombinedPts.FirstName, 
     dbo.CombinedPts.LastName, 
     ISNULL(dbo.CombinedPts.EGSPts, 0) AS EGS, 
     ISNULL(dbo.CombinedPts.IRPts, 0) AS IR, 
     DATENAME(dw, dbo.CombinedPts.Date) AS WkDay, 
     DATEDIFF(YY, dbo.PlayerMaster.Birthdate, {fn current_date()}) - CASE WHEN   (MONTH(dbo.PlayerMaster.Birthdate)=MONTH({fn current_date()}) AND DAY(dbo.PlayerMaster.Birthdate) > DAY({fn current_date()}) OR MONTH (dbo.PlayerMaster.Birthdate) > MONTH ({fn current_date()}))  THEN 1   ELSE 0   END   AS Age
FROM       
     dbo.CombinedPts, dbo.PlayerMaster 
WHERE      
     (DATEDIFF(YY, dbo.PlayerMaster.Birthdate, {fn current_date()}) >= 50) 
     AND (dbo.CombinedPts.EGSPts >= 5 OR dbo.CombinedPts.IRPts >= 5) 
     AND (dbo.CombinedPts.Account = dbo.PlayerMaster.Account) 
     AND (Date BETWEEN '10/11/2010 00:00:00' AND '10/11/2010 13:59:59' OR Date BETWEEN '10/12/2010 00:00:00' AND '10/12/2010 13:59:59') 
     AND (DATEPART(dw, dbo.CombinedPts.Date) = 2 OR DATEPART(dw, dbo.CombinedPts.Date) = 3)
GROUP BY   
     dbo.CombinedPts.Account, 
     dbo.CombinedPts.FirstName, 
     dbo.CombinedPts.LastName, 
     DATENAME(dw, dbo.CombinedPts.Date), 
     dbo.CombinedPts.EGSPts, 
     dbo.CombinedPts.IRPts, 
     dbo.PlayerMaster.Birthdate
ORDER BY
     dbo.CombinedPts.Account

附带说明一下,在我们的 SQL Server 2005 中,我没有创建子查询的权限(我已经成为 UNION ALL 的狂热用户 --> 到目前为止,这在查询中证明是无用的)我也没有权限生成表,所以我所做的一切都必须在一个查询中......到目前为止,所有有意义的帮助都是使用表和子查询。

【问题讨论】:

  • dbo.CombinedPts、dbo.PlayerMaster可以加入哪个栏目?您是否在 Management Studio 中运行它?
  • 我不知道子查询可能会被权限拒绝...您确定这是权限错误而不是子查询中的语法错误吗?
  • 我们在网络上运行它,并且它以某种方式设置为只有一个人(我们的 IT 主管)可以编辑任何内容,即我无法在 SQL Server 2005 中保存任何内容,因此也无法创建子查询,因为它不会不要让我链接它们,因为它们未保存。他一直告诉我他会修复它,这样我就可以使用视图和设计(它也不会让我这样做),所以我可以保存表和子查询,但实际上不会对数据库进行更改,但这还没有发生.

标签: sql-server date sql-server-2005


【解决方案1】:

结构略有不同,但大体相同。我使用别名而不是完整引用。它也未经测试,但解析干净。

AND DATEDIFF(m,cp.Date,GETDATE()) < 2 为您提供 2 个月的窗口。

编辑 - 添加了日期和时间的子句。 AND (DATEPART(dw, cp.Date) in (2,3) AND DATEPART(hh,cp.Date) > 14) 仅在周一和周二下午 2:00(1400 小时)之后为您提供服务

SELECT cp.Account
, cp.FirstName
, cp.LastName
, ISNULL(cp.EGSPts,0) AS EGS
, ISNULL(cp.IRPts,0) AS IR
, DATENAME(dw, cp.Date) AS WkDay
, DATEDIFF(YY, pm.Birthdate, GETDATE()) 
        - CASE WHEN (MONTH(pm.Birthdate) = MONTH(GETDATE()) 
        AND DAY(pm.Birthdate) > DAY(GETDATE()) 
        OR MONTH (pm.Birthdate) > MONTH (GETDATE()) )
        THEN 1 
        ELSE 0 
        END AS Age

FROM dbo.CombinedPts cp
join dbo.PlayerMaster pm on cp.Account = pm.Account

WHERE DATEDIFF(YY, pm.Birthdate, GETDATE()) >= 50
 AND (cp.EGSPts >= 5 OR cp.IRPts >= 5)
 AND (DATEPART(dw, cp.Date) in (2,3) AND DATEPART(hh,cp.Date) > 14)
 AND DATEDIFF(m,cp.Date,GETDATE()) < 2

GROUP BY cp.Account
, cp.FirstName
, cp.LastName
, DATENAME(dw, cp.Date)
, cp.EGSPts
, cp.IRPts
, pm.Birthdate

ORDER BY cp.Account

【讨论】:

  • 它工作正常,并且在大多数情况下提供了正确的参数。 ^^ 问题,如何指定周一00:00:00到周一13:59:59的时段(周二同理)?
  • 哦,你在我问之前回答了我的问题!
【解决方案2】:

我不是 100% 清楚你想要实现什么。

我想你想要这样的东西:

select *
  from CombinedPts
 where CombinedPts.Date > start_of_contest
  and CombinedPts.Date < end_of_contest
  and ( datepart( dw, CombinedPts.Date ) = 2 or datepart( h, CombinedPts.Date ) < 14 )
  and ( datepart( dw, CombinedPts.Date ) = 3 or datepart( h, CombinedPts.Date ) < 14 )
 having sum( CombinedPts.EGSPts ) >= 5

此处使用日期部分以确保它在正确的日期和时间。

【讨论】:

  • 我也不是 100%...这只是他们让我做的第二次分析...看起来比我看到的要好,但参数比显示的要多例如年龄 >= 50(仅在 PlayerMaster 中找到)、EGS >=5、IR >= 5,并且仅在下午 2 点之前的周一或周二(其余在 CombinedPts 中找到)。
猜你喜欢
  • 2019-01-02
  • 2016-08-13
  • 2013-11-19
  • 2023-01-12
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
  • 2021-09-21
  • 2014-10-12
相关资源
最近更新 更多