我没有使用 SQL Server,我只能提供一般指导:
1)将文件加载到数据库中,列中的整行,添加行号。结果将类似于(rid
是行号):
rid rline
1 1, 6997, 01234
2 2, 012345, 5678999, Y, 11, 20130301
3 2, 012345, 5678988, Y, 11, 20130301
4 1, 6647, 01234
5 2, 012345, 5678999, Y, 11, 20130301
6 2, 012345, 5678988, Y, 11, 20130301
2)使用一些SQL来获取所需形状的数据。这意味着您必须为每行 2 找到第一行 1。未测试:
select
csvdata.rline,
csvdata.rid,
(select rline from csvdata x where rline like '1,%' and x.rid < csvdata.rid order by x.rid desc limit 1) as TopRline
from
csvdata
where
rline like '2,%' -- this will limit lines to only those with the detail
希望这将产生以下包含三列的结果:
rid rline TopRline
2 2, 012345, 5678999, Y, 11, 20130301 1, 6997, 01234
3 2, 012345, 5678988, Y, 11, 20130301 1, 6997, 01234
5 2, 012345, 5678999, Y, 11, 20130301 1, 6647, 01234
6 2, 012345, 5678988, Y, 11, 20130301 1, 6647, 01234
3) 使用某些 SQL 函数将数据拆分为列(例如,在 PostgreSQL 中,text_to_array()
会这样做)。假设 2 的结果存储在表 temp 中,则类似于:
select
(string_to_array(rline,','))[1] as column1,
(string_to_array(rline,','))[2] as column2,
(string_to_array(rline,','))[3] as column3,
(string_to_array(rline,','))[4] as column4,
(string_to_array(rline,','))[5] as column5,
(string_to_array(rline,','))[6] as column6,
(string_to_array(TopRline,','))[1] as column1top,
(string_to_array(TopRline,','))[2] as column2top,
(string_to_array(TopRline,','))[3] as column3top,
from
temp
4)将数据存储在您想要的任何表中。