求问一sql按照星期排序

treeway 2007-05-16 09:37:52
查询出的结果数据如下:
星期 人数
星期二 1
星期六 2
星期日 3
星期三 4
星期四 5
星期五 6
星期一 7

怎么样按照星期一 星期二...星期日的顺序来排列?
...全文
758 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fcuandy 2007-05-16
  • 打赏
  • 举报
回复
不能.datename的第二参数是日期型数据. '星期一',这是个字串
treeway 2007-05-16
  • 打赏
  • 举报
回复
datename(weekday,tbtable.nowdata) 星期
这个函数可不可以改成 星期1
星期2
星期3
星期4
星期5
星期6
星期日
这样改?
fcuandy 2007-05-16
  • 打赏
  • 举报
回复
select '星期' + y,ISNULL(人数,0) FROM
(
select 1 x,'一' y
union all
select 2,'二'
union all
....
select 7,'日')
) b
LEFT join tb a
on charindex(y,星期)>0
order by x
treeway 2007-05-16
  • 打赏
  • 举报
回复
我这里查询出来的星期也是动态的,
就是说有可能只有一个星期的几天。。。。。
fcuandy 2007-05-16
  • 打赏
  • 举报
回复
上面有手误.多打个y
fcuandy 2007-05-16
  • 打赏
  • 举报
回复
order by
replace(replace(replace(replace(replace(replace(replace(replace(星期,'一','1'),'二','2'),'三','3'),'四','4'),'五','5'),'六','6'),'日','7'),'星期','')

这样不直观或者

select a.* from tb a
inner join
(
select 1 x,'一' y
union all
select 2,'二'
union all
....
select 7,'日')
) b
on y charindex(y,星期)>0
order by x
dawugui 2007-05-16
  • 打赏
  • 举报
回复
select * from tb
order by case 星期 when '星期一' then 1
when '星期二' then 2
when '星期三' then 3
when '星期四' then 4
when '星期五' then 5
when '星期六' then 6
when '星期日' then 7
end
dawugui 2007-05-16
  • 打赏
  • 举报
回复
如何让ORDER BY按指定的顺序排序

表a里有个列叫Type,是商品类别,就3种情况:S,A,B,如下:
id name type
1 一班 S
2 五班 A
3 三班 B
4 四班 B
5 二班 A
6 六班 S
现在我需要按照‘S’,‘A’,‘B’的顺序排序,如下:
1 一班 S
6 六班 S
5 二班 A
2 五班 A
3 三班 B
4 四班 B

SELECT *
FROM tbl_test
ORDER BY "@#$$%#$%@$@#$@$@#@$这里应该咋写?"

select * from a where type='S' union all
select * from a where type='A' union all
select * from a where type='B'
select id , name ,type
from a
order by case type when 'S' then 1
when 'A' then 2 when 'B' then 3 else 4 end (如果对ID排序,则加最后加,id)
select id , name ,type
from (select *, case type when 'S' then 1 when 'A' then 2 else 3 end as seq from a) X
order by seq
上诉对ID列没有进行排序,如果在上诉基础上对ID再进行排序。
select id , name ,type(假设有列id ,name, type)
from (select *, case type when 'S' then 1 when 'A' then 2 else 3 end as seq from a) X
order by seq,id
1 一班 S
6 六班 S
2 五班 A
5 二班 A
3 三班 B
4 四班 B

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