我有一个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
通过此查询输出的示例是:
我想要一行其beam_current值最接近10的行,但我有很多行,其值接近10.如何解决这个问题??
答案 0 :(得分: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