如何将Oracle Rows作为列计数。?

时间:2014-02-20 12:32:30

标签: sql oracle pivot

这是我的表格视图。 [专栏名称&记录]。

MERCHANT_ID |CARD_TYPE  |DENOMINATION   |STOCK_IN_HAND

1000017     |CDMA_PP    |1000           | 5 
10000000    |CDMA_PP    |50             | 1 
10000000    |CDMA_PP    |500            | 1 
10000000    |CDMA_PP    |1000           | 1 
10000001    |CDMA_PP    |1000           | 1 

现在我的要求是需要为那些card_denominations创建包含单独列的表。

MERCHANT_ID |CARD_TYPE  |500       |1000     |STOCK_IN_HAND

1000017     |CDMA_PP    | 0        | 1       | 1
10000000    |CDMA_PP    | 1        | 1       | 2
10000001    |CDMA_PP    | 0        | 1       | 1

所以请帮我解决这个问题。 [库存在手显示总卡] 感谢。

1 个答案:

答案 0 :(得分:1)

这应该是一个起点:

SELECT 
*
FROM
(
SELECT 1000017  as merchant_id   ,'CDMA_PP' as cardtype, 1000 as denomination, 5 as stock from dual 
UNION ALL SELECT 10000000,'CDMA_PP',50,1 from dual 
UNION ALL SELECT 10000000,'CDMA_PP',500,1 from dual 
UNION ALL SELECT 10000000,'CDMA_PP',1000,1  from dual
UNION ALL SELECT 10000001,'CDMA_PP',1000,1  from dual
)
PIVOT
(SUM(stock)
FOR denomination in (50,500,1000))