Oracle最新的Sql笔试题及答案

时间:2023-03-08 16:55:13

部门表(SM_DEPT)

字段名称 数据类型 是否主键 注释
DEPT_ID NUMBER Y 部门ID
PARENT_DEPARTMENT_ID NUMBER N 上级部门
DEPARTMENT_NAME VARCHAR2(50) N 部门名称

用户部门中间表(SM_USER_DEPT)

字段名称 数据类型 是否主键 注释
ID NUMBER Y 主键
DEPT_ID NUMBER N 部门ID
USER_ID NUMBER N 用户ID

用户表(SM_USER)

字段名称 数据类型 是否主键 注释
USER_ID NUMBER Y 用户ID
USER_NAME VARCHAR2(50) N 用户名称
LOGON_NAME VARCHAR2(50) N 登录名
IS_SALES VARCHAR2(1) N 是否为业务员

客户信息表(CTM_CUSTOMERS)

字段名称 数据类型 是否主键 注释
CUSTOMER_ID NUMBER Y 客户ID
CUSTOMER_NAME VARCHAR2(50) N 客户姓名
CITY VARCHAR2(50) N 所属城市
STATE VARCHAR2(25) N 所属州
ZIP_CODE VARCHAR2(10) N 邮政编码
CONTACT_NAME VARCHAR2(50) N 联系人
ADDRESS VARCHAR2(50) N 联系地址
TYPE VARCHAR2(50) N 客户类型
COMMENTS VARCHAR2(100) N 备注

订单头表(PIM_HEADERS)

字段名称 数据类型 是否主键 注释
PI_ID NUMBER Y 订单ID
PI_NO VARCHAR2(50) N 订单号
CREATION_DATE DATE N 创建日期
CUSTOMER_ID NUMBER N 关联客户表的客户ID
OPERATOR_ID NUMBER N 关联用户表的用户ID
CURRENCY_CODE VARCHAR2(50) N 订单币种
EXCHAHGE_RATE NUMBER N 汇率

订单行表(PIM_LINES)

字段名称 数据类型 是否主键 注释
PI_ID NUMBER N 订单ID
PI_LINES_ID VARCHAR2(50) Y 订单行ID
PRODUCT_CODE NUMBER N 产品编码
QUANTITY NUMBER N 产品数量
PRICE NUMBER N 产品单价

问题

1.编写SQL语句,查询PIM_HEADERS表,符合日期在2011-03-01和2011-03-25之间的PI订单,显示订单号,创建日志,订单客户名称,订单业务员名称

select
t1.PI_NO
,t1.CREATION_DATE
,t2.CUSTOM_NAME
,t3.USER_NAME
from (select
*
from PIM_HEADERS
where CREATION_DATE between
to_date('2011-03-01','yyyy-mm-dd')
and
to_date('2011-03-25','yyyy-mm-dd')
) t1
left join CTM_CUSTOMERS t2
on t1.CUSTOMER_ID = t2.CUSTOMER_ID
left join SM_USER t3
on t1.OPERATOR_ID = t3.USER_ID

2.编写SQL语句,查询所有部门名称及其给部门的业务员的个数,部门在2011-01-01到2011-03-01之间创建的PI订单数

select t1._DEPARTMENT_NAME,count(t2.USER_NAME),count(t3.PI_NO)
from SM_DEPT t1
left join SM_USER_DEPT t2
on t1.dept_id = t2.dept_id
left join (select
*
from PIM_HEADERS
where CREATION_DATE between
to_date('2011-01-01','yyyy-mm-dd')
and
to_date('2011-03-01','yyyy-mm-dd')
) t3
on t2.user_id = t3.OPERATOR_ID
group by t1.DEPT_ID

3.编写SQL语句,查询所有客户及其相关PI订单号,PI创建日期,PI订单相关业务员姓名及其业务所属部门名称

 select
t1.*
,t2.PI_NO
,t2.CREATION_DATE
,t3.USER_NAME
,t5.DEPARTMENT_NAME
from SM_CUSTOMERS t1
left join PIM_HEADERS t2
on t1.CUSTOER_ID = t2.CUSTOMER_ID
left join SM_USER t3
on t2.OPERATOR_ID = t3.user_id
left join SM_USER_DEPT t4
on t3.USER_ID = t4.USER_ID
left join SM_DEPT t5
on t4.DEPT_ID = t5.DEPT_ID

4.编写SQL语句,更新没有下过PI订单的客户名称的备注为“从未发生业务往来”

update CTM_CUSTOMERS t1 set COMMENTS = '从未反生业务往来'
where not exists (
select PI_NO from PIM_HEADERS t2
where t2.CUSTOMER_ID = t1.CUSTOMER_ID
) ;
commit;

5.编写SQL语句,查询所有客户名称及其所属城市,并按照城市名称降序,用户名称升序排序

 select CUSTOM_NAME,CITY
from CTM_CUSTOMERS
order by city desc,CUSTOMER_NAME asc

6.编写SQL语句,查询出所有的客户的PI订单的下单金额,显示客户名称,订单币种,订单总数量及订单总额

 select
t2.CUSTOMER_NAME
,t1.CURRENCY_CODE
,t3.TOTAL
,t4.TOTAL_MONEY
from PIM_HEADERS t1
left join CTM_CUSTOMERS t2
on t1.CUSTOMER_ID = t2.CUSTOMER_ID
left join (
select PI_ID,sum(QUANTITY) TOTAL,sum(QUANTITY*PRICE) TOTAL_MONEY
from PIM_LINES
group by PI_ID ) t3
on t1.PI_ID = t3.PI_ID;

7.编写函数,函数需传入部门ID,返回结果部门对应所有业务员姓名连接起来,中间用逗号隔开

create or replace function getUserByDeptId
( dept_id number) return varchar2
is
res varchar2(200);
cursor mycursor(did number) is
select t2.CUSTOMER_NAME
from (select *
from SM_USER_DEPT
where dept_id = did
) t1
left join SM_USER t2
on t1.USER_ID = t2.USER_ID ; begin
for c_row in mycursor(dept_id) loop
res := res ||','|| c_row
end loop;
return res;
end;