2013-01-23 117 views
0

我正在使用Lime Survey並最終希望將Crystal Reports用於我的最終輸出,並且正在尋找介入步驟的幫助。每個響應記錄有一行,有100多個問題,分爲幾個部分。輸出看起來像是每個問題都有一列的交叉表,但是我需要在數據處理之前將數據轉化爲未知數據,然後才能在Crystal Reports中使用它。用Crystal爲Crystal Reports準備LimeSurvey輸出

根據調查,可能有4個部分或可能有多達15個。那麼,有沒有一種方法可以根據部分的數量動態地使用sql?

爲了說明 - 在Excel中輸出石灰調查看起來像這樣


ID Subject Relationship 1Section 1SQuestion1 1SQuestion2 2Section 2SQuestion1 2SQuestion2 
1 John Boss   1Section 2   4   2Section 3   4 
2 John Peer   1Section 4   3   2Section 2   5 
3 Sally Boss   1Section 3   3   2Section 4   5 
4 Sally Peer   1Section 5   6   2Section 1   3 

這裏是我最終需要它看起來像

 
ID Subject Relationship 1Section Col5   Col6 
1 John Boss   1Section 1SQuestion1  2 
1 John Boss   1Section 1SQuestion2  4 
2 John Peer   1Section 1SQuestion1  4 
2 John Peer   1Section 1SQuestion2  3 
3 Sally Boss   1Section 1SQuestion1  3 
3 Sally Boss   1Section 1SQuestion2  3 
4 Sally Peer   1Section 1SQuestion1  5 
4 Sally Peer   1Section 1SQuestion2  6 
1 John Boss   2Section 2SQuestion1  3 
1 John Boss   2Section 2SQuestion2  4 
2 John Peer   2Section 2SQuestion1  2 
2 John Peer   2Section 2SQuestion2  5 
3 Sally Boss   2Section 2SQuestion1  4 
3 Sally Boss   2Section 2SQuestion2  5 
4 Sally Peer   2Section 2SQuestion1  1 
4 Sally Peer   2Section 2SQuestion2  3 

感謝

+0

最後,我覺得你真的需要您發送給水晶是最終的調查數據表:遞增的主鍵,響應編號,主題名,關係名,節#,問題# ,回答#。或者,您也可以將調查ID發送給調查ID,然後您可以隨時在該數據表中存儲儘可能多的調查和響應。 – Ally

+0

好好玩了石灰調查的演示後,我現在明白了一點。那麼,你是否有權訪問數據庫,並且必須依賴這些Excel文件? – Ally

+0

我確實可以訪問數據庫,你有一些見解嗎?謝謝 – xyz

回答

0

如果要執行這個數據在sql中轉換的話可以用一個UNION ALL查詢:

select id, subject, relationship, `1Section`, '1sQuestion1' col5, `1sQuestion1` col6 
from yourtable 
union all 
select id, subject, relationship, `1Section`, '1sQuestion2' col5, `1sQuestion2` col6 
from yourtable 
union all 
select id, subject, relationship, `2Section`, '2sQuestion1' col5, `2sQuestion1` col6 
from yourtable 
union all 
select id, subject, relationship, `2Section`, '2sQuestion2' col5, `2sQuestion2` col6 
from yourtable 

請參閱SQL Fiddle with Demo。其給出結果:

| ID | SUBJECT | RELATIONSHIP | 1SECTION |  COL5 | COL6 | 
--------------------------------------------------------------- 
| 1 | John |   Boss | 1Section | 1sQuestion1 | 2 | 
| 2 | John |   Peer | 1Section | 1sQuestion1 | 4 | 
| 3 | Sally |   Boss | 1Section | 1sQuestion1 | 3 | 
| 4 | Sally |   Peer | 1Section | 1sQuestion1 | 5 | 
| 1 | John |   Boss | 1Section | 1sQuestion2 | 4 | 
| 2 | John |   Peer | 1Section | 1sQuestion2 | 3 | 
| 3 | Sally |   Boss | 1Section | 1sQuestion2 | 3 | 
| 4 | Sally |   Peer | 1Section | 1sQuestion2 | 6 | 
| 1 | John |   Boss | 2Section | 2sQuestion1 | 3 | 
| 2 | John |   Peer | 2Section | 2sQuestion1 | 2 | 
| 3 | Sally |   Boss | 2Section | 2sQuestion1 | 4 | 
| 4 | Sally |   Peer | 2Section | 2sQuestion1 | 1 | 
| 1 | John |   Boss | 2Section | 2sQuestion2 | 4 | 
| 2 | John |   Peer | 2Section | 2sQuestion2 | 5 | 
| 3 | Sally |   Boss | 2Section | 2sQuestion2 | 5 | 
| 4 | Sally |   Peer | 2Section | 2sQuestion2 | 3 | 
+0

你好bluefeet,感謝你的時間和代碼,它非常受歡迎。 – xyz

+0

bluefeet我還有一個問題。相反,我想將輸出作爲兩個表格;一個表是1Section的數據,第二個表是2Section的數據。我用sqlfiddle玩過一些,但是我對sql的知識非常有限。 – xyz

+0

@Tim如果數據在不同的表中,你可以做同樣的事情。看到這個演示 - http://www.sqlfiddle.com/#!2/d0aff/1 – Taryn