SQL声明帮助:选择可交易的产品直到特定时间
假设我有一张存储订单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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这有点混乱,但应该起作用。我怀疑可能有一种丑陋的方法,但是哦。但是,我确实假设您正在使用最新版本的SQL Server,该版本在时区域上支持
。
首先,我们得到当前日期;我假设“今天”是从服务器的角度来看的。然后,我们
将
转换为date
(要删除时间),然后返回到datetime2
,因此我们可以在时区使用,我们声称是中欧。 SQL Server自动处理DST,并假定
dateTime2
值对时区是正确的。然后,我们将该时间更改为UTC和将
转换为dateTime
,因此列上没有隐含的转换。然后,我们在上边界上执行相同的操作,但是在8个小时内添加了: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 adate
(to remove the time) and then back to adatetime2
, so we can useAT TIME ZONE
, which we state to be Central Europe. SQL Server which automatically process for DST and it'll assume thedatetime2
value was correct for the timezone. Then we change that time to UTC andCONVERT
back to adatetime
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:db<>fiddle
该怎么办:
What about :