Benutzer mit den meisten Antworten
convert information in rows to rows and columns

Frage
-
I have imported information from a long website into a EXCEL-Table.
Actually it is a list with adress information and also some graphics for each adress item.
Opening this EXCEL-file, I find my information completely in the first column.
Each adress item is a block of rows starting with 2 or 3 empty rows, then 7 rows with valid information .
The task is now to bring this information into the normal table structure, so my table should have at least 7 columns with the necessary fields.
How can I do this ?
Integration service has to read the EXCEL-file row-by-row until the first row with valid data. Then this line and the following lines have to be imported into the different fields.
Is there a task for this integration services available ?
Antworten
-
Hi Nico,
due to the fact, that this is a german forum, i will answer in german! Please leave a note, if this is a problem for you!Ich würde das Problem anders angehen und einfach den gesamten Inhalt der ersten Spalte aus der Excel-Datei in eine Tabelle importieren. Diese würde ich dann mit einer Stored Procedure verarbeiten, die die entsprechende Logik beinhaltet. Du könntest z. B. auch mit einigen Hilfsspalten und Updates das Problem angehen:
declare @Import as table (Lfnr integer identity, Zeile varchar(255), id integer); Insert into @Import(Zeile) values (''); Insert into @Import(Zeile) values (''); Insert into @Import(Zeile) values ('aa'); Insert into @Import(Zeile) values ('ab'); Insert into @Import(Zeile) values ('ac'); Insert into @Import(Zeile) values (''); Insert into @Import(Zeile) values (''); Insert into @Import(Zeile) values ('dd'); Insert into @Import(Zeile) values ('de'); Insert into @Import(Zeile) values ('de'); Insert into @Import(Zeile) values (''); Insert into @Import(Zeile) values (''); Insert into @Import(Zeile) values ('ff'); Insert into @Import(Zeile) values ('fg'); Insert into @Import(Zeile) values ('fh'); -- Bereichgsrenzen feststecken update @Import set id = Bereich from @Import ii inner Join (Select i1.Lfnr, ROW_NUMBER() over(Partition by i1.id order by i1.Lfnr) as Bereich from @Import i1 left Join @Import i2 on i1.Lfnr-1 = i2.Lfnr where i1.Zeile <> '' and (i2.Zeile = '' OR i2.Zeile IS null)) x on ii.Lfnr = x.Lfnr; update @Import set id = New_Id from @Import i1 inner join (Select i.*, (Select max(id) from @Import i2 where i2.Lfnr <= i.Lfnr) as New_Id from @Import i) x on i1.Lfnr = x.Lfnr ; SELECT id, [1] AS F1,[2] AS F2,[3] AS F3 FROM (Select id, Zeile, ROW_NUMBER() over(Partition by id ORDER by Lfnr) as Bereich_Lfnr from @Import where Zeile <> '') i PIVOT ( max(i.Zeile) FOR i.Bereich_Lfnr IN ( [1],[2],[3] ) ) as x;
Einen schönen Tag noch,
Christoph
--
Microsoft SQL Server MVP
www.insidesql.org/blogs/cmu- Als Antwort markiert Robert BreitenhoferModerator Montag, 26. März 2012 09:01
Alle Antworten
-
Hi Nico,
due to the fact, that this is a german forum, i will answer in german! Please leave a note, if this is a problem for you!Ich würde das Problem anders angehen und einfach den gesamten Inhalt der ersten Spalte aus der Excel-Datei in eine Tabelle importieren. Diese würde ich dann mit einer Stored Procedure verarbeiten, die die entsprechende Logik beinhaltet. Du könntest z. B. auch mit einigen Hilfsspalten und Updates das Problem angehen:
declare @Import as table (Lfnr integer identity, Zeile varchar(255), id integer); Insert into @Import(Zeile) values (''); Insert into @Import(Zeile) values (''); Insert into @Import(Zeile) values ('aa'); Insert into @Import(Zeile) values ('ab'); Insert into @Import(Zeile) values ('ac'); Insert into @Import(Zeile) values (''); Insert into @Import(Zeile) values (''); Insert into @Import(Zeile) values ('dd'); Insert into @Import(Zeile) values ('de'); Insert into @Import(Zeile) values ('de'); Insert into @Import(Zeile) values (''); Insert into @Import(Zeile) values (''); Insert into @Import(Zeile) values ('ff'); Insert into @Import(Zeile) values ('fg'); Insert into @Import(Zeile) values ('fh'); -- Bereichgsrenzen feststecken update @Import set id = Bereich from @Import ii inner Join (Select i1.Lfnr, ROW_NUMBER() over(Partition by i1.id order by i1.Lfnr) as Bereich from @Import i1 left Join @Import i2 on i1.Lfnr-1 = i2.Lfnr where i1.Zeile <> '' and (i2.Zeile = '' OR i2.Zeile IS null)) x on ii.Lfnr = x.Lfnr; update @Import set id = New_Id from @Import i1 inner join (Select i.*, (Select max(id) from @Import i2 where i2.Lfnr <= i.Lfnr) as New_Id from @Import i) x on i1.Lfnr = x.Lfnr ; SELECT id, [1] AS F1,[2] AS F2,[3] AS F3 FROM (Select id, Zeile, ROW_NUMBER() over(Partition by id ORDER by Lfnr) as Bereich_Lfnr from @Import where Zeile <> '') i PIVOT ( max(i.Zeile) FOR i.Bereich_Lfnr IN ( [1],[2],[3] ) ) as x;
Einen schönen Tag noch,
Christoph
--
Microsoft SQL Server MVP
www.insidesql.org/blogs/cmu- Als Antwort markiert Robert BreitenhoferModerator Montag, 26. März 2012 09:01
-
Hallo NicoNi,
Ich gehe davon aus, dass die Antwort Dir weitergeholfen hat.
Solltest Du noch "Rückfragen" dazu haben, so gib uns bitte Bescheid.Grüße,
Robert
Robert Breitenhofer, MICROSOFT
Bitte haben Sie Verständnis dafür, dass im Rahmen dieses Forums, welches auf dem Community-Prinzip „Entwickler helfen Entwickler“ beruht, kein technischer Support geleistet werden kann oder sonst welche garantierten Maßnahmen seitens Microsoft zugesichert werden können.