2013-01-23 50 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