Use multiple aggregate columns in pivot query in SQL Server

Last Reply 3 months ago By pandeyism

Posted 3 months ago

Hi,

I have a table which gives following output using query

"select VisaModeID,VisaValidity,StayValidity,ProcessTime from Service where VisaTypeID=1"

Output:

Now I need Pivot Related Query which gives below output from above table.

should be like:

 

Posted 3 months ago Modified on 3 months ago

Hi chetan,

Refer below link and provided by me query both take reference and change query itself.

Use multiple aggregate columns in pivot query in SQL Server

 


Posted 3 months ago

Hi chetan,

Refer below query.

SQL

CREATE TABLE #tblVisa(VisaModeId INT, VisaValidity Varchar(max),StayValidity Varchar(max), ProcessTime VARCHAR(max))

INSERT INTO #tblVisa VALUES(1,'60 days from the date issue','14 days from date of entry','14 days from date of entry')
INSERT INTO #tblVisa VALUES(2,'30 days from the date issue','30 days from date of entry','14 days from date of entry')
SELECT * FROM
(
	SELECT VisaModeId,[VisaValidity] FROM #tblVisa
) as tbl
PIVOT(MAX(VisaValidity) FOR [VisaModeId] IN ([1],[2])) as PVT
DROP TABLE #tblVisa