0

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

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'.

如果记录不止一次,我如何才能返回记录?

4

5 回答 5

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
于 2012-08-08T23:38:26.173 回答
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 之后的记录

于 2012-08-09T00:16:06.657 回答
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
于 2012-08-09T05:27:15.720 回答
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
于 2012-08-09T06:27:17.213 回答
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
            ) 
于 2012-08-09T06:48:49.293 回答