Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在Where Time子句中选择多个值等于_Sql_Sql Server_Sql Server 2005_Where Clause - Fatal编程技术网

Sql 在Where Time子句中选择多个值等于

Sql 在Where Time子句中选择多个值等于,sql,sql-server,sql-server-2005,where-clause,Sql,Sql Server,Sql Server 2005,Where Clause,我的桌子看起来像这样: CONVERT(VARCHAR(8), DATEADD(mi, 10, time), 114) 表1: 注意:这个表实际上非常大,有更多的列和数百万行 | Time | tmp | productionID | | 10:00:00 | 2.2 | 5 | | 10:00:05 | 5.2 | 5 | | 10:00:11 | 7.4 | 5

我的桌子看起来像这样:

 CONVERT(VARCHAR(8), DATEADD(mi, 10, time), 114)
表1: 注意:这个表实际上非常大,有更多的列和数百万行

 |  Time      |  tmp   |  productionID  |
 |  10:00:00  |   2.2  |    5           |
 |  10:00:05  |   5.2  |    5           |
 |  10:00:11  |   7.4  |    5           |
 |  ......    |   3.2  |    5           |
 |  10:10:02  |   4.5  |    5           |
注意:TIMECHAR是一个varchar,所以我假设我需要这样做:

 CONVERT(VARCHAR(8), DATEADD(mi, 10, time), 114)
我需要做的是:

 select time, tmp 
 from mytable
 where productionid = somevalue 
 and time = first_time_stamp associated to some productionID(ie. 10:00:00 table above)
     time = 10 minutes after the first_time_stamp with some productionID
     time = 20 minutes after the first_time_stamp with some productionID
     ...25, 30, 40, 60, 120, 180 minutes 
我希望这是有道理的。我不确定做这件事的正确方法是什么。我的意思是我想到了以下过程: -选择带有某些productionID的第一时间戳 -再加上10分钟, -加上20分钟等等。。然后使用透视表并使用联接链接到表1 一定有更简单的办法

提前感谢您的专业知识

预期的样本输出:

    |  Time    | tmp
    | 10:00:00 | 2.2 
    | 10:10:02 | 4.5
    | 10:20:54 | 2.3 
    | 10:30:22 | 5.3

如果动态创建一个间隔表,并将其与每个ProductionID的开始时间交叉连接,则可以从属于同一类别的MyTable中提取记录,并选择仅检索第一个记录

; with timeSlots (startSlot, endSlot) as (
  select 0, 10
  union all
  select 10, 20
  union all
  select 25, 30
  union all
  select 30, 40
  union all
  select 40, 60
  union all
  select 60, 120
  union all
  select 120, 180
),
startTimes (ProductionID, minTime) as (
  select ProductionID, min([Time])
    from MyTable
   group by ProductionID
),
groupedTime (ProductionID, [Time], [Tmp], groupOrder) as (
  select myTable.ProductionID,
         myTable.Time,
         myTable.Tmp,
         row_number () over (partition by myTable.productionid, timeSlots.startSlot
                           order by mytable.Time) groupOrder
    from startTimes
   cross join timeslots
   inner join myTable
      on startTimes.ProductionID = myTable.ProductionID
     and convert(varchar(8), dateadd(minute, timeSlots.startSlot, convert(datetime, startTimes.MinTime, 114)), 114) <= mytable.Time
     and convert(varchar(8), dateadd(minute, timeSlots.endSlot, convert(datetime, startTimes.MinTime, 114)), 114) > myTable.Time
)
select ProductionID, [Time], [Tmp]
  from groupedTime
 where groupOrder = 1

.

如果时间中没有缺失序列,则此操作将起作用。我的意思是,如果每10分钟的增量都有时间值,那么这就行了

create table times([Time] time,tmp float,productionID int)
INSERT INTO times
VALUES('10:10:00',2.2,5),
      ('10:00:05',5.2,5),         
      ('10:00:11',7.4,5),
      ('10:00:18',3.2,5),         
      ('10:10:02',4.5,5),
      ('10:20:22',5.3,5)

select * from times

Declare @min_time time
select @min_time = MIN(time) from times

;WITH times1 as (select row_number() over (order by (select 0)) as id,time, tmp from times where productionID = 5)
,times2 as(
select id,time,tmp from times1 where time=@min_time 
union all
select t1.id,t1.time,t1.tmp from times2 t2 inner join times1 t1 on cast(t1.time as varchar(5))=cast(DATEADD(mi,10,t2.Time) as varchar(5)) --where t1.id-1=t2.id
)

,result as (select MAX(time) as time from times2
group by CAST(time as varchar(5)))

select distinct t2.Time,t2.tmp from times2 t2,Result r where t2.Time =r.time order by 1

你能展示一些示例输出吗?@Aushin如上所示-我已将其添加到我的帖子中。因此,我注意到时间间隔不是10分钟。你在找至少10分钟后的第一个吗?另外,你的具体目标是10、20、25、30、40、60、120、180吗?这种模式似乎很奇怪。数据中会有缺口吗?如果10点20分没有时间怎么办。。。而是说下一次是10:23。。。那会是返回的记录吗?还有,你需要担心几天吗?将日期/时间/时间戳值存储为字符串数据在任何形式下都没有任何帮助-最好将其转换为更正确的类型。代码非常好-有几个问题,因为我的sql知识没有你在groupdedTime中的sql知识那么好,在转换语句中,你为什么放:myTable。我看到发生了什么,您将时间段中的时间添加到最小时间,我不明白为什么我们在之后使用运算符。为什么这只适用于我的一些ProductionID,我没有仔细查看,可能是因为我的巨表中的间隔时间不匹配right@akwarywo抱歉耽搁了。此条件用于消除不属于当前时隙的myTable.Time,以便在该范围内的行中拾取行数。否则,每个时隙中都会有minTime,并且会有七行相同的数据。你能发布这个查询有问题的数据吗?我想自己检查一下。@akwarywo哦,时间段有一个错误-应该有一个是20,25。