SELECT <non-pivoted column>,
[first pivoted column] AS <column name>,
[second pivoted column] AS <column name>,
...
[last pivoted column] AS <column name>
FROM
(<SELECT query that produces the data>)
AS <alias for the source query>
PIVOT
(
<aggregation function>(<column being aggregated>)
FOR
[<column that contains the values that will become column headers>]
IN ( [first pivoted column], [second pivoted column],
... [last pivoted column])
) AS <alias for the pivot table>
<optional ORDER BY clause>;
SELECT * -- Total invoices per gender
FROM (
SELECT invoice, gender
FROM sales
) d
PIVOT (
sum(invoice)
FOR gender IN ('F' AS "Women", 'M' AS "Men")
);
-- Table Sales:
CREATE TABLE sales (
gender VARCHAR2(1 BYTE), -- 'F' or 'M'
invoice NUMBER
);
SELECT SalesAgent AS PivotSalesAgent, India, US, UK FROM tblAgentsSales
PIVOT
(
SUM(SalesAmount) FOR SalesCountry IN (India, US, UK)
)
AS testPivotTable
SELECT SalesAgent GrpBySalesAgent, SalesCountry, SUM(SalesAmount) Sales from tblAgentsSales
GROUP BY SalesAgent, SalesCountry
select SalesAgent TableSalesAgent, SalesCountry, SalesAmount from tblAgentsSales
select *
from
(
select game, player, goals
from yourtable
) src
pivot
(
sum(goals)
for player in ([John], [Paul], [Mark], [Luke])
) piv
order by game
SELECT SalesAgent AS PivotSalesAgent, India, US, UK FROM tblAgentsSales
PIVOT
(
SUM(SalesAmount) FOR SalesCountry IN (India, US, UK)
)
AS testPivotTable
SELECT SalesAgent GrpBySalesAgent, SalesCountry, SUM(SalesAmount) Sales
from tblAgentsSales
GROUP BY SalesAgent, SalesCountry
select SalesAgent TableSalesAgent, SalesCountry, SalesAmount
from tblAgentsSales
SELECT * FROM(
SELECT date,line_number,machine_number,TIME,VAL FROM(
select *
from output_tbl
unpivot
(
VAL
for TIME in ([0000HR])
) uvt
)tb1 )tb3
PIVOT (
max(VAL)
FOR TIME IN ([0000HR])
)pvt