需要查询更正
Query correction needed
select stdcode,name,degree_code,phone,startsemester,endsemester
from (
select distinct stdcode as stdcode,name as name,degree_code as degree_code,phone as phone,
(
SELECT sem_code
FROM V_ALLSTUDATA b
WHERE a.name = b.name
and a.stdcode= b.stdcode
and a.degree_code=b.degree_code
and a.phone=b.phone
AND startsem=(select min(startsem)
from V_ALLSTUDATA b)
) as startsemester,
(
SELECT sem_code
FROM V_ALLSTUDATA b
WHERE a.name = b.name
and a.stdcode= b.stdcode
and a.degree_code=b.degree_code
and a.phone=b.phone
AND startsem=(select
max(startsem) from V_ALLSTUDATA a)
) as endsemester
from V_ALLSTUDATA a
);
我想 select sem_code
作为 startsem_code
和 sem_code
作为 lastsem_code
我该如何解决这个错误?
ORA-01427: single-row subquery returns more than one row
01427. 00000 - "single-row subquery returns more than one row"
*Cause:
*Action:
因为子查询明确要求匹配min(startsem)
或max(startsem)
,我相信子查询只会return一个值。但是,它们 可能 return 相同值的多个实例。
为了防止这种情况发生,我认为您需要将 distinct
添加到两个子查询中,如下所示:
(select distinct sem_code
from V_ALLSTUDATA b
where a.name = b.name and a.stdcode= b.stdcode and a.degree_code=b.degree_code and a.phone=b.phone
and endsem=(select max(startsem) from V_ALLSTUDATA a)
) as endsemester
您在单个查询中有多个行,所以我认为您将使用 rownum。
所以试试下面的查询
select stdcode,name,degree_code,phone,startsemester,endsemester
from (
select distinct stdcode as stdcode,name as name,degree_code as degree_code,phone as phone,
(
SELECT sem_code
FROM V_ALLSTUDATA b
WHERE a.name = b.name and a.stdcode= b.stdcode and a.degree_code=b.degree_code
and a.phone=b.phone
AND startsem=(select min(startsem) from V_ALLSTUDATA b where rownum=1)
) as startsemester,
(select sem_code
from V_ALLSTUDATA b
where a.name = b.name and a.stdcode= b.stdcode and a.degree_code=b.degree_code and a.phone=b.phone
and endsem=(select max(startsem) from V_ALLSTUDATA a where rownum=1)
) as endsemester
from V_ALLSTUDATA a
);
问题出在 return startsemester
和 endsemester
值的一个(或两个)SELECT
语句中。例如:
(SELECT sem_code
FROM V_ALLSTUDATA b
WHERE a.name = b.name
AND a.stdcode = b.stdcode
AND a.degree_code = b.degree_code
AND a.phone = b.phone
AND startsem = (SELECT MIN (startsem)
FROM V_ALLSTUDATA b)) AS startsemester
不能return超过一个值。由于我们没有您的数据,因此无法回答 为什么 您有 too_many_rows
。有几种解决方法,例如
- 使用聚合之一,例如
MAX
,例如select max(sem_code) from ...
- 看看
distinct
是否有帮助,例如select distinct sem_code from ...
- 将
rownum
包含在 where
子句中,例如... and rownum = 1
但是 - 从我的角度来看 - 你应该研究导致错误的原因并适当地修复它。也许您在 where
子句中遗漏了更多条件;谁知道?我们没有,你可能会。
with tt AS
( select f.stdcode,
f.name,
f.degree_code,
f.phone,
MIN(f.startsem)as startsemdate,
MAX(f.startsem)as endsemedate
FROM V_GRADUATED f
GROUP BY
f.stdcode,
f.name,
f.degree_code,
f.phone)
select tt.stdcode,
tt.name,
tt.degree_code,
tt.phone,
(select sem_code
from v_graduated a
where a.stdcode=tt.stdcode
and a.startsem=tt.startsemdate) startsemester,
(select sem_code
from v_graduated a
where a.stdcode=tt.stdcode
and a.startsem=tt.endsemedate) endsemester
from tt
这就是我想要的......> 这是正确答案
enter image description here
select stdcode,name,degree_code,phone,startsemester,endsemester
from (
select distinct stdcode as stdcode,name as name,degree_code as degree_code,phone as phone,
(
SELECT sem_code
FROM V_ALLSTUDATA b
WHERE a.name = b.name
and a.stdcode= b.stdcode
and a.degree_code=b.degree_code
and a.phone=b.phone
AND startsem=(select min(startsem)
from V_ALLSTUDATA b)
) as startsemester,
(
SELECT sem_code
FROM V_ALLSTUDATA b
WHERE a.name = b.name
and a.stdcode= b.stdcode
and a.degree_code=b.degree_code
and a.phone=b.phone
AND startsem=(select
max(startsem) from V_ALLSTUDATA a)
) as endsemester
from V_ALLSTUDATA a
);
我想 select sem_code
作为 startsem_code
和 sem_code
作为 lastsem_code
我该如何解决这个错误?
ORA-01427: single-row subquery returns more than one row
01427. 00000 - "single-row subquery returns more than one row"
*Cause:
*Action:
因为子查询明确要求匹配min(startsem)
或max(startsem)
,我相信子查询只会return一个值。但是,它们 可能 return 相同值的多个实例。
为了防止这种情况发生,我认为您需要将 distinct
添加到两个子查询中,如下所示:
(select distinct sem_code
from V_ALLSTUDATA b
where a.name = b.name and a.stdcode= b.stdcode and a.degree_code=b.degree_code and a.phone=b.phone
and endsem=(select max(startsem) from V_ALLSTUDATA a)
) as endsemester
您在单个查询中有多个行,所以我认为您将使用 rownum。 所以试试下面的查询
select stdcode,name,degree_code,phone,startsemester,endsemester
from (
select distinct stdcode as stdcode,name as name,degree_code as degree_code,phone as phone,
(
SELECT sem_code
FROM V_ALLSTUDATA b
WHERE a.name = b.name and a.stdcode= b.stdcode and a.degree_code=b.degree_code
and a.phone=b.phone
AND startsem=(select min(startsem) from V_ALLSTUDATA b where rownum=1)
) as startsemester,
(select sem_code
from V_ALLSTUDATA b
where a.name = b.name and a.stdcode= b.stdcode and a.degree_code=b.degree_code and a.phone=b.phone
and endsem=(select max(startsem) from V_ALLSTUDATA a where rownum=1)
) as endsemester
from V_ALLSTUDATA a
);
问题出在 return startsemester
和 endsemester
值的一个(或两个)SELECT
语句中。例如:
(SELECT sem_code
FROM V_ALLSTUDATA b
WHERE a.name = b.name
AND a.stdcode = b.stdcode
AND a.degree_code = b.degree_code
AND a.phone = b.phone
AND startsem = (SELECT MIN (startsem)
FROM V_ALLSTUDATA b)) AS startsemester
不能return超过一个值。由于我们没有您的数据,因此无法回答 为什么 您有 too_many_rows
。有几种解决方法,例如
- 使用聚合之一,例如
MAX
,例如select max(sem_code) from ...
- 看看
distinct
是否有帮助,例如select distinct sem_code from ...
- 将
rownum
包含在where
子句中,例如... and rownum = 1
但是 - 从我的角度来看 - 你应该研究导致错误的原因并适当地修复它。也许您在 where
子句中遗漏了更多条件;谁知道?我们没有,你可能会。
with tt AS
( select f.stdcode,
f.name,
f.degree_code,
f.phone,
MIN(f.startsem)as startsemdate,
MAX(f.startsem)as endsemedate
FROM V_GRADUATED f
GROUP BY
f.stdcode,
f.name,
f.degree_code,
f.phone)
select tt.stdcode,
tt.name,
tt.degree_code,
tt.phone,
(select sem_code
from v_graduated a
where a.stdcode=tt.stdcode
and a.startsem=tt.startsemdate) startsemester,
(select sem_code
from v_graduated a
where a.stdcode=tt.stdcode
and a.startsem=tt.endsemedate) endsemester
from tt
这就是我想要的......> 这是正确答案
enter image description here