两个sql查询的不同输出有一个row_number()语句

时间:2015-02-25 10:33:11

标签: sql-server inner-join row-number

我有两个SQL查询,一个是row_number(),另一个是没有它。我知道通过使用row_number和partition by语句,不会发生重复值。但是当我执行两个查询然后是row_number的一些行时也没有显示没有重复值的查询。 带分区的第一个SQL查询是

;WITH cte
     AS (SELECT b.logtime,
                b.beam_current,
                b.beam_energy,
case 
                         when a.st2_vs2_bag1_onoff=0 then c.st2_vs2_bag1_rb ELSE 0 END as st2_vs2_bag1_rb,
                         CASE when a.st2_vs2_bag2_onoff=0  then c.st2_vs2_bag2_rb else '0' END as st2_vs2_bag2_rb,
                         CASE when a.st2_vs2_bag3_onoff=0  then c.st2_vs2_bag3_rb else '0' END as st2_vs2_bag3_rb,
                         CASE when a.st2_vs2_bag4_onoff=0  then c.st2_vs2_bag4_rb else '0' END as st2_vs2_bag4_rb,
                         CASE when a.st2_vs2_bag5_onoff=0  then c.st2_vs2_bag5_rb else '0' END as st2_vs2_bag5_rb,
                         CASE when a.st2_vs2_bag6_onoff=0  then c.st2_vs2_bag6_rb else '0' END as st2_vs2_bag6_rb,
                         CASE when a.st2_vs2_bag7_onoff=0  then c.st2_vs2_bag7_rb else '0' END as st2_vs2_bag7_rb,
                         CASE when a.st2_vs2_bag8_onoff=0  then c.st2_vs2_bag8_rb else '0' END as st2_vs2_bag8_rb,
                         CASE when a.st2_vs2_bag9_onoff=0  then c.st2_vs2_bag9_rb else '0' END as st2_vs2_bag9_rb ,
                         Row_number() OVER(partition BY b.beam_current
                    ORDER BY b.logtime) RN
         from INDUS2_BDS.dbo.DCCT b INNER JOIN(INDUS2_VACUUM.dbo.main_vacuum_analog c inner join INDUS2_VACUUM.dbo.main_vacuum_status a on c.logtime=a.logtime)
                        ON a.LOGTIME = b.LOGTIME
                         WHERE b.logtime between '2014-10-10 07:17:00' and '2014-10-10 08:46:00' and b.beam_current in('10.00','20.02','30.01','39.99','50.00','59.99','70.00','80.01','89.99','99.98','110.06','120.01','129.98','140.02','150.00','159.99','170.00','180.00','189.99','199.97','209.98') 
                AND b.beam_energy BETWEEN '550' AND '551')

                Select logtime,beam_current,beam_energy,st2_vs2_bag1_rb,st2_vs2_bag2_rb,st2_vs2_bag3_rb,st2_vs2_bag4_rb,st2_vs2_bag5_rb,st2_vs2_bag6_rb,st2_vs2_bag7_rb,st2_vs2_bag8_rb,st2_vs2_bag9_rb
                from cte where rn=1 order by logtime

它的输出是 enter image description here

具有相同列值和内部连接和条件的其他sql查询是 -

select b.beam_current, b.beam_energy,case 
                         when a.st2_vs2_bag1_onoff=0 then c.st2_vs2_bag1_rb ELSE 0 END as st2_vs2_bag1_rb,
                         CASE when a.st2_vs2_bag2_onoff=0  then c.st2_vs2_bag2_rb else '0' END as st2_vs2_bag2_rb,
                         CASE when a.st2_vs2_bag3_onoff=0  then c.st2_vs2_bag3_rb else '0' END as st2_vs2_bag3_rb,
                         CASE when a.st2_vs2_bag4_onoff=0  then c.st2_vs2_bag4_rb else '0' END as st2_vs2_bag4_rb,
                         CASE when a.st2_vs2_bag5_onoff=0  then c.st2_vs2_bag5_rb else '0' END as st2_vs2_bag5_rb,
                         CASE when a.st2_vs2_bag6_onoff=0  then c.st2_vs2_bag6_rb else '0' END as st2_vs2_bag6_rb,
                         CASE when a.st2_vs2_bag7_onoff=0  then c.st2_vs2_bag7_rb else '0' END as st2_vs2_bag7_rb,
                         CASE when a.st2_vs2_bag8_onoff=0  then c.st2_vs2_bag8_rb else '0' END as st2_vs2_bag8_rb,
                         CASE when a.st2_vs2_bag9_onoff=0  then c.st2_vs2_bag9_rb else '0' END as st2_vs2_bag9_rb 
                        from INDUS2_BDS.dbo.DCCT b INNER JOIN(INDUS2_VACUUM.dbo.main_vacuum_analog c inner join INDUS2_VACUUM.dbo.main_vacuum_status a on c.logtime=a.logtime)
                        ON a.LOGTIME = b.LOGTIME and (b.beam_current like '%9.96' or b.beam_current like '%9.97' or  b.beam_current like '%9.98' or  b.beam_current like '%9.99'  or  b.beam_current like '%0' or b.beam_current like '%_0.01' or  b.beam_current like '%_0.02' or  b.beam_current like '%_0.03' or  b.beam_current like '%_0.04' or  b.beam_current like '%_0.05' or  b.beam_current like '%_0.06')
                        and b.logtime between '2014-10-10 07:17:00' and '2014-10-10 08:46:00' and b.beam_current in('10.00','20.02','30.01','39.99','50.00','59.99','70.00','80.01','89.99','99.98','110.06','120.01','129.98','140.02','150.00','159.99','170.00','180.00','189.99','199.97','209.98') order by b.beam_current

其输出是 - enter image description here

AS从sql查询我们可以看到查询都有相同的内连接和条件。但是输出是不同的。这个原因无法理解。

0 个答案:

没有答案