Oracle最新的Sql笔试题及答案

部门表(SM_DEPT)

字段名称数据类型是否主键注释
DEPT_IDNUMBERY部门ID
PARENT_DEPARTMENT_IDNUMBERN上级部门
DEPARTMENT_NAMEVARCHAR2(50)N部门名称

用户部门中间表(SM_USER_DEPT)

字段名称数据类型是否主键注释
IDNUMBERY主键
DEPT_IDNUMBERN部门ID
USER_IDNUMBERN用户ID

用户表(SM_USER)

字段名称数据类型是否主键注释
USER_IDNUMBERY用户ID
USER_NAMEVARCHAR2(50)N用户名称
LOGON_NAMEVARCHAR2(50)N登录名
IS_SALESVARCHAR2(1)N是否为业务员

客户信息表(CTM_CUSTOMERS)

字段名称数据类型是否主键注释
CUSTOMER_IDNUMBERY客户ID
CUSTOMER_NAMEVARCHAR2(50)N客户姓名
CITYVARCHAR2(50)N所属城市
STATEVARCHAR2(25)N所属州
ZIP_CODEVARCHAR2(10)N邮政编码
CONTACT_NAMEVARCHAR2(50)N联系人
ADDRESSVARCHAR2(50)N联系地址
TYPEVARCHAR2(50)N客户类型
COMMENTSVARCHAR2(100)N备注

订单头表(PIM_HEADERS)

字段名称数据类型是否主键注释
PI_IDNUMBERY订单ID
PI_NOVARCHAR2(50)N订单号
CREATION_DATEDATEN创建日期
CUSTOMER_IDNUMBERN关联客户表的客户ID
OPERATOR_IDNUMBERN关联用户表的用户ID
CURRENCY_CODEVARCHAR2(50)N订单币种
EXCHAHGE_RATENUMBERN汇率

订单行表(PIM_LINES)

字段名称数据类型是否主键注释
PI_IDNUMBERN订单ID
PI_LINES_IDVARCHAR2(50)Y订单行ID
PRODUCT_CODENUMBERN产品编码
QUANTITYNUMBERN产品数量
PRICENUMBERN产品单价

问题

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;
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 程序猿惹谁了 设计师:白松林 返回首页
实付 29.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值