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