2012-01-28 144 views
0

我從google文檔中提取了一組非結構化電子表格,並將它們加載到具有以下結構的數據庫中以供進一步分析。sql查詢從單元格表重建電子表格的行

enter image description here

我的目標是從像這樣的行去:

SpreadsheetTitle WorksheetTitle Row Column Value 
Copy of updated accounting backup (a-e) Accuquote 3 1 September 2005 
Copy of updated accounting backup (a-e) Accuquote 5 2 Rate 
Copy of updated accounting backup (a-e) Accuquote 5 3 # of Sales 
Copy of updated accounting backup (a-e) Accuquote 5 4 Total Spent 
Copy of updated accounting backup (a-e) Accuquote 6 1 Accuquote 
Copy of updated accounting backup (a-e) Accuquote 6 2 $70 
Copy of updated accounting backup (a-e) Accuquote 6 3 45 
Copy of updated accounting backup (a-e) Accuquote 6 4 $3150 
Copy of updated accounting backup (a-e) Accuquote 6 5 PAID 

要這樣:

SpreadsheetTitle WorksheetTitle Row Column1 Column2 Column3.....etc...etc.. 

我可以想辦法通過寫不進行-sql代碼,但我很想知道是否可以沿着遞歸查詢(也許)或透視函數的方式使用某些東西。

回答

1

我不是一個支點專家:http://msdn.microsoft.com/en-us/library/ms177410.aspx

但會是這樣的工作:

SELECT SpreadsheetTitle, 
     WorksheetTitle, 
     row, 
     [1] AS Col1, 
     [2] AS Col2 
FROM (SELECT 'spreadsheet' SpreadsheetTitle, 'worksheet' WorksheetTitle, 0 Row, 1 AS [ColumnID], 1 VALUE 
      UNION ALL 
      SELECT 'spreadsheet' SpreadsheetTitle, 'worksheet' WorksheetTitle, 0 Row, 2 AS [ColumnID], 2 VALUE 
      UNION ALL 
      SELECT 'spreadsheet2' SpreadsheetTitle, 'worksheet' WorksheetTitle, 0 Row, 1 AS [ColumnID], 3 VALUE 
     ) AS flat PIVOT 
    (MAX(Value) FOR [ColumnID] IN ([1], [2])) AS pivoted 
相關問題