July 25, 2016 at 7:58 am
I need to change the column headings to start with a letter what would be the best way to do this.The heading are 2013,2014,2015,2016 I would like for them to start with a letter. x2013,x2014,x2015,x2016 something like that.
select
*
from
(SELECT
datepart(WK,ompOrderDate) as week_
,ompOrderSubtotalBase
,DATEPART(yyyy,ompOrderDate) as year_
FROM m1_kf.dbo.SalesOrders
WHERE ompClosed = - 1 and ompCustomerOrganizationID is not null
and
(
ompOrderDate >= '01-01-2016'
AND ompOrderDate <= '12-31-2016'
)
OR
(
ompOrderDate >= convert(char(11),DATEADD(yy, -1, '01-01-2016'),101)
AND ompOrderDate <= convert(char(11),DATEADD(yy, -1,'12-31-2016'),101)
)
OR (
ompOrderDate >= convert(char(11),DATEADD(yy, -2, '01-01-2016'),101)
AND ompOrderDate <= convert(char(11),DATEADD(yy, -2, '12-31-2016'),101)
)
OR (
ompOrderDate >= convert(char(11),DATEADD(yy, -3, '01-01-2016'),101)
AND ompOrderDate <= convert(char(11),DATEADD(yy, -3, '12-31-2016'),101)
)
) as src
pivot
(
sum(ompOrderSubtotalBase)
for year_ in ([2013], [2014], [2015] , [2016])
) piv
order by
week_
July 25, 2016 at 8:05 am
Yes, that's why we have column alias. Remove the star(*) and explicitly list the columns and add alias as needed.
July 25, 2016 at 8:17 am
I tried this but it did not work. the column does change to x2013 but the data rows for that column become 2013 etc
select
week_,2013 as x2013,2014 as x2014, 2015 as x2015, 2016 as x2016
from
(SELECT
datepart(WK,ompOrderDate) as week_
,ompOrderSubtotalBase
,DATEPART(yyyy,ompOrderDate) as year_
FROM m1_kf.dbo.SalesOrders
WHERE ompClosed = - 1 and ompCustomerOrganizationID is not null
and
(
ompOrderDate >= '01-01-2016'
AND ompOrderDate <= '12-31-2016'
)
OR
(
ompOrderDate >= convert(char(11),DATEADD(yy, -1, '01-01-2016'),101)
AND ompOrderDate <= convert(char(11),DATEADD(yy, -1,'12-31-2016'),101)
)
OR (
ompOrderDate >= convert(char(11),DATEADD(yy, -2, '01-01-2016'),101)
AND ompOrderDate <= convert(char(11),DATEADD(yy, -2, '12-31-2016'),101)
)
OR (
ompOrderDate >= convert(char(11),DATEADD(yy, -3, '01-01-2016'),101)
AND ompOrderDate <= convert(char(11),DATEADD(yy, -3, '12-31-2016'),101)
)
) as src
pivot
(
sum(ompOrderSubtotalBase)
for year_ in ([2013], [2014], [2015] , [2016])
) piv
order by
wee
July 25, 2016 at 8:22 am
kat35601 (7/25/2016)
I tried this but it did not work. the column does change to x2013 but the data rows for that column become 2013 etc
select
week_,2013 as x2013,2014 as x2014, 2015 as x2015, 2016 as x2016
from
(SELECT
datepart(WK,ompOrderDate) as week_
,ompOrderSubtotalBase
,DATEPART(yyyy,ompOrderDate) as year_
FROM m1_kf.dbo.SalesOrders
WHERE ompClosed = - 1 and ompCustomerOrganizationID is not null
and
(
ompOrderDate >= '01-01-2016'
AND ompOrderDate <= '12-31-2016'
)
OR
(
ompOrderDate >= convert(char(11),DATEADD(yy, -1, '01-01-2016'),101)
AND ompOrderDate <= convert(char(11),DATEADD(yy, -1,'12-31-2016'),101)
)
OR (
ompOrderDate >= convert(char(11),DATEADD(yy, -2, '01-01-2016'),101)
AND ompOrderDate <= convert(char(11),DATEADD(yy, -2, '12-31-2016'),101)
)
OR (
ompOrderDate >= convert(char(11),DATEADD(yy, -3, '01-01-2016'),101)
AND ompOrderDate <= convert(char(11),DATEADD(yy, -3, '12-31-2016'),101)
)
) as src
pivot
(
sum(ompOrderSubtotalBase)
for year_ in ([2013], [2014], [2015] , [2016])
) piv
order by
wee
try enclosing the column names up top in brackets too? like [2013], [2014] etc?
July 25, 2016 at 8:29 am
[Brackets] worked Thanks
July 25, 2016 at 12:08 pm
awesome!
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply