需要选择查询或删除查询以获取此结果或删除重复的数据

时间:2019-05-29 11:04:59

标签: sql sql-server database select sql-delete

我获取数据,我想选择或删除查询以得到预期的结果

sql服务器查询

select *,
         row_number() over (order by (select '' )) 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 = '52' and okeydata like 'approve'
                  )

///// 试试这个查询,但是不行

使用cte AS( 选择 *,              row_number()超过(按(选择``)排序)作为seqnum       来自[elo]。[dbo]。[objkeys]       在何处使用parentid(选择p​​arentid                          来自[elo]。[dbo]。[objekte]内部联接                               [elo]。[dbo]。[objkeys]                                在objid = parentid上                          其中objmask ='52'和okeydata如'approve'                       ) )删除seqnum> 1

的cte

实际结果

parentid    okeyno      okeyname        okeydata            
 652          0          NAME             uwk           
 652          1         NUMBER             1        
 652          2         ALAIETIMAD      approve     
 652          51        ELO_FNAME       16.jpg      

 653          0         NAME              uwk           
 653          1        NUMBER              1        
 653          2       ALAIETIMAD         approve        
 653         51       ELO_FNAME          16.jpg     

 656          0         NAME              b     
 656          1        NUMBER             1     
 656          2       ALAIETIMAD        approve     
 656         51       ELO_FNAME          14.jpg     

 657         0      NAME                  b     
 657         1      NUMBER                1     
 657         2      ALAIETIMAD         approve      
 657         51     ELO_FNAME          16.jpg       

 661         0      NAME              abdalah       
 661         1      NUMBER              1       
 661         2      ALAIETIMAD        approve       
 661         51     ELO_FNAME         16.jpg        

 662         0      NAME             abdalah        
 662         1      NUMBER             1        
 662         2      ALAIETIMAD       approve        
 662         51     ELO_FNAME        16.jpg 

需要的结果是

parentid    okeyno      okeyname        okeydata            
 652          0      NAME                 uwk           
 652          1     NUMBER                 1        
 652          2     ALAIETIMAD         approve      
 652          51    ELO_FNAME          16.jpg       


 656          0     NAME                 b      
 656          1     NUMBER               1      
 656          2     ALAIETIMAD       approve        
 656         51     ELO_FNAME        14.jpg     



 661         0      NAME             abdalah        
 661         1      NUMBER             1        
 661         2      ALAIETIMAD       approve        
 661         51     ELO_FNAME        16.jpg     

1 个答案:

答案 0 :(得分:1)

您可以尝试以下方法吗?

SELECT * FROM 
(
select *,
         row_number() over (order by (select '' )) 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 = '52' and okeydata like 'approve'
                  )
) as t
where parentid in
(
SELECT DISTINCT min(parentid) FROM 
(
select *,
         row_number() over (order by (select '' )) 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 = '52' and okeydata like 'approve'
                  )
) as t2
where okeyname = 'NAME'
group by okeyno,okeyname,okeydata
)