请教sql问题???

islkeng 2004-06-12 10:34:03
我现在有一个表,字段为:id,bt,dis,类型分别为:int,datetime,float。
如下:
1 2004-6-10 12:03:00 102.56
1 2004-6-10 12:04:03 103.7
1 2004-6-10 12:04:20 104.1
2 2004-6-10 12:03:02 452.67
2 2004-6-10 12:03:54 453.14
2 2004-6-10 12:04:11 454.23
。。。。。。。。。。。。。。。
现在我想用sql语句实现如下效果:
id 时间点1 距离1 时间点2 距离2 时间点3 距离3
1 2004-6-10 12:03:00 102.56 2004-6-10 12:04:03 103.7 。。。。
2 2004-6-10 12:03:02 452.67 2004-6-10 12:03:54 453.14。。。

效果即为,以id为行,将所有id的数据变为一条记录???
望各位大哥帮帮小弟!!!感激万分!!!
...全文
164 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
islkeng 2004-06-15
  • 打赏
  • 举报
回复
再次谢谢邹大哥!
dut 2004-06-12
  • 打赏
  • 举报
回复
不固定用斑竹的方法
dut 2004-06-12
  • 打赏
  • 举报
回复
固定三个:

select id,
min(case when (select count(*) from tablename where id=a.id and bt<=a.bt)=1 then bt else '9999-12-31' end) as 时间点1,
sum(case when (select count(*) from tablename where id=a.id and bt<=a.bt)=1 then dis else 0 end) as 距离1,
min(case when (select count(*) from tablename where id=a.id and bt<=a.bt)=2 then bt else '9999-12-31' end) as 时间点2,
sum(case when (select count(*) from tablename where id=a.id and bt<=a.bt)=2 then dis else 0 end) as 距离2,
min(case when (select count(*) from tablename where id=a.id and bt<=a.bt)=3 then bt else '9999-12-31' end) as 时间点3,
sum(case when (select count(*) from tablename where id=a.id and bt<=a.bt)=3 then dis else 0 end) as 距离3
from tablename a
group by id


zjcxc 元老 2004-06-12
  • 打赏
  • 举报
回复
--查询处理
select nid=0,id,dt,dis=cast(dis as varchar)
into #t
from 表
order by id

declare @id int,@nid int,@s varchar(8000)
update #t set @nid=case @id when id then @nid+1 else 1 end
,nid=@nid,@id=id
select @nid=max(nid),@s='' from #t
while @nid>0
select @s=',时间点'+cast(@nid as varchar)+'=max(case nid when '
+cast(@nid as varchar)+' then dt end),距离'
+cast(@nid as varchar)+'=max(case nid when '
+cast(@nid as varchar)+' then dis end)'
+@s
,@nid=@nid-1
exec('select id'+@s+' from #t group by id')
zjcxc 元老 2004-06-12
  • 打赏
  • 举报
回复
表中有主键吗? 没有主键的话,得用临时表
zjcxc 元老 2004-06-12
  • 打赏
  • 举报
回复
declare @nid int,@s varchar(8000)
select @nid=max(nid),@s='' from(
select nid=count(*) from 表
group by id)a
while @nid>0
select @s=',时间点'+cast(@nid as varchar)+'=max(case nid when '
+cast(@nid as varchar)+' then dt end),距离'
+cast(@nid as varchar)+'=max(case nid when '
+cast(@nid as varchar)+' then dis end)'
+@s
,@nid=@nid-1
exec('select id'+@s+' from(
select nid=(select count(*) from 表 where id=a.id orderid<=a.orderid),*
from 表 a
)a group by id')
zjcxc 元老 2004-06-12
  • 打赏
  • 举报
回复
--如果表中数据量不大,可以不用临时表,这样写:

declare @nid int,@s varchar(8000)
select @nid=max(nid),@s='' from(
select nid=count(*) from 表
group by id)a
while @nid>0
select @s=',时间点'+cast(@nid as varchar)+'=max(case nid when '
+cast(@nid as varchar)+' then dt end),距离'
+cast(@nid as varchar)+'=max(case nid when '
+cast(@nid as varchar)+' then dis end)'
+@s
,@nid=@nid-1
exec('select id'+@s+' from(
select nid=(select count(*) from 表 where id=a.id orderid<=a.orderid),*
from 表 a
)agroup by id')

zjcxc 元老 2004-06-12
  • 打赏
  • 举报
回复
这种语句很难解释,看不懂你改改表名,字段名套用就行了.

如果表中的数据多,用临时表的方法快一些.
islkeng 2004-06-12
  • 打赏
  • 举报
回复
邹大哥,谢谢你的帮助,但小弟看不懂你的语句,我的表有主键,orderid,值从1开始,1、2、3、4。。。。。。。。
麻烦你再详细点说明,谢谢!

34,575

社区成员

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

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