表结构假设是这样的
type          int
id          int
createtime datetime

条件: createtime 在数据库中存放数据是“yyyy-MM-dd HH:mm:ss” ,我只需要做每日统计判断即“yyyy-MM-dd”现在我要查询出,createtime时间相同(“yyyy-MM-dd”)时type=3与type=10的条数各多少,createtime时间不同时,type=3与type=10各多少条?请达人赐教,怎么写?

解决方案 »

  1.   


    select distinct Date(a.createtime)
    ,a.type,
    count(a.type)  as '移动',count(b.type) as'联通'
    from  datetest a
    join datetest b on Date(a.createtime) = Date(b.createtime)
    where a.type=3  b.type=2
    group by a.createtime;我现在这么写的,结果根本不对求怎么写?
      

  2.   


    select createtime, Sum(case when type=3 then 1 else 0 end) iType3, Sum(case when type=10 then 1 else 0 end) iType10 from datetest group by createtime
      

  3.   

    SELECT 
    CONVERT(VARCHAR(10),createtime,120),
    sum(CASE WHEN type=3 THEN 1 ELSE 0 END ) AS [3的数量],
    sum(CASE WHEN type=10 THEN 1 ELSE 0 END ) AS [10的数量] 
    FROM tb 
    GROUP BY CONVERT(VARCHAR(10),createtime,120) 
      

  4.   

    type   id       createtime3 1 2010-05-11 08:00:00
    10 2 2010-05-11 09:00:00
    10 5 2011-05-11 10:56:073 3 2010-05-12 21:00:00
    3 4 2011-05-19 10:40:02比如说这样,type=10是联通,type=3是移动,
    我现在要求出的结果要这样:createtime    移动条数    联通条数2010-05-11      1          2
                         
    2010-05-12      1          02011-05-19      1          0
    请问怎么写?