派生 table 在 where 命令中无法识别
Derived table not recognised in where command
我是新手,还在学习所以请原谅我的代码。我已经用谷歌搜索并作为最后的手段发帖寻求帮助。希望哪位资深程序员能帮忙
我想要达到的目标:哪门课程的学生人数低于平均水平?
我正在使用 SQL Server Management Studio v18.
我的代码不起作用,我不明白为什么派生的 table 没有被接受。我知道我可以创建另一个子查询并实现它,但仍然想了解为什么派生 table C 在这种情况下不起作用?
请帮忙解释并建议实现此查询的最简单方法。
select
c.course, c.num_of_students
from
(select
course, count(pname) as num_of_students
from
Studies
group by
COURSE) c
where
c.num_of_students < (select avg(c.num_of_students) from c);
我收到这个错误:
Invalid object name 'c'
这个无效的对象名称在 WHERE 子句行中突出显示。
数据在预览中正确显示。
数据:
PNAME
INSTITUTE
COURSE
COURSEFEE
ANAND
SABHARI
PGDCA
4500
ALTAF
COIT
DCA
7200
JULIANA
BDPS
MCA
22000
KAMALA
PRAGATHI
DCA
5000
MARY
SABHARI
PGDCA
4500
NELSON
PRAGATHI
DAP
6200
PATRICK
PRAGATHI
DCAP
5200
QADIR
APPLE
HDCA
14000
RAMESH
SABHARI
PGDCA
4500
REBECCA
BRILLIANT
DCAP
11000
REMITHA
BDPS
DCS
6000
REVATHI
SABHARI
DAP
5000
VIJAYA
BDPS
DCA
48000
派生的 table 在一个查询中的范围有限,并且只能在 FROM
之后被引用一次。您有几种选择,其中最好的是 CTE:
WITH c AS (
SELECT
course, COUNT(pname) as num_of_students
FROM Studies
GROUP BY course
)
SELECT
c.course, c.num_of_students
FROM c
WHERE
c.num_of_students < (SELECT AVG(c.num_of_students) FROM c);
其他选项,尽管在这种情况下可能不太理想,除非您需要在其他查询中使用派生 table 中的结果,但可以定义临时 table 或 table变量:
温度table:
DROP TABLE IF EXISTS #c;
SELECT
course, COUNT(pname) as num_of_students
INTO #c
FROM Studies
GROUP BY course;
SELECT
c.course, c.num_of_students
FROM #c c
WHERE
c.num_of_students < (SELECT AVG(c.num_of_students) FROM c);
DROP TABLE #c;
Table变量:
DECLARE @c TABLE (
course VARCHAR(100),
num_of_students INT
);
INSERT @c (course, num_of_students)
SELECT course, COUNT(pname)
FROM Studies
GROUP BY course;
SELECT
c.course, c.num_of_students
FROM @c c
WHERE
c.num_of_students < (SELECT AVG(c.num_of_students) FROM c);
我是新手,还在学习所以请原谅我的代码。我已经用谷歌搜索并作为最后的手段发帖寻求帮助。希望哪位资深程序员能帮忙
我想要达到的目标:哪门课程的学生人数低于平均水平?
我正在使用 SQL Server Management Studio v18.
我的代码不起作用,我不明白为什么派生的 table 没有被接受。我知道我可以创建另一个子查询并实现它,但仍然想了解为什么派生 table C 在这种情况下不起作用?
请帮忙解释并建议实现此查询的最简单方法。
select
c.course, c.num_of_students
from
(select
course, count(pname) as num_of_students
from
Studies
group by
COURSE) c
where
c.num_of_students < (select avg(c.num_of_students) from c);
我收到这个错误:
Invalid object name 'c'
这个无效的对象名称在 WHERE 子句行中突出显示。
数据在预览中正确显示。
数据:
PNAME | INSTITUTE | COURSE | COURSEFEE |
---|---|---|---|
ANAND | SABHARI | PGDCA | 4500 |
ALTAF | COIT | DCA | 7200 |
JULIANA | BDPS | MCA | 22000 |
KAMALA | PRAGATHI | DCA | 5000 |
MARY | SABHARI | PGDCA | 4500 |
NELSON | PRAGATHI | DAP | 6200 |
PATRICK | PRAGATHI | DCAP | 5200 |
QADIR | APPLE | HDCA | 14000 |
RAMESH | SABHARI | PGDCA | 4500 |
REBECCA | BRILLIANT | DCAP | 11000 |
REMITHA | BDPS | DCS | 6000 |
REVATHI | SABHARI | DAP | 5000 |
VIJAYA | BDPS | DCA | 48000 |
派生的 table 在一个查询中的范围有限,并且只能在 FROM
之后被引用一次。您有几种选择,其中最好的是 CTE:
WITH c AS (
SELECT
course, COUNT(pname) as num_of_students
FROM Studies
GROUP BY course
)
SELECT
c.course, c.num_of_students
FROM c
WHERE
c.num_of_students < (SELECT AVG(c.num_of_students) FROM c);
其他选项,尽管在这种情况下可能不太理想,除非您需要在其他查询中使用派生 table 中的结果,但可以定义临时 table 或 table变量:
温度table:
DROP TABLE IF EXISTS #c;
SELECT
course, COUNT(pname) as num_of_students
INTO #c
FROM Studies
GROUP BY course;
SELECT
c.course, c.num_of_students
FROM #c c
WHERE
c.num_of_students < (SELECT AVG(c.num_of_students) FROM c);
DROP TABLE #c;
Table变量:
DECLARE @c TABLE (
course VARCHAR(100),
num_of_students INT
);
INSERT @c (course, num_of_students)
SELECT course, COUNT(pname)
FROM Studies
GROUP BY course;
SELECT
c.course, c.num_of_students
FROM @c c
WHERE
c.num_of_students < (SELECT AVG(c.num_of_students) FROM c);