求助sql中排序语句

fcy1983 2008-02-19 11:08:55
我有一组数据,请问sql排序语句如何写?及如何实现表的存储?谢谢!
年份  流量  序号 排序后的流量(大->小) 频率p
1886 16400 1 18700 0.88
1917 18700 2 17400 1.75
1931 15700 3 16400 2.63
1947 15000 4 15700 3.51
1955 17400 5 15000 4.39

其中p=m/n+1
...全文
458 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
中国风 2008-02-20
  • 打赏
  • 举报
回复
p=m/n+1

-------m和n在那里。。。。。。
中国风 2008-02-20
  • 打赏
  • 举报
回复
好像明白了这样:

declare @T table([年份] int,[流量] int, [序号] int)
insert @T select 1886, 16400, 1
insert @T select 1917, 18700, 2
insert @T select 1931, 15700, 3
insert @T select 1947, 15000, 4
insert @T select 1955, 17400, 5

select
a.*,
b.[流量] as [排序后的流量],
cast( a.[流量]*1.0/b.[流量] as decimal(18,2)) as 频率p
from
@T a
join
(select *,(select count(1) from @T where 流量>=t1.流量) as con from @T t1)b
on a.[序号]=b.con

/*

(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)

年份 流量 序号 排序后的流量 频率p
----------- ----------- ----------- ----------- --------------------
1886 16400 1 18700 .88
1917 18700 2 17400 1.07
1931 15700 3 16400 .96
1947 15000 4 15700 .96
1955 17400 5 15000 1.16

(所影响的行数为 5 行)


*/
中国风 2008-02-20
  • 打赏
  • 举报
回复
改改
select
*,
isnull((select max(流量) from table1 where 流量<t1.流量),0)as 排序后的流量,
cast(流量*1.0/(select max(流量) from table1 where 流量<t1.流量) as decimal(18,2)) as 频率p
from
Table1 t1
中国风 2008-02-20
  • 打赏
  • 举报
回复
select
*,
(select count(1) from table1 where 流量>=t1.流量)as 排序后的流量,
cast(流量*1.0/(select count(1) from table1 where 流量>=t1.流量) as decimal(18,2)) as 频率p
from
Table1 t1
fcy1983 2008-02-20
  • 打赏
  • 举报
回复
如果有60组数据,有没有简单写法 ?
-狙击手- 2008-02-19
  • 打赏
  • 举报
回复
update T
set p = m/n+1
-狙击手- 2008-02-19
  • 打赏
  • 举报
回复
select *
from T
order by 流量 desc

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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