急急急急急

stevenslk 2004-07-20 12:22:56
下面有一个数据文件,怎么才能做能一个表呀
000102100480 60
000102100579 72
000102100579 67
000102101272 60
000102101272 34
要做成
000102100480 60 0
000102100579 72 67
000102101272 60 34
请指教,快呀,,,,,
...全文
81 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
WangZWang 2004-07-20
  • 打赏
  • 举报
回复
create table test(
id varchar(30),
score integer)

Insert test
Select
'000102100480', 60
union all select
'000102100579', 72
union all select
'000102100579', 67
union all select
'000102101272', 60
union all select
'000102101272', 34
000102101272 60 34

Select * from #test

--创建一个处理函数
Create function uf_functionName(
@id varchar(30),
@score integer
)returns varchar(8000)
as
begin
declare @re varchar(8000)
set @re=''
if @score=0
select @re=@re+' '+cast(score as varchar)
from test
where id=@id

else
select @re=@re+' '+cast(score as varchar)
from test
where id=@id
return(stuff(@re,1,1,''))
end
go

--调用函数实现查询
select distinct id,score=dbo.uf_functionName(id,score)
from test
group by id,score
go

truncate table test
drop table test


--显示
id score
000102100480 60
000102100579 72 67
000102101272 60 34

--(所影响的行数为 3 行)
frankwong 2004-07-20
  • 打赏
  • 举报
回复
分为两个步骤
1、从数据文件导入到数据表中:
a.使用bulk insert 命令或openrowset查询实现

bulk insert 数据表
from '文件全路径'
with (
FIELDTERMINATOR = ' ', --空格列分隔符
ROWTERMINATOR = '\n' --行分隔符
)
futulove 2004-07-20
  • 打赏
  • 举报
回复
你的第一列有没有重复三次或更多的可能
BigGunLee 2004-07-20
  • 打赏
  • 举报
回复
你要生成的表估计会有多少列?

34,873

社区成员

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

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