我在A表中有2个字段,注册时间(t1)、在此注册时间(t2),现在想统计每天注册的人数,
如果表中的某条数据,t1有值、t2没值======就按t1统计,
如果表中的某条数据,t1有值、t2有值======此时就把这条数据对应的t2值替换成t1来统计,但并没有更新到数据库中去,请问怎么解决呢,谢了

解决方案 »

  1.   

    case when else end实现
      

  2.   

    select (case when t2 is null then convert(varchar(10),t1,120) else convert(varchar(10),t2,120) end) [day] , count(1) [cnt] 
    from tb 
    group by (case when t2 is null then convert(varchar(10),t1,120) else convert(varchar(10),t2,120) end)
      

  3.   

    不存在t1没值、t2有值 的情况对吧
    --try
    select convert(varchar(10),case when t2 is null then t1 else t2 end,120),count(*)
    from tb 
    group by  convert(varchar(10),case when t2 is null then t1 else t2 end,120)
      

  4.   

    我想在问下,对这种t1为var字段的,怎么让它从小到大排列,而 不是什么按降序(desc)排序的时候,2012-3-9比较2012-3-19大,我分析,可能是因为var型的就按一个位置一个位置比,因为9大于1,所以它就默认2012-3-9大于2012-3-19,这个怎么解决呢?谢谢了
      

  5.   


    我想在问下,对这种t1为var字段的,怎么让它从小到大排列,而 不是什么按降序(desc)排序的时候,2012-3-9比较2012-3-19大,我分析,可能是因为var型的就按一个位置一个位置比,因为9大于1,所以它就默认2012-3-9大于2012-3-19,这个怎么解决呢?谢谢了
      

  6.   

    --如果t1是字符串,需要转换为时间类型后再做比较
    order by cast(t1 as datetime)
      

  7.   

    --如果你的t1,t2是字符串,那么我在前面的SQL语句需要更改为如下:
    select (case when t2 is null then convert(varchar(10),cast(t1 as datetime),120) else convert(varchar(10),cast(t2 as datetime),120) end) [day] , count(1) [cnt] 
    from tb 
    group by (case when t2 is null then convert(varchar(10),cast(t1 as datetime),120) else convert(varchar(10),cast(t2 as datetime),120) end)
      

  8.   

    主要是group by这个地方,我的想法是:如果t2有值,还是按t1排序,只是把t2的值当成t1来排,但不更t1的值为t2,就是想实现这样的
      

  9.   

    select case when Convert(VARCHAR(10),t2,23) is null then Convert(VARCHAR(10),t1,23) else Convert(VARCHAR(10),t2,23)  end as 't1'
    from tb 
    group by (case when Convert(VARCHAR(10),t2,23) is null then Convert(VARCHAR(10),t1,23) else Convert(VARCHAR(10),t2,23) end)
      

  10.   

    select case when Convert(VARCHAR(10),t2,23) is null then Convert(VARCHAR(10),t1,23) else Convert(VARCHAR(10),t2,23)  end as 't1'
    from tb 
    group by  Convert(VARCHAR(10),t1,23)