Oracle SQL - 帮助在 Select 语句中使用 Case
CREATE TABLE student_totalexp2 nologging compress AS
SELECT /*+parallel(a,4)*/ DISTINCT a.member_sk,
CASE
WHEN b.end_date IS NULL THEN
SYSDATE - MIN(TO_DATE(b.start_date,'yyyymm'))
ELSE
(MAX(TO_DATE(b.end_date,'yyyymm')) - MIN(TO_DATE(b.start_date,'yyyymm')))
END as days_experience
FROM student_schools a
JOIN rdorwart.position_rd b ON a.member_sk = b.member_sk
WHERE days_experience < 730
GROUP BY a.member_sk;
SELECT COUNT(*)
FROM student_experience;
知道为什么我不断收到此错误:错误报告:
SQL 错误:ORA-00904:“DAYS_EXPERIENCE”:无效标识符 00904。00000 -“%s:无效标识符” *原因:
*行动:
CREATE TABLE student_totalexp2 nologging compress AS
SELECT /*+parallel(a,4)*/ DISTINCT a.member_sk,
CASE
WHEN b.end_date IS NULL THEN
SYSDATE - MIN(TO_DATE(b.start_date,'yyyymm'))
ELSE
(MAX(TO_DATE(b.end_date,'yyyymm')) - MIN(TO_DATE(b.start_date,'yyyymm')))
END as days_experience
FROM student_schools a
JOIN rdorwart.position_rd b ON a.member_sk = b.member_sk
WHERE days_experience < 730
GROUP BY a.member_sk;
SELECT COUNT(*)
FROM student_experience;
Any idea why I keep getting this error: Error report:
SQL Error: ORA-00904: "DAYS_EXPERIENCE": invalid identifier 00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能在
WHERE
子句中引用别名。要么使用子查询,要么最好将整个CASE...END
放入 where 子句中。根据OP的评论更新查询:
You cannot reference a alias in the
WHERE
clause. Either use a subquery, or better just the entireCASE...END
into your where clause.Updated query per OP's comments:
下面是问题中查询的直接简化,将 MAX(任何行)与 MIN(任何行)进行比较。 Scrum Meister 的答案修复了OP 的逻辑也是如此,以正确地满足工作之间的差距。
This should be all you need. Having the student_schools table JOINed seems to add no value, unless there are cases where position_rd records exist without a
student_schools
record.如果您确实需要验证
student_schools
- 只需向其添加 INNER JOIN 即可。其他地方都不需要它。The below is a direct simplification of the query in the question, taking MAX (any row) against MIN (any row). The Scrum Meister's answer fixes the OP's logic as well, to correctly cater for gaps between jobs.
This should be all you need. Having the student_schools table JOINed seems to add no value, unless there are cases where position_rd records exist without a
student_schools
record.If you do need to validate
student_schools
- just add an INNER JOIN to it. Nowhere else is it needed.您不能直接在 where 子句中使用字段的 guiven 名称。您需要重复where子句中的逻辑
you cannot use guiven name of the field directly in where clause. You need to repeat the logic in where clause
对于简短且更具可读性的代码,请使用外部 SELECT:
For short and more readable code, use an outer SELECT: