有如下2数据表
table1
--------------------------------------
  编号    姓名    科室     学历   岁数   ...
  001     张三    财务     中专    20   ... 
  002     李四    财务     大专    25   ...
  003     王五    商务     本科    24   ...
  004     李二    商务     大专    32   ...
  005     吴立    商务     中专    19   ...
  006     钱明    设备     本科    27   ...
--------------------------------------
table2
--------------------------------------
  编号    姓名    科室     学历    工作经验 ...
  001     林一    财务     中专      1年   ...
  002     王四    财务     大专      3年   ...
  003     张五    商务     本科      2年   ...
  004     李八    商务     大专      9年   ...
  005     吴二    商务     中专      2年   ...
  006     明一    设备     本科      5年   ...
--------------------------------------
现在我要求统计如下的结果:
--------------------------------------
  科室    中专     大专    本科 
--------------------------------------
  财务     2        2      
  商务              4       2
  设备                      2
--------------------------------------
请问SQL语句咋写?

解决方案 »

  1.   

    select 科室, '中专', '大专', '本科' form table
      union 
    select  科室, sum(中专), sum(大专), sum(本科) from table
    group by 科室这样行不行?
      

  2.   

    hehe,不管对与错,我都表示谢谢,但是这不对。
    我知道一个表的SQL查询语句,但是两个表就不知道了。
    一个表的查询:
    select 科室,
    (select count(*) from table1 b where a.科室 = b.科室 and 学历 = '中专'),
    (select count(*) from table1 b where a.科室 = b.科室 and 学历 = '大专'),
    (select count(*) from table1 b where a.科室 = b.科室 and 学历 = '本科')
    from table1 a
    group by 科室
      

  3.   

    如果没有同名现象,可以为:
    select 科室 count(学历)
    where
    (select 姓名    科室     学历
    from table1,table2
    where table1.姓名<>table2.姓名)
    group by( 科室)
    不知道对不对,但大概就是这个意思!
      

  4.   

    打错了
    select 科室 count(学历)
    from
    (select 姓名    科室     学历
    from table1,table2
    where table1.姓名<>table2.姓名)
    group by( 科室)
      

  5.   

    Select tmp.科室, Sum(tmp.中专) as 中专, Sum(tmp.大专) as 大专, Sum(tmp.本科) as 本科
    From (
    select 科室, 
           Sum(Case 学历 When '中专' then 1 else 0 end) 中专,
           Sum(Case 学历 When '大专' then 1 else 0 end) 大专,
           Sum(Case 学历 When '本科' then 1 else 0 end) 本科
    From Table1
    Group by 科室
    Union ALL
    select 科室, 
           Sum(Case 学历 When '中专' then 1 else 0 end) 中专,
           Sum(Case 学历 When '大专' then 1 else 0 end) 大专,
           Sum(Case 学历 When '本科' then 1 else 0 end) 本科
    From Table2
    Group by 科室
    ) Tmp 
    Group by tmp.科室
    Order by tmp.科室
      

  6.   

    使用交叉表可以的
    试试:
    TRANSFORM Count([表1].[编号])
    SELECT [表1].[科室], Count([表1].[编号]) AS [总计 编号]
    FROM 表1
    GROUP BY [表1].[科室]
    PIVOT [表1].[学历];
    自己改改
      

  7.   

    我的应该行  如果可以马上给分
    select 科室,
    (select count(*) from (select 科室,学历 from table1 union select 科室,学历 from table2)   where  学历 = '中专') as 中专,
    (select count(*) from (select 科室,学历 from table1 union select 科室,学历 from table2)   where  学历 = '大专') as 大专,
    (select count(*) from (select 科室,学历 from table1 union select 科室,学历 from table2)   where  学历 = '本科') as 本科
    from (select 科室,学历 from table1 union select 科室,学历 from table2)
      

  8.   

    对不起   把union 改为union all
    在科室前加 一个 distinct
      

  9.   

    这是一个交叉查询的问题,以前也有人问过。
    Select distinct 科室 ,
           (Select count(*) from table2 where 科室=table1.科室 and 学历 = 大专) as 大专,
           (Select count(*) from table2 where 科室=table1.科室 and 学历 = 中专) as 中专,
           (Select count(*) from table2 where 科室=table1.科室 and 学历 = 本科) as 本科
    from table1
    group by 科室
      

  10.   

    还是对不起 我把最后结果给你  我已经测试过了
    select distinct 科室,
    (select count(*) from (select 科室,学历 from table1 union all select 科室,学历 from table2) a  where  a.学历 = '中专') as 中专,
    (select count(*) from (select 科室,学历 from table1 union all select 科室,学历 from table2) b  where  b.学历 = '大专') as 大专,
    (select count(*) from (select 科室,学历 from table1 union all select 科室,学历 from table2) c where  c.学历 = '本科') as 本科
    from (select 科室,学历 from table1 union select 科室,学历 from table2)
      

  11.   

    首先将两个表合成一个表,再做操作
    select 科室,
    (select count(*) from table1 b where a.科室 = b.科室 and 学历 = '中专'),
    (select count(*) from table1 b where a.科室 = b.科室 and 学历 = '大专'),
    (select count(*) from table1 b where a.科室 = b.科室 and 学历 = '本科')
    from (select 姓名,科室,学历 from table1  union select 姓名,科室,学历 from table2) a
    group by 科室
    类似SQL语句在oracle中执行通过
      

  12.   

    不知道Table1和Table2之间有任何关系? 有重覆纪录吗?
      

  13.   

    对不起大家,我现在才上来。
    1.我的两个表没有重复的记录,而且不能合为一个表。
    2.对于今天留言的的朋友都有分.小弟在这里谢过.我想了个语句,而且在SQL中调试通过,\运用了临时表的技术.
    select distinct(aa.科室) as 科室,sum(aa.中专) as 中专,
    sum(aa.大专) as 大专,sum(aa.本科) as 本科 from
    ((select 科室,
    (select count(*) from table1 b where a.科室 = b.科室 and 学历 = '中专'),
    (select count(*) from table1 b where a.科室 = b.科室 and 学历 = '大专'),
    (select count(*) from table1 b where a.科室 = b.科室 and 学历 = '本科') 
    from table1 a group by 科室)
    union
    ((select 科室,
    (select count(*) from table2 b where a.科室 = b.科室 and 学历 = '中专'),
    (select count(*) from table2 b where a.科室 = b.科室 and 学历 = '大专'),
    (select count(*) from table b where a.科室 = b.科室 and 学历 = '本科') 
    from table2 a group by 科室)
    as aa group by aa.科室其中aa为临时表名但是我在这里有几个问题请教大家,因为又出现了新问题.
    1.因为我的表还有个字段是时间,即每个人加如公司的时间,例如2000-02-16
    现在我想知道在某段时间假如公司的人.查询的时候出现了问题.
    请问怎样能解决呢?
      

  14.   

    在delphi中我加入了时间查询,老是报告我出现了" 非法字段".
      

  15.   

    如果沒猜錯的話,之所以這麼難的原因在於你的數據庫表格設計存在問題,
    如果設計合理的話,任何查詢要求都可以做到,
    說實話,看你的表格覺得很別扭條件允許的話還是改表吧,當然如果是要為別人的遺作打補丁的話,那真的
    是難為你了
    我不太熟悉高難度的技術,我一般都是通過多步操作解決這種問題的
    比如臨時表,比如循環,不要總求一步到位上面各位存在一個問題,即各位總是把學歷值(大專,本科等)寫入條件裡,
    那如果再多幾個學位(如博士),是不是又要更改這段SQL語句?
      

  16.   

    Sum(Case 学历 When '中专' then 1 else 0 end) 中专,
           Sum(Case 学历 When '大专' then 1 else 0 end) 大专,
           Sum(Case 学历 When '本科' then 1 else 0 end) 本科
    你用程序构造上面的语句就可以解决你提到的问题(增加博士等)
      

  17.   

    1.其实我的两个表是前几天看到论坛上有人post的题目,我突然想到我自己写的后台数据库中有类似的表,就把别人的表作为例子来问了。
        2.以上几位朋友提出的建议都很好,我以后也会在建立后台数据库和写查询语言的时候多往这方面考虑。
        3.我系统数据库用了一同是8张表,有一些字段是相同的,但是每个表又有完全不同的字段,而且有严格的制度要求必须分开,这涉及到政府的规章制度和某些专门法律,不能改变,正所谓“投鼠忌器”。
        以上各位的解答和忠告,小弟我会记住的,再次表示感谢!!!
      

  18.   

    那这样吧--不知行不行,用delphi 自带的SQL BUILDER试试,也许会解决的.