找到所有三个值都增加的第一行

时间:2018-05-02 19:06:52

标签: sql sql-server sql-server-2016

鉴于此数据集

declare @TestData Table
(
  Time int,
  Temp_Front int, 
  Temp_Center int, 
  Temp_Back int
);

insert into @TestData 
values 
    (2, 26, 27, 27), 
    (3, 26, 28, 27), 
    (4, 27, 28, 27), 
    (5, 27, 28, 28);


select Time ,
  Temp_Front , 
  Temp_Center , 
  Temp_Back   from @TestData

我需要找到第一行,其中所有三个Temp值都大于第一行的临时值。

所以Temp_Front> 26,Temp_centre> 27,Temp_back> 27.

我尝试过很多东西,但我总是会遇到很多变数。而且很乱。

我不会用这个弄脏图片。所以我只是将问题和数据集包括在内。

6 个答案:

答案 0 :(得分:2)

计算行号并使用条件聚合来获取第一行的值。然后可以将其用于比较,以获得符合使用TOPORDER BY所述标准的第一行。

with first_row as 
(
  select t.*, row_number() over(order by time) as rnum from @TestData t
)
select top 1 Time, Temp_Front, Temp_Center, Temp_Back
from 
    (
        select f.*, 
                max(case when rnum=1 then Temp_Front end) over() as first_tempfront,
                max(case when rnum=1 then Temp_Center end) over() as first_tempcenter,
                max(case when rnum=1 then Temp_Back end) over() as first_tempback
        from first_row f
    ) Dummy
where 
  temp_front > first_tempfront and temp_center > first_tempcenter and temp_back > first_tempback
order by rnum

答案 1 :(得分:2)

有趣。我会这样做:

with first_row as (
      select top (1) td.*
      from @testdata td
      order by time
     )
select top (1) td.*
from @testdata td cross join
     first_row fr
where td.temp_front > fr.temp_front and
      td.temp_center > fr.temp_center and
      td.temp_back > fr.temp_back
order by time asc;

我发现窗口功能对这个问题特别有帮助。

如果您要使用窗口功能:

select top (1) td.*
from (select td.*,
             first_value(temp_front) over (order by time) as fr_temp_front,
             first_value(temp_center) over (order by time) as fr_temp_center,
             first_value(temp_back) over (order by time) as fr_temp_back
      from @testdata td
     ) td
where td.temp_front > fr_temp_front and
      td.temp_center > fr_temp_center and
      td.temp_back > fr_temp_back
order by time asc;

答案 2 :(得分:2)

对于迟到的回复感到抱歉。你可以用这个:

Error: Cannot find module 'react-transform-hmr/lib/index.js'
at Function.Module._resolveFilename (module.js:325:15)
at Function.require.resolve (internal/module.js:16:19)
at makeMakeHMRConfig7 (/home/local/MPLLC/malwinder.singh/demoApp/node_modules/react-native/node_modules/metro/src/babel-bridge.js:120:31)
at Object.<anonymous> (/home/local/MPLLC/malwinder.singh/demoApp/node_modules/react-native/node_modules/metro/src/babel-bridge.js:49:24)
at Module._compile (module.js:409:26)
at Module._extensions..js (module.js:416:10)
at Object.require.extensions.(anonymous function) [as .js] (/home/local/MPLLC/malwinder.singh/demoApp/node_modules/react-native/node_modules/babel-register/lib/node.js:152:7)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)

完整测试查询:https://pastebin.com/3Hx33SfX

答案 3 :(得分:0)

加入仅包含第一行的派生表,并检查WHERE子句或JOIN条件中的所有三列。使用TOP 1仅获取满足条件的第一行。

答案 4 :(得分:0)

如果,我理解正确您也可以使用 temp 值的SqlDataAdapter du = new SqlDataAdapter("Select * From ogrenciler Where adsoyad = @CombBox2", con); du.SelectCommand.Parameters.Add("@ComboBox2", SqlDbType.NVarChar, 20).Value = comboBox2.SelectedValue.ToString(); DataSet dy = new DataSet();

sum

答案 5 :(得分:0)

这是一个使用循环的解决方案:

declare @minrow int = (select min(time) from @testdata)
declare @iterator int = @minrow+1
declare @tempfront varchar(max)
declare @tempcenter varchar(max)
declare @tempback varchar(max)
declare @originaltempfront varchar(max)=(select  temp_front from  @testdata where time=@minrow)
declare @originaltempcenter varchar(max)=(select  temp_center from  @testdata where time=@minrow)
declare @originaltempback varchar(max) =(select  temp_back from  @testdata where time=@minrow)

while @iterator<= (select max(time) from @testdata)
begin
select @tempfront= temp_front from  @testdata where time=@iterator
select @tempcenter= temp_center from  @testdata where time=@iterator
select @tempback= temp_back from  @testdata where time=@iterator

if exists(select 1 from @testdata where @tempfront>@originaltempfront and @tempback>@originaltempback
and @tempcenter>@originaltempcenter and time=@iterator)
select * from @testdata where @tempfront>@originaltempfront and @tempback>@originaltempback
and @tempcenter>@originaltempcenter and time=@iterator
if @@rowcount <> 0 break

set @iterator=@iterator+1
end