- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在此表中存储营业时间。企业可以在同一天有多个营业时间。 关闭时间可以是当天午夜之后。
CREATE TABLE [Process].[OpeningHours](
[openinghoursid] [int] IDENTITY(1,1) NOT NULL,
[businessid] [int] NOT NULL,
[daynumber] [int] NOT NULL,
[opentime] [time](7) NOT NULL,
[duration] [int] NOT NULL,
[closetime] AS (dateadd(minute,[duration],[opentime])
)
此表中的示例数据包括:
INSERT [Process].[OpeningHours]
([openinghoursid], [businessid], [daynumber], [opentime], [duration])
VALUES (79, 18, 2, CAST(N'12:00:00' AS Time), 165),
(80, 18, 2, CAST(N'18:00:00' AS Time), 240),
(81, 18, 3, CAST(N'12:00:00' AS Time), 165),
(82, 18, 3, CAST(N'18:00:00' AS Time), 240),
(83, 18, 4, CAST(N'12:00:00' AS Time), 165),
(84, 18, 4, CAST(N'18:00:00' AS Time), 240),
(85, 18, 5, CAST(N'12:00:00' AS Time), 165),
(86, 18, 5, CAST(N'18:00:00' AS Time), 240),
(87, 18, 6, CAST(N'12:00:00' AS Time), 165),
(88, 18, 6, CAST(N'18:00:00' AS Time), 300),
(89, 18, 7, CAST(N'12:00:00' AS Time), 165),
(90, 18, 7, CAST(N'18:00:00' AS Time), 600),
(91, 18, 1, CAST(N'12:00:00' AS Time), 180);
现在我想创建一个函数,该函数返回企业当前是否营业或已关闭的情况。
CREATE FUNCTION [Process].[ufnIsSpaceOpen](@businessid int)
RETURNS BIT
AS
BEGIN
DECLARE @currentdatetime DATETIME = GETDATE();
DECLARE @dayofweek INT = DATEPART(dw,@currentdatetime);
DECLARE @currentdate DATETIME = CONVERT(DATE, @currentdatetime);
DECLARE @isopen BIT;
SELECT @isopen = COUNT(*)
FROM Process.OpeningHours
WHERE
daynumber = @dayofweek
AND businessid = @businessid
AND
(
@currentdatetime >= @currentdate + CONVERT(DATETIME, opentime)
AND
@currentdatetime <=
CASE
WHEN closetime < '00:00:00' THEN @currentdate + CONVERT(DATETIME, closetime)
ELSE DATEADD(DAY,1,@currentdate) + CONVERT(DATETIME, closetime)
END
);
RETURN @isopen;
END;
GO
我正在使用 COUNT() 来查看是否有任何行与条件匹配,如果 0 匹配则表示已关闭,如果 COUNT() 大于 0 则表示已打开。当关闭时间在当天内时,此功能有效,但当关闭时间在午夜之后或当前时间在午夜之后时,此功能不起作用。
知道如何修复它吗?
编辑:感谢您的所有回复。最后我继续使用@DenisRubashkin 的解决方案。对于任何感兴趣的人来说,这是我使用的最终函数:
CREATE FUNCTION [Process].[ufnIsSpaceOpen](@businessid int)
RETURNS BIT
AS
BEGIN
DECLARE @isopen BIT;
DECLARE @Date DATETIME = GETDATE();
SELECT @isopen = COUNT(*)
FROM
(
SELECT (CAST(CAST(@Date AS DATE) AS DATETIME) + CAST(h.opentime AS DATETIME)) AS Opened,
DATEADD(mi, h.duration, (CAST(CAST(@Date AS DATE) AS DATETIME) + CAST(h.opentime AS DATETIME))) AS Closed
FROM Process.OpeningHours h
WHERE h.daynumber = DATEPART(dw, @Date)
AND businessid = @businessid
UNION
SELECT (CAST(DATEADD(day, -1, CAST(@Date AS DATE)) AS DATETIME) + CAST(h.opentime AS DATETIME)) AS Opened,
DATEADD(mi, h.duration, (CAST(DATEADD(day, -1, CAST(@Date AS DATE)) AS DATETIME) + CAST(h.opentime AS DATETIME))) AS Closed
FROM Process.OpeningHours h
WHERE h.daynumber = CASE WHEN DATEPART(dw, @Date) = 1
THEN 7
ELSE DATEPART(dw, @Date) - 1
END
AND businessid = @businessid
) w
WHERE @Date BETWEEN Opened AND Closed
RETURN @isopen;
END;
最佳答案
SQL Server 的日期/时间函数很糟糕。这是一个查询,首先获取今天工作日的开放时间并将其转换为今天的开放时间(例如,星期四 13:00 到 2017-03-30 13:00)。然后用它来检查它现在是否打开。
select count(*) as isopen
from
(
select
cast(cast(getdate() as date) as datetime) + opentime as opendatetime,
duration
from process.openinghours
where daynumber = datepart(dw, getdate())
) as today
where getdate() between opendatetime and dateadd(mi, duration, opendatetime);
您可以简单地将其应用到您的函数中,以使其更具可读性和方便性。
更新:我们也必须考虑昨天,以获得午夜以上的开放时间。感谢 Serg 指出了这一点。不幸的是,查询变得更加复杂:
select count(*) as isopen
from
(
select -- today's opening hours
cast(cast(getdate() as date) as datetime) + opentime as opendatetime,
duration
from process.openinghours
where daynumber = datepart(dw, getdate())
union all
select -- yesterday's opening hours
cast(dateadd('d', -1, cast(getdate() as date)) as datetime) + opentime as opendatetime,
duration
from process.openinghours
where daynumber % 7 + 1 = datepart(dw, getdate())
) as today_and yesterday
where getdate() between opendatetime and dateadd(mi, duration, opendatetime);
更新2:这是一个简化版本,我将所有开放时间转换为过去六天加上今天(希望我的数学正确)。我还添加了在上面的查询中忘记的businessid。
select count(*) as isopen
from
(
select
cast(dateadd('d', - (7 + datepart(dw, getdate()) - daynumber) % 7, cast(getdate() as date)) as datetime) + opentime as opendatetime,
duration
from process.openinghours
where businessid = @businessid
) as these_last_seven_days
where getdate() between opendatetime and dateadd(mi, duration, opendatetime);
关于sql - 在 T-SQL 中查找企业当前是否营业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43114296/
我试图通过这段代码读取未知数量的整数: while (1) { int c = getchar (); if (c == EOF) break;
我正试图找到一个类似于谷歌分析日期选择器的日期选择器: 知道 jQuery 是否提供了类似的东西吗? 最佳答案 这个 Twitter Bootstrap 风格的日期范围选择器非常接近。 https:/
我正在使用 javascript。如何获取当前 URL 的路径并将其分配给我的代码?这是我的代码: $(document).ready(function() { $(".share").hides
如何获得今天的Julian day number (JDN)相等的?或任何日期? 我看了又看,但只发现了一些产生“year-dayOfYear”的函数,而不是:2457854。 最佳答案 在 bash
我有相当简单的 UDP 服务器写在 c 上。 有时我需要知道在套接字中排队的所有 udp 数据包(字节)的当前长度。 据我了解,getsockopt 没有得到这样的信息。 欢迎使用 Linux 和 F
我一直在寻找几个小时来找到一个可以在图像中添加诸如“填充:5px”之类的东西的插件。每个人都通过纯 html 做到这一点吗?我们的客户需要一种方法来简单地使用按钮或右键单击上下文菜单来添加它。有什么建
是否有可能获得当前正在执行的 TCL 脚本的完整路径? 在 PHP 中,它将是:__FILE__ 最佳答案 根据“当前正在执行的 TCL 脚本”的含义,您实际上可能会寻找 info script ,甚
我最近从直接使用 ISession 转向了包装的 ISession,即工作单元类型模式。 我曾经使用 SQL Lite(内存中)对此进行测试。我有一个简单的帮助器类,它配置我的 SessionFact
我按照步骤操作 here在 WebStorm 中配置代码完成和其他内容,但我仍然收到以下语法错误。 我该如何解决这个问题? 最佳答案 通过相应地将“JavaScript 语言版本”(Settings/
我可以为我团队的 TFS 当前 Sprint 任务板添加书签吗?我们有两周的冲刺,因此 URL 每两周更改一次。 默认 URL 的形式为: http://[Server]/tfs/[Project]/
是否有 Subversion 命令可以显示当前版本号? 在svn checkout之后,我想启动一个脚本并需要变量中的修订号。如果有像 svn info get_revision_number 这样的
我正在编写表单的一个组件 首次安装组件时,sources={{}} ,一本空字典。由于该组件包装了现有的 Javascript 库,因此我正在实现一个自定义比较函数。为了让这个 diffing 函数
无论系统时间设置为多少以及机器所在的时区,我都需要正确的 UTC 时间。 (即使我必须打电话到互联网才能同步......) 是否有一些库或其他方法可以优雅地做到这一点? 最佳答案 如果您想获得准确可靠
我一边编码,一边拿出一些我和 friend 建立的旧网站来重新开始工作。我已经有一段时间没有做过任何 AJAX 了,当我试图找出我的代码失败的地方时,我发现没有显示很多资源。我猜这是因为我使用的是旧方
由于对性能的巨大影响,我从不怀疑我现在的桌面CPU是否有分支预测。当然可以。但各种 ARM 产品又如何呢? iPhone或Android手机有分支预测吗?较旧的任天堂 DS?基于 PowerPC 的
我有一个具有以下有效负载的 JWT: { "id": "394a71988caa6cc30601e43f5b6569d52cd7f6df", "jti": "394a71988caa6cc30
从其他一些帖子中,我能够通过以下方式获取当前 URI: 但是以下方法不起作用: 我很好奇为什么上面的方法不起作用,以及如何将当前 URI 分配给字符串。 最佳答案 每the javadocs ,g
我在表格 View 中有几个单元格。现在在任何给定的时间点,我想计算 View 中单元格的当前高度,即如果它是 View 的 3/4,它应该返回 (cellheight)*3/4 高度。 我通过以下方
这是网站的身份验证脚本。这安全吗?是最近的节目吗?它已经过时了吗?是否有“更好更安全的方法”我很新,但我没有看到太多地方使用 header 授权。 如有任何帮助,我们将不胜感激!这是我制作的第一个登录
我已经在其他 stackoverflow 线程上检查过这个错误,但在我的代码中没有发现任何错误。也许我累了,但我觉得还好。 网站.urls.py: from django.conf.urls impo
我是一名优秀的程序员,十分优秀!