我有一个为表编写的select语句,即STAY表,其中2个日期字段即ADMSN_DT和DSCHRG_DT不可用。所以我被告知通过加入claim_id从另一个名为CLM表的表中获取这些字段。
所以我将我的选择查询框架为
select
Member_id as MemberID,
(select a.ADMSN_DT from CLM a, STAY b where a.cl_id = b.claim_id) as Admission_Date,
(select a.DSCHRG_DT from CLM a, STAY b where a.cl_id = b.claim_id) as Discharge_Date,
field 1,
feild 2, such as ... some fields from STAY table
from STAY;
这里的问题是我从这张表中得到了2条记录,claim_id是不同的,但是我在记录中都得到了相同的日期。
例如: -
Claim_id Admission_date Discharge_date
1234 11/12/2015 12/12/2015 -- this claim id has dates in clm table
5678 11/12/2015 12/12/2015 -- this claim id has no dates in clm table
它只是将相同的值从第一条记录复制到第二条记录。为什么要避免这种情况?非常感谢任何帮助。
谢谢!
答案 0 :(得分:1)
尝试以下查询;
select Member_id as MemberID,
(select a.ADMSN_DT from CLM a where a.cl_id = s.claim_id) as Admission_Date,
(select a.DSCHRG_DT from CLM a where a.cl_id = s.claim_id) as Discharge_Date
from STAY s;
答案 1 :(得分:1)
您的代码在子查询结果和从封闭查询返回的其他结果之间生成笛卡尔积[/ p>
试试这个:
select
Member_id as MemberID,
a.ADMSN_DT as Admission_Date,
a.DSCHRG_DT as Discharge_Date,
field 1,
feild 2, ...
from STAY b, CLM a
where a.cl_id = b.claim_id(+)