我正在创建一个总共使用大约 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 索赔 ID (ICN)] 的一行(“Y”行),该 [MCO 索赔 ID (ICN)] 在 [标头索赔已挂起] 列中同时具有“Y”和“N”。我认为按行号分区可能是解决方案?
如果我需要提供更多信息或者这没有意义,请告诉我。一旦解决了我遇到的 VPN 问题,我就会立即提供示例数据。预先感谢。
这是一些示例数据:
[MCO 索赔 ID (ICN)] [标头索赔已挂起]
1 15059C063424A1 是
2 15059C063424A1 N
3 15218C098293A2 N
在上面的示例中,假设第 1 行和第 2 行的每列(未显示)中的所有值都相同,但 [Header Claim Ever Pending] 列除外。我只想查看每个 Claimid 的“Y”记录(如果存在)(而不是同时查看 Y 和 N 记录)。另外,如果 [MCO Claim ID (ICN)] 没有“Y”记录,那么我想查看“N”记录。
添加 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