【问题标题】:Avoid Duplicate Rows When One Column is Not Unique当一列不唯一时避免重复行
【发布时间】:2018-12-03 10:37:11
【问题描述】:

我正在创建一个总共使用大约 20 列的查询。由于一列不同,我有很多重复的记录。此列是一种标志,只能包含“Y”或“N”。我的问题是,如果标志是“Y”,那么对于同一个声明,还有一个带有“N”标志的附加行。我怎样才能只显示带有“Y”的行。旁注:有些结果只有一个正确的“N”,所以我不能在 where 子句中使用 flag = 'Y'。

查询:

if object_id('tempdb.dbo.#HeaderCount') is not null drop table #HeaderCount
Select Distinct 
[MCO Claim ID (ICN)] = c.claimid
,[Header Claim Acceptance Status] = Case When c.reject = 'Y' Then 'X' Else 'A' End
,[Header Claim Source Status] = Case When c.ClaimID not like '%A%' or c.claimid not like '%R%'                                                                           Then 'OR'
                                 When c.claimID like '%A%' Then 'AJ'
                                 When c.claimID like '%R%' Then 'RV' End
,[Header Claim Ever Pended] = Case When ca.Orig_Status = 'PEND' Then 'Y' Else 'N' End 
,[Header Claim Adjudication Payment Status] =  c.Status
,[Member Medicaid ID] = ek.carriermemid
,[LDH Billing Provider ID] = '' --Optional
,[Billing Provider NPI] = p2.NPI
,[Servicing Provider NPI] = p.NPI
,[Header From Date of Service] = cast(c.StartDate as date)
,[Header To Date of Service] = cast(c.EndDate as date)
,[Date Claim Received by the MCO] = cast(c.CleanDate as date)
,[Date Claim Adjudicated by the MCO] = cast(c.adjuddate as date)
,[Date Claim Paid by the MCO] = cast(c.paiddate as date)
,[Billed Charges] = c.totalamt
,[MCO Paid Amount] = c.totalpaid
,c.formtype
,[BillType] = c.FacilityCode + c.BillClassCode
,c.reject
,c.status
into #HeaderCount
from PlanReport_QNXT_LA.dbo.claim c (NOLOCK)
left join PlanReport_QNXT_LA.dbo.affiliation a2     (NOLOCK)--pay to affiliation
on a2.affiliationid = c.affiliationid
left join PlanReport_QNXT_LA.dbo.provider p2    (NOLOCK)    --pay to provider
on a2.affiliateid = p2.provid
inner join PlanReport_QNXT_LA.dbo.provider p    (NOLOCK)    --rendering provider
on p.provid = c.provid
inner join PlanReport_QNXT_LA.dbo.claim_audit ca    (NOLOCK)
on c.claimid = ca.claimid
inner join PlanReport_QNXT_LA.dbo.member m      (NOLOCK)
on c.memid = m.memid
inner join PlanReport_QNXT_LA.dbo.enrollkeys ek     (NOLOCK)
on m.memid = ek.memid
and ek.segtype = 'int'
Where c.cleandate between '1/1/2017' and '1/31/2017'
Order By c.claimid

我只想为每个 [MCO Claim ID (ICN)] 在 [Header Claim Ever Pended] 列中同时包含“Y”和“N”的一行(“Y”行)查看。我认为按行号分区可能是解决方案?

如果我需要提供更多信息或者这没有意义,请告诉我。我正在努力在解决我遇到的 VPN 问题后立即提供示例数据。提前致谢。

以下是一些示例数据:

[MCO Claim ID (ICN)] [Header Claim Ever Pended]

1 15059C063424A1 是的

2 15059C063424A1 否

3 15218C098293A2 否

在上面的示例中,假设每列(未显示)中的所有值对于第 1 行和第 2 行都是相同的,除了列 [Header Claim Ever Pended]。我只想查看每个 claimid 的“Y”记录(如果存在)(不是 Y 和 N 记录)。另外,如果 [MCO Claim ID (ICN)] 没有“Y”记录,那么我想查看“N”记录。

【问题讨论】:

    标签: sql duplicates unique distinct


    【解决方案1】:

    添加 ROW_NUMBER:

    with cte as
     ( -- DISTINCT is calulated after ROW_NUMBER
        Select Distinct 
        [MCO Claim ID (ICN)] = c.claimid
        ,[Header Claim Acceptance Status] = Case When c.reject = 'Y' Then 'X' Else 'A' End
        ,[Header Claim Source Status] = Case When c.ClaimID not like '%A%' or c.claimid not like '%R%'                                                                           Then 'OR'
                                         When c.claimID like '%A%' Then 'AJ'
                                         When c.claimID like '%R%' Then 'RV' End
        ,[Header Claim Ever Pended] = Case When ca.Orig_Status = 'PEND' Then 'Y' Else 'N' End 
        ,[Header Claim Adjudication Payment Status] =  c.Status
        ,[Member Medicaid ID] = ek.carriermemid
        ,[LDH Billing Provider ID] = '' --Optional
        ,[Billing Provider NPI] = p2.NPI
        ,[Servicing Provider NPI] = p.NPI
        ,[Header From Date of Service] = cast(c.StartDate as date)
        ,[Header To Date of Service] = cast(c.EndDate as date)
        ,[Date Claim Received by the MCO] = cast(c.CleanDate as date)
        ,[Date Claim Adjudicated by the MCO] = cast(c.adjuddate as date)
        ,[Date Claim Paid by the MCO] = cast(c.paiddate as date)
        ,[Billed Charges] = c.totalamt
        ,[MCO Paid Amount] = c.totalpaid
        ,c.formtype
        ,[BillType] = c.FacilityCode + c.BillClassCode
        ,c.reject
        ,c.status
        from PlanReport_QNXT_LA.dbo.claim c (NOLOCK)
        left join PlanReport_QNXT_LA.dbo.affiliation a2     (NOLOCK)--pay to affiliation
        on a2.affiliationid = c.affiliationid
        left join PlanReport_QNXT_LA.dbo.provider p2    (NOLOCK)    --pay to provider
        on a2.affiliateid = p2.provid
        inner join PlanReport_QNXT_LA.dbo.provider p    (NOLOCK)    --rendering provider
        on p.provid = c.provid
        inner join PlanReport_QNXT_LA.dbo.claim_audit ca    (NOLOCK)
        on c.claimid = ca.claimid
        inner join PlanReport_QNXT_LA.dbo.member m      (NOLOCK)
        on c.memid = m.memid
        inner join PlanReport_QNXT_LA.dbo.enrollkeys ek     (NOLOCK)
        on m.memid = ek.memid
        and ek.segtype = 'int'
        Where c.cleandate between '1/1/2017' and '1/31/2017'
     ),
    cte_with_rn as
     ( -- now apply the ROW_NUMBER
       select *,
          row_number()
          over (partition by  [MCO Claim ID (ICN)]
                order by [Header Claim Ever Pended] desc) as rn -- 'Y' sorts before 'N'
       from cte
     )
    select *   -- change to all colujmns except rn
    into #HeaderCount
    from cte_with_rn 
    where rn = 1
    Order By claimid
    

    【讨论】:

      【解决方案2】:

      这成功了...

      Select ROW_NUMBER() over(PARTITION BY [MCO Claim ID (ICN)] ORDER BY 
      [Header Claim Ever Pended] Desc) Row#
      Into #temptable
      From #HeaderCount
      
      Select *
      From #temptable
      Where Row# = 1
      

      【讨论】:

      • 您不需要添加分区列来排序。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-25
      相关资源
      最近更新 更多