create table product (
ProductNumber varchar(10) ,
ProductName varchar(10),
SalesQuantity int,
Salescountry varchar(10)
);
Sample value:
insert into product values
('P1','PenDrive', 50,'US')
, ('P2','Mouse', 100,'UK')
, ('P3','KeyBoard', 250,'US')
, ('P1','PenDrive', 300,'US')
, ('P2', 'Mouse', 450,'UK')
, ('P5','Dvd', 50,'UAE');
I want to dynamically generate the name of Salescountry and display the country SalesQuantity total sales.
Expected result:
ProductName US UK UAE
------------- ---------------
PenDrive 350 0 0
Mouse 0 550 0
KeyBoard 250 0 0
Dvd 0 0 50
I did it with SQL Server 2008 R2:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
SET @cols = STUFF((SELECT distinct',' + QUOTENAME(SalesCountry)
FROM Product
FOR XML PATH(''), TYPE
).value('.','NVARCHAR(MAX)')
,1,1,'')
set @query ='SELECT ProductName, '+ @cols +' from
(
select ProductName
, SalesQuantity as q
, Salescountry
from Product
) x
pivot
(
SUM(q)
for Salescountry in ('+ @cols +')
) p'
PRINT(@query);
execute(@query);
How to achieve this goal in Postgres?
SELECT *
FROM crosstab (
'SELECT ProductNumber, ProductName, Salescountry, SalesQuantity
FROM product
ORDER BY 1'
, $$SELECT unnest('{US,UK,UAE1}'::varchar[])$$
) AS ct (
"ProductNumber" varchar
, "ProductName" varchar
, "US" int
, "UK" int
, "UAE1" int);
Detailed description:
> PostgreSQL Crosstab Query
> Pivot on Multiple Columns using Tablefunc
Perform complete dynamic query on different numbers of different Salescountry?
> Dynamic alternative to pivot with CASE and GROUP BY
I have a table called Product:
create table product (
ProductNumber varchar(10),
ProductName varchar(10),
SalesQuantity int,
Salescountry varchar(10)
);
Sample value:
insert into product values
('P1','PenDrive', 50,'US' )
, ('P2','Mouse', 100,'UK')
, ('P3','KeyBoard', 250,'US')
, ('P1' ,'PenDrive', 300,'US')
, ('P2','Mouse', 450,'UK')
, ('P5','Dvd', 50,'UAE' );
I want to dynamically generate the name of Salescountry and display the total sales volume of SalesQuantity in that country.
Expected results:
ProductName US UK UAE
----------------------------
PenDrive 350 0 0
Mouse 0 550 0
KeyBoard 250 0 0
Dvd 0 0 50
I did it with SQL Server 2008 R2:
DECLARE @ cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
SET @cols = STUFF((SELECT dis tinct',' + QUOTENAME(SalesCountry)
FROM Product
FOR XML PATH(''), TYPE
).value('.','NVARCHAR(MAX)')
,1,1,'')
set @query ='SELECT ProductName, '+ @cols +' from
(
select ProductName
, SalesQuantity as q
, Salescountry
from Product
) x
pivot
(
SUM(q)
for Salescountry in ('+ @cols + ')
) p'
PRINT(@query);
execute(@query);
How to achieve this goal in Postgres?
SELECT *
FROM crosstab (
'SELECT ProductNumber, ProductName, Salescountry, SalesQuantity
FROM product< br /> ORDER BY 1'
, $$SELECT unnest('{US,UK,UAE1}'::varchar[])$$
) AS ct (
"ProductNumber" varchar
, "ProductName" varchar
, "US" int
, "UK" int
, "UAE1" int);
Detailed description: p>
> PostgreSQL Crosstab Query
> Pivot on Multiple Columns using Tablefunc
Perform full dynamic query on different number of different Salescountry?
> Dynamic alternative to pivot with CASE and GROUP BY