怎么处理列与列之间的数据比较?

bguest 2005-10-17 08:48:30
有一表如下:
---------------------------------------
ID S01 S02 S03 S04 S05 BFi
1 32 2 4 23 23
2 23 4 2 2 50
3 40 20 23 62 10
---------------------------------------
怎么把每一行记录中S01,S02,S03,S04,S05中最大的那个值比较出来,作为单独一个列呢?
...全文
150 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zlp321002 2005-10-17
  • 打赏
  • 举报
回复
--如果有主键,写一个函数,处理每条记录的最大值,循环处理函数,求表的最大值,效率不高!
bguest 2005-10-17
  • 打赏
  • 举报
回复
哈....多谢楼上的兄弟,也算是有了可行的语句。想想我哪表里面有24个字段(其实是代表24个时辰的通话量)就头大了。
出去一下,回来结贴,期待更多的方法,学习学习。
QQMagicer 2005-10-17
  • 打赏
  • 举报
回复
select *,
case when (s01>s02 and s01>s03 and s01>s04 and s01>s05 ) then s01
when (s02>s01 and s02>s03 and s02>s04 and s02>s05 ) then s02
when (s03>s01 and s03>s02 and s03>s04 and s03>s05 ) then s03
when (s04>s01 and s04>s02 and s04>s03 and s04>s05 ) then s04
when (s05>s01 and s05>s02 and s05>s03 and s05>s04 ) then s05
else ''end as 新列
from 表
bguest 2005-10-17
  • 打赏
  • 举报
回复
咳~~~多谢楼上的两位,可能我开始的表达让大家误会了。想得到的数据不是一列中的最大值,而是一行数据中的最大值。

ID S01 S02 S03 S04 S05 BFi
1 32 2 4 23 23 --->最大值为32
2 23 4 2 2 50 --->最大值为50

该怎么写语句呢?谢谢
bguest 2005-10-17
  • 打赏
  • 举报
回复
咳~~~多谢楼上的两位,可能我开始的表达让大家误会了。想得到的数据不是一列中的最大值,而是一行数据中的最大值。

ID S01 S02 S03 S04 S05 BFi
1 32 2 4 23 23 --->最大值为32
2 23 4 2 2 50 --->最大值为50

该怎么写语句呢?谢谢
Alisa 2005-10-17
  • 打赏
  • 举报
回复
--生成测试数据
create table abc(id char(10),S01 int,S02 int,S03 int,S04 int,S05 int)
insert into abc select '1','32','2','4','23','23'
insert into abc select '2','58','6','4','23','23'
insert into abc select '3','63','2','4','23','23'
insert into abc select '4','31','2','9','23','84'
insert into abc select '5','11','2','4','23','23'
insert into abc select '6','39','2','4','80','23'
insert into abc select '7','52','2','4','23','23'

select * from abc

---查询语句
select max(s01) s01,max(s02) s01,max(s03) s03,max(s04) s04,max(s05) s05
from abc
zlp321002 2005-10-17
  • 打赏
  • 举报
回复
select 新列=max(S01) from 表
union all
select 新列=max(S02) from 表
union all
select 新列=max(S03) from 表
union all
select 新列=max(S04) from 表
union all
select 新列=max(S05) from 表

34,594

社区成员

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

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