有一个user表,有许多类型的用户信息(type 有1到9个类型表示9个类型的用户),其中有个inputtime字段,记录录入的时间,
   那么我有一个这样的需求,想在前台提供一个时间(如 2012-12)取出这个月的不同类型的数量.那么这个语句该咋写呢?程序用的spring.
   谢谢大家!sql  java

解决方案 »

  1.   


    select count(*) from user where inputtime = '2012-12' group by type;
      

  2.   

    select count(*) from user wherer inputtime = to_date('2012-12','yyyy-mm') group by type
    时间类型的字段需要转换
      

  3.   

    SELECT type,COUNT(*) FROM testTb GROUP BY type 判断时间方法每个数据库函数不一样
      

  4.   

    select  *  from 
    (select  count(*) as newsgnum   from  userg   where  time  > '2012-01'  and type='1') sg
    ,
    (select  count(*) as newjlnum   from  userg   where  time  > '2012-01'  and type='2') jl
    ,
    (select  count(*) as newjsnum   from  userg   where  time  > '2012-01'  and type='3') js
    ,
    (select  count(*) as newjcnum   from  userg   where  time  > '2012-01'  and type='4') jc
    ,
    (select  count(*) as newkcnum   from  userg   where  time  > '2012-01'  and type='5') kc
    ,
    (select  count(*) as newzxnum   from  userg   where  time  > '2012-01'  and type='6') zx
    ,
    (select  count(*) as newzbnum   from  userg   where  time  > '2012-01'  and type='7') zb
    ,
    (select  count(*) as newclnum   from  userg   where  time  > '2012-01'  and type='8') cl
    ,
    (select  count(*) as newbxnum   from  userg   where  time  > '2012-01'  and type='9') bx这是我写的一个语句。
    能得到这样的结果
    我想要的是能穿个时间实时的查处这些数字来。。
    谢谢!
      

  5.   

    select sum(if(type=1,1,0))as newsgnum,sum(if(type=2,1,0))as newjlnum,sum(if(type=3,1,0))as newjsnum,sum(if(type=4,1,0))as newjcnum,sum(if(type=5,1,0))as newkcnum,sum(if(type=6,1,0))as newzxnum,sum(if(type=7,1,0))as newzbnum,sum(if(type=8,1,0))as newclnum,sum(if(type=9,1,0))as newbxnum from userg where time>'2012-01'
      

  6.   

    SELECT TRUNC(A.INPUTTYPE),TO_CHAR(A.TYPE) as TYPE,count(*) as count
    FROM  TABLE A GROUP BY to_char(A.TYPE),TRUNC(A.CINPUTTIME) ;
      

  7.   

    请问lz是什么数据库 我写的是mysql的语法应该没问题
      

  8.   

    哦,不好意思,该早告诉你的  是sqlserver 2005。
      

  9.   

    你还是用这个吧 按顺序依次是type 1 - 9的值 需要你后台去联系上
    SELECT type,COUNT(*) FROM userg GROUP BY type ORDER BY type ASC;
      

  10.   

    ORACLE 写的 写了3行
    select sum(decode(type,1,cc,0))  as sg,
    select sum(decode(type,2,cc,0))  as jl,
    select sum(decode(type,3,cc,0))  as js from
    (select type, count(*)  as cc  from  userg   where  time  > '2012-01'  group by type)
    大致这样 比那个的效率应该要高一点,希望能帮到你