Sql 在select子句中添加额外列时返回不同记录数的Oracle脚本
我的脚本执行有问题。下面的脚本在我的数据库中返回744条记录,但如果我在添加“cnt”列为select list时重新编写此脚本,此脚本将返回740条记录,尝试了很多次,但在“select子句”中添加额外列时找不到减少结果行数的原因 它返回740条记录Sql 在select子句中添加额外列时返回不同记录数的Oracle脚本,sql,oracle,Sql,Oracle,我的脚本执行有问题。下面的脚本在我的数据库中返回744条记录,但如果我在添加“cnt”列为select list时重新编写此脚本,此脚本将返回740条记录,尝试了很多次,但在“select子句”中添加额外列时找不到减少结果行数的原因 它返回740条记录 需要知道导致此问题的可能原因请给出表的DDL和显示问题的最小工作数据样本(最好是insert语句)(这是在中创建最小工作示例的良好资源)。不选择任何列的情况下,计数(*)是多少?也给出第二个完整的查询。应该有一些区别。因为select子句终于运行
需要知道导致此问题的可能原因请给出表的DDL和显示问题的最小工作数据样本(最好是insert语句)(这是在中创建最小工作示例的良好资源)。不选择任何列的情况下,计数(*)是多少?也给出第二个完整的查询。应该有一些区别。因为select子句终于运行了。在此之前,所有连接/位置/组/按顺序完成。因此,仅更改select查询不会理想地更改结果。如果是这样,我会非常惊讶。尝试使用+规则提示运行这两个查询。Oracle中有几个错误,不同的exec计划导致错误的查询结果。顺便问一下,db的版本是什么?不是11gR1吗?@ibre5041:是的DB版本是11gR1。@Utsav:我同样感到惊讶,因为cnt列不在初始选择列表中,而是在where子句中使用,如[nvl(cnt,0)2],为了查看cnt有什么值,我在select中添加了cnt,令人惊讶的是,结果计数发生了变化,然后我通过从select中添加和删除cnt对其进行了多次测试,结果发生了变化。
Select NVL(E.Employee_Ident, '-') EmpId,
Z.EmpLastName || ', ' || z.EmpFirstname EmpName,
z.DOB,
z.EmpPersonId,
z.DepPersonId,
NVL(z.PlanType, 131999) PlanType,
z.Empstat,
z.Status from (Select ML.personid EmpPersonId,
EL.personid elpersonid,
EL.plantype elplantype,
Ml.plantype mlplantype,
ML.name_last EmpLastName,
ML.name_first EmpFirstName,
EL.DepId DepPersonId,
EL.name_last DepLastName,
EL.name_first DepFirstName,
ML.plantype PlanType,
ML.EmpStat,
NVL(EL.status, 2) Status,
NVL(to_char(EL.Birthdate, 'mm/dd/yyyy'), '-') DOB,
sum(case
when status = 0 or status = 1 or EmpStat = 1 or EmpStat = 2 then
1
else
0
end) over(partition by DepId) Cnt
from (select DCES.*,
cfm.employee_personid PersonId,
Dep.Name_Last,
Dep.Name_First,
Dep.Personid DepId,
Dep.Birthdate
from (select ChildId,
v2.plantype PlanType,
nvl(v1.status, v2.status) status
from (select d.family_member_personid ChildId,
d.plantype PlanType,
d.status status
from cb_enrollment_status d
where d.employee_personid <>
d.family_member_personid
and d.enrollment_type <> 120008
and d.plantype in (131001, 131002)) v1 partition by(v1.ChildId)
right outer join (select 131001 plantype, 2 status
from dual
union all
select 131002 plantype, 2 status
from dual) v2
on (v1.plantype = v2.plantype)) DCES,
cb_person dep,
cb_family_member cfm
where cfm.family_member_personid = DCES.ChildId(+)
and cfm.delete_status = 0
and cfm.special_enrollee <> 1
and cfm.family_member_personid = Dep.personid) EL,
(select p.personid,
p.name_last,
p.name_first,
v2.plantype PlanType,
nvl(v1.status, v2.status) Empstat
from cb_person p,
(select s.employee_personid PersonId,
s.plantype PlanType,
s.status status
from cb_enrollment_status s
where s.employee_personid = s.family_member_personid
and s.enrollment_type <> 120008
and s.employee_personid in
(select distinct employee_personid
from cb_family_member
where delete_status = 0
and special_enrollee <> 1)
and s.status in (0, 1)
and s.plantype in (131001, 131002)) v1 partition by(v1.personid)
right outer join (select 131001 plantype, 2 status
from dual
union all
select 131002 plantype, 2 status from dual) v2
on (v1.plantype = v2.plantype)
where p.personid = v1.personid) ML
where ML.Personid = EL.Personid(+)
and ML.PlanType = NVL(EL.PlanType, ML.PlanType)) Z,
(select distinct *
from (select e.Employee_Ident,
personid,
change_sequence,
max(change_sequence) over(partition by personid) r
from cb_employee e
where coalesce(E.Hiredate_Rehire,
E.Hiredate_Original,
to_date(to_char(sysdate, 'MM/DD/YYYY'),
'MM/DD/YYYY')) <=
to_date(to_char(sysdate, 'MM/DD/YYYY'), 'MM/DD/YYYY'))
where change_sequence = r) E where e.personid = z.emppersonid order by lower(z.EmpLastName),
lower(z.EmpFirstName),
z.EmpPersonId,
lower(z.DepLastName),
lower(z.DepFirstName),
z.DepPersonId,
z.PlanType
Select NVL(E.Employee_Ident, '-') EmpId,
Z.EmpLastName || ', ' || z.EmpFirstname EmpName,
z.DOB,
z.EmpPersonId,
z.DepPersonId,
NVL(z.PlanType, 131999) PlanType,
z.Empstat,
z.Status, Z.cnt