从 sql 查询中的所有可用列中删除仅一个列值的重复值

remove duplicate values of only one column value from all the available columns in sql query

我有一个包含三列的 sql 查询。我想删除 beam_current column.How 中的任何重复值退出以在 so.I 中工作 sql-server2012

我使用了 Distinct,但后来我也得到了 beam_current 的重复值。 我的 sql 查询是-

select DISTINCT (beam_current), logtime, beam_energy 
from INDUS2_BDS.dbo.DCCT 
where logtime between '2014-08-09 01:13:03' and '2014-08-09 02:16:53'
      and (beam_current like '%9.96' 
           or beam_current like '%9.97' 
           ... etc ...) 
      and beam_energy between '550' and '552'

EDIT-1 我的输出是-

第一列 29.98 重复 thrice.I 只需要与 29 对应的行中的任何一个。98.How 这样做??

distinct 关键字作用于整行(所有列),因此:

select DISTINCT (beam_current), logtime, beam_energy 

等同于:

select DISTINCT beam_current, logtime, beam_energy 

等同于:

select DISTINCT ((beam_current)), (logtime), (((((beam_energy)))))

您可以使用 row_number() 到 select 每个值 beam_energy 的最新行:

select  *
from    (
        select  row_number() over (
                    partition by beam_current
                    order by logtime desc) as rn
        ,       *
        from    INDUS2_BDS.dbo.DCCT 
        where   logtime between '2014-08-09 01:13:03' and '2014-08-09 02:16:53'
                and (beam_current like '%9.96' 
                    or beam_current like '%9.97' 
                    ... etc ...) 
                and beam_energy between '550' and '552'
        ) numbered_rows
where   rn = 1 -- Latest row per beam_current

对于 beam_current 的每个值,单个行组合似乎是不同的。 如果你只会尝试 select Distinct(beam_current) from INDUS2_BDS.dbo.DCCT where .... 那么只有你会得到那个独特的价值。

Group by 也不起作用,因为它会将在 group by keyword.So 之后写入的列从左到右分组,无论如何你会得到相同的结果。

这将为 beam_current 的每个值 return 1 行:

;WITH CTE AS
(
SELECT
  row_number() over (partition by beam_current order by (select 1)) rn,
  beam_current, logtime, beam_energy 
FROM INDUS2_BDS.dbo.DCCT 
WHERE 
  logtime between '2014-08-09 01:13:03' and '2014-08-09 02:16:53'
  and (beam_current like '%9.96' or beam_current like '%9.97' 
       or beam_current like '%9.98' or  beam_current like '%9.99'
       or beam_current like '%0' or beam_current like '%_0.01' 
       or beam_current like '%_0.02' or beam_current like '%_0.03' 
       or beam_current like '%_0.04' or beam_current like '%_0.05' 
       or beam_current like '%_0.06') 
       and beam_energy between 550 and 552
)
SELECT beam_current, logtime, beam_energy 
FROM CTE
WHERE rn = 1