如何在Oracle SQL中从表中取消透视

如何在Oracle SQL中从表中取消透视,oracle,oracle11g,oracle12c,Oracle,Oracle11g,Oracle12c,我有1-5块砖的累进折扣。不同交易的板块可能有所不同。我想在5个变量中存储所有5个值 我的桌子是这样的 期望值如下所示 示例:2 如果我的桌子是这样的 期望值如下所示 示例:3 如果我的桌子是这样的 期望值如下所示 也只能有一个或两个楼板 提前感谢这是一个多列轴,而不是一个取消轴: --BUILD Sample dataset called MyTable WITH MyTable as ( SELECT 0 slab_from, 100 slab_to, 1 discount F

我有1-5块砖的累进折扣。不同交易的板块可能有所不同。我想在5个变量中存储所有5个值

我的桌子是这样的

期望值如下所示

示例:2

如果我的桌子是这样的

期望值如下所示

示例:3

如果我的桌子是这样的

期望值如下所示

也只能有一个或两个楼板


提前感谢

这是一个多列轴,而不是一个取消轴:

--BUILD Sample dataset called MyTable 
WITH MyTable as (
SELECT 0 slab_from, 100 slab_to, 1 discount FROM dual union all
SELECT 100 slab_from, 200 slab_to, 5 discount FROM dual union all
SELECT 200 slab_from, 99999999 slab_to, 8 discount FROM dual ),

--Now build a CTE with a row number that we can use to pivot the data.
CTE as (SELECT Slab_from, Discount, row_number() over (Order by slab_FROM) RN 
     FROM myTable)

--Now build the data set.  Though I'm a bit confused why there are 4 slabs and 3 discounts in your expected results...

SELECT * FROM (SELECT * FROM CTE)
PIVOT (
max(slab_from) as SLAB, max(Discount) as Discount --[We could add max(Slab_to) SLABTO] to get the max value for each as well if needed...
for RN in (1,2,3,4,5)  --RowNumber values 1-5 since 5 is max according to question
);
上述情况给了我们:

+--------+------------+--------+------------+--------+------------+--------+------------+--------+------------+
| 1_SLAB | 1_DISCOUNT | 2_SLAB | 2_DISCOUNT | 3_SLAB | 3_DISCOUNT | 4_SLAB | 4_DISCOUNT | 5_SLAB | 5_DISCOUNT |
+--------+------------+--------+------------+--------+------------+--------+------------+--------+------------+
|      0 |          1 |    100 |          5 |    200 |          8 |        |            |        |            |
+--------+------------+--------+------------+--------+------------+--------+------------+--------+------------+
如果希望列名以0开头,只需从CTE中的RN中减去1即可

可能的答案2:Tweeked:假设所有楼板范围都从0开始,并且1-5是为可能/可能不在基表中的实际行保留的

--BUILD Sample dataset called MyTable 
WITH MyTable as (
SELECT 0 slab_from, 100 slab_to, 1 discount FROM dual union all
SELECT 100 slab_from, 200 slab_to, 5 discount FROM dual union all
SELECT 200 slab_from, 99999999 slab_to, 8 discount FROM dual),

--Now build a CTE with a row number that we can use to pivot the data.
CTE as (SELECT 0 "0_SLAB", Slab_to, Discount, row_number() over (Order by slab_FROM) RN 
     FROM myTable)

--Now build the data set.  Though I'm a bit confused why there are 4 slabs and 3 discounts in your expected results...

SELECT * FROM (SELECT * FROM CTE)
PIVOT (
max(slab_to) as SLAB, max(Discount) as Discount --[We could add max(Slab_to) SLABTO] to get the max value for each as well if needed...
for RN in (1,2,3,4,5)  --RowNumber values 1-5 since 5 is max according to question
);
这里唯一的区别是我使用slab_to,并且我将0_slab硬编码为0,以便所有其他可能的范围从1-5调整

列是带前缀的,而不是带后缀的,但pivot就是这样做的。而且,它们不是按所有折扣的先后顺序排列的;但再一次;枢轴就是这样做的;但只要数据正确且可重复,我认为列顺序和名称并不重要


但我仍在为为什么需要枢轴而挣扎。。您在一个规范化的基于行的表中拥有所需的数据,提取数据在应用程序中迭代并呈现数据我不知道为什么需要数据透视。

那么这是基于0的开始还是基于1的开始?折扣是1板是0…你也有重叠是100板在折扣1或5?板应该重叠。100的牌号是1。如果有4个牌号,为什么没有第四个折扣呢?谢谢。我也得出了可能的答案2。