2015-01-13 26 views
-1

我有一个SQL查询,通过它我从三个表中检索所有匹配的logtime和每当列st1_vs1_bag4_onoff ='0',然后对应的列在不同的列表格st1_vs1_bag4_rb将显示其值,否则0将显示在st1_vs1_bag4_rb中。如何查找最接近给定列值的行

beam_current列也被检索。现在我只想显示那些非常接近beam_current 10,20,30 ...到220的行。只有一行对应于每个值。

我有一个SQL查询,它将检索beam_current接近10,20,30至220的行,但会检索与每个值对应的许多行。

我的SQL查询:

select 
    b.LOGTIME, b.beam_current, b.beam_energy, 
    case 
     when a.st1_vs1_bag1_onoff = 0 and a.logtime = c.logtime 
      then c.st1_vs1_bag1_rb 
      else 0 
    end as st1_vs1_bag1_rb, 
    case 
     when a.st1_vs1_bag2_onoff = '0' and a.logtime = c.logtime 
      then c.st1_vs1_bag2_rb 
      else '0' 
    end as st1_vs1_bag2_rb , 
    case 
     when a.st1_vs1_bag3_onoff = '0' and a.logtime = c.logtime 
      then c.st1_vs1_bag3_rb 
      else '0' 
    end as st1_vs1_bag3_rb, 
    case 
     when a.st1_vs1_bag4_onoff = '0' and a.logtime = c.logtime 
      then c.st1_vs1_bag4_rb 
      else '0' 
    end as st1_vs1_bag4_rb , 
    case 
     when a.st1_vs1_bag5_onoff = '0' and a.logtime = c.logtime 
      then c.st1_vs1_bag5_rb 
      else '0' 
    end as st1_vs1_bag5_rb , 
    case 
     when a.st1_vs1_bag6_onoff = '0' and a.logtime = c.logtime 
      then c.st1_vs1_bag6_rb 
      else '0' 
    end as st1_vs1_bag6_rb , 
    case 
     when a.st1_vs1_bag7_onoff = '0' and a.logtime = c.logtime 
      then c.st1_vs1_bag7_rb 
      else '0' 
    end as st1_vs1_bag7_rb , 
    case 
     when a.st1_vs1_bag8_onoff = '0' and a.logtime = c.logtime 
      then c.st1_vs1_bag8_rb 
      else '0' 
    end as st1_vs1_bag8_rb 
from 
    INDUS2_BDS.dbo.DCCT b 
inner join 
    (main_vacuum_analog c 
inner join 
    main_vacuum_status a on c.logtime = a.logtime) ON a.LOGTIME = b.LOGTIME 
      and (cast(cast(b.beam_current as decimal) % 10.0 as real) <= 0.01) OR (cast(cast  (b.beam_current as decimal)% 10.0 as real)>= 9.99)) 
and (cast(cast(b.beam_current as decimal)as real) >= 9.99) -- to set the lower limit of 9.99 
and(cast(cast(b.beam_current as decimal)as real) <= 220.10) 
and b.logtime between '2014-10-10 07:17:00' and '2014-10-10 08:46:00' 

这检索与大约50行值附近10等,以这种方式获取周围459行的所有值,直到220可我只是一个检索最靠近每个值的行,这样我就可以检索到22行?

编辑1

样品通过这个查询的输出是:

enter image description here

我想单行其beam_current值最接近10,但我得到了许多行,其值接近10.如何解决这个问题?

+0

是否可以张贴一些示例数据为3个表,输出应该从样本数据是什么? – Tak

回答

0

根据您的意见,看起来您需要预处理您的DCCT表格,以便为您提供LogTime组中最大的值(10,20,30,40 ...)。

如果我的假设是正确的,您需要进入子查询,它为您提供LogTime的Beam_Current,然后加入到其他表中。

希望下面的代码有帮助。

declare @DCCT table (LOGTIME datetime, beam_current varchar(10), beam_energy numeric(10,5)) 

insert into @DCCT (LOGTIME, beam_current, beam_energy) 
select '2014-10-10 07:29:31', '9.5',551.0052 
union all 
select '2014-10-10 07:29:32', '9.61',550.9918 
union all 
select '2014-10-10 07:29:33', '9.91',551.0052 
union all 
select '2014-10-10 07:29:34', '9.78',550.9918 
union all 
select '2014-10-10 07:29:35', '19.5',551.0052 
union all 
select '2014-10-10 07:29:36', '12.61',550.9918 
union all 
select '2014-10-10 07:29:37', '15.91',551.0052 
union all 
select '2014-10-10 07:29:38', '35.5',551.0052 
union all 
select '2014-10-10 07:29:39', '37.61',550.9918 
union all 
select '2014-10-10 07:29:40', '210.91',551.0052 
union all 
select '2014-10-10 07:29:41', '216.91',551.0052 


if OBJECT_ID('tempdb..#Temp_DCCT') is not null 
    drop table #Temp_DCCT 

select * 
, case 
    when cast(beam_current as real) < 10 then 1 
    when cast(beam_current as real) >= 10 and cast(beam_current as real) < 20 then 2 
    when cast(beam_current as real) >= 20 and cast(beam_current as real) < 30 then 3 
    when cast(beam_current as real) >= 30 and cast(beam_current as real) < 40 then 4 
    when cast(beam_current as real) >= 40 and cast(beam_current as real) < 50 then 5 
    -- add all other groups here ..... 
    else 6 -- change else to last group 
    end as beam_current_grp 
into #Temp_DCCT 
from @DCCT 
where LOGTIME between '2014-10-10 07:17:00' and '2014-10-10 08:46:00' 


select * 
from 
(
    select * 
     , ROW_NUMBER() over (partition by beam_current_grp order by beam_current_grp ASC, beam_current DESC) as rownum 
    from #Temp_DCCT 
) T_DCCT 
where rownum = 1 

从我用来生产数据的结果DCCT数据将是:

LOGTIME      beam_current beam_energy  beam_current_grp rownum 
2014-10-10 07:29:33.000  9.91   551.00520  1     1 
2014-10-10 07:29:35.000  19.5   551.00520  2     1 
2014-10-10 07:29:39.000  37.61   550.99180  4     1 
2014-10-10 07:29:41.000  216.91   551.00520  6     1