select count(*),type_code,depm_code 
from t_info
group by type_code,depm_code

解决方案 »

  1.   

    不是很理解你的意思
    不过用outer join 似乎可以
      

  2.   

    select (select count(*) from t_info a where b.depm_code = a.depm_dode and c.type_code = a.type_code group by c.type_code,b.depm_code) as count1,c.type_code,b.depm_code
    from t_depm b,t_type c只是没有文章的话查出来是NULL值,可以在前台处理一下就可以了
      

  3.   

    这样旧可以了:select ISNULL((select count(*) from t_info a where b.depm_code = a.depm_dode and c.type_code = a.type_code group by c.type_code,b.depm_code
    ),0) as count1,c.type_code,b.depm_code
    from t_depm b,t_type c
      

  4.   

    select nvl(num2,0) as num3,type_code,depm_code from 
     (select nvl(num1,0) as num2,type_code,depm_code from 
       (select count(*) as num1,type_code,depm_code from t_info) tab11 
        ,t_depm tab12 where tab1.depm_code (+)=tab12.depm_code ) tab21
     ,t_type tab22 where tab21.type_code(+)=tab22.type_code 
    group by type_code,depm_code
      

  5.   

    信息表(t_info)表有三个字段:信息代码、栏目代码、部门代码
    部门表(t_depm)有一个字段:部门代码
    栏目表(t_type)有一个代码:栏目代码
    在部门表中多加一个部门名称的字段bmname
    在栏目表中多加一个栏目名称的字段lmname
    如下:测试通过
    select b.bmname,c.lmname,count(*) from t_info a,t_depm b,t_type c where a.depm_code=b.depm_code and a.type_code=c.type_code group by b.bmname,c.lmname,a.type_code,a.depm_code
      

  6.   

    回复人: ruanbing(软冰) ( ) 信誉:99  2004-10-27 13:56:00  得分: 0  
     
     
       这样不合要求呀,如果栏目下没文章栏目就不会被查出来,如果部门下没文章部门也不会被查出来
      
     
    楼主的要求太高了。一条语句想做这么多。去问问邹老大吧。。写存储过程吧。。
      

  7.   

    declare @table1 table (
      id int
      ,count int
      ,type_code nvarchar(20)
      ,depm_code nvarchar(20)
    )
    然后一条一条插入最后
    select * from @table1可以放入一个存储过程当中
      

  8.   

    这个我试了一下可以select a.*,b.*,count(c.info_code)
    from t_depm a cross join t_type b
    left join t_info c on c.depm_code=a.depm_code and c.type_code=b.type_code group by a.depm_code,b.Type_code
      

  9.   

    http://community.csdn.net/Expert/ForumList_Search.asp?searchtype=1&bigclassid=57&smallclassid=5701&searchKeys=%BD%BB%B2%E6%B1%ED&author=&tabletype=now
      

  10.   

    这就是行列转换
    数据库表中数据为:时间         名称     数据
    2000325      name1      1
    2001325      name2      2
    2002325      name3      3
    现在要表现形式为:时间       name1        name2         name3
    2000325      1            0             0
    2001325      0            2             0
    2002325      0            0             3     我是准备绑定DataGraid   SQL语句怎么写?
    解决方法:declare @s varchar(8000)
    set @s=''
    select @s=@s+',['+名称+']=sum(case 名称 when '''+名称+''' then 数据 else 0 end)'
    from(select distinct 名称 from 表) a
    exec('select 时间'+@s+' from 表 group by 时间')
      

  11.   

    select t.depm_code,
        t.type_code,
        count(decode(a.info_code, null, 0, 1)) as info_count
    from t_info a 
        right join (
            select b.depm_code,
                c.type_code
            from t_depm b,
                t_type c
        ) t on 
            a.depm_code = t.depm_code
            and a.type_code = t.type_code
    group by t.depm_code,t.type_code
    ;
      

  12.   

    因为t_info表中未必能够出现所有的depm_code以及type_code
    所以,关联的时候
    只能用t_info去右连t_depm和t_type但是,一张表不能同时右连到多张表上(左联是可以的)
    所以,我想了个办法:将t_info右连到depm_code和type_code的笛卡尔积上
    于是就得到以上的sql语句不过,我不太确定这种方式是否违背前述的原则(一张表不能同时右连到张张表上)试试吧另外,我也挺怀疑这个sql语句的效率---当表depm_code和type_code中数据比较多的时候
      

  13.   

    谢谢大家,我的问题解决了
    我修改了一下上面的sql语句达到了我得要求这个问题困扰了我好久,真的太感谢你们了向回答我问题的同志们致敬!向所有的程序员致敬!