k1ng87
asked on
pivot syntax
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 ','.
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
Can you tell me the table structure,values and what is the data you want?
ASKER
My table structure is the following:
[TRANS].[dbo].[ttg_ssrs_re porting_me trics_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
[TRANS].[dbo].[ttg_ssrs_re
([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
In that case you first need to unpivot and then pivot
See the below:
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
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
ASKER
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.
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.
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
ASKER
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.
Msg 8117, Level 16, State 1, Line 1
Operand data type sql_variant is invalid for sum operator.
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window