注册 登录
编程论坛 SQL Server论坛

关于SQL的一个问题,请进呀兄弟们 谢谢

yts111 发布于 2007-01-19 10:34, 1048 次点击
如我想实现
也就是把里面除了time的列值不一样其他各列是一样的数据,而我想把他们合并成一行,把time字段分解成一行中的time1,time2,time3,time4
如:卡号 名字 日期 时间
001 yy 2007-01-02 07:00
001 yy 2007-01-02 12:00 想实现
卡号 名字 日期 时间一 时间二
两条记录的成为一条记录
谢谢,各位,帮我看看好吗,万分感谢!
只有本站会员才能查看附件,请 登录

13 回复
#2
棉花糖ONE2007-01-19 10:39

select cardid,name,convert(varchar(10),time,120) as date,convert(varchar(8),time,108) as time from table

#3
Kendy1234562007-01-19 11:09

太难了! 如果是2行合并,就是time1,time2,3行就得多个time3 也就是说 写代码的时候事先是不知道最后会拼成多少个time列的,timeN的N= select max((select cnt = count(*) from tblCard group by card id)). 再说 返回的列数都不固定 你怎么设计grid来显示它?

把你碰到的实际问题讲一下 你这种解决方案很不合理

#4
Kendy1234562007-01-19 11:20
以下是引用Kendy123456在2007-1-19 11:09:57的发言:

太难了! 如果是2行合并,就是time1,time2,3行就得多个time3 也就是说 写代码的时候事先是不知道最后会拼成多少个time列的,timeN的N= select max((select cnt = count(*) from tblCard group by card id)). 再说 返回的列数都不固定 你怎么设计grid来显示它?

把你碰到的实际问题讲一下 你这种解决方案很不合理

汗 怎么不能编辑自己的贴了! N的取值写错了 反正最后的列数就是每个card中每天的记录数中最大的那一个
N= select max(cnt) from (select cardid,max(cnt) as cnt from (select cardid,count(*),date as cnt from tableCard group by cardid,date))

#5
棉花糖ONE2007-01-19 11:21

刚才没看清题目.如果要实现你那个的话,应该要借助临时表,你可以先去查查quotename的帮助
我的思路是
借助临时表加一个排序字段,将排序字段全部转换成time1,time2,....的格式,再利用quotename(列名,N'''')获取所有列的值,就相当于实现行列转换

#6
yts1112007-01-19 11:44
恩,那些数据就是我插入到一个表中的;
而现在我就想写一个视图 来实现,把这个表中的只有一个字段列值不同其他列都相同的进行合并,成一列根据group by cardid 可还没写好!兄弟们能帮帮忙么!
#7
6ygg2007-01-19 13:14

用临时表应该可以吧?
#8
yts1112007-01-19 14:09
兄弟能能不再具体点行么!万分感谢!
#9
棉花糖ONE2007-01-19 17:50


if object_id('shiyan') is not null
drop table shiyan
go
create table shiyan(id varchar(3),kk varchar(3),t datetime,tt varchar(10))
insert into shiyan select '002','yy','2007-01-02','07:30:00'
insert into shiyan select '002','yy','2007-01-02','11:20:00'
insert into shiyan select '002','yy','2007-01-02','08:30:00'
insert into shiyan select '002','yy','2007-01-02','12:20:00'
insert into shiyan select '001','yy','2007-01-02','07:30:00'
insert into shiyan select '001','yy','2007-01-02','11:20:00'
insert into shiyan select '001','yy','2007-01-02','08:30:00'
insert into shiyan select '001','yy','2007-01-02','12:20:00'
select id,kk,convert(varchar(10),t,120) as t,convert(varchar(8),tt,108) as tt from shiyan
结果
id kk t tt
---- ---- ---------- --------
002 yy 2007-01-02 07:30:00
002 yy 2007-01-02 11:20:00
002 yy 2007-01-02 08:30:00
002 yy 2007-01-02 12:20:00
001 yy 2007-01-02 07:30:00
001 yy 2007-01-02 11:20:00
001 yy 2007-01-02 08:30:00
001 yy 2007-01-02 12:20:00

(所影响的行数为 8 行)
if object_id('sy') is not null
drop table sy
go
select *into sy from (
select bj=(select count(1) from shiyan where s.id=id and s.kk=kk and s.t=t and tt<=s.tt ),id,kk,convert(varchar(10),t,120) as t,convert(varchar(8),tt,108) as tt from shiyan s) m
select * from sy
结果
bj id kk t tt
----------- ---- ---- ---------- --------
1 002 yy 2007-01-02 07:30:00
3 002 yy 2007-01-02 11:20:00
2 002 yy 2007-01-02 08:30:00
4 002 yy 2007-01-02 12:20:00
1 001 yy 2007-01-02 07:30:00
3 001 yy 2007-01-02 11:20:00
2 001 yy 2007-01-02 08:30:00
4 001 yy 2007-01-02 12:20:00

(所影响的行数为 8 行)

alter table sy alter column bj varchar(20)
update sy set bj='time'+bj
select * from sy order by bj asc
结果
bj id kk t tt
-------------------- ---- ---- ---------- --------
time1 002 yy 2007-01-02 07:30:00
time1 001 yy 2007-01-02 07:30:00
time2 001 yy 2007-01-02 08:30:00
time2 002 yy 2007-01-02 08:30:00
time3 002 yy 2007-01-02 11:20:00
time3 001 yy 2007-01-02 11:20:00
time4 001 yy 2007-01-02 12:20:00
time4 002 yy 2007-01-02 12:20:00

(所影响的行数为 8 行)

declare @sql varchar(4000)
set @sql=' '
select @sql=@sql+','+quotename(bj,N'''')+'=max(case bj when '+quotename(bj,N'''')+' then tt else ''00:00'' end)' from sy group by bj
select @sql='select id,kk,t'+@sql+' from sy group by id,kk,t'
exec(@sql)

结果
id kk t time1 time2 time3 time4
---- ---- ---------- -------- -------- -------- --------
001 yy 2007-01-02 07:30:00 08:30:00 11:20:00 12:20:00
002 yy 2007-01-02 07:30:00 08:30:00 11:20:00 12:20:00
楼主不知道是不是你想要的

#10
bygg2007-01-19 18:51
jinyuzhang 嘅不知道你是做什么的,呵
#11
棉花糖ONE2007-01-19 18:55

大三

#12
yts1112007-01-19 23:39
恩,谢谢 jinyuzhang,结果是我想要的,真的感谢呀,可就是感觉那么麻烦呀,有没简便点的了呀!兄弟
#13
棉花糖ONE2007-01-20 17:27

老大没简单的了,你自己写成存储过程吧,可以用表变量来代替临时表

#14
Kendy1234562007-01-22 10:09
9楼解法很强悍呀~PFPF
1