0

私は Lime Survey を使用していますが、最終的に Crystal Reports を最終出力に使用したいと考えており、その間の手順について助けを求めています。応答レコードごとに 1 つの行があり、100 以上の質問がいくつかのセクションに分割されています。出力は、質問ごとに 1 列のクロス集計のように見えますが、Crystal Reports で作業するには、データのピボットを解除する必要があります。

調査によっては、4 つのセクションがある場合もあれば、15 ものセクションがある場合もあります。では、セクションの数に基づいて、SQL でこれを動的に行う方法はありますか?

説明のために - Excel では、Lime Survey からの出力は次のようになります。


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 サブジェクト 関係 1Section Col5 Col6
1 ジョン・ボス 1 セクション 1S 質問 1 2
1 ジョン・ボス 1 セクション 1S 質問 2 4
2 ジョン・ピア 1セクション 1S質問1 4
2 ジョン・ピア 1 セクション 1S 質問 2 3
3 サリーボス 1Section 1SQuestion1 3
3 サリーボス 1Section 1SQuestion2 3
4 サリー・ピア 1セクション 1S質問1 5
4 サリー・ピア 1Section 1SQuestion2 6
1 ジョン・ボス 2 セクション 2S 質問 1 3
1 ジョン・ボス 2 セクション 2S 質問 2 4
2 ジョン・ピア 2セクション 2S 質問 1 2
2 ジョン・ピア 2セクション 2S質問2 5
3 サリーボス 2Section 2SQuestion1 4
3 サリーボス 2Section 2SQuestion2 5
4 サリー・ピア 2Section 2SQuestion1 1
4 サリー・ピア 2 セクション 2S 質問 2 3

ありがとう

4

1 に答える 1

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 FiddlewithDemoを参照してください。結果は次のようになります。

| 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 |
于 2013-01-24T14:38:39.247 に答える