數據庫轉換問題﹐思歸﹐大力﹐幫幫我﹗﹗﹗﹗﹗﹗﹗﹗﹗

dahroswell 2003-09-15 10:13:15
求各路高手幫忙﹐我有這樣一個表A

empid empno empdate emptime

1756 AA000042 2002-05-19 60328
1756 AA000042 2002-05-19 60332
1756 AA000042 2002-05-19 60336
1756 AA000042 2002-05-19 60340
1756 AA000042 2002-05-20 29755
1756 AA000042 2002-06-11 41864
1756 AA000042 2002-06-11 50900
1757 AA000021 2002-05-20 30200
1759 AA000010 2002-12-14 59400
1759 AA000010 2002-12-28 79740
1759 AA000010 2003-04-03 63360
1760 AA000011 2002-05-22 45005
1760 AA000011 2002-05-22 48408
1760 AA000011 2002-05-22 63005
1760 AA000011 2002-05-22 66441
.
.
.
.
.
.

要將上表A轉換為下表B
是按日期不同來轉換成行的
empid empno t1 t2 t3 t4

1756 AA000042 2002-05-19 60328 2002-05-19 60332 2002-05-19 60336 2002-05-19 60340
1756 AA000042 2002-05-20 29755
1756 AA000042 2002-06-11 41864 2002-06-11 50900
1757 AA000021 2002-05-20 30200
1759 AA000010 2002-12-14 59400
1759 AA000010 2002-12-28 79740
1759 AA000010 2003-04-03 63360
1760 AA000011 2002-05-22 45005 2002-05-22 48408 2002-05-22 63005 2002-05-22 66441
高分相贈﹐謝謝各位大俠了﹗﹗﹗
思歸﹐大力﹐幫我﹗﹗﹗
...全文
43 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yoki 2003-09-15
  • 打赏
  • 举报
回复
就是,如果是蚂蚁这种情况的话可以
create function getstr(@empid int,@empno varchar(20),@empdate datetime)
returns varchar(2000)
as
begin
declare @str varchar(2000)
set @str=''
select @str=@str+','+convert(varchar(11),empdate,120)+emptime from a where empid=@empid and empno=@empno and empdate=@empdate
set @str=right(@str,len(@str)-1)
return @str
end
go


select empid,empno,dbo.getstr(empid,empno,empdate) areas from a group by empid,empno,empdate
yoki 2003-09-15
  • 打赏
  • 举报
回复
其实应该为 col 1
表示所属的列在新加列中的位置 1表示在第一列,2表示在第2列....
CrazyFor 2003-09-15
  • 打赏
  • 举报
回复
能不能用下面的方式呢.这样就简单多了.

empid empno 所有日期

1756 AA000042 2002-05-19 60328, 2002-05-19 60332, 2002-05-19 60336, 2002-05-19 60340
....
yoki 2003-09-15
  • 打赏
  • 举报
回复
我没有测试,不知道对否?

row为别名,1为初始值
dahroswell 2003-09-15
  • 打赏
  • 举报
回复
請問﹐你的ROW 1 表示什么呀﹖多謝了﹗﹗﹗
yoki 2003-09-15
  • 打赏
  • 举报
回复
select *,row 1 into c from a
declare @int int,@a int,@b varchar(20),@c datetime
update c set @int=case when @a=empid and @b=empno and @c=empdate then @int+1 else 1 end,@a=empid,@b=empno,@c=empdate,row=@int

