oracle-case

Oracle-case 笔试题

教师号  星期号 是否有课
 1    2   有
 1    3   有
 2    1   有
 3    2   有
 1    2   有

写一条sql语句变为这样的表

教师号 星期一 星期二 星期三
 1       2   1 
 2   1   
 3       1

各星期下的数字表示:对应的教师在星期几已经排的课数

解:
1、先做出这样的一个表:

CREATE TABLE t_teacher (t_id NUMBER,t_q NUMBER ,t_yn VARCHAR2(10));
INSERT INTO t_teacher VALUES(1,2,'有');
INSERT INTO t_teacher VALUES(1,3,'有');
INSERT INTO t_teacher VALUES(2,1,'有');
INSERT INTO t_teacher VALUES(3,2,'有');
INSERT INTO t_teacher VALUES(1,2,'有');
COMMIT;

2、查询

select t_id 教师号,
sum(case when t_yn='有' and t_q=1 then 1 else 0 end) 星期一,
sum(case when t_yn='有' and t_q=2 then 1 else 0 end) 星期二,
sum(case when t_yn='有' and t_q=3 then 1 else 0 end) 星期三,
sum(case when t_yn='有' and t_q=4 then 1 else 0 end) 星期四,
sum(case when t_yn='有' and t_q=5 then 1 else 0 end) 星期五,
sum(case when t_yn='有' and t_q=6 or t_q=7 then 1 else 0 end) 星期日
from t_teacher
group by t_id;

结果:

Oracle error 00904 typically occurs when an invalid SQL statement is executed, specifically due to the use of an invalid identifier or column name in the query. This error is formally described as "invalid identifier", which means that the database engine cannot recognize a column name or table alias used in the SQL statement [^1]. ### Common Causes and Explanations 1. **Typographical Errors in Column or Table Names** A common cause of this error is a misspelled column or table name within the SQL query. Oracle SQL is case-insensitive for SQL keywords but case-sensitive for identifiers if they are quoted. For instance, using `SELECT emloyee_id FROM employees` will trigger this error because `emloyee_id` is not a valid column name. 2. **Use of Reserved Keywords as Identifiers** Using Oracle reserved keywords as column names or table names without proper quotation can cause this error. For example, naming a column `ORDER` without enclosing it in double quotes can confuse the SQL parser. 3. **Improper Use of Quotation Marks** When identifiers are defined with mixed or uppercase letters during table creation, they must be enclosed in double quotes when referenced. For instance, a column created as `"EmployeeID"` must be queried as `SELECT "EmployeeID" FROM employees`. 4. **Missing or Improper Aliasing in Joins** In complex queries involving multiple tables, failing to define or reference table aliases correctly may lead to this error. ### Resolution Strategies 1. **Verify Column and Table Names** Carefully check the SQL statement for any typographical errors in column or table names. Cross-referencing the schema definition is a good practice to ensure the correct usage of identifiers. Example: ```sql -- Correct usage SELECT employee_id FROM employees; ``` 2. **Avoid Reserved Keywords** Avoid using Oracle reserved keywords as identifiers. If unavoidable, enclose them in double quotes to inform the SQL parser. Example: ```sql -- Avoid using reserved keywords CREATE TABLE orders ( id NUMBER, "ORDER" VARCHAR2(50) -- Properly quoted reserved keyword ); ``` 3. **Use Double Quotes for Case-Sensitive Identifiers** When identifiers were defined with case sensitivity, always use double quotes to reference them in queries. Example: ```sql -- Querying a case-sensitive column name SELECT "EmployeeID" FROM "Employees"; ``` 4. **Correctly Use Table Aliases** Define and reference table aliases properly in complex queries to avoid ambiguity. Example: ```sql -- Proper use of table aliases SELECT e.employee_id, d.department_name FROM employees e JOIN departments d ON e.department_id = d.department_id; ``` By addressing these aspects of SQL query construction, the Oracle error 00904 can be effectively resolved. Ensuring proper syntax and identifier usage is key to preventing this error from occurring during database operations. ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值