Need help on Pivot in SQL Server - sql

Need help on pivot.. My i/p and o/p as below(pls see the attached image)..Could u pls help me on the query...

Here created sample data for getting result
IF OBJECT_ID('Tempdb..#Temp') IS NOT NULL
DROP TABLE #Temp
;WIth cte(EmployeeID, Activity_Date,Activity_Type)
AS
(
SELECT 1,'6/20/2016' ,'Mail_send' UNION ALL
SELECT 2,'6/22/2011' ,'Mail_Received' UNION ALL
SELECT 1,'10/11/2016','Mail_Replied' UNION ALL
SELECT 3,'10/31/2016','Mail_deleted' UNION ALL
SELECT 2,'2/11/2016' ,'Mail_Forwared'
)
SELECT * INTO #Temp FROM cte
We can get the result by using dynamic Sql with Pivot
DECLARE #dynamicCol nvarchar(max),
#Sql nvarchar(max)
SELECT #dynamicCol=STUFF((SELECT DISTINCT ', ' +QUOTENAME(Activity_Type) FROM #Temp
FOR XML PATH('')),1,1,'')
SET #Sql='
SELECT [EmployeeID] , '+ #dynamicCol +' From
(
SELECT * From
#temp
)AS Src
PIVOT
(
MAX([Activity_Date]) For [Activity_Type] IN ('+#dynamicCol+')
)
AS Pvt
'
PRINT #Sql
EXEC(#Sql)
Result
EmployeeID Mail_deleted Mail_Forwared Mail_Received Mail_Replied Mail_send
--------------------------------------------------------------------------------------
1 NULL NULL NULL 10/11/2016 6/20/2016
2 NULL 2/11/2016 6/22/2011 NULL NULL
3 10/31/2016 NULL NULL NULL NULL

Try the following Query, this should serve your purpose:
select * from
(select [Employee Id],Activity_Date,Activity_Type from Employee_Activity) tbl
pivot
(
max(Activity_Date) for Activity_Type in (Mail_Send,Mail_Received,Mail_Replied,Mail_deleted,Mail_Forwarded)
)as pvt

Related

How to write vertical columns [duplicate]

This question already has answers here:
Understanding PIVOT function in T-SQL
(7 answers)
Closed 3 years ago.
How can I write a vertical table horizontally with sql
  I want to make the following table like the example in the second picture
example
ı want to write
Try this
DECLARE #Sql nvarchar(max),
#DynamicColumn nvarchar(max),
#MaxDynamicColumn nvarchar(max)
SELECT #DynamicColumn = STUFF((SELECT DISTINCT', '+QUOTENAME(CAST(Col1 AS VARCHAR(50)))
FROM #Temp FOR XML PATH ('')),1,1,'')
SELECT #DynamicColumn
SET #Sql='SELECT '+ #DynamicColumn+'
FROM
(
SELECT *
FROM #Temp o
)AS src
PIVOT
(
MAX(Col2) FOR [Col1] IN ('+#DynamicColumn+')
) AS Pvt
'
EXEC (#Sql)
PRINT #Sql
Hi if understand your query i think this query can help you to get excepted result :
CREATE TABLE #TEMP (colName varchar(250), colOther varchar(250))
INSERT INTO #TEMP
SELECT 'Desen', '2908A' UNION ALL
SELECT 'Desen', '2908A' UNION ALL
SELECT 'Desen', '2908A' UNION ALL
SELECT 'Desen', '2908A' UNION ALL
SELECT 'Ebat', '125x200 R' UNION ALL
SELECT 'Ebat', '125x200 R' UNION ALL
SELECT 'Ebat', '125x200 R' UNION ALL
SELECT 'Ebat', '125x200 R' UNION ALL
SELECT 'ZeminRengi', 'KEMIK' UNION ALL
SELECT 'ZeminRengi', 'KEMIK' UNION ALL
SELECT 'ZeminRengi', 'KEMIK' UNION ALL
SELECT 'ZeminRengi', 'KEMIK'
select Desen,Ebat,ZeminRengi
from #TEMP
PIVOT (
MAX(colOther)
FOR colName IN (Desen,Ebat,ZeminRengi)) AS Pvt
DROP TABLE #TEMP
See different link and google :
Understanding PIVOT function in T-SQL
MSDN
Google- search

MsSql "group by" result table design

How to convert Result 1 to result 2. I can not get result2 using the pivot table.
Result1
Result1 QUERY
SELECT Country,City,Count(*) as "Count"
FROM Customers
GROUP BY Country,City
Result2
There is no such feature of SQL other that PIVOT where, as you have discovered, you need to know the values up front.
This formatting is typically done in the output part of the system, such as your report engine. SSRS for example can handle this nicely & out of the box.
You would need dynamic SQL to achieve this lower down in the stack.
Try this dynamic sql
IF OBJECT_ID('tempdb..#Temp') IS NOT NULL
DROP TABLE #Temp
CREATE TABLE #Temp([Country] VARCHAR(20),[City] VARCHAR(20),[Count] INT)
INSERT INTO #Temp
SELECT 'Germany' ,'Aachen' ,1 UNION ALL
SELECT 'USA' ,'Albuquerque',1 UNION ALL
SELECT 'USA' ,'Anchorage' ,1 UNION ALL
SELECT 'Denmark' ,'Arhus' ,1 UNION ALL
SELECT 'Spain' ,'Barcelone' ,1
DECLARE #Colmn nvarchar(max),
#Sql nvarchar(max)
SELECT #Colmn=STUFF((SELECT distinct ', '+QUOTENAME([Country]) FROM #Temp
FOR XML PATH ('')),1,1,'')
SET #Sql =' SELECT Cityynames ,'+ #Colmn +' FROM
(
SELECT *,Country AS Countrynames,City AS Cityynames
FROM #Temp
)dt
PIVOT
(
MAX([Count]) FOR [Country] IN ('+#Colmn+')
) AS Pvt'
PRINT #Sql
EXEC (#Sql)

Getting Unique values from a row - SQL Server

I have columns something like this:
col1 | col2 | col3 | col4 | col5 | col6 |
-----+------+------+------+------+------+
a | b | c | a | c | c
I am trying to get unique values in the column it self PER row.
So ideally, I want a,b,c to be returned
I tried doing PIVOT and applying a DISTINCT but that doesn't go well as there are other columns that I couldn't show in the question.
So is there another way that this could be obtained?
Thanks in advance
Is this what you are looking for ..?
IF OBJECT_ID('tempdb..#Pivot_data')IS NOT NULL
DROP TABLE #Pivot_data
IF OBJECT_ID('tempdb..#tab')IS NOT NULL
DROP TABLE #tab
select * into #tab from
(select 'a'AS COL1,'b'AS COL2,'c'AS COL3,'a'AS COL4,'c'AS COL5,'c' COL6)AS A
DECLARE #Columns nvarchar(max) ,#QUERY NVARCHAR(MAX)
;WITH CTE AS (
SELECT COLUMNSS,COL_VALUES,ROW_NUMBER()OVER(PARTITION BY COL_VALUES ORDER BY (SELECT 1))RN FROM (
SELECT * FROM #tab
)AS A
UNPIVOT(COL_VALUES FOR COLUMNSS IN([col1],[col2],[col3],[col4],[col5],[col6])) AS B
)
,FINAL_Result as (select COLUMNSS,COL_VALUES from CTE where RN=1)
SELECT * INTO #Pivot_data FROM FINAL_Result
SET #Columns= (SELECT STUFF((SELECT ',['+COLUMNSS+']' FROM #Pivot_data FOR XML PATH('')),1,1,''))
SET #QUERY=N'SELECT * FROM (
SELECT * FROM #Pivot_data
) AS A
PIVOT (MAX(COL_VALUES)FOR COLUMNSS IN('+#Columns+'))
AS B'
PRINT #QUERY
EXEC (#QUERY)
Logic :
From the given table i did unpivot and generated a Row_number() based on the column values and considered only which are Row_num=1 i.e Distinct columns values . and finally, i Pivoted the resultant data .
It seems to be CROSS APPLY would be work here
select a.Col from table t
cross apply (
values (t.Col1), (t.Col2), (t.Col3),
(t.Col4), (t.Col5), (t.Col6)
)a(Col)
group by a.Col
AND, do the PIVOT with your own way
Try this code
IF OBJECT_ID('tempdb..#Temptab')IS NOT NULL
DROP TABLE #Temptab
;With cte(col1 , col2 , col3 , col4 , col5 , col6 )
AS
(
SELECT 'a' , 'b' , 'c' , 'a' , 'c' , 'c'
)
SELECT DISTINCT AllColumn
INTO #Temptab FROM cte
CROSS APPLY ( VALUES (col1),( col2) , (col3) , (col4) , (col5) , (col6)
) AS A (AllColumn)
DECLARE #SqlQuery nvarchar(max)
,#Sql nvarchar(max)
SELECT #SqlQuery='SELECT DISTINCT '+STUFF((SELECT ', '+ReqColumn FROM
(
SELECT ''''+AllColumn +'''' +' AS Col'+ CAST(Seq AS VARCHAR(2)) As ReqColumn
FROm
(
SELECT ROW_NUMBER()OVER(Order by AllColumn) AS Seq,AllColumn FROM #Temptab
)dt
)dte FOR XML PATH ('')),1,1,'') +' From #Temptab'
PRINT #SqlQuery
EXEC(#SqlQuery)
Result
Col1 Col2 Col3
----------------------
a b c
You can first union all columns to get unique values and then use the GROUP_CONCAT() function in MySql or if your database is Oracle then use list_agg() function:
Please try the below SQL:
select GROUP_CONCAT(col1 SEPARATOR ', ') from (
select '1' as 'serial', col1 from pivot
union
select '1' as 'serial',col2 from pivot
union
select '1' as 'serial',col3 from pivot
union
select '1' as 'serial',col4 from pivot
union
select '1' as 'serial',col5 from pivot
union
select '1' as 'serial' ,col6 from pivot
)derived
GROUP BY serial;
Output:
Note : I have make the 'serial' column in my query to do the group by. You can use the your identifier field in group by clause if you have any.

Can I pivot dynamic table with no group by in SQL Server

This is my data in table (left join from field table and value table)
This is my expected result table after use pivot function
Thanks for help ^___^
I suggest you to use dynamic SQL as fieldnames number may very in future:
DECLARE #columns nvarchar(max),
#sql nvarchar(max)
SELECT #columns = COALESCE(#columns,'') + ',' + QUOTENAME(c.fieldname)
FROM [Columns] c
ORDER BY c.cid
SELECT #sql = N'
SELECT *
FROM (
SELECT v.[row],
c.fieldname,
v.fieldvalue
FROM [Values] v
INNER JOIN [Columns] c
ON v.cid = c.cid
) t
PIVOT (
MAX(fieldvalue) FOR fieldname IN ('+STUFF(#columns,1,1,'')+')
) pvt'
EXEC sp_executesql #sql
Will output:
row FirstName LastName Email Phone
1 Arnun Saelim Arnun.s#outlook.com 0922743838
2 Micheal Saelim Micheal#gmail.com 0886195353
I'm not sure why group by got into your mind, but here is a working example of what you are trying to achieve.
select * into #temp from
( values
(1,'A','AV'),
(1,'B','BV'),
(1,'C','CV'),
(2,'A','AV'),
(2,'B','BV'),
(2,'C','CV'))
as t(row, FieldName, FieldValue)
select *
from #temp
PIVOT
(MAX(FieldValue) FOR FieldName in ([A],[B],[C])) as pvt
Does that work for you?

Pivot multiple columns in SQL Server 2008

I am trying to convert column to rows. I am beginner to PIVOT. Tried below code but error is coming as 'Error converting data type nvarchar to datetime.
The incorrect value "Tot_GPS_Cnt" is supplied in the PIVOT operator.'
CREATE TABLE #tbl_Res1
(
CallDate DATETIME,
Tot_GPS_Cnt INT,
Tot_GND_Cnt INT,
Per_Ratio NUMERIC(10,2)
)
INSERT INTO #tbl_Res1
SELECT '2015-04-24 00:00:00.000','40','26','65.00' UNION ALL
SELECT '2015-04-25 00:00:00.000','22','14','63.64' UNION ALL
SELECT'2015-04-26 00:00:00.000','27','21','77.78' UNION ALL
SELECT'2015-04-27 00:00:00.000','41','23','56.10'
Source Table
Desired Output
I have tried with below query bu failing. Please help. Thanks in advance
SELECT CallDate=col, Tot_GPS_Cnt, Tot_GND_Cnt, Per_Ratio
FROM
( select CallDate, col, value from #tbl_Res1
cross apply
(
SELECT 'Tot_GPS_Cnt',cast(Tot_GPS_Cnt as varchar(10)) UNION ALL
SELECT 'Tot_GND_Cnt', cast(Tot_GND_Cnt as varchar(10)) UNION ALL
SELECT 'Per_Ratio', cast(Per_Ratio as varchar(10))
) c (col,value)
) d
PIVOT
(
max(value) for CallDate in ([Tot_GPS_Cnt], [Tot_GND_Cnt], [Per_Ratio])
) as piv
The correct syntax for PIVOT is:
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>
Hence, you have to place date values not [Tot_GPS_Cnt], [Tot_GND_Cnt], [Per_Ratio] in place of pivoted columns:
SELECT CallDate=col, [2015-04-24], [2015-04-25], [2015-04-26], [2015-04-27]
FROM
( select CallDate, col, value from #tbl_Res1
cross apply
(
SELECT 'Tot_GPS_Cnt',cast(Tot_GPS_Cnt as varchar(10)) UNION ALL
SELECT 'Tot_GND_Cnt', cast(Tot_GND_Cnt as varchar(10)) UNION ALL
SELECT 'Per_Ratio', cast(Per_Ratio as varchar(10))
) c (col,value)
) d
PIVOT
(
max(value) for CallDate in ([2015-04-24], [2015-04-25], [2015-04-26], [2015-04-27])
) as piv
Thank you #Giorgos Betsos. I just made dynamic solution. Just wanted to share with all.
DECLARE #coldata VARCHAR(500);
DECLARE #sql VARCHAR(MAX);
SELECT #coldata = COALESCE(#coldata + '], [', '') + CONVERT(VARCHAR(10),calldate,110)
FROM #tbl_Res1;
SELECT #coldata = '[' + #coldata +']';
SELECT #coldata;
SET #sql = 'SELECT CallDate=col, ' + #coldata + '
FROM
( SELECT CallDate, col, value FROM #tbl_Res1
cross apply
(
SELECT ''Tot_GPS_Cnt'', CAST(Tot_GPS_Cnt as VARCHAR(10)) UNION ALL
SELECT ''Tot_GND_Cnt'', CAST(Tot_GND_Cnt as VARCHAR(10)) UNION ALL
SELECT ''Per_Ratio'', CAST(Per_Ratio as VARCHAR(10))
) c (col,value)
) d
PIVOT
(
MAX(value) FOR CallDate IN (' + #coldata + ')
) as piv'
SELECT #sql;
EXECUTE (#SQL);