初学SQL*PLUS,写的这个表查询运行很慢,结果一定是错的,为什麽 ?!
问题描述:
创建一个查询显示雇员总数,和在 1995、1996、1997 和1998受雇的雇员人数。创建适当的列标题。 SELECT COUNT(a.employee_id) AS"TATAL",
        COUNT(b.employee_id) AS"1995",
        COUNT(c.employee_id) AS"1996",
        COUNT(d.employee_id) AS"1997",
        COUNT(e.employee_id) AS"1998"
 FROM   employees a,
         (SELECT employee_id FROM employees WHERE hire_date > '31-12月-95') b,
         (SELECT employee_id FROM employees WHERE hire_date > '31-12月-96') c,
      (SELECT employee_id FROM employees WHERE hire_date > '31-12月-97') d,
      (SELECT employee_id FROM employees WHERE hire_date > '31-12月-98') e

解决方案 »

  1.   

    还有输出格式示例:

    TOTAL |  1995|  1996|  1997|  1998
        100     20     20     30    30 

      

  2.   

    select to_char(hiredate, 'yyyy-mm-dd'), count(1)
      from employee
     where hire_date between to_date('1995-01-01', 'yyyy-mm-dd') and
           to_date('1998-12-31', 'yyyy-mm-dd')
     group by to_char(hiredate, 'yyyy-mm-dd')
      

  3.   

    你这是笛卡尔积啊,当然慢。用这种写法:SELECT COUNT(a.employee_id) AS"TATAL",
           sum(case when a.hire_date > date '1995-01-01' then 1 else 0 end) 1995,
           sum(case when a.hire_date > date '1996-01-01' then 1 else 0 end) 1996,
           sum(case when a.hire_date > date '1997-01-01' then 1 else 0 end) 1997,
           sum(case when a.hire_date > date '1998-01-01' then 1 else 0 end) 1998,
     FROM   employees a
      

  4.   

    lz佩服你的sql啊,呵呵~~真的是初学者啊!
      

  5.   

    SELECT COUNT(a.employee_id) AS"TATAL",
           sum(case when a.hire_date > date '1995-01-01' then 1 else 0 end) 1995,
           sum(case when a.hire_date > date '1996-01-01' then 1 else 0 end) 1996,
           sum(case when a.hire_date > date '1997-01-01' then 1 else 0 end) 1997,
           sum(case when a.hire_date > date '1998-01-01' then 1 else 0 end) 1998,
     FROM   employees a
      

  6.   

    TO_CHAR(HI   COUNT(1)
    ---------- ----------
    1995-05-01          1
    1995-05-18          1
    1995-07-14          1
    1995-10-17          1
    1996-01-27          1
    1996-01-30          1
    1996-02-04          1
    1996-02-17          1
    1996-03-04          1
    1996-05-11          1
    1996-06-14          1
    格式不是要这样子的。
      

  7.   

    codearts的查询句子有错:ERROR 位于第 2 行:
    ORA-00923: 未找到预期 FROM 关键字
      

  8.   

    多了个,号SELECT COUNT(a.employee_id) AS"TATAL",
           sum(case when a.hire_date > date '1995-01-01' then 1 else 0 end) 1995,
           sum(case when a.hire_date > date '1996-01-01' then 1 else 0 end) 1996,
           sum(case when a.hire_date > date '1997-01-01' then 1 else 0 end) 1997,
           sum(case when a.hire_date > date '1998-01-01' then 1 else 0 end) 1998
     FROM   employees a
      

  9.   

    格式是这个样子地:

         TATAL       1995       1996       1997       1998
    ---------- ---------- ---------- ---------- ----------
    1161763200 1161763200 1161763200 1161763200 1161763200
      

  10.   

    select count(a.employee_id) as "tatal"
      sum(case when a.hire_date>date'1995-01-01 then 1 else 0 end')1995,
       .
       .
       .
    from employees a;
      

  11.   

    是那别名那里有错的呵,我该过了!谢谢codearts!^_^