时间范围-Sql

发布于 2024-11-06 00:39:29 字数 373 浏览 2 评论 0原文

请帮助我解决我的问题。因此,我有一个名为“RATES”的表,其中包含以下列:

id (int) 
rate (money) 
start_time (datetime) 
end_time(datetime)

示例数据:

1 150 8:00am 6:00pm 
2 200 6:00pm 4:00am
3 250 8:00am 4:00am (the next day)

我要做的就是选择给定时间所在的所有 id。

例如给定时间:晚上 9:00,输出应该是 2,3

问题是我得到的时间范围是早上 8 点到第二天凌晨 4 点,我不知道该怎么办。请帮忙!预先感谢:D

please help me with my problem. So, I have a table named 'RATES' which contains these columns:

id (int) 
rate (money) 
start_time (datetime) 
end_time(datetime)

example data:

1 150 8:00am 6:00pm 
2 200 6:00pm 4:00am
3 250 8:00am 4:00am (the next day)

What I have to do is to select all the id(s) to where a given time would fall.

e.g given time: 9:00 pm, the output should be 2,3

The problem is I got this time range between 8am to 4am the next day and I don't know what to do. Help, please! thanks in advance :D

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

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

发布评论

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

评论(3

许仙没带伞 2024-11-13 00:39:29

假设 @Andriy M 是正确的:

  • 如果 end_time<=start_time 则数据的跨度不会超过 24 小时
  • ,则 end_time 属于第二天

then what you're looking for is this:

Declare @GivenTime DateTime
Set @GivenTime = '9:00 PM'
Select ID
  From Rates
 Where (Start_Time<End_Time And Start_Time<=@GivenTime And End_Time>=@GivenTime)
    Or (Start_Time=End_Time And Start_Time=@GivenTime)
    Or (Start_Time>End_Time And (Start_Time>=@GivenTime Or End_Time<=@GivenTime))

Assuming that @Andriy M is correct:

  • Data never spans more than 24 hours
  • if end_time<=start_time then end_time belongs to the next day

then what you're looking for is this:

Declare @GivenTime DateTime
Set @GivenTime = '9:00 PM'
Select ID
  From Rates
 Where (Start_Time<End_Time And Start_Time<=@GivenTime And End_Time>=@GivenTime)
    Or (Start_Time=End_Time And Start_Time=@GivenTime)
    Or (Start_Time>End_Time And (Start_Time>=@GivenTime Or End_Time<=@GivenTime))
檐上三寸雪 2024-11-13 00:39:29

我从来没有真正使用过 MS SQL,但这也许会有所帮助。

我本来打算建议这样的事情,但是按照你设置数据的方式,这会失败。

<代码>
从价格中选择 ID
其中 datepart(hh, start_time) <= 9 AND datepart(hh, end_time) >= 9;

如果您希望获得正确的数据,您将需要使用实际日期进行搜索。

<代码>
从价格中选择 ID
WHERE start_time <= '2011-1-1 9:00' AND end_time >= '2011-1-1 9:00';

这可能不完全正确,但它可能会帮助您寻找正确的方向。

I don't really ever use MS SQL, but maybe this will help.

I was going to suggest something like this, but by the way you have your data set up, this would fail.


SELECT id FROM RATES
WHERE datepart(hh, start_time) <= 9 AND datepart(hh, end_time) >= 9;

You'll have you search using the actual date if you expect to get the correct data back.


SELECT id FROM RATES
WHERE start_time <= '2011-1-1 9:00' AND end_time >= '2011-1-1 9:00';

This may not be exactly correct, but it may help you look in the right direction.

泪意 2024-11-13 00:39:29

我想@gbn 不会帮助你。我将尝试填写。

给定一个名为 timedata 的表,其范围最多只超过一天。

WITH normalized AS
(
   SELECT * 
   FROM timedata
   WHERE datepart(day,start_time) = datepart(day,endtime)
   UNION ALL
   SELECT id, rate, start_time, dateadd(second,dateadd(day,datediff(day,0,end_time),0),-1) as end_time 
   FROM timedata
   WHERE not (datepart(day,start_time) = datepart(day,endtime))
   UNION ALL
   SELECT id, rate,dateadd(day,datediff(day,0,end_time),0) as start_time, end_time
   FROM timedata
   WHERE not (datepart(day,start_time) = datepart(day,endtime))
)
SELECT * 
FROM normalized
WHERE datepart(hour,start_time) < @inhour
  AND datepart(hour,end_time) > @inhour

这利用了 CTE 和截断日期时间值的技巧。要了解此技巧,请阅读以下问题和答案:在 SQL Server 中计算日期

以下是此查询的概述:

创建一个规范化表,其中每个时间跨度仅超过一天

  • 通过选择同一天发生的所有行,

。然后,对于跨越两天的每个条目,加入

  • 选择开始时间和第二天之前的一秒作为所有该跨度的结束时间。

选择

  • end_time 日期的中午 ​​12 点作为 starttime 和 end_time。

最后,您使用此标准化表上的小时指示器执行选择。

如果您的范围超过一天,您将需要使用递归 CTE 来获取相同的标准化表。

I guess @gbn is not going to help you. I will try and fill in.

Given -- a table called timedata that has ranges only going over at most one day

WITH normalized AS
(
   SELECT * 
   FROM timedata
   WHERE datepart(day,start_time) = datepart(day,endtime)
   UNION ALL
   SELECT id, rate, start_time, dateadd(second,dateadd(day,datediff(day,0,end_time),0),-1) as end_time 
   FROM timedata
   WHERE not (datepart(day,start_time) = datepart(day,endtime))
   UNION ALL
   SELECT id, rate,dateadd(day,datediff(day,0,end_time),0) as start_time, end_time
   FROM timedata
   WHERE not (datepart(day,start_time) = datepart(day,endtime))
)
SELECT * 
FROM normalized
WHERE datepart(hour,start_time) < @inhour
  AND datepart(hour,end_time) > @inhour

This makes use of a CTE and a trick to truncate datetime values. To understand this trick read this question and answer: Floor a date in SQL server

Here is an outline of what this query does:

Create a normalized table with each time span only going over one day by

  • Selecting all rows that occur on the same day.

Then for each entry that spans two days joining in

  • Selecting the starttime and one second before the next day as the end time for all that span.

and

  • Selecting 12am of the end_time date as the starttime and the end_time.

Finally you perform the select using the hour indicator on this normalized table.

If your ranges go over more than one day you would need to use a recursive CTE to get the same normalized table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文