MS_SQL数据库高手请入,解决了,再送300分......

toperscj 2003-09-29 09:34:28
我需要对数据库中的一个数据表作统计查询。
查询的要求是:按照几个字段进行排序,但是有个前提:
举个例子来说:表中的一个数据子段为Style(不在排序子段中),
他可以有三个值100,200,300。现在需要Style值为200的排在最前面。100的跟在
200后面,300在最后。各自进行排序。然后显示结果为:200的自动排序,100的自动排序,300自动排序。
各位XDJM.请帮忙啊,俺火烧屁股了......
...全文
42 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
znef88 2003-09-30
  • 打赏
  • 举报
回复
大家都认真的测试好了再发。不要骗我们这些人了
  • 打赏
  • 举报
回复
关注
wcolor 2003-09-30
  • 打赏
  • 举报
回复
select * from 表 order by (case when style = 200 then 1
when style = 100 then 2
when style = 300 then 3
end),主字段

things 2003-09-30
  • 打赏
  • 举报
回复
1.
select * from table where style = 200 order by field1, field2
union
select * from table where style <> 200 order by field1, field2

2.
select field1, field2, ...fieldn,
GroupId =
CASE style
WHEN 200 THEN 0
ELSE 1
END
order by GroupId, field1, field2

如果不认 GroupId ,加上单引号试试。没有SQLSERVER,没有试验。

第二种方法主要是注意CASE的使用
select field1, field2, ...fieldn,
GroupId =
CASE
WHEN style=200 THEN 0
WHEN style=100 THEN 1
WHEN style=300 THEN 2
END
order by GroupId, field1, field2

or

select field1, field2, ...fieldn,
CASE style
WHEN 200 THEN 0
WHEN 100 THEN 1
WHEN 300 THEN 2
END
order by style, field1, field2

没有SQL数据库,都是想当然的,自己试试吧
zilong123208650 2003-09-30
  • 打赏
  • 举报
回复
select * from table order by (case when style =200 then 1
when style =100 then 2
when style =300 then 3
end),主字段
txlicenhe 2003-09-30
  • 打赏
  • 举报
回复
只能是这两种之一了。
1:
select * from 你的表 order by charindex(','+cast(style as varchar(10))+',',',200,100,300,'),其他字段
2:
select * from 表 order by (case when style = 200 then 1
when style = 100 then 2
when style = 300 then 3
end),其他字段
awjx 2003-09-30
  • 打赏
  • 举报
回复
好帖!
yujohny 2003-09-30
  • 打赏
  • 举报
回复
select * from 表 order by (case when style = 200 then 1
when style = 100 then 2
when style = 300 then 3
end)
zjcxc 2003-09-30
  • 打赏
  • 举报
回复
楼主的意思不太明白.表中只有100,200,300这三个值吗?

txlicenhe 2003-09-30
  • 打赏
  • 举报
回复
select * from 你的表 order by charindex(','+cast(style as varchar(10))+',',',200,100,300,'),其他字段
伍子V5 2003-09-30
  • 打赏
  • 举报
回复
不知道明白你的意思没有
col style
3 100
5 200
9 100
1 300
2 200
8 300
排序后
2 200
5 200
3 100
9 100
1 300
8 300
select col,style from table group by style,col
order by
(case when style = 200 then 1
when style = 100 then 2
when style = 300 then 3
end),col
大道至简_wu 2003-09-30
  • 打赏
  • 举报
回复
select 1 as aid,* from T where style =200
union
select 2 as aid,* from T where style =100
union
select 3 as aid,* from T where style =300
order by aid
LoveSQL 2003-09-30
  • 打赏
  • 举报
回复
select * from 你的表 order by charindex(','+cast(style as varchar(10))+',',',200,100,300,'),其他字段

或:

select * from 表 order by (case when style = 200 then 1
when style = 100 then 2
when style = 300 then 3
end),其他字段
eddiezhuo 2003-09-30
  • 打赏
  • 举报
回复
up
pengdali 2003-09-29
  • 打赏
  • 举报
回复
select * from 你的表 order by charindex(','+cast(style as varchar(10))+',',',200,100,300,'),其他字段
playyuer 2003-09-29
  • 打赏
  • 举报
回复
select *
from T
order by (case when style = 200 then 1
when style = 100 then 2
when style = 300 then 3
end)
w_rose 2003-09-29
  • 打赏
  • 举报
回复
当然啦,如果只有三个值而且是“死”的,也许:

select ... from 查询结果 order by case style when 200 then 50 else style end

可行。
w_rose 2003-09-29
  • 打赏
  • 举报
回复
关键就是:你自已指出排序的顺序,SQL Server默认排序不用。
w_rose 2003-09-29
  • 打赏
  • 举报
回复
方法而已,不要死抠代码是否有错:

create table myorder(style primary key,顺序号)
select .....
from 查询结果 as a left join myorder as b on a.style=b.style
order by b.顺序号

22,206

社区成员

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

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