数据库中業種的存储如下图:
SELECT * FROM M_TORIHIKISAKI_GYOSYU
其中GYIUSYU_CD字段代表不同的業種
而画面需要实现下图所示样式:(将每条数据的業種横向展开显示)
这种情况下便可以使用PIVOT方法,代码如下:
SELECT
C1,
C2,
C3,
C4,
C5,
C6,
C7,
C8,
C9,
C10,
C11,
C12
FROM M_TORIHIKISAKI_GYOSYU
PIVOT(MAX(GYIUSYU_CD)
FOR GYIUSYU_CD IN(1 AS C1,
2 AS C2,
3 AS C3,
4 AS C4,
5 AS C5,
6 AS C6,
7 AS C7,
8 AS C8,
9 AS C9,
10 AS C10,
11 AS C11,
12 AS C12)
)
其执行结果为:
为实现页面效果,可使用decode方法,SQL片段如下
DECODE(C1,1 ,'true','false') AS C1
,DECODE(C2,2 ,'true','false') AS C2
,DECODE(C3,3 ,'true','false') AS C3
,DECODE(C4,4 ,'true','false') AS C4
,DECODE(C6,6 ,'true','false') AS C6
,DECODE(C7,7 ,'true','false') AS C7
,DECODE(C8,8 ,'true','false') AS C8
,DECODE(C9,9 ,'true','false') AS C9
,DECODE(C10,10 ,'true','false') AS C10
,DECODE(C11,11 ,'true','false') AS C11
,DECODE(C12,12 ,'true','false') AS C12
这样,检索后的结果就是true 或 false ,然后将检索结果赋给checkbox,就能按要求显示了。