declare @sql varchar(8000)
set @sql='select empid,empno'
select @sql=@sql+',case row when ''' + cast(row as varchar(10)) + ''' then convert(varchar(11),empdate,120)+emptime else null end as t'+ cast(row as varchar(10))
from (select distinct row from c) as cc

select @sql=@sql+' from c group by empid,empno,convert(varchar(11),empdate,120)+emptime,empdate,emptime,row'
exec (@sql)
yoki 2003-09-15
  • 打赏
  • 举报
回复
select *,row 1 into c from a
declare @int int,@a int,@b varchar(20),@c datetime
update c set @int=case when @a=empid and @b=empno and @c=empdate then @int+1 else 1 end,@a=empid,@b=empno,@c=empdate,row=@int

declare @sql varchar(8000)
set @sql='select empid,empno'
select @sql=@sql+',case row when ''' + cast(row as varchar(10)) + ''' then convert(varchar(11),empdate,120)+emptime else null end as C'+ cast(row as varchar(10))
from (select distinct row from c) as cc

select @sql=@sql+' from c group by empid,empno,convert(varchar(11),empdate,120)+emptime,empdate,emptime,row'
exec (@sql)
dahroswell 2003-09-15
  • 打赏
  • 举报
回复
可還是不行﹐出現下列錯誤﹐請您幫我看看好嗎﹖謝謝您﹗﹗﹗﹗﹗
伺服器: 訊息 130,層級 15,狀態 1,行 1
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
伺服器: 訊息 170,層級 15,狀態 1,行 3
Line 3: Incorrect syntax near '='.
伺服器: 訊息 170,層級 15,狀態 1,行 4
Line 4: Incorrect syntax near '='.
伺服器: 訊息 170,層級 15,狀態 1,行 5
Line 5: Incorrect syntax near '='.
CSDNM 2003-09-15
  • 打赏
  • 举报
回复
select empid,empno,
min(case when (select count(*) from a where empid=x.empid and empno=x.empno and empdate=empdate and emptime<=x.emptime)=1 then empdate+' '+emptime end) as t1,
min(case when (select count(*) from a where empid=x.empid and empno=x.empno and empdate=empdate and emptime<=x.emptime)=2 then empdate+' '+emptime end) as t2,
min(case when (select count(*) from a where empid=x.empid and empno=x.empno and empdate=empdate and emptime<=x.emptime)=3 then empdate+' '+emptime end) as t3,
min(case when (select count(*) from a where empid=x.empid and empno=x.empno and empdate=empdate and emptime<=x.emptime)=4 then empdate+' '+emptime end) as t4
from a x
group by empid,empno,empdate
order by empid,empno,empdate

dahroswell 2003-09-15
  • 打赏
  • 举报
回复
但是我的數據庫是動態的﹐
日期是一個無法預料的﹐它每天都會增加几千上萬條的。
gjqdsdcn 2003-09-15
  • 打赏
  • 举报
回复


select empid,case when empdate = + empdate + then empdate +'' + emptime
FROM (SELECT empdate,emptime FROM a)
yoki 2003-09-15
  • 打赏
  • 举报
回复
问题是列数不固定啊,难办
CSDNM 2003-09-15
  • 打赏
  • 举报
回复
---测试数据

create table #a(
empid int,
empno varchar(20),
empdate varchar(20),
emptime varchar(20)
)
go

insert #a
select
1756, 'AA000042', '2002-05-19', '60328'
union all
select
1756, 'AA000042', '2002-05-19', '60332'
union all
select
1756, 'AA000042', '2002-05-19', '60336'
union all
select
1756, 'AA000042', '2002-05-19', '60340'
union all
select
1756, 'AA000042', '2002-05-20', '29755'
union all
select
1756, 'AA000042', '2002-06-11', '41864'
union all
select
1756, 'AA000042', '2002-06-11', '50900'
union all
select
1757, 'AA000021', '2002-05-20', '30200'
union all
select
1759, 'AA000010', '2002-12-14', '59400'
union all
select
1759, 'AA000010', '2002-12-28', '79740'
union all
select
1759, 'AA000010', '2003-04-03', '63360'
union all
select
1760, 'AA000011', '2002-05-22', '45005'
union all
select
1760, 'AA000011', '2002-05-22', '48408'
union all
select
1760, 'AA000011', '2002-05-22', '63005'
union all
select
1760, 'AA000011', '2002-05-22', '66441'
go


--语句
select empid,empno,
min(t1) as t1,
min(t2) as t2,
min(t3) as t3,
min(t4) as t4
from (
select empid,empno,empdate,
case when (select count(*) from #a where empid=x.empid and empno=x.empno and empdate=x.empdate and emptime<=x.emptime)=1 then empdate+' '+emptime end as t1,
case when (select count(*) from #a where empid=x.empid and empno=x.empno and empdate=x.empdate and emptime<=x.emptime)=2 then empdate+' '+emptime end as t2,
case when (select count(*) from #a where empid=x.empid and empno=x.empno and empdate=x.empdate and emptime<=x.emptime)=3 then empdate+' '+emptime end as t3,
case when (select count(*) from #a where empid=x.empid and empno=x.empno and empdate=x.empdate and emptime<=x.emptime)=4 then empdate+' '+emptime end as t4
from #a x
) as y
group by empid,empno,empdate
order by empid,empno,empdate
go

结果:
empid empno t1 t2 t3 t4
----------- -------------------- ----------------------------------------- ----------------------------------------- ----------------------------------------- -----------------------------------------
1756 AA000042 2002-05-19 60328 2002-05-19 60332 2002-05-19 60336 2002-05-19 60340
1756 AA000042 2002-05-20 29755 NULL NULL NULL
1756 AA000042 2002-06-11 41864 2002-06-11 50900 NULL NULL
1757 AA000021 2002-05-20 30200 NULL NULL NULL
1759 AA000010 2002-12-14 59400 NULL NULL NULL
1759 AA000010 2002-12-28 79740 NULL NULL NULL
1759 AA000010 2003-04-03 63360 NULL NULL NULL
1760 AA000011 2002-05-22 45005 2002-05-22 48408 2002-05-22 63005 2002-05-22 66441

(所影响的行数为 8 行)

22,207

社区成员

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

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