使用SQL Server,我想加入以下两个查询来创建一行,其中患者在同一服务日期拥有这两个代码(80048和80053)。如果更容易,我可以拉两行(行为80048,行为80053)
(Select PatLname + PatFname + cast(PatBdate as varchar(50)) as "ID", c.HCPCS as "HCPCS", c.SvDate
From Claim as a
left join claimDtl as c on a.claim_id = c.claimDtl_id
Where
c.HcPcS = 80048
and c.SvDate between '10-01-2016' and '11-01-2016'
group by PatLname, PatFname, PatBdate, c.HCPCS,c.SvDate) Tbl1
Inner Join
(Select PatLname + PatFname + cast(PatBdate as varchar(50))as "ID", c.HCPCS as "HCPCS", c.SvDate
From claim as a
left join claimDtl as c on a.claim_id = c.claimDtl_id
Where
c.HcPcS = 80053
and c.SvDate between '10-01-2016' and '11-01-2016'
group by PatLname, PatFname, PatBdate, c.HCPCS, c.SvDate) Tbl2
on Tbl1.ID = Tbl2.ID
where Tbl1.Svdate = Tbl2.SvDate
答案 0 :(得分:1)
我认为你所采用的方法可能存在一些问题:
那说,回答你的问题(我认为):
select distinct Tbl1.ID ,
Tbl1.SvDate
from
(select PatLname + PatFname + cast(PatBdate as varchar(50)) as "ID",
c.SvDate
from Claim as a
inner join claimDtl as c on a.claim_id = c.claimDtl_id
Where c.HcPcS = 80048) as Tbl1
inner join
(select PatLname + PatFname + cast(PatBdate as varchar(50)) as "ID",
c.SvDate
from Claim as a
inner join claimDtl as c on a.claim_id = c.claimDtl_id
Where c.HcPcS = 80053) as Tbl2
on Tbl1.ID = Tbl2.ID
and Tbl1.SvDate = Tbl2.SvDate
答案 1 :(得分:0)
谨慎使用between
date
和datetime
值。通常最好将两个语句明确分解为>=
和<
。
查看Aaron Bertrand的文章 Bad habits to kick : mis-handling date / range queries 。
使用 common table expression 版with cte as (
select distinct
ID = PatLname + PatFname + cast(PatBdate as varchar(50))
, c.HCPCS
, c.SvDate
, rn = row_number() over (partition by PatLname,PatFname,PatBdate,c.SvDate order by c.HCPCS)
from Claim as a
left join claimDtl as c on a.claim_id = c.claimDtl_id
where c.HCPCS in (80048,80053)
and c.SvDate >= '10-01-2016'
and c.SvDate < '11-01-2016'
)
select
Id
, Hcpcs
, SvDate
from cte
where rn = 2;
如果您不需要返回HCPCS(我不知道您为什么会这样做),这也可以起作用:
select
PatLname + PatFname + cast(PatBdate as varchar(50)) as "ID"
--, c.HCPCS as "HCPCS"
, UniqueHcpcs = count(Distinct c.HCPCS)
, c.SvDate
from Claim as a
left join claimDtl as c on a.claim_id = c.claimDtl_id
where c.HCPCS in (80048,80053)
and c.SvDate >= '10-01-2016'
and c.SvDate < '11-01-2016'
group by PatLname, PatFname, PatBdate, /*c.HCPCS,*/ c.SvDate
having UniqueHcpcs >1;
答案 2 :(得分:0)
这应该通过在第一个条目合格后进行二次连接来实现。防止需要聚合和分组。我假设患者姓名/出生日期在主要索赔表中,别名为“a”。
因此,从索赔到索赔详情的加入是您要查找的主要代码,并且它在日期范围内。
一旦您拥有该特定发现和特定日期,请再次加入相同声明和服务日期的声明详细信息,还可以加入您正在寻找的SECOND代码。
Select
PatLname + PatFname + cast(PatBdate as varchar(50)) as "ID",
c.HCPCS as "HCPCS",
c2.HcPcS as "HCPCS2",
c.SvDate
From
Claim as a
join claimDtl as c
on a.claim_id = c.claimDtl_id
AND c.HcPcS = 80048
and c.SvDate between '10-01-2016' and '11-01-2016'
join claimDtl as c2
on a.claim_id = c2.claimDtl_id
AND c2.HcPcS = 80053
and c.SvDate = c2.SvDate
如果某人甚至没有资格获得80048索赔,则无需尝试80053代码
答案 3 :(得分:0)
如果您将左连接表中的值测试到可以通过内连接替换左外连接的位置
我认为你的小组是为了得到不同的行
尝试这样的事情:
with tmp as (
Select distinct PatLname + PatFname + cast(PatBdate as varchar(50)) as ID, c.HCPCS, c.SvDate
From Claim as a inner join claimDtl as c on a.claim_id = c.claimDtl_id
Where c.HcPcS in ( 80048, 80053)
and c.SvDate between '10-01-2016' and '11-01-2016'
)
select * from tmp t1 inner join t2 on t1.ID=t2.ID and t1.Svdate=t2.Svdate
where t1.HCPCS=80048 and t2.HCPCS=80053