返回重复的值

时间:2012-08-08 23:32:57

标签: sql sql-server sql-server-2008 tsql

我正在尝试根据以下内容获取所有重复的记录:

accession_id, 
  check_num,
  procedure_code,
  paid_amt,

此查询将返回上述内容以及这些字段重复的次数。

然而,我想只返回那些不止一次出现的那些。

  select    
  ACCESSION_PAYMENTS_DAILY_KEY, 
  accession_id, 
  check_num,
  procedure_code,
  paid_amt,
  row_number() 
  over 
  (partition by   accession_id, check_num,procedure_code,paid_amt order by ACCESSION_PAYMENTS_DAILY_KEY) as occurrence
          from      [MILLENNIUM_DW_DEV].[dbo].[F_PAYOR_PAYMENTS_DAILY]         
   where PROCEDURE_CODE is not null
   and PAID_AMT>0

当我将过滤条件occurrence>1添加到上述查询时,我收到此错误:

Msg 207, Level 16, State 1, Line 13
Invalid column name 'occurrence'.

如果记录多次出现,我怎么才能返回记录呢?

5 个答案:

答案 0 :(得分:2)

如果要在WHERE子句中使用别名,则可以将其包装在另一个SELECT语句中。

SELECT *
FROM
(
  select ACCESSION_PAYMENTS_DAILY_KEY, 
    accession_id, 
    check_num,
    procedure_code,
    paid_amt,
    row_number() over (partition by   accession_id, check_num,procedure_code,paid_amt order by ACCESSION_PAYMENTS_DAILY_KEY) as occurrence
  from      [MILLENNIUM_DW_DEV].[dbo].[F_PAYOR_PAYMENTS_DAILY]         
  where PROCEDURE_CODE is not null
     and PAID_AMT>0
) x
WHERE x.occurrence>1

答案 1 :(得分:1)

也许您应该尝试使用GROUP BY / HAVING子句:

SELECT accession_id, check_num, procedure_code, paid_amt,
       COUNT(ACCESSION_PAYMENTS_DAILY_KEY) as occurrences 
FROM [MILLENNIUM_DW_DEV].[dbo].[F_PAYOR_PAYMENTS_DAILY] 
GROUP BY accession_id, check_num, procedure_code,paid_amt 
HAVING COUNT(ACCESSION_PAYMENTS_DAILY_KEY) > 1

HAVING子句过滤GROUP BY

之后的记录

答案 2 :(得分:1)

没有想法你的表是如何被编入索引的,但是自我左联接呢?它可能值得测试,但我无法想象它比上述任何一种方法都快。除非将accession_id和check_num编入索引,否则我甚至不会尝试这个...

Select
  t1.*
from [MILLENNIUM_DW_DEV].[dbo].[F_PAYOR_PAYMENTS_DAILY] as t1
left join [MILLENNIUM_DW_DEV].[dbo].[F_PAYOR_PAYMENTS_DAILY] as t2
  on t2.accession_id = t1.accession_id
  and t2.check_num = t1.check_num
  and t2.procedure_code = t1.procedure_code
  and t2.paid_amt = t1.paid_amt
where t2.ACCESSION_PAYMENTS_DAILY_KEY is null

答案 3 :(得分:1)

试试这个:

select accession_id, 
    check_num,
    procedure_code,
    paid_amt,
    COUNT(*) as occurrance
  from      [MILLENNIUM_DW_DEV].[dbo].[F_PAYOR_PAYMENTS_DAILY]         
  where PROCEDURE_CODE is not null
     and PAID_AMT>0
     group by  accession_id,check_num,procedure_code,paid_amt
     having COUNT(*) > 1

答案 4 :(得分:1)

也许是另类......

;WITH MoreOne_CTE()
    AS
    (
    SELECT       
        ACCESSION_PAYMENTS_DAILY_KEY   
    FROM  [MILLENNIUM_DW_DEV].[dbo].[F_PAYOR_PAYMENTS_DAILY]     
    GROUP BY 
          accession_id,    
          check_num,   
          procedure_code,   
          paid_amt
    HAVING  COUNT(*) > 1     
    )
SELECT       
        ACCESSION_PAYMENTS_DAILY_KEY ,    
        accession_id,    
        check_num,   
        procedure_code,   
        paid_amt           
FROM [MILLENNIUM_DW_DEV].[dbo].[F_PAYOR_PAYMENTS_DAILY]   x  
WHERE
        PROCEDURE_CODE IS NOT NULL
        AND PAID_AMT>0 
        AND EXISTS 
            (
                    SELECT 1 
                    FROM MoreOne_CTE y 
                    WHERE x.ACCESSION_PAYMENTS_DAILY_KEY = y.ACCESSION_PAYMENTS_DAILY_KEY
            )