select 人名,sum(case when 预计完成时间 >= 实际完成时间 then 1 else 0),sum(case when 预计完成时间 < 实际完成时间 then 1 else 0)
from 工单表
where 际完成时间 between '2002-6-1' and '2002-7-1'
group by 人名没有测试,基本上是这个方法了,试一试吧。

解决方案 »

  1.   

    建立事件:
    function getDelayRecord(othQueyr:TADOQuery;xm:string;Data1,Data2:TDataTime):integer;在DataSet中加入所有要显示的字段,另建一个Calculated字段,
    在onCalcFields事件中写下列语句:
    Text:=IntToStr(getDelayRecord(othQuery,DataSet.FieldByName('人名').asstring,Data1,Data2);
      

  2.   

    楼上思路正确:select 人名,count(*) as 6月份完成工单数,sum(case when 预计完成时间 > 实际完成时间 then 1 else 0) as 6月份超期工单数
    from 工单表
    where 实际完成时间 between '2002-6-1' and '2002-7-1'
    group by 人名
      

  3.   

    哦,呵呵,是Yang_(扬帆破浪) (  ) 的对,我取得的是按期和超期的,不完全符合楼主的需要。to: Yang_(扬帆破浪)
         怎么不作Sqlserver版主了吗。
      

  4.   

    这个SQL查询应该是这个样子:select 人名,count(*) as 6月份完成工单数,sum(预计完成时间) as 6月份超期工单数 from 工单表 while 预计完成时间 > 实际完成时间
      

  5.   

    to:lyxinfo(雒) 
      比较忙,所以就辞了。
      

  6.   

    select 人名,count(*) as 6月份完成工单数,sum(case when 预计完成时间 > 实际完成时间 then 1 else 0) as 6月份超期工单数
    from 工单表
    where 实际完成时间 between '2002-6-1' and '2002-7-1'
    group by 人名
      

  7.   

    扬帆破浪和lyxinfo的查询命令是对的,在sql7中运行正常,但在程序中运行出错:invalid use of keyword ,Token:WHEN,
    我用的是BDE+BCB5.
    请二位再帮忙。
      

  8.   

    select T1.人名,
    (select count (*) from 公单表 as T2 where 预计完成时间>=实际完成时间 and T1.人名=T2.人名) as 定单数, 
    (select count(*) from 公单表 asT2 where 预计完成时间<实际完成时间
    and T1.人名=T2.人名) as 超期数
    from 工单表 as T1
    where T1.实际完成时间 between '2002-6-1' and '2002-7-1'
    order by 人名
    避免when出错
      

  9.   

    看来是BDE不支持CASE WHEN !?
    用zhxfzhxf1(zhxfzhxf1) 的语句试一下,不过应该加DISTINCT,避免重复。select DISTINCT T1.人名,
    (select count (*) from 公单表 as T2 where 预计完成时间>=实际完成时间 and T1.人名=T2.人名) as 定单数, 
    (select count(*) from 公单表 asT2 where 预计完成时间<实际完成时间
    and T1.人名=T2.人名) as 超期数
    from 工单表 as T1
    where T1.实际完成时间 between '2002-6-1' and '2002-7-1'
    order by 人名
      

  10.   

    select T1.人名,
    (select count (*) from 公单表 as T2 where 预计完成时间>=实际完成时间 and T1.人名=T2.人名) as 定单数, 
    (select count(*) from 公单表 asT2 where 预计完成时间<实际完成时间
    and T1.人名=T2.人名) as 超期数
    from 工单表 as T1
    where T1.实际完成时间 between '2002-6-1' and '2002-7-1'
    order by 人名
      

  11.   

    select DISTINCT T1.人名,
    (select count (*) from 公单表 as T2 where T1.人名=T2.人名) as 定单数, 
    (select count(*) from 公单表 asT2 where 预计完成时间<实际完成时间
    and T1.人名=T2.人名) as 超期数
    from 工单表 as T1
    where T1.实际完成时间 between '2002-6-1' and '2002-7-1'
    order by 人名
      

  12.   

    更正:select T1.人名,
    (select count (*) from 公单表 as T2 where T1.人名=T2.人名) as 定单数, 
    (select count(*) from 公单表 as T2 where T1.预计完成时间<T1.实际完成时间 and T1.人名=T2.人名) as 超期数
    from 工单表 as T1
    where T1.实际完成时间 between '2002-6-1' and '2002-7-1'
    order by 人名
      

  13.   

    TO headit () 
    看到消息了吗?
    count(*) 与 count(公单编号)应该是一样的<--如果公单编号不为空