不好意思
上面的
表T_LX  
AJBH,XZDM,AJRQ  
2005101,320000,2005-01-01  
2005103,320000,2005-02-14  //---由于AJBH是主键不能重复,刚才写错了

解决方案 »

  1.   

    this,满足要求么?:
    select a.XZMC,
    c.LFRS as 来信次数,
    sum(case when c.LFRS=1 then 1 else 0 end) as 个人访次数,
    sum(case when c.LFRS=1 then 1 else 0 end) as 集体访次数,
    sum(case when c.LFRS>=2 and c.LFRS<=4 then c.LFRS else 0 end) as 集体访人数,
    sum(case when c.LFRS=1 then 1 else 0 end) as 群体访人数,
    sum(case when c.LFRS>4 then c.LFRS else 0 end) as 群体访次数
    from S_XZDM a,T_LX b,T_LF c
    where a.XZDM=b.XZDM and a.XZDM=c.XZDM
    group by a.XZMC, c.LFRS
      

  2.   

    错了,这个:
    select a.XZMC,
    c.LFRS as 来信次数,
    sum(case when c.LFRS=1 then 1 else 0 end) as 个人访次数,
    sum(case when c.LFRS>=2 and c.LFRS<=4 then 1 else 0 end) as 集体访次数,
    sum(case when c.LFRS>=2 and c.LFRS<=4 then c.LFRS else 0 end) as 集体访人数,
    sum(case when c.LFRS>4 then 1 else 0 end) as 群体访人数,
    sum(case when c.LFRS>4 then c.LFRS else 0 end) as 群体访次数
    from S_XZDM a,T_LX b,T_LF c
    where a.XZDM=b.XZDM and a.XZDM=c.XZDM
    group by a.XZMC, c.LFRS
      

  3.   

    to
    flyfing(江山如此多娇):
    谢谢你的答复,但是“c.LFRS as 来信次数,”,此处错了,“来信次数”应该是指向“T_LX”表的
      

  4.   

    上面的方法没有做到的是按照XZJB来统计,比如要求统计XZJB=2的所有的XZMC下的表,保留空记录。

    南京市 18 20 6 12 5 30
    江阴市 0  0  0  0 0  0
    ......
      

  5.   

    试下这个:
    select xzmc ,(select count(*) from T_LX b where b.xzdm=a.xzdm) as 来信次数,
                 (select count(*) from T_LF c where c.xzdm=a.xzdn and lfrs=1) as 个人访次数,
      (select count(*) from T_LF d where d.xzdm=a.xzdn and lfrs>2 and lfrs<5) as 集体访次数,
     (select sum(LFRS) from T_LF e where e.xzdm=a.xzdn and lfrs>2 and lfrs<5) as 集体访人数,
      (select count(*) from T_LF f where f.xzdm=a.xzdn and lfrs>4) as 群体访次数,
     (select sum(LFRS) from T_LF g where g.xzdm=a.xzdn and lfrs>4) as 群体访人数
     from S_XZDM a