我得到一个 ERROR subquery must return only one column。我该如何解决这个问题?
I get an ERROR subquery must return only one column. How do I solve this?
select count(distinct "WorkContracts"."SubstituteTeacher") as "Count", "Accounts"."FirstName", "Accounts"."LastName", "Candidates"."Confirmed", "Candidates"."FinalStatus"
from dbo."Accounts"
left join dbo."WorkContracts" on "Accounts"."ID" = "WorkContracts"."SubstituteTeacher"
full outer join dbo."SubstituteTeacherPeriods" on "Accounts"."ID" = "SubstituteTeacherPeriods"."CreatedBy"
full outer join dbo."Candidates" ON "Accounts"."ID" = "Candidates"."ModifiedBy"
where "WorkContracts"."WorkContractType" != 3
and "SubstituteTeacherPeriods"."StartTime" > TO_TIMESTAMP('2018-09-18 00:00:00', 'yyyy-MM-dd HH24:mi:ss')
and "SubstituteTeacherPeriods"."EndTime" < TO_TIMESTAMP('2018-09-19 00:00:00', 'yyyy-MM-dd HH24:mi:ss')
and "SubstituteTeacherPeriods"."Discriminator" = 'AvailabilityPeriod'
and "Candidates"."FinalStatus" = (select max("FinalStatus") from dbo."Candidates")
and "Candidates"."Confirmed"= (select max("Confirmed")"Confirmed", "SubstituteTeacher" from dbo."Candidates" group by "SubstituteTeacher")
group by "Accounts"."FirstName", "Accounts"."LastName", "Candidates"."FinalStatus", "Candidates"."Confirmed"
这似乎是导致错误的行:
and "Candidates"."Confirmed" = (select max("Confirmed")"Confirmed", "SubstituteTeacher"
from dbo."Candidates" group by "SubstituteTeacher")
首先,将单个标量值与具有 两个 值的记录进行比较没有意义。但是,比这更糟糕的是,RHS 上的子查询可能返回不止一条记录。尝试更改为:
AND "Candidates"."Confirmed" IN (SELECT MAX("Confirmed") FROM dbo."Candidates"
GROUP BY "SubstituteTeacher")
这是您在修复此问题后重构的整个查询:
SELECT
COUNT(DISTINCT wc.SubstituteTeacher) AS Count,
a.FirstName,
a.LastName,
c.Confirmed,
c.FinalStatus
FROM dbo.Accounts a
LEFT JOIN dbo.WorkContracts wc
ON a.ID = wc.SubstituteTeacher
FULL OUTER JOIN dbo.SubstituteTeacherPeriods s
ON a.ID = s.CreatedBy
FULL OUTER JOIN dbo.Candidates c
ON a.ID = c.ModifiedBy
WHERE
wc.WorkContractType != 3 AND
s.StartTime >= '2018-09-18 00:00:00' AND s.EndTime < '2018-09-19 00:00:00' AND
s.Discriminator = 'AvailabilityPeriod' AND
c.FinalStatus = (SELECT MAX(FinalStatus) FROM dbo.Candidates) AND
c.Confirmed IN (SELECT MAX(Confirmed) FROM dbo.Candidates
GROUP BY SubstituteTeacher)
GROUP BY
a.FirstName,
a.LastName,
c.FinalStatus,
c.Confirmed;
请注意,通过引入 table 别名并在各处删除不必要的双引号,查询变得更具可读性。
如果您想要使用 Confirmed
列的每位教师的最新记录,那么我们可以使用 ROW_NUMBER
:
WITH cte AS (
SELECT
COUNT(DISTINCT wc.SubstituteTeacher) AS Count,
a.FirstName,
a.LastName,
c.Confirmed,
c.FinalStatus,
ROW_NUMBER() OVER (PARTITION BY a.FirstName, a.LastName ORDER BY Confirmed DESC) rn
FROM dbo.Accounts a
... -- the rest of the above query
)
SELECT
Count,
FirstName,
LastName,
Confirmed,
FinalStatus
FROM cte
WHERE rn = 1;
select count(distinct "WorkContracts"."SubstituteTeacher") as "Count", "Accounts"."FirstName", "Accounts"."LastName", "Candidates"."Confirmed", "Candidates"."FinalStatus"
from dbo."Accounts"
left join dbo."WorkContracts" on "Accounts"."ID" = "WorkContracts"."SubstituteTeacher"
full outer join dbo."SubstituteTeacherPeriods" on "Accounts"."ID" = "SubstituteTeacherPeriods"."CreatedBy"
full outer join dbo."Candidates" ON "Accounts"."ID" = "Candidates"."ModifiedBy"
where "WorkContracts"."WorkContractType" != 3
and "SubstituteTeacherPeriods"."StartTime" > TO_TIMESTAMP('2018-09-18 00:00:00', 'yyyy-MM-dd HH24:mi:ss')
and "SubstituteTeacherPeriods"."EndTime" < TO_TIMESTAMP('2018-09-19 00:00:00', 'yyyy-MM-dd HH24:mi:ss')
and "SubstituteTeacherPeriods"."Discriminator" = 'AvailabilityPeriod'
and "Candidates"."FinalStatus" = (select max("FinalStatus") from dbo."Candidates")
and "Candidates"."Confirmed"= (select max("Confirmed")"Confirmed", "SubstituteTeacher" from dbo."Candidates" group by "SubstituteTeacher")
group by "Accounts"."FirstName", "Accounts"."LastName", "Candidates"."FinalStatus", "Candidates"."Confirmed"
这似乎是导致错误的行:
and "Candidates"."Confirmed" = (select max("Confirmed")"Confirmed", "SubstituteTeacher"
from dbo."Candidates" group by "SubstituteTeacher")
首先,将单个标量值与具有 两个 值的记录进行比较没有意义。但是,比这更糟糕的是,RHS 上的子查询可能返回不止一条记录。尝试更改为:
AND "Candidates"."Confirmed" IN (SELECT MAX("Confirmed") FROM dbo."Candidates"
GROUP BY "SubstituteTeacher")
这是您在修复此问题后重构的整个查询:
SELECT
COUNT(DISTINCT wc.SubstituteTeacher) AS Count,
a.FirstName,
a.LastName,
c.Confirmed,
c.FinalStatus
FROM dbo.Accounts a
LEFT JOIN dbo.WorkContracts wc
ON a.ID = wc.SubstituteTeacher
FULL OUTER JOIN dbo.SubstituteTeacherPeriods s
ON a.ID = s.CreatedBy
FULL OUTER JOIN dbo.Candidates c
ON a.ID = c.ModifiedBy
WHERE
wc.WorkContractType != 3 AND
s.StartTime >= '2018-09-18 00:00:00' AND s.EndTime < '2018-09-19 00:00:00' AND
s.Discriminator = 'AvailabilityPeriod' AND
c.FinalStatus = (SELECT MAX(FinalStatus) FROM dbo.Candidates) AND
c.Confirmed IN (SELECT MAX(Confirmed) FROM dbo.Candidates
GROUP BY SubstituteTeacher)
GROUP BY
a.FirstName,
a.LastName,
c.FinalStatus,
c.Confirmed;
请注意,通过引入 table 别名并在各处删除不必要的双引号,查询变得更具可读性。
如果您想要使用 Confirmed
列的每位教师的最新记录,那么我们可以使用 ROW_NUMBER
:
WITH cte AS (
SELECT
COUNT(DISTINCT wc.SubstituteTeacher) AS Count,
a.FirstName,
a.LastName,
c.Confirmed,
c.FinalStatus,
ROW_NUMBER() OVER (PARTITION BY a.FirstName, a.LastName ORDER BY Confirmed DESC) rn
FROM dbo.Accounts a
... -- the rest of the above query
)
SELECT
Count,
FirstName,
LastName,
Confirmed,
FinalStatus
FROM cte
WHERE rn = 1;