本人sql学的不好。 求教各位大神。今天领导给个需求 统计报表  按照要求统计分类计算数量 需求如下;
有一个表table1,table2,table3 用m_id相关联(但是table2里面的m_id记录会有字段type 还要统计为A的
table3 中有来源 source 字段 source字段共有十种 A,B,C……
老板要求在显示出这些  
总的数量  typeA的数量  source=A的数量 source=B的数量……例如 table1有10个  那总的数量就是10 ,但是关联到table2中type为A的只有8个  这8个又有 source=A的5 source=B的0个 source=C的3个
这条记录就是
10  8  5  0  3
求教这跳sql应该怎么写   

解决方案 »

  1.   

     5  0  3 是从type='A'的记录里面取的?select count(1)总数量,count(decode(b.type,'A',1)),
      count(case when b.type='A' and c.source='A' then 1 end),
      count(case when b.type='A' and c.source='B' then 1 end),
      count(case when b.type='A' and c.source='C' then 1 end),
    .....
    from table1 a,table2 b,table3 c
    where a.m_id=b.m_id(+)
      and a.m_id=c.m_id(+)
      

  2.   

    对 5  0  3 是从type='A'的记录里面取的
      

  3.   

    看看上面的代码吧,就是这个这个需求写的
    筛选某个日期在where条件后加一个a.date1>to_date(...) and a.date1<...就行了