其实列转行是比较简单的用sum和decode函数就可以了,但是我遇到的项目,不知道数据谁设计的居然字段里面还设计成long类型,此long类型与java中的不同,oracle中的long是指可变长二进制数据,最长2G,哎没办法只好转换了。
以下是hsqldb的转换,用到了转换函数convert,可以将long类型的转换为double类型,其中d.value是long类型,status是int类型,所以value需要转换
SELECT P.NAME,P.ID,
SUM(DECODE(D.K_NAME,‘a‘,convert(d.value,SQL_DOUBLE),NULL)) AS a1,
SUM(DECODE(D.K_NAME,‘a‘,convert(d.value,SQL_DOUBLE),NULL)) AS b1,
SUM(DECODE(D.K_NAME,‘a‘,D.STATUS,NULL)) AS a1_status,
SUM(DECODE(D.K_NAME,‘a‘,D.STATUS,NULL)) AS b1_status
FROM
P LEFT JOIN
D ON P.ID=D.id
WHERE a IN (‘a‘,‘b‘)
GROUP BY P.NAME,P.ID
ORDER BY decode(a,null,0,a) DESC LIMIT 0,10
h2是内存数据库,跟hsqldb类似,但是性能好像没有hsqldb稳定
SELECT P.NAME,P.ID,
SUM(DECODE(D.K_NAME,‘a‘,convert(d.value,number),NULL)) AS a1,
SUM(DECODE(D.K_NAME,‘a‘,convert(d.value,number),NULL)) AS b1,
SUM(DECODE(D.K_NAME,‘a‘,D.STATUS,NULL)) AS a1_status,
SUM(DECODE(D.K_NAME,‘a‘,D.STATUS,NULL)) AS b1_status
FROM
P LEFT JOIN
D ON P.ID=D.id
WHERE a IN (‘a‘,‘b‘)
GROUP BY P.NAME,P.ID
ORDER BY decode(a,null,0,a) DESC LIMIT 0,10
本文出自 “努力的蜗牛” 博客,请务必保留此出处http://newhouse2000.blog.51cto.com/4355310/1536108