求教oracle 行变列的函数 50
我有一张表(table)有4条数据我希望按照按照C1列分组,讲C2列变成行(C2列的名称是不固定的,不能用decode函数),C3列的值是C1行跟C2列去确定的,出现的分...
我有一张表(table) 有4条数据
我希望按照按照C1列分组,讲C2列变成行(C2列的名称是不固定的,不能用decode函数),C3列的值是C1行跟C2列去确定的,出现的分组效果如下图
请问sql要如何实现 展开
我希望按照按照C1列分组,讲C2列变成行(C2列的名称是不固定的,不能用decode函数),C3列的值是C1行跟C2列去确定的,出现的分组效果如下图
请问sql要如何实现 展开
展开全部
PS:以下直接复制,在Oracle中执行,我的只能作参考
--数据库的表
SELECT
LEVEL C1,
CASE
WHEN MOD(LEVEL, 2) = 1 THEN
'A'
ELSE
'B'
END AS C2,
CASE
WHEN MOD(LEVEL, 3) = 0 THEN
'A'
WHEN MOD(LEVEL, 3) = 1 THEN
'B'
ELSE
'C'
END AS C3
FROM DUAL
CONNECT BY LEVEL <= 10;
--参考实例如下:
select
A.C1,A.C3 A,B.C3 B
from
( SELECT
LEVEL C1,
CASE
WHEN MOD(LEVEL, 2) = 1 THEN
'A'
ELSE
'B'
END AS C2,
CASE
WHEN MOD(LEVEL, 3) = 0 THEN
'A'
WHEN MOD(LEVEL, 3) = 1 THEN
'B'
ELSE
'C'
END AS C3
FROM DUAL
CONNECT BY LEVEL <= 10
) A
LEFT JOIN
(SELECT
LEVEL C1,
CASE
WHEN MOD(LEVEL, 2) = 1 THEN
'A'
ELSE
'B'
END AS C2,
CASE
WHEN MOD(LEVEL, 3) = 0 THEN
'A'
WHEN MOD(LEVEL, 3) = 1 THEN
'B'
ELSE
'C'
END AS C3
FROM DUAL
CONNECT BY LEVEL <= 10
)B
ON A.C2 != B.C2
GROUP BY A.C1,A.C3,B.C3
ORDER BY C1
--数据库的表
SELECT
LEVEL C1,
CASE
WHEN MOD(LEVEL, 2) = 1 THEN
'A'
ELSE
'B'
END AS C2,
CASE
WHEN MOD(LEVEL, 3) = 0 THEN
'A'
WHEN MOD(LEVEL, 3) = 1 THEN
'B'
ELSE
'C'
END AS C3
FROM DUAL
CONNECT BY LEVEL <= 10;
--参考实例如下:
select
A.C1,A.C3 A,B.C3 B
from
( SELECT
LEVEL C1,
CASE
WHEN MOD(LEVEL, 2) = 1 THEN
'A'
ELSE
'B'
END AS C2,
CASE
WHEN MOD(LEVEL, 3) = 0 THEN
'A'
WHEN MOD(LEVEL, 3) = 1 THEN
'B'
ELSE
'C'
END AS C3
FROM DUAL
CONNECT BY LEVEL <= 10
) A
LEFT JOIN
(SELECT
LEVEL C1,
CASE
WHEN MOD(LEVEL, 2) = 1 THEN
'A'
ELSE
'B'
END AS C2,
CASE
WHEN MOD(LEVEL, 3) = 0 THEN
'A'
WHEN MOD(LEVEL, 3) = 1 THEN
'B'
ELSE
'C'
END AS C3
FROM DUAL
CONNECT BY LEVEL <= 10
)B
ON A.C2 != B.C2
GROUP BY A.C1,A.C3,B.C3
ORDER BY C1
追问
C2列的值是不固定的,因为是程序里那列是手动输入的,不可能知道人家那列要写什么,所以sql中不可能存在‘A’,‘B’这种写死的东西,case when跟decode本身就差不多,这样是不可能的实现的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询