SQL存储过程日期时间按位置过滤

祖扎纳

我在SQL Server中的存储过程有问题,有一个包含两个datetime列的表,一个基于位置和实体进行过滤的开始时间和结束时间。

在位置报告中,设置了“开始时间”和“结束时间”的过滤条件时,仅考虑以下位置记录:开始时间至少等于“过滤开始时间”且结束时间不晚于“过滤结束时间”建立报告。例如,实际上John Doe从8:30到9:30在A室,然后从9:30到10:30在B室,则从9:00到10:00的位置报告将不包含任何记录。约翰·多伊(John Doe)的下落。

理想的行为是应该包括在“结束时间”之前开始并在“开始时间”之后结束的位置间隔(与各个位置记录相反)。就表示方式而言,对于上述John Doe,输出报告应在9:00至9:30的A室和B室9:30到10:00的John Doe中显示John Doe,并在位置间隔的限制不在过滤约束之内。

这是可能吗?如果需要任何其他信息,请告知我,目前我正在使用基本的AND locationchangehistory.starttime> = Starttime AND locationchangehistory.endtime <= @Endtime)

这是公司正在使用的完整存储过程,我希望格式正确显示:-

                     @Asset Varchar (MAX) = NULL OUTPUT, 
                     @Location Varchar (MAX) = NULL OUTPUT,
                     @Ward Varchar (MAX) = NULL OUTPUT,
                     @Zone Varchar (MAX) = NULL OUTPUT,
                     @Floor Varchar (MAX) = NULL OUTPUT,
                     @Starttime datetime OUTPUT,
                     @Endtime datetime OUTPUT,
                     @Top int,
                     @FacilityID int

                     AS
                     SELECT DISTINCT TOP (@Top)


                     location.name AS 'Location', 
                     monitoredentity.name AS 'Asset', 
                     zone.name AS 'Zone', 
                     floor.name AS 'Floor',
                     ward.name AS 'Area',
                     locationchangehistory.starttime AS 'Starttime', 
                     locationchangehistory.endtime AS 'Endtime', 
                     CONVERT(varchar(max), DATEDIFF(SECOND, 
                     locationchangehistory.starttime, locationchangehistory.endtime) / 3600) + ':' + RIGHT('0' + CONVERT(varchar(2), DATEDIFF(SECOND, locationchangehistory.starttime, 
                     locationchangehistory.endtime) % 3600 / 60), 2) + ':' + RIGHT('0' + CONVERT(varchar(2), DATEDIFF(SECOND, locationchangehistory.starttime, 
                     locationchangehistory.endtime) % 60), 2) AS 'TimeInPlace' 


                     FROM floor INNER JOIN
                     zone ON zone.floor = floor.id INNER JOIN
                     ward ON zone.id = ward.zone INNER JOIN
                     location ON ward.id = location.ward INNER JOIN
                     locationchangehistory ON location.id =                        locationchangehistory.location INNER JOIN
                     monitoredentity ON monitoredentity.id =    locationchangehistory.entity




                     WHERE 


                     (monitoredentity.type  =  4 
               AND   floor.facilityid = @FacilityID
               AND   zone.facilityid = @FacilityID
               AND   ward.facilityid = @FacilityID
               AND   Location.facilityid = @FacilityID
               AND   locationchangehistory.facility = @FacilityID
               AND   monitoredentity.facilityid = @FacilityID 
               AND  charindex(',' + cast(monitoredentity.id AS VARCHAR(MAX))                  + ',', ',' + @Asset + ',') > 0
               AND locationchangehistory.starttime >= @Starttime 
               AND locationchangehistory.endtime <= @Endtime)
               AND ((charindex(',' + cast(Location.id AS VARCHAR(MAX)) + ',', ',' + @location + ',') > 0 OR  charindex(',' + cast(Ward.id AS VARCHAR(MAX)) + ',', ',' + @Ward + ',') > 0
               OR  charindex(',' + cast(zone.id AS VARCHAR(MAX)) + ',', ',' + @Zone + ',') > 0) OR  charindex(',' + cast(floor.id AS VARCHAR(MAX)) + ',', ',' + @Floor + ',') > 0)




                ORDER by locationchangehistory.starttime DESC`
wsDev

刚刚更改了选择范围,然后在哪里尝试了。

SELECT DISTINCT TOP (@Top)
                     location.name AS 'Location', 
                     monitoredentity.name AS 'Asset', 
                     zone.name AS 'Zone', 
                     floor.name AS 'Floor',
                     ward.name AS 'Area',
                     locationchangehistory.starttime AS 'Starttime', 
                     case when locationchangehistory.endtime<@Endtime then
                        locationchangehistory.endtime else
                            @Endtime
                        end  'Endtime', 
                     CONVERT(varchar(max), DATEDIFF(SECOND, 
                     locationchangehistory.starttime, locationchangehistory.endtime) / 3600) + ':' + RIGHT('0' + CONVERT(varchar(2), DATEDIFF(SECOND, locationchangehistory.starttime, 
                     locationchangehistory.endtime) % 3600 / 60), 2) + ':' + RIGHT('0' + CONVERT(varchar(2), DATEDIFF(SECOND, locationchangehistory.starttime, 
                     locationchangehistory.endtime) % 60), 2) AS 'TimeInPlace' 


                     FROM floor INNER JOIN
                     zone ON zone.floor = floor.id INNER JOIN
                     ward ON zone.id = ward.zone INNER JOIN
                     location ON ward.id = location.ward INNER JOIN
                     locationchangehistory ON location.id =                        locationchangehistory.location INNER JOIN
                     monitoredentity ON monitoredentity.id =    locationchangehistory.entity

               WHERE 

               locationchangehistory.starttime between locationchangehistory.starttime  and locationchangehistory.endtime
               AND locationchangehistory.endtime >= locationchangehistory.starttime

               AND (monitoredentity.type  =  4 
               AND   floor.facilityid = @FacilityID
               AND   zone.facilityid = @FacilityID
               AND   ward.facilityid = @FacilityID
               AND   Location.facilityid = @FacilityID
               AND   locationchangehistory.facility = @FacilityID
               AND   monitoredentity.facilityid = @FacilityID 
               AND  charindex(',' + cast(monitoredentity.id AS VARCHAR(MAX))                  + ',', ',' + @Asset + ',') > 0
               --AND locationchangehistory.starttime >= @Starttime 
               --AND locationchangehistory.endtime <= @Endtime)
               AND ((charindex(',' + cast(Location.id AS VARCHAR(MAX)) + ',', ',' + @location + ',') > 0 OR  charindex(',' + cast(Ward.id AS VARCHAR(MAX)) + ',', ',' + @Ward + ',') > 0
               OR  charindex(',' + cast(zone.id AS VARCHAR(MAX)) + ',', ',' + @Zone + ',') > 0) OR  charindex(',' + cast(floor.id AS VARCHAR(MAX)) + ',', ',' + @Floor + ',') > 0)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从存储过程返回日期时间

来自分类Dev

按日期时间过滤图表

来自分类Dev

如何按日期过滤日期时间字段?

来自分类Dev

日期时间与PostgreSQL,Python和存储过程

来自分类Dev

如何使用GoLang在Google App Engine数据存储中按日期/时间过滤

来自分类Dev

按bash中的日期和时间过滤

来自分类Dev

按日期时间过滤GraphQL文章

来自分类Dev

节点MongoDB本机-按日期/时间过滤

来自分类Dev

按日期时间过滤子文档

来自分类Dev

从字符串转换日期和/或时间时,运行SQL存储过程的掩盖错误失败

来自分类Dev

如何从存储过程返回的日期时间对象中获取日期?

来自分类Dev

SQL Server存储过程传递日期参数

来自分类Dev

SSIS SQL Server 2008 R2-传递给存储过程后,将日期时间存储到表中

来自分类Dev

SQL:按日期时间字段分组,但仅将日期与查询中其他位置使用的日期一起使用

来自分类Dev

按某个时间间隔(不是范围)过滤日期时间列表

来自分类Dev

使用PHP按日期过滤SQL结果

来自分类Dev

我的 sql 按最近日期过滤

来自分类Dev

如何在日期时间存储过程中使用BETWEEN

来自分类Dev

将日期时间值发送到存储过程

来自分类Dev

带有Postgresql,Python和存储过程的日期时间

来自分类Dev

使用group by为SQL过滤日期时间

来自分类Dev

存储过程过滤

来自分类Dev

搜索存储过程(SQL Server)-从字符串转换日期和/或时间时转换失败-简单吗?

来自分类Dev

SQL:按日期时间筛选数据

来自分类Dev

gnuplot:按日期过滤,不带xdata蜂鸣时间

来自分类Dev

在时间戳字段中按日期过滤结果

来自分类Dev

使用Linq按可为空的日期时间字段过滤

来自分类Dev

按开始和结束时间用日期过滤ArrayList

来自分类Dev

如何按日期时间优化数组过滤?

Related 相关文章

  1. 1

    从存储过程返回日期时间

  2. 2

    按日期时间过滤图表

  3. 3

    如何按日期过滤日期时间字段?

  4. 4

    日期时间与PostgreSQL,Python和存储过程

  5. 5

    如何使用GoLang在Google App Engine数据存储中按日期/时间过滤

  6. 6

    按bash中的日期和时间过滤

  7. 7

    按日期时间过滤GraphQL文章

  8. 8

    节点MongoDB本机-按日期/时间过滤

  9. 9

    按日期时间过滤子文档

  10. 10

    从字符串转换日期和/或时间时,运行SQL存储过程的掩盖错误失败

  11. 11

    如何从存储过程返回的日期时间对象中获取日期?

  12. 12

    SQL Server存储过程传递日期参数

  13. 13

    SSIS SQL Server 2008 R2-传递给存储过程后,将日期时间存储到表中

  14. 14

    SQL:按日期时间字段分组,但仅将日期与查询中其他位置使用的日期一起使用

  15. 15

    按某个时间间隔(不是范围)过滤日期时间列表

  16. 16

    使用PHP按日期过滤SQL结果

  17. 17

    我的 sql 按最近日期过滤

  18. 18

    如何在日期时间存储过程中使用BETWEEN

  19. 19

    将日期时间值发送到存储过程

  20. 20

    带有Postgresql,Python和存储过程的日期时间

  21. 21

    使用group by为SQL过滤日期时间

  22. 22

    存储过程过滤

  23. 23

    搜索存储过程(SQL Server)-从字符串转换日期和/或时间时转换失败-简单吗?

  24. 24

    SQL:按日期时间筛选数据

  25. 25

    gnuplot:按日期过滤,不带xdata蜂鸣时间

  26. 26

    在时间戳字段中按日期过滤结果

  27. 27

    使用Linq按可为空的日期时间字段过滤

  28. 28

    按开始和结束时间用日期过滤ArrayList

  29. 29

    如何按日期时间优化数组过滤?

热门标签

归档