微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 SQL 中格式化日期时间时,有没有办法保留语言环境?

如何解决在 SQL 中格式化日期时间时,有没有办法保留语言环境?

我有一个包含对象的表,该表定义了一些端点来测试(结构)的延迟,一个表保存结果(延迟),以及每个“目标”的表(端点被测试的地方)

我想从它被击中的所有位置获取特定结构的每个时间间隔的平均延迟

该结构下每个目标的时间戳以 10 分钟为间隔(相差约 1 秒)

我在按时间戳分组时遇到问题,其中格式化时间戳(以消除第二个差异)并将其转换回日期时间会丢失它的语言环境(以 UTC 存储,但似乎在本地时间出现)

有什么更好的方法可以解决这个问题,或者有什么方法可以格式化日期 s.t.区域设置是否保留?

SELECT 
    structure.Id as TargetId,Avg(latencies.Latency) as Latency,latencies.Timestamp
    FROM  StructureTable structure
    INNER JOIN TargetsTable targets on targets.StructureId = structure.Id
    Outer apply
    (
        Select
            CAST(format(Timestamp,'yyyy-MM-dd hh:mm:00') as DATETIME) as Timestamp,Latency,TargetId
        from LatenicesTable
        where TargetId = targets.Id and Timestamp < endDate and Timestamp > startDate
    ) as latencies
    where structure.Id = structureId and latencies.Latency > 0
    group by latencies.Timestamp
    order by latencies.Timestamp asc

编辑: 结果示例 w/ 格式 + Cast

Id  Latency Timestamp
22  546     2021-05-09 01:00:00.000
22  540     2021-05-09 01:10:00.000
22  535     2021-05-09 01:20:00.000
22  543     2021-05-09 01:30:00.000
22  551     2021-05-09 01:40:00.000
22  546     2021-05-09 01:50:00.000

并且没有

Id  Latency Timestamp
22  548     2021-05-09 05:00:02.313
22  471     2021-05-09 05:00:02.453
22  619     2021-05-09 05:00:02.547
22  607     2021-05-09 05:00:02.593
22  477     2021-05-09 05:00:02.937
22  561     2021-05-09 05:10:01.627
22  641     2021-05-09 05:10:01.657
22  470     2021-05-09 05:10:01.673
22  481     2021-05-09 05:10:01.893
22  617     2021-05-09 05:10:02.000

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。