2

I've got a problem I've not come across before. I've got an Excel datasource I've loaded into SQL table and I am trying to transform into something a bit more sensible. What I've got is column A in the SQL table from Excel that contains all 3 fields of data but I need to add columns to replace the groups of Owner and Stage

Note a user can have many stages and a stage can have many projects. Project names are unique.

example

My Source Table that I have imported from Excel has 1 column

[ColumnA]
Owner: David Jones 
  Stage1: Suspect 
Project A  
Project B 
 Stage2:Qualified Suspect
Project C 
Project D  
Owner: John Doe
  Stage1:Suspect
Project E 
  Stage2:Qualified Suspect
Project F 
Project G 

then I want my target rows to look like

[owner]        [stage]                      [project]
David Jones    Stage1:Suspect               ProjectA
David Jones    Stage1:Suspect               ProjectB
David Jones    Stage2:Qualified Suspect     ProjectC
David Jones    Stage1:Qualified Suspect     ProjectD
John Doe       Stage1:Suspect               ProjectE
John Doe       Stage1:Qualified Suspect     ProjectF
John Doe       Stage1:Qualified Suspect     ProjectG

Thanks in advance

G

3
  • 1
    You might be better doing your data cleaning prior to loading the spreadsheet in. How is your data in that column separated? It it's a consistent format, it might be easier to write a small macro to clean it up prior to loading.
    – Paddy
    Sep 1, 2010 at 9:52
  • Thanks for the replay Paddy but thats not an option (long story). The file is generated by a third party application and whilst a macro processing method may help out, I need to read newly generated files each week. I suppose I could then write some code to apply the macro to each new worksheet but it sounds a bit of a messy solution. Cheers G
    – Gilly
    Sep 1, 2010 at 11:40
  • What do you have on each row in that one column?
    – Abdallah
    Sep 1, 2010 at 13:12

3 Answers 3

3

Table_1 is a SQL Table you would import your Excel into.

    select * from Table_1
declare @temp varchar(50)
declare @owner varchar(50)
declare @stage varchar(50)
Declare My_Cursor cursor 
For
Select ColumnA
from Table_1

Open My_Cursor
Fetch Next from My_Cursor into @temp
while(@@Fetch_Status=0)
begin
if @temp like 'Owner%'
set @owner = @temp
if @temp like 'Stage%'
set @stage=@temp
if @temp like 'Project%'
begin
select LTRIm(SUBSTRing(@owner,7,LEN(@owner))),@stage,@temp
end
Fetch NExt from My_Cursor into @temp
end
close My_Cursor
deallocate My_Cursor

Output:

    ColumnA
--------------------------------------------------
Owner: David Jones
Stage1: Suspect
Project A
Project B
Stage2: Qualified Suspect
Project C
Project D
Owner: John Doe
Stage1: Suspect
Project E
Stage2: Qualified Suspect
Project F
Project G

(13 row(s) affected)


-------------------------------------------------- -------------------------------------------------- --------------------------------------------------
David Jones                                        Stage1: Suspect                                    Project A

(1 row(s) affected)


-------------------------------------------------- -------------------------------------------------- --------------------------------------------------
David Jones                                        Stage1: Suspect                                    Project B

(1 row(s) affected)


-------------------------------------------------- -------------------------------------------------- --------------------------------------------------
David Jones                                        Stage2: Qualified Suspect                          Project C

(1 row(s) affected)


-------------------------------------------------- -------------------------------------------------- --------------------------------------------------
David Jones                                        Stage2: Qualified Suspect                          Project D

(1 row(s) affected)


-------------------------------------------------- -------------------------------------------------- --------------------------------------------------
John Doe                                           Stage1: Suspect                                    Project E

(1 row(s) affected)


-------------------------------------------------- -------------------------------------------------- --------------------------------------------------
John Doe                                           Stage2: Qualified Suspect                          Project F

(1 row(s) affected)


-------------------------------------------------- -------------------------------------------------- --------------------------------------------------
John Doe                                           Stage2: Qualified Suspect                          Project G

(1 row(s) affected)

Replace the select statement with an insert into your table. Let me know if it works.

4
  • It relies on the order in the cursor being the same as the order in which the rows were inserted into the table. This is not reliable.
    – user359040
    Sep 1, 2010 at 17:14
  • @Mark Bannister, Yes it relies on the order by according to the example the whole relationship depends on the order. When he imports the excel it should stay in the same order as it was.
    – Gage
    Sep 1, 2010 at 17:31
  • "it should stay in the same order" - as a general rule, you can't rely on this in relational databases (including SQLServer).
    – user359040
    Sep 2, 2010 at 9:43
  • Thanks heaps, this will get me over the line and also thanks to everyone for your comments/thoughts. I will keep the issue of order in mind and put some tests around the process to ensure I know if it changes. Thanks again G
    – Gilly
    Sep 3, 2010 at 1:00
1

If I have understood this correctly, the owner and stage of a project are determined by the last owner and stage record(s) read in before the project row.

Which produces a big problem - in most relational databases (including SQLServer), you can't guarantee that the output order of a query matches the order that the rows were entered into the table. In other words, just from querying [ColumnA], there is no way to tell that Project A belongs to David Jones and Stage1:Suspect, rather than (eg.) John Doe and Stage2:Qualified Suspect.

If there isn't already (at the very least) a column holding the Excel row number in the database source table, then such a column should be added and the table should be reloaded.

I think it would be simpler to amend the Excel spreadsheet to derive the owner, stage and project columns, and to load the table from the amended spreadsheet.

1

Assuming your single column is already available in the database table, one thing I can suggest is for you to write a dynamic sql (Stored procedure). Create a new table with 3 columns(owner, stage, project) say FinalResult.

Then you can get each row from your excel datatable using a cursor and insert the results based on your logic into that new FinalResult table.

Finaly you can retrieve those results from the new table to get desired result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.