日期:2014-05-17  浏览次数:20829 次

问一道SQL笔试题,谢谢!
题目如下:
CREATE TABLE table1(
  FID int,//记录id
  FName varchar(10),//员工姓名
  FDate datetime,//日期
  FSalary float//金额
)
表记录了公司内所有员工的薪水发放记录(注:薪水可能是按周发放,也可能是按月、按日发放),数据可能跨年。
(1)现要求得出名为“张三”的员工在本年所得薪水总和,请写出实现的sql语句
(2)现要求计算出每个员工的按年,月统计的薪水报表,请写出实现的sql语句
(3)现要求得出月薪水介于500-800的员工姓名(要求名字不重复,含500和800)


------解决方案--------------------
SQL code
-- JUST TRY IT ..

-- 1:
SELECT SUM(FSALARY) "SALARYS"
  FROM TABLE1
 WHERE FNAME = '张三'
   AND TO_CHAR(FDATE,'YYYY')=TO_CHAR(SYSDATE,'YYYY');
   
-- 2:
SELECT TO_CHAR(FDATE,'YYYY') "YEAR",
       TO_CHAR(FDATE,'YYYYMM') "MONTH",
       SUM(FSALARY) "SALARYS"
  FROM TABLE1
 GROUP BY TO_CHAR(FDATE,'YYYY'),TO_CHAR(FDATE,'YYYYMM');
 
-- 3:
SELECT DISTINCT
       FNAME
  FROM (SELECT FNAME,
               TO_CHAR(FDATE,'YYYY') "YEAR",
               TO_CHAR(FDATE,'YYYYMM') "MONTH",
               SUM(FSALARY) "SALARYS"
          FROM TABLE1
         GROUP BY FNAME,
                  TO_CHAR(FDATE,'YYYY'),
                  TO_CHAR(FDATE,'YYYYMM')
       )TT
 WHERE SALARYS BETWEEN 500 AND 800;