是必须要把三个count ,sum统计出来再比较取最大值?
-----
是的!把统计出来的结果写成派生表的形式,再进行统计!

解决方案 »

  1.   

    -------
    select max(count(field1),count(field2),count(field3)),max(sum(field1),sum(field2),sum(field3)) from 表
      

  2.   

    那我说清楚一点: 是这样的: 比如,我要得到一个值,和这个值相关的是三个count,这三个count分别是统计符合 a,b,c 的各有多少个记录值,不就是三个select count(*)...语句吗?我现在想要得到这三个count值的最大值,我想问的是有没有SQL语句能直接实现的,因为我有很多要统计,如果都统计出来再排列大小,太麻烦了
      

  3.   

    恩,我的意思就是象wzh1215这位说的这样的,但是有一点不一样,我统计的count不是某一列,而是select count(*) from 表 where ....这样的,请问怎么写呢?
      

  4.   

    select max(counts)
    from
    (
    select count(*) counts from 表 where ....
    union all
    select count(*) from 表 where ....
    union all
    select count(*) from 表 where ....
    ) tmp
      

  5.   

    (
    select count(*) counts from 表 where ....
    union all
    select count(*) from 表 where ....
    union all
    select count(*) from 表 where ....
    ) 表的别名
      

  6.   

    一个或多个派生表,这些派生表是 FROM 子句中的 SELECT 语句,以别名或用户指定的名称来引用这些派生表。FROM 子句中 SELECT 语句的结果集构成了外层 SELECT 语句所用的表。例如,下面的 SELECT 语句使用派生表查找是否有哪家书店备有 pubs 数据库中所有种类的书籍: 
    SELECT ST.stor_id, ST.stor_name
    FROM stores AS ST,
         (SELECT stor_id, COUNT(DISTINCT title_id) AS title_count
          FROM sales
          GROUP BY stor_id
         ) AS SA
    WHERE ST.stor_id = SA.stor_id
      AND SA.title_count = (SELECT COUNT(*) FROM titles)
      

  7.   

    错误已经解决,是飞扬的梦所说的没有表的别名导致的,我将表的别名写成数据库中的一个表名,就正确了,不过不知道这样会不会对那张表有什么影响?问题是我得到最大值的同时能不同知道这个最大值是哪个count的结果呢??
      

  8.   

    表的别名可以任一的,并不一定要数据库中有此表名!
    select max(counts)
    from
    (
    select count(*) counts from 表 where 条件1
    union all
    select count(*) from 表 where 条件2
    union all
    select count(*) from 表 where 条件3
    ) tmp
    --这样不是可以得到最大值的吗!
      

  9.   

    是这样的,我写tmp就说不存在列tmp,把tmp换成数据库中的表就正确了,能得到结果了
      

  10.   

    求count的最大值是正确的,但是求sum的最大值时说sum的参数不能为bit类型,可是我要统计他的和,不能用sum了吗??