Pivot table not working properly - sql

I have following query:
select * from dbPratiche
pivot
(
count(Compagnia)
for
(convert(char(3), [Data creazione pratica], 0))
in ([jan],[feb],[mar],[apr],[may],[jun],[jul],[aug],[sep],[October],[nov],[dec])
) pvt
I want to pivot my table on monthname from one [Data creazione pratica] column having date values in it.
But I'm getting an error:
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near '('.
I checked all brackets, all brackets are correct.
Please tell me where I am making a mistake in this query

I solved it through:
select * from (
select convert(char(3), [Data creazione pratica], 0) as monthOF ,[Compagnia]
from dbPratiche where ISNULL([Data creazione pratica],'')!='')as temp
pivot
(
count(Compagnia)
for monthOF in
(jan,feb,mar,apr,may,jun,jul,aug,sep,Oct,nov,dec)
) pvt

Related

Creating a temp table and using aggregate select query

I am trying to create a temp table from a sub select query and am receiving an error message
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ';'
Here is my query:
select *
into #temp
from
(select dollars, PostingDate, EntryDescription
from MillsEntOps.dbo.OE_InvoiceGLSumm
where Postingdate between '2018/03/01' and '2018/04/11')
The error message is :
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ')'.
Any ideas what I am doing wrong?
Alternatively you can write:
select dollars, PostingDate, EntryDescription
into #TEMP
from MillsEntOps.dbo.OE_InvoiceGLSumm
where Postingdate between '2018/03/01' and '2018/04/11'
I think you missed alias name
select * into #temp from ( select dollars, PostingDate, EntryDescription from MillsEntOps.dbo.OE_InvoiceGLSumm where Postingdate between '2018/03/01' and '2018/04/11')temp_table /* alias name temp_table */

How to pivot ntext datatype column in sql server

Can you please help with the below query as I have to convert the row into the column.
select question,[1],[2],[3],[4] from
(
select
qm.question, cm.choice,
cast (row_number() over (partition by question order by question) as nvarchar(max)) as tbl
from
questionm qm, choicem cm
where
qm.questionID = 1 and cm.questionid = 1) CTE
PIVOT
(
max(cast((choice) as nvarchar(max)))
for tbl in ([1],[2],[3],[4])
) tmp
The datatype of question and choice is ntext and after execution the script is throwing error :
Msg 488, Level 16, State 1, Line 16
Pivot grouping columns must be comparable. The type of column "question" is "ntext", which is not comparable.

Error converting data type nvarchar to int in multiple Pivot on same column SQL

I am trying to use multiple pivot in a single query on same column.
Since i am using same column "month" for all the pivot's, i have added column header to the select list and have added different alias name. Month column has int datatype value. Below is the code:
Select * FROM
(
SELECT
[team],
Count_O,
Count_Of_OA,
Avg,
[month]+ '_a' as month_a,
[month] + '_b' as month_b,
[users]
FROM [#Temp]
) AS X
PIVOT
(
MAX(Count_OA)
FOR [month_a] IN ([4_a], [5_a], [6_b])
) AS PivotTable1
PIVOT
(
MAX(Count_O)
FOR [month_b] IN ([4_b], [5_b], [6_b])
) AS PivotTable2
When i execute this, I get the below error:
Msg 8114, Level 16, State 1, Line 44
Error converting data type nvarchar to int.
Any inputs would be much appreciated.
Try this
Select * FROM
(
SELECT
[team],
Count_O,
Count_Of_OA,
Avg,
Convert(varchar(max),[month])+ '_a' as month_a,
Convert(varchar(max),[month]) + '_b' as month_b,
[users]
FROM [#Temp]
) AS X
PIVOT
(
MAX(Count_OA)
FOR [month_a] IN ([4_a], [5_a], [6_b])
) AS PivotTable1
PIVOT
(
MAX(Count_O)
FOR [month_b] IN ([4_b], [5_b], [6_b])
) AS PivotTable2

T-SQL pivot formatting unexpected identifier when filtering column based on string value

Can someone explain what is wrong with this syntax for creating a pivot in SQL Server Management Studio? I tried adding the identifier ([Name].'email') with no success.
Error message when executed:
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'email'.
Select ID
,email as EMail
,phone as Phone
From (
....
) aaa
pivot (max([Data]) for [Name] in ([email],[phone]) ) pvt
If your system is not case sensitive, you can get away with
Select *
From (
....
) aaa
pivot (max([Data]) for [Name] in ([Email],[Phone]) ) pvt

Struggling with SQL Pivot Query Syntax

Anyone tell me whats wrong with my SQL, having a hard time with this today. The error is:
Msg 156, Level 15, State 1, Line 11
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 11
Incorrect syntax near ')'.
SELECT *
FROM (
SELECT
left(datename(month,TransactionDateTime),3) as [month], year(TransactionDateTime) as [year],
count(*) as Total
FROM quotations
) as s
PIVOT
(
SUM(Total)
FOR [year] IN (select distinct year(TransactionDateTime) from quotations)
) AS pivot
The shape I am after is... So years as column names then 12 rows for each month. Below is just to illustrate the shape
// var data = google.visualization.arrayToDataTable([
// ['Month', '2013', '2014', '2015'],
// ['Jan', 10, 30, 31],
// ['Feb', 11, 30, 32],
//]);
The syntax for pivot is as follows (From TechNet):
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>
You can see that inside IN clause there should be column names enclosed in [] which can be a problem if the column names are dynamic. This problem can be easily solved by using dynamic SQL.
Here is an example:
DECLARE #cols AS NVARCHAR(MAX)
DECLARE #query AS NVARCHAR(MAX)
SET #cols = STUFF((SELECT distinct ',' + QUOTENAME(year(TransactionDateTime))
FROM Quotations
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
SET #query =
'SELECT *
FROM (
SELECT
left(datename(month,TransactionDateTime),3) as [month], year(TransactionDateTime) as [year],
count(*) as Total
FROM quotations
) as s
PIVOT
(
SUM(Total)
FOR [year] IN (' + #cols + ')
) AS pivot'
EXECUTE(#query)
I don't know what your schema is so there may be a mistake with table/column names. If this doesn't help I can be more precise if you share your schema and a sample data.
The problem is with the following part of your query
(select distinct year(TransactionDateTime) from quotations)
You need to provide a static list of years inside the in clause.