代码如下:
Select part_no, part_description, 1 x1 , 2 as x2, 3 as x3
From (
Select part_no, part_description, quantity --, cost
, rn
From (
select part_no, ifsapp.Inventory_Part_API.Get_Description(CONTRACT,PART_NO) as Part_description
, TRUNC(date_applied, 'DAY')+1 start_of_week, Sum( quantity ) quantity
, Sum( ifsapp.Inventory_Transaction_Cost_API.Get_Sum_Unit_Cost(transaction_id, 'TRUE', 'TRUE') *QUANTITY ) as Cost
, rn
from ifsapp.INVENTORY_TRANSACTION_HIST Left Outer Join Rowsnum On TRUNC(date_applied, 'DAY')+1 = dt
where part_no='CUTC12444' and
direction='-'
and date_applied between
Case When '&No_Of_Weeks' Is Null Then trunc(add_months(sysdate, -11),'mm')
When '&No_Of_Weeks' Is not Null Then To_Date('&Period_ending_date','DD/MM/YYYY') - ('&No_Of_Weeks'*7) End
and
Nvl(To_Date('&Period_ending_date','DD/MM/YYYY'), last_day(trunc(sysdate)) )
and contract Not In ('CPH','CTM','VMS','CGP')
Group by part_no, ifsapp.Inventory_Part_API.Get_Description(CONTRACT,PART_NO), TRUNC(date_applied, 'DAY')+1, rn
) data Where Nvl(rn,0) != 0
) src
Pivot
( Sum( Quantity ) for rn In ( 1,2,3 ) )
无需进行大量查询,您获得结果的原因是您正在做的事情:
Select part_no, part_description, 1 x1 , 2 as x2, 3 as x3
这里是
1
、2
和 3
和数字文字,而不是子查询中的值。
你的枢轴有
for rn In ( 1,2,3 )
并且由于您没有提供别名,结果集将为这些列提供与值相同的名称 - 但作为带引号的标识符(因为
1
不是有效的列名称,但 "1"
是 - 请参阅对象命名更多规则)。
这意味着您在引用它们时还需要使用带引号的标识符:
Select part_no, part_description, "1" x1 , "2" as x2, "3" as x3
或者只是
Select part_no, part_description, "1" , "2", "3"
您还可以在数据透视子句中使用别名,使用前缀作为每个值的全名。