gpt4 book ai didi

sql-server - 使用 UNION 时 SQL 返回的结果更少?

转载 作者:行者123 更新时间:2023-12-02 00:53:22 24 4
gpt4 key购买 nike

我有一个 SQL Server 存储过程,当我添加联合和后半部分时,它没有给出所有结果。前半部分会给我所有 6 个结果,使用并集,我只得到 5 个。

有 3 位患者有自己的[图表编号],每个患者都有 2 项应显示的项目。如果患者的两个条目的 [CPTCode] 相同,则当我添加并集时,仅显示两个条目之一(它为我提供了所有 6 个具有相同 [cptcode] 且没有并集的条目)。后半部分实际上并没有提取任何关于我现在正在做的事情的信息,但它是必要的。

select      /*Patients with chart numbers*/
B1.[chart Number],
'0' as newPatient,
isnull(Lytec.[Last Name],'') as [Last Name],
isnull(Lytec.[First Name],'') as [First Name],
isnull(Address.[Name],
Lytec.[Facility Code]) as [Facility],
isnull(B1.DOS,'') as [DOS],
isnull(Ins.[Name],
Lytec.[Primary Code]) as [Primary Code],
isnull(B1.[CPTCode],'') as cptCode,
isnull(B1.[Units],'') as [Units],
isnull(B1.[Modifiers],'') as [Modifiers],
isnull(B1.[cptCodeOther],'') as [cptCodeOther],
isnull(B1.[cptCode2],'') as cptCode2,
isnull(B1.[Units],'') as [Units2],
isnull(B1.[Modifiers2],'') as [Modifiers2],
isnull(B1.[cptCodeOther2],'') as [cptCodeOther2],
'name' as ProviderName
from
[sandboxmr].dbo.patient Lytec
left join [dbo].[Billing] B1 on B1.[Chart Number] = Lytec.[Chart Number]
left join [sandboxmr].dbo.[Address] Address on Lytec.[Facility Code] = Address.[Code]
left join [sandboxmr].dbo.[Insurance] Ins on Lytec.[Primary Code] = Ins.[Code]
where
b1.[userid] = 54
and (b1.[DateSubmitted] >= convert(varchar(25),'2011-8-31',101)
and b1.[DateSubmitted] <= convert(varchar(25),'2011-9-22',101))

union

select /*Patients without chart numbers*/
cast(P.id as varchar(15)) as [chart number],
'1' as newPatient,
isnull(P.[Last Name],'') as [Last Name],
isnull(P.[First Name],'') as [First Name],
isnull(Address.[Name],
P.[Facility Code]) as [Facility],
isnull(IV.DOS,isnull(SV.DOS,'')) as [DOS],
isnull(Ins.[Name],P.[Primary_Code]) as [Primary Code],
isnull(IV.[cptCode],isnull(SV.[cptCode],'')) as cptCode,
isnull(IV.[Units],isnull(SV.[Units],'')) as [Units],
isnull(IV.[Modifiers],isnull(SV.[Modifiers],'')) as [Modifiers],
isnull(IV.[cptcodeother],isnull(SV.[cptcodeother],'')) as [cptCodeOther],
isnull(IV.[cptCode2],isnull(SV.[cptCode2],'')) as cptCode2,
isnull(IV.Units2,isnull(SV.Units2,'')) as [Units2],
isnull(IV.[Modifiers2],isnull(SV.[Modifiers2],'')) as [Modifiers2],
isnull(IV.[cptCodeOther2],isnull(SV.[cptCodeOther2],'')) as [cptCodeOther2],
'Name' as ProviderName
from
[DNSList].[dbo].[Patient] P
left join [dbo].[InitialVisits] IV on p.emr_id = IV.patientid
left join [dbo].[SubsequentVisits] SV on p.emr_id = SV.patientid
left join [sandboxmr].dbo.[Address] Address on P.[Facility Code] = Address.[Code]
left join [sandboxmr].dbo.[Insurance] Ins on P.[Primary_Code] = Ins.[Code]
where
p.[userid] = 54
and p.[Chart Number] is null
and (p.[DateSubmitted] >= convert(varchar(25),'2011-8-31',101)
and p.[DateSubmitted] <= convert(varchar(25),'2011-9-22',101))
order by
[Last Name]

为什么会这样,我该如何解决?我尝试将 distinct 添加到 [cptcode] 区域,但它当然会生成错误。

感谢您提供的任何帮助!

最佳答案

UNION 将消除重复的行,无论它们是位于两个结果集之间,还是在您的情况下,仅位于一个结果集中。尝试使用 UNION ALL 代替。

关于sql-server - 使用 UNION 时 SQL 返回的结果更少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7287964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com