Solved

pivot syntax

Posted on 2010-08-23
10
322 Views
Last Modified: 2012-05-10
can anyone help me with this syntax...i get this error at line 5

Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ','.
select *
from ttg_monthly_revenue_vw
pivot
(
	sum([sales]),
	sum([cogs]),
	sum([trans billing amt]) 
	for [month] in (
					[JAN],[FEB],[MAR],[APR],[MAY],[JUN],[JUL],[AUG],[SEP],[OCT],[NOV],[DEC]
					) PVTTBL

Open in new window

0
Comment
Question by:k1ng87
  • 5
  • 4
10 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 33504550
something like this?

select 	max([JAN]) as [Jan],

	max([JAN_cogs]) as [Jan_cogs],

	max([JAN_tba]) as [JAN_tba],

	... and so on...

from (

	select 	sales, 

		[cogs], 

		[trans billing amt], 

		[month], 

		[month] + '_cogs' as [month_cogs], 

		[month] + '_tba' as [month_tba]

	from ttg_monthly_revenue_vw

) o

pivot(sum([sales]) for [month] in ([JAN],[FEB],[MAR],[APR],[MAY],[JUN],[JUL],[AUG],[SEP],[OCT],[NOV],[DEC]) PVTTBL

pivot(sum([cogs]) for [month_cogs] in ([JAN_cogs],[FEB_cogs],[MAR_cogs],[APR_cogs],[MAY_cogs],[JUN_cogs],[JUL_cogs],[AUG_cogs],[SEP_cogs],[OCT_cogs],[NOV_cogs],[DEC_cogs]) PVTTBL1

pivot(sum([trans billing amt]) for [month_tba] in ([JAN_tba],[FEB_tba],[MAR_tba],[APR_tba],[MAY_tba],[JUN_tba],[JUL_tba],[AUG_tba],[SEP_tba],[OCT_tba],[NOV_tba],[DEC_tba]) PVTTBL2

Open in new window

0
 

Expert Comment

by:GaneshSP
ID: 33529680
Can you tell me the table structure,values and what is the data you want?
0
 
LVL 1

Author Comment

by:k1ng87
ID: 33532543
My table structure is the following:

 [TRANS].[dbo].[ttg_ssrs_reporting_metrics_vw]
           ([segment]
           ,[group]
           ,[division]
           ,[division_city]
           ,[inv_year_id]
           ,[inv_month_id]
           ,[TransCost]
           ,[Sales]
           ,[COGS]
           ,[trans billing amt])
 
I would like to pivot the table to the following:

Segment | group | Division | division_city | Inv_year_ID | Entry_Type | Jan | Feb | Mar | Apr | May | etc...
--------------------------------------------------------------------------------------------------------------------------------------------------------
                                                                                            transcost
                                                                                            COGS
                                                                                            transbillingAmt
                                                                                            Sales
0
 
LVL 41

Expert Comment

by:ralmada
ID: 33533144
In that case you first need to unpivot and then pivot
See the below:

select 	[segment]

	,[group]

	,[division]

        ,[division_city]

	,[inv_year_id]

	,Entry_type

	,[JAN],[FEB],[MAR],[APR],[MAY],[JUN],[JUL],[AUG],[SEP],[OCT],[NOV],[DEC]

from 					

(

	select      a.[segment]

        	   ,a.[group]

	           ,a.[division]

        	   ,a.[division_city]

	           ,a.[inv_year_id]

        	   ,a.[inv_month_id]

		   ,up.Entry_type

        	   ,up.Amount

	from ttg_ssrs_reporting_metrics_vw a

	unpivot (amount for Entry_type in (Transcost, Sales, COGCS, [trans billing amt])) up

) o

pivot (sum(Amount) for inv_month_id in ([JAN],[FEB],[MAR],[APR],[MAY],[JUN],[JUL],[AUG],[SEP],[OCT],[NOV],[DEC])) p

Open in new window

0
 
LVL 41

Expert Comment

by:ralmada
ID: 33533243
same query, a bit more tidy:

select	[segment]

	,[group]

	,[division]

	,[division_city]

	,[inv_year_id]

	,Entry_type

	,[JAN],[FEB],[MAR],[APR],[MAY],[JUN],[JUL],[AUG],[SEP],[OCT],[NOV],[DEC]

from 					

(

	select	a.[segment]

		,a.[group]

		,a.[division]

		,a.[division_city]

		,a.[inv_year_id]

		,a.[inv_month_id]

		,up.Entry_type

		,up.Amount

	from ttg_ssrs_reporting_metrics_vw a

	unpivot (amount for Entry_type in (Transcost, Sales, COGCS, [trans billing amt])) up

) o

pivot (sum(Amount) for inv_month_id in ([JAN],[FEB],[MAR],[APR],[MAY],[JUN],[JUL],[AUG],[SEP],[OCT],[NOV],[DEC])) p

Open in new window

0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 1

Author Comment

by:k1ng87
ID: 33533786
getting this error:

Msg 8167, Level 16, State 1, Line 1
The type of column "Sales" conflicts with the type of other columns specified in the UNPIVOT list.
Msg 207, Level 16, State 1, Line 19
Invalid column name 'COGCS'.
Msg 4104, Level 16, State 1, Line 19
The multi-part identifier "a.segment" could not be bound.
Msg 4104, Level 16, State 1, Line 19
The multi-part identifier "a.group" could not be bound.
Msg 4104, Level 16, State 1, Line 19
The multi-part identifier "a.division" could not be bound.
Msg 4104, Level 16, State 1, Line 19
The multi-part identifier "a.division_city" could not be bound.
Msg 4104, Level 16, State 1, Line 19
The multi-part identifier "a.inv_year_id" could not be bound.
Msg 4104, Level 16, State 1, Line 19
The multi-part identifier "a.inv_month_id" could not be bound.
0
 
LVL 41

Expert Comment

by:ralmada
ID: 33534137
ok, check this now:
select	[segment]

	,[group]

	,[division]

	,[division_city]

	,[inv_year_id]

	,Entry_type

	,[JAN],[FEB],[MAR],[APR],[MAY],[JUN],[JUL],[AUG],[SEP],[OCT],[NOV],[DEC]

from 					

(

	select	[segment]

		,[group]

		,[division]

		,[division_city]

		,[inv_year_id]

		,[inv_month_id]

		,up.Entry_type

		,up.Amount

	from (

		select 	segment, group, division, division_city,inv_year_id, inv_month_id,

			convert(sql_variant, Transcost) as Transcost,

			convert(sql_variant, Sales) as Sales,

			convert(sql_variant, COGCS) as COGCs,

			convert(sql_variant, [trans billing amt]) as [trans billing amt]

		from ttg_ssrs_reporting_metrics_vw

	) a

	unpivot (amount for Entry_type in (Transcost, Sales, COGCS, [trans billing amt])) up

) o

pivot (sum(Amount) for inv_month_id in ([JAN],[FEB],[MAR],[APR],[MAY],[JUN],[JUL],[AUG],[SEP],[OCT],[NOV],[DEC])) p

Open in new window

0
 
LVL 1

Author Comment

by:k1ng87
ID: 33534215
now getting this error...we're getting closer!! hahaha


Msg 8117, Level 16, State 1, Line 1
Operand data type sql_variant is invalid for sum operator.
0
 
LVL 1

Author Comment

by:k1ng87
ID: 33534228
if I remove the sql_variant conversion in line 20-23 I get this error then:


Msg 8167, Level 16, State 1, Line 1
The type of column "Sales" conflicts with the type of other columns specified in the UNPIVOT list.
0
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 33534798
what is the datatype of the Transcost, Sales, COGCS and trans billing amt column? can you just make them all the same datatype?
The below will make them all decimal 10, 2.
 
 

select	[segment]

	,[group]

	,[division]

	,[division_city]

	,[inv_year_id]

	,Entry_type

	,[JAN],[FEB],[MAR],[APR],[MAY],[JUN],[JUL],[AUG],[SEP],[OCT],[NOV],[DEC]

from 					

(

	select	[segment]

		,[group]

		,[division]

		,[division_city]

		,[inv_year_id]

		,[inv_month_id]

		,up.Entry_type

		,up.Amount

	from (

		select 	segment, group, division, division_city,inv_year_id, inv_month_id,

			convert(decimal(10,2), Transcost) as Transcost,

			convert(decimal(10,2), Sales) as Sales,

			convert(decimal(10,2), COGCS) as COGCs,

			convert(decimal(10,2), [trans billing amt]) as [trans billing amt]

		from ttg_ssrs_reporting_metrics_vw

	) a

	unpivot (amount for Entry_type in (Transcost, Sales, COGCS, [trans billing amt])) up

) o

pivot (sum(Amount) for inv_month_id in ([JAN],[FEB],[MAR],[APR],[MAY],[JUN],[JUL],[AUG],[SEP],[OCT],[NOV],[DEC])) p

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
encyps queries mssql 15 39
Create A query for disabled users 5 30
Need help subtracting a value within my script 7 42
Query Peformance + mulitple query plans 9 48
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now