如何从此结果中删除重复项

时间:2019-05-27 09:23:59

标签: sql sql-server database

如何从此结果中删除重复项

select *,
       row_number() over (order by (select null)) as seqnum
from [elo].[dbo].[objkeys] 
where parentid in (select parentid
                   from [elo].[dbo].[objekte] 
                   inner join [elo].[dbo].[objkeys] on objid = parentid 
                   where objmask like 26 and okeydata like 'yes'
                   )

输出是

parentid   okeyname  okeydata
  1          name   a
  1      number     1
  1        yes_no   yes 
  2          name   a
  2      number     1
  2        yes_no   yes
  3          name   a
  3      number     1
  3        yes_no   no
  4          name   a
  4      number     2
  4          yes_no no
  5          name   b
  5      number     3
  5          yes_no yes
  6          name   b
  6      number     3
  6          yes_no yes
  7          name   b
  7      number     3
  7          yes_no no
  8          name   b
  8      number     3
  8          yes_no no

需要这样

parentid   okeyname  okeydata
  1          name   a
  1      number     1
  1          yes_no yes 

  3          name   a
  3      number     1
  3          yes_no no

  5          name   b
  5      number     3
  5          yes_no yes


  7          name   b
  7      number     3
  7          yes_no no

4 个答案:

答案 0 :(得分:0)

尝试以下-

select * from
(
      select *,row_number() over (partition by okeyname,okeydata order by parentid) as seqnum
      from [elo].[dbo].[objkeys] inner join [elo].[dbo].[objekte]
      on objid = parentid 
      where objmask like 26 and okeydata like 'yes'
)A where seqnum=1

答案 1 :(得分:0)

似乎没有任何重复的记录,但是从resut集中,您似乎需要最小的parentid并尝试如下操作

    with cte as
    (
    your query output
    ) select min(parentid),okeyname,okeydata
   from cte group by okeyname,okeydata

答案 2 :(得分:0)

如果您可以忽略“父母身份”,则可以使用以下代码。

select distinct okeyname, okeydata, 
             row_number() over (order by (select null)) as seqnum
      from [elo].[dbo].[objkeys] 
      where parentid in (select parentid
                         from [elo].[dbo].[objekte] inner join
                              [elo].[dbo].[objkeys] 
                               on objid = parentid 
                         where objmask like 26 and okeydata like 'yes'
                        )

答案 3 :(得分:0)

我认为透视数据是一种更好的格式,让您轻松地进行所需的操作。首先枢纽:

select parentid,
       max(case when okeyname = 'name' then okeydata end) as name,
       max(case when okeyname = 'number' then okeydata end) as number,
       max(case when okeyname = 'yes_no' then okeydata end) as name
from [elo].[dbo].[objkeys]
group by parentid ;

然后要获取第一个父代,请使用子查询:

select min(parentid) as parentid, name, number, yes_no
from (select parentid,
             max(case when okeyname = 'name' then okeydata end) as name,
             max(case when okeyname = 'number' then okeydata end) as number,
             max(case when okeyname = 'yes_no' then okeydata end) as yes_no
      from [elo].[dbo].[objkeys]
      group by parentid
     ) p
group by name, number, yes_no;

如果您想要原始格式,请取消透视:

select nny.parentid, v.*
from (select min(parentid) as parentid, name, number, yes_no
      from (select parentid,
                   max(case when okeyname = 'name' then okeydata end) as name,
                   max(case when okeyname = 'number' then okeydata end) as number,
                   max(case when okeyname = 'yes_no' then okeydata end) as yes_no
            from [elo].[dbo].[objkeys]
            group by parentid
           ) p
      group by name, number, yes_no
     ) nny cross apply
     (values ('name', name), 
             ('number', number),
             ('yes_no', yes_no)
     ) v(okeyname, okeydata)