Answers for "sql pivot"

SQL
5

sql pivot

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
);
Posted by: Guest on April-13-2021
0

sql server pivot rows to columns

select Firstname, Amount, PostalCode, LastName, AccountNumber
from
(
  select value, columnname
  from yourtable
) d
pivot
(
  max(value)
  for columnname in (Firstname, Amount, PostalCode, LastName, AccountNumber)
) piv;
Posted by: Guest on December-29-2020
0

sql pivot table

select *
from
(
  select game, player, goals
  from yourtable
) src
pivot
(
  sum(goals)
  for player in ([John], [Paul], [Mark], [Luke])
) piv
order by game
Posted by: Guest on August-11-2021
0

pivot table sql

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
Posted by: Guest on February-04-2021
0

pivot table sql server

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
Posted by: Guest on February-04-2021
-2

sql server pivot rows to columns

-- Dynamic Pivot with unknown number of columnnames

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(ColumnName) 
                    from yourtable
                    group by ColumnName, id
                    order by id
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = N'SELECT ' + @cols + N' from 
             (
                select value, ColumnName
                from yourtable
            ) x
            pivot 
            (
                max(value)
                for ColumnName in (' + @cols + N')
            ) p '

exec sp_executesql @query;
Posted by: Guest on December-29-2020

Code answers related to "SQL"

Browse Popular Code Answers by Language