简体   繁体   English

多个连接子查询SQL Server 2008

[英]Multiple joins subquery SQL Server 2008

在此输入图像描述

On the left you'll see my design for one table, on the right you'll see the results from the subquery in the SQL below. 在左侧,您将看到我的设计为一个表,在右侧,您将在下面的SQL中看到子查询的结果。 I'm trying to join the subquery on the three fields patientID,claimsFromDate,claimsThroughDate on tblClaims, and have the outer query associate the correct tblClaims.ID with the three part join. 我正在尝试在patientID,claimsFromDate,claimsThroughDate上的三个字段patientID,claimsFromDate,claimsThroughDate上加入子查询,并让外部查询将正确的tblClaims.ID与三部分连接相关联。

The errors I'm getting: 我得到的错误:

line 3, incorrect syntax near the keyword select and incorrect syntax near the ), line 12 第3行,关键字附近不正确的语法选择and附近),第12行不正确的语法

select tblClaims.id, t.primaryCode
from t
(
select patientid, claimsfromdate, claimsthroughDate, primarycode from myTable
union
select patientid, claimsfromDate, claimsthroughDate, secondaryCode from myTable
union
select patientID, claimsfromdate, claimsthroughDate, tertiarycode from myTable

) as t
inner join t on tblclaims.patientid=t.patientid 
and tblclaims.claimsfromdate=t.claimsfromdate
and tblclaims.cllaimsthroughdate=t.claimsfromdate

EDIT: The inner query is to reconcile a multi column field. 编辑:内部查询是协调多列字段。 It returns 1.5 million rows. 它返回150万行。 the fixed query I ran returned 3.5 million which was 我运行的固定查询返回了350万,这是

select tblClaims.id, t.primarycode from ( select patientid, claimsfromdate, claimsthroughDate, primarycode from myTable ) as t inner join tblclaims on tblclaims.patientid=t.patientid and tblclaims.claimsfromdate=t.claimsfromdate and tblclaims.cllaimsthroughdate=t.claimsfromdate

Try this: 尝试这个:

select tblClaims.id, t.primarycode 
from 
(
select patientid, claimsfromdate, claimsthroughDate, primarycode from myTable
) as t
inner join tblclaims on tblclaims.patientid=t.patientid 
and tblclaims.claimsfromdate=t.claimsfromdate
and tblclaims.cllaimsthroughdate=t.claimsfromdate

Did you try using Distinct ? 你尝试过使用Distinct吗?

select DISTINCT tblClaims.id, t.primarycode 
from 
(
    select patientid, claimsfromdate, claimsthroughDate, primarycode from myTable
) as t
inner join tblclaims on tblclaims.patientid=t.patientid 
and tblclaims.claimsfromdate=t.claimsfromdate
and tblclaims.cllaimsthroughdate=t.claimsfromdate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM