SQL分区依据删除重复项

时间:2015-03-19 20:25:33

标签: sql sql-server

所以这让我绝对疯狂。我认为这是与父母或其他事情有关的问题。由于列“cpt_code”(这些值不同),我得到了重复的行。我只想要绝对最小日期,无论CPT_Codes有什么不同。我到底是做错了什么?在“--end query”之后,我在第42行收到错误“。”“。

with test as 
 (
 select account_id
 , PROV_name,PROV_ID
 ,  pat_name
 ,  cpt_code
 , modifier_one
 , modifier_two
 , MODIFIER_THREE
 , mindate
 , ROW_NUMBER() over (PARTITION BY [account_id] ORDER BY mindate DESC) as rownum
 from (

 --start query
 select PROV_name,PROV_ID,  pat_name, account_id, cpt_code, modifier_one,
        modifier_two,MODIFIER_THREE, min(ORIG_SERVICE_DATE) as mindate

 from

 opds.dbo.mmp_charges left outer join patient
 on dbo.PATIENT.PAT_ID =  opds.dbo.mmp_charges .INT_PAT_ID
 left outer join CLARITY_DEP  
 on opds.dbo.mmp_charges.DEPT_ID = CLARITY_dep.DEPARTMENT_ID
 left outer join CLARITY_SER
 on opds.dbo.mmp_charges.performing_Prov_id = clarity_ser.PROV_ID

 where( CPT_CODE   = '99201' or CPT_CODE  = '99202' or cpt_code = '99203' 
       or cpt_code = '99204' or cpt_code = '99205' 
       or CPT_CODE   = '99211' or CPT_CODE  = '99212' 
       or cpt_code = '99213' or  cpt_code = '99214' 
       or cpt_code = '99215' or  CPT_CODE   = '99241' 
       or CPT_CODE  = '99242' or cpt_code = '99243' 
       or  cpt_code = '99244' or cpt_code = '99245')

  and opds.dbo.mmp_charges.dept_id = 20500101
  and (PERFORMING_PROV_ID = 100570  or PERFORMING_PROV_ID = 100503 
       or PERFORMING_PROV_ID = 116948  or PERFORMING_PROV_ID = 111836 or
          PERFORMING_PROV_ID = 116939) 
  and 

  ((modifier_one not like  'tc' or MODIFIER_ONE is null) 
    and (modifier_two not like 'tc' or modifier_two is null))


 and account_id = '9076'

  group by     PROV_name,PROV_ID,  pat_name, account_id, cpt_code,
    modifier_one, modifier_two,MODIFIER_THREE,    
    opds.dbo.mmp_charges.ORIG_SERVICE_DATE
 --end query      
 )

 Select * from test where rownum = 1

2 个答案:

答案 0 :(得分:0)

我认为你错过了with子句

的结束语
    .................

 group by     PROV_name,PROV_ID,  pat_name, account_id, cpt_code,
     modifier_one, modifier_two,MODIFIER_THREE,    
     opds.dbo.mmp_charges.ORIG_SERVICE_DATE  --end query        
   )
 )  
Select * from test where rownum = 1

答案 1 :(得分:0)

我认为你不需要小组和基于CTE的方法,只需CTE即可:

with test as (
    select 
        prov_name,
        prov_id,
        pat_name,
        account_id,
        modifier_one,
        modifier_two,
        modifier_three,
        orig_service_date,
        row_number() over (
            partition by 
            /* might not need all of these, maybe account_id discriminates enough */
                prov_name,
                prov_id,
                pat_name,
                account_id,
                modifier_one,
                modifier_two,
                modifier_three 
            order by
                orig_service_date
        ) as rownum
    from
        opds.dbo.mmp_charges 
            left outer join 
        dbo.patient
            on dbo.patient.pat_id =  opds.dbo.mmp_charges.int_pat_id
            left outer join 
        dbo.clarity_dep
            on opds.dbo.mmp_charges.dept_id = clarity_dep.department_id
            left outer join
        dbo.clarity_ser
            on opds.dbo.mmp_charges.performing_prov_id = clarity_ser.prov_id
    where 
        cpt_code in (
            '99201', '99202', '99203', '99204', '99205', '99211',
            '99212', '99213', '99214', '99215', '99241', '99242',
            '99243', '99244', '99245'
        ) and
        opds.dbo.mmp_charges.dept_id = 20500101 and 
        performing_prov_id in (100570, 100503, 116948, 111836, 116939) and 
        (modifier_one not like  'tc' or modifier_one is null) and 
        (modifier_two not like 'tc' or modifier_two is null) and 
        account_id = '9076'
)
Select
    *
from
    test
where
    rownum = 1

order by中的row_number()可确保您获得包含分区最短日期的行。

我无法从问题中分辨出partition by条款中的内容。它可能只是account_id