select round(sum(sumflow)) sumflow, to_char(recdate,'''+'day'+''') groupdetail from t
group by to_char(recdate-1,'''+'day'+''')';出来的分组顺序是按照汉语拼音顺序:   星期二、星期六、。。
怎么才能让它按星期一、星期二排序呢??select round(sum(sumflow)) sumflow, to_char(recdate,'''+'d'+''') groupdetail from t
group by to_char(recdate-1,'''+'d'+''')';这个可以按照1、2、3。。排序,可我想从数据库中直接得到中文显示的星期天数,我就把上面的改为:select round(sum(sumflow)) sumflow,to_char(recdate,'''+'day'+''') chineseshow , to_char(recdate,'''+'d'+''') groupdetail from t
group by to_char(recdate-1,'''+'d'+''')'出现错误提示:不是正确的groupby语句该怎么办才行呢?望大侠点拨!!!

解决方案 »

  1.   

    declare @t table (a varchar(10))
    insert into @t select 'a'
    insert into @t select 'b'
    insert into @t select 'c'
    select * from @t
    /*
    a
    b
    c
    */--想a ,c,b
    declare @t table (a varchar(10))
    insert into @t select 'a'
    insert into @t select 'b'
    insert into @t select 'c'
    select * from @t order by case when a='a' then 1  when a='b' then 3 when  a='c' then 2 end/*
    a
    c
    b
    */
      

  2.   


    declare @t table (a varchar(10))
    insert into @t select 'a'
    insert into @t select 'b'
    insert into @t select 'c'
    select case a when 'a' then 1 when 'b' then 3 when 'c' then 2  end id,* from @t   order by id/*
    1 a
    2 c
    3 b
    */
      

  3.   

    小米+步枪:
     表中没有直接的星期几的字段,而且要求也不能对表进行改动
    我用的是一个adoquery查询
      

  4.   

    例子:
    有一数据表名为:TB,其中有一字段为日期时间型名为T_DAY.
    数据如下:/*
    2008-04-13 00:00:00.000 星期日
    2008-04-14 00:00:00.000 星期一
    2008-04-08 00:00:00.000 星期二
    2008-04-09 00:00:00.000 星期三
    2008-04-10 00:00:00.000 星期四
    2008-04-11 00:00:00.000 星期五
    2008-04-12 00:00:00.000 星期六
    */那么要按星期来排序,SQL语句为:select t_day,
    case datepart(dw,t_day) when 2 then '星期一'
    when 3 then '星期二' 
    when 4 then '星期三' 
    when 5 then '星期四' 
    when 6 then '星期五' 
    when 7  then '星期六' 
    when 1 then '星期日' 
    end from tb order by 
    case datepart(dw,t_day) when 2 then 1
    when 3 then 2 
    when 4 then 3 
    when 5 then 4 
    when 6 then 5 
    when 7  then 6 
    when 1 then 7
    end 
    /*
    结果:
    2008-04-14 00:00:00.000 星期一
    2008-04-08 00:00:00.000 星期二
    2008-04-09 00:00:00.000 星期三
    2008-04-10 00:00:00.000 星期四
    2008-04-11 00:00:00.000 星期五
    2008-04-12 00:00:00.000 星期六
    2008-04-13 00:00:00.000 星期日
    */改为:DELPHI的语句就是:with adoquery1 do begin
    close;
    sql.text:='select * from tb order by '+
    'case datepart(dw,t_day) when 2 then 1 '+
    'when 3 then 2 '+
    'when 4 then 3 '+
    'when 5 then 4 '+
    'when 6 then 5 '+
    'when 7  then 6 '+
    'when 1 then 7 end ';
    open;
    end;