输入数据
需要根据ID透视数据。并且需要输出如下:
需要输出
需要合适的解决方案,以提供有效的输出。
添加文字作为图片的替代选择:
输入数据:
+---+----+-------+------+------ +---------+-------+
|ID |Q_NO| SUB_CD|PR_TX |C_TX |AD_DTL_TX| B_NM |
+---+----+-------+------+------ +---------+-------+
|111| 1| A |asd |sdf |qwerwet |qqwe |
|111| 1| B |ger |sdt |uutty | ttt |
|112| 1| B |yut |www |yyjy | y |
|114| 1| A |atd |stf |qwwet |qe |
|114| 1| B |atw |rf |qwet |qwp |
|114| 2| A |aq |yf |qyet |qoe |
|117| 1| A |aee |yrr |qyet |qoe |
|117| 2| A |tq |uf |et |oe |
+---+----+-------+------+------ +---------+-------+
必需输出:
+---+-----------+---------+---------+-----------+------------+---------+-----------+-------------+--------------+-----------+------------+-------+----------+---------+-------------+-----------+-----------+----------+
|ID |Q_NO_1A |SUB_CD_1A| PR_TX_1A|C_TX_1A |AD_DTL_TX_1A| B_NM_1A| Q_NO_1B| SUB_CD_1B| PR_TX_1B |C_TX_1B |AD_DTL_TX_1B|B_NM_1B| Q_NO_2A |SUB_CD_2A| PR_TX_2A |C_TX_2A |AD_DTL_TX_2A| B_NM_2A|
+---+-----------+---------+---------+-----------+------------+---------+-----------+-------------+--------------+-----------+------------+-------+----------+---------+-------------+-----------+-----------+----------+
|111| yes | yes | asd | sdf | qwerwet| qqwe| yes| yes| ger | sdt | uutty | ttt | null | null | null | null | null | null |
|112| null | null | null | null | null |null |yes | yes | yut | www | yyj |y | null | null | null |null | null |null |
|114| yes | yes | atd | stf | qwwet | qe | yes | yes | atw | rf | qwe | qwp | yes | yes | aq | yf | qyet |qoe |
|117| yes | yes | aee |yrr | qyet | qoe | null | null | null | null | null | null| yes | yes | tq | uf | et | oe |
+---+-----------+---------+---------+-----------+------------+---------+-----------+-------------+--------------+-----------+------------+-------+----------+---------+-------------+-----------+-----------+----------+
答案 0 :(得分:2)
几个工会和一个支点。
select *
from
(
select id, concat('Q_NO_', q_no, sub_cd) as title, 'yes' as value from YourTable
union all
select id, concat('SUB_CD_', q_no, sub_cd) as title, 'yes' as value from YourTable
union all
select id, concat('PR_TX_', q_no, sub_cd) as title, pr_tx as value from YourTable
union all
select id, concat('C_TX_', q_no, sub_cd) as title, c_tx as value from YourTable
union all
select id, concat('AD_DTL_', q_no, sub_cd) as title, ad_dtl_tx as value from YourTable
union all
select id, concat('B_NM_', q_no, sub_cd) as title, b_nm as value from YourTable
) q
pivot (max(value) FOR title IN (
[Q_NO_1A],[SUB_CD_1A],[PR_TX_1A],[C_TX_1A],[AD_DTL_1A],[B_NM_1A],
[Q_NO_1B],[SUB_CD_1B],[PR_TX_1B],[C_TX_1B],[AD_DTL_1B],[B_NM_1B],
[Q_NO_2A],[SUB_CD_2A],[PR_TX_2A],[C_TX_2A],[AD_DTL_2A],[B_NM_2A]
)
) pvt;