SQL声明帮助:选择可交易的产品直到特定时间

发布于 2025-01-22 12:47:15 字数 1321 浏览 2 评论 0原文

假设我有一张存储订单ID和相应产品的表格 - 像这样:

Product ID | Order ID (PK) | Order Placement Date
Apple      | 2455          | 2022-04-18 13:55:50.100
Apple      | 2456          | 2022-04-18 15:50:40.100
Appel      | 3457          | 2022-04-18 17:59:10.100
Appel      | 3458          | 2022-04-18 18:40:10.100
Appel      | 3459          | 2022-04-19 09:30:50.100
Appel      | 3459          | 2022-04-19 14:20:20.100

我如何每天从昨天18:00 CET和18:00 CET之前的任何点进行交易的所有产品(订单)当天。因此,新产品在当天18:00之后可交易,首次放置在第二天选择数据?

另外订单放置日期将是dateTime - UTC TimeZoned,我正在与SQL Server 2019一起工作。

类似于

SELECT DISTINCT Product ID, Order ID, Order Placement Date
FROM TableName
Where Order Placement Date --> From yesterday 18h until today 18:00 CET

2022-04-18时选择结果样本:

Product ID | Order ID (PK) | Order Placement Date
Apple      | 2455          | 2022-04-18 13:55:50.100
Apple      | 2456          | 2022-04-18 15:50:40.100
Appel      | 3457          | 2022-04-18 17:59:10.100

选择结果在2022-04-19时采样:

Product ID | Order ID (PK) | Order Placement Date
Appel      | 3458          | 2022-04-18 18:40:10.100
Appel      | 3459          | 2022-04-19 09:30:50.100
Appel      | 3459          | 2022-04-19 14:20:20.100

Say I have a table which stores order IDs and the corresponding products - like this:

Product ID | Order ID (PK) | Order Placement Date
Apple      | 2455          | 2022-04-18 13:55:50.100
Apple      | 2456          | 2022-04-18 15:50:40.100
Appel      | 3457          | 2022-04-18 17:59:10.100
Appel      | 3458          | 2022-04-18 18:40:10.100
Appel      | 3459          | 2022-04-19 09:30:50.100
Appel      | 3459          | 2022-04-19 14:20:20.100

How can I get on a daily basis all the products that are tradable ( Orders placed ) at any point from 18:00 CET yesterday and before 18:00 CET that day. So new products tradable after 18:00 CET on that day, placed for the first time, must be included in the next day select data ?

Also Order Placement Date would be a DateTime - UTC timezoned and I'm working with SQL Server 2019.

Something like

SELECT DISTINCT Product ID, Order ID, Order Placement Date
FROM TableName
Where Order Placement Date --> From yesterday 18h until today 18:00 CET

Select result sample when in 2022-04-18 :

Product ID | Order ID (PK) | Order Placement Date
Apple      | 2455          | 2022-04-18 13:55:50.100
Apple      | 2456          | 2022-04-18 15:50:40.100
Appel      | 3457          | 2022-04-18 17:59:10.100

Select result sample when in 2022-04-19 :

Product ID | Order ID (PK) | Order Placement Date
Appel      | 3458          | 2022-04-18 18:40:10.100
Appel      | 3459          | 2022-04-19 09:30:50.100
Appel      | 3459          | 2022-04-19 14:20:20.100

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

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

发布评论

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

评论(2

风向决定发型 2025-01-29 12:47:15

这有点混乱,但应该起作用。我怀疑可能有一种丑陋的方法,但是哦。但是,我确实假设您正在使用最新版本的SQL Server,该版本在时区域上支持

首先,我们得到当前日期;我假设“今天”是从服务器的角度来看的。然后,我们转换为date(要删除时间),然后返回到datetime2,因此我们可以在时区使用,我们声称是中欧。 SQL Server自动处理DST,并假定dateTime2值对时区是正确的。然后,我们将该时间更改为UTC和转换为dateTime,因此列上没有隐含的转换。然后,我们在上边界上执行相同的操作,但是在8个小时内添加了:

CREATE TABLE dbo.YourTable (ProductID int,
                            OrderID int,
                            OrderPlacementDate datetime); --Per question, this is UTC

INSERT INTO dbo.YourTable (ProductID,
                           OrderID,
                           OrderPlacementDate)
VALUES(2,2,'20220418 21:48:59.123'), --Would be 2022-04-18 23:48:59.123 CEST
      (1,1,'20220418 22:15:17.167'), --Would be 2022-04-19 00:15:17.167 CEST
      (1,1,'20220419 15:15:17.167'), --Would be 2022-04-19 17:15:17.167 CEST
      (1,1,'20220419 16:15:17.167'); --Would be 2022-04-19 18:15:17.167 CEST
GO


SELECT *
FROM dbo.YourTable
WHERE OrderPlacementDate >= CONVERT(datetime,(CONVERT(datetime2(3),CONVERT(date,GETDATE())) AT TIME ZONE 'Central Europe Standard Time') AT TIME ZONE 'UTC')
  AND OrderPlacementDate <= CONVERT(datetime,DATEADD(HOUR,18,CONVERT(datetime2(3),CONVERT(date,GETDATE())) AT TIME ZONE 'Central Europe Standard Time' AT TIME ZONE 'UTC'))
GO

DROP TABLE dbo.YourTable

This is a little messy, but should work. I suspect there might be a less ugly method but oh well. I do, however, assume you are using a recent version of SQL Server, which supports AT TIME ZONE.

First we get the current date; I am assuming that "today" is from the server's perspective. Then we CONVERT to a date (to remove the time) and then back to a datetime2, so we can use AT TIME ZONE, which we state to be Central Europe. SQL Server which automatically process for DST and it'll assume the datetime2 value was correct for the timezone. Then we change that time to UTC and CONVERT back to a datetime so that there is no implicit conversions on the column. Then we do the same for the upper boundary, but with 8 hours added on:

CREATE TABLE dbo.YourTable (ProductID int,
                            OrderID int,
                            OrderPlacementDate datetime); --Per question, this is UTC

INSERT INTO dbo.YourTable (ProductID,
                           OrderID,
                           OrderPlacementDate)
VALUES(2,2,'20220418 21:48:59.123'), --Would be 2022-04-18 23:48:59.123 CEST
      (1,1,'20220418 22:15:17.167'), --Would be 2022-04-19 00:15:17.167 CEST
      (1,1,'20220419 15:15:17.167'), --Would be 2022-04-19 17:15:17.167 CEST
      (1,1,'20220419 16:15:17.167'); --Would be 2022-04-19 18:15:17.167 CEST
GO


SELECT *
FROM dbo.YourTable
WHERE OrderPlacementDate >= CONVERT(datetime,(CONVERT(datetime2(3),CONVERT(date,GETDATE())) AT TIME ZONE 'Central Europe Standard Time') AT TIME ZONE 'UTC')
  AND OrderPlacementDate <= CONVERT(datetime,DATEADD(HOUR,18,CONVERT(datetime2(3),CONVERT(date,GETDATE())) AT TIME ZONE 'Central Europe Standard Time' AT TIME ZONE 'UTC'))
GO

DROP TABLE dbo.YourTable

db<>fiddle

哭泣的笑容 2025-01-29 12:47:15

该怎么办:

SELECT *
FROM TableName
WHERE [Order Placement Date] < DATETIMEFROMPARTS(YEAR(GETDATE()),MONTH(GETDATE()),DAY(GETDATE()),18,0,0,0)
AND [Order Placement Date] >= DATETIMEFROMPARTS(YEAR(DATEADD(day, -1, GETDATE())),MONTH(DATEADD(day, -1, GETDATE())),DAY(DATEADD(day, -1, GETDATE())),18,0,0,0)

What about :

SELECT *
FROM TableName
WHERE [Order Placement Date] < DATETIMEFROMPARTS(YEAR(GETDATE()),MONTH(GETDATE()),DAY(GETDATE()),18,0,0,0)
AND [Order Placement Date] >= DATETIMEFROMPARTS(YEAR(DATEADD(day, -1, GETDATE())),MONTH(DATEADD(day, -1, GETDATE())),DAY(DATEADD(day, -1, GETDATE())),18,0,0,0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文