Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.



531 posts

Ultimate Geek

Trusted

Topic # 87906 10-Aug-2011 11:03 Send private message

hey trying to rename a header on the pivot,
The month is stored as int type, can i convert it to month name (jan, feb, march) across the top of the pivoted table instead of numbers?

how would i go about this? 

Example:
SELECT*

FROM Table1

PIVOT(

MAX(Score)

FOR[Month]IN([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]))

AS p
 
 Thanks!

Create new topic
2483 posts

Uber Geek

Trusted

  Reply # 505044 11-Aug-2011 15:33 Send private message

Is this homework? If not, I'll help you out.



531 posts

Ultimate Geek

Trusted

  Reply # 505057 11-Aug-2011 15:57 Send private message

it is homework :) figured it out now using 'AS'

thanks! 

2483 posts

Uber Geek

Trusted

  Reply # 505078 11-Aug-2011 16:22 Send private message

Why don't you post your solution then someone else can learn from you too! :)

2483 posts

Uber Geek

Trusted

  Reply # 505101 11-Aug-2011 17:06 Send private message

P.S. Not sure I really know what you mean by using 'AS'. I'd probably go with something like:

SELECT *
FROM (SELECT DateName(Month, DateAdd(Month, [Month], 0) - 1) as [MonthName], Score FROM Table_1) AS SourceTable
PIVOT (Max(Score) FOR [MonthName] IN ([January], [February], [March], [April], [May], [June], [July], [August], [September], [October], [November], [December])) AS PivotTable

but keen to see other approaches.



531 posts

Ultimate Geek

Trusted

  Reply # 505805 12-Aug-2011 22:39 Send private message

turns out i havnt got it sorted haha, your method didn't work above as i have it saved as a varchar not date type. 

The method i was trying was to use was
SELECT Month [1] As Jan, [2] as Feb etc

but it didn't work as there not columns till after being pivoted, so i get a non existent column error. 

215 posts

Master Geek


  Reply # 505821 12-Aug-2011 23:18 Send private message

As its homework I don't want to do it completely for you...plus you haven't said which DBMS this is for but...

cough derived tables cough common table expressions cough




531 posts

Ultimate Geek

Trusted

  Reply # 507031 15-Aug-2011 21:48 Send private message

i did it finally !!! yay!!

SELECT*

FROM (

SELECT

UserID,

caseMonth

      When 1 then 'Jan'

      WHEN 2 THEN 'Feb'

      WHEN 3 THEN 'March'

      WHEN 4 THEN 'April'

      WHEN 5 THEN 'May'

      WHEN 6 THEN 'June'

      WHEN 7 THEN 'July'

      WHEN 8 THEN 'Aug'

      WHEN 9 THEN 'Sept'

      WHEN 10 THEN 'Oct'

      WHEN 11 THEN 'Nov'

      WHEN 12 THEN 'Dec'

    END as [Month],

Score

From dbo.Table1

) temp

PIVOT(

MAX(Score)

FOR[Month]IN(

[Jan],[Feb],[March],[April],[May],[June],[July],[Aug],[Sept],[Oct],[Nov],[Dec]

)

)PivotTable
 

Create new topic



Twitter »
Follow us to receive Twitter updates when new discussions are posted in our forums:



Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:



Follow us to receive Twitter updates when new jobs are posted to our jobs board:



Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:




News »

Trending now »
Hot discussions in our forums right now:

Fecked up religious people strike again :-(
Created by Mark, last reply by bradstewart on 24-May-2013 17:31 (80 replies)
Pages... 4 5 6


Cannabis is illegal yet we have really strong 'legal highs' ?
Created by qwerty7, last reply by freitasm on 23-May-2013 23:20 (74 replies)
Pages... 3 4 5


Xbox One
Created by DjShadow, last reply by Kingy on 24-May-2013 13:48 (68 replies)
Pages... 3 4 5


A new project coming to Geekzone
Created by freitasm, last reply by DarthKermit on 24-May-2013 19:41 (341 replies)
Pages... 21 22 23


Troublesome transition to VDSL
Created by oseiler, last reply by michaelmurfy on 24-May-2013 13:57 (18 replies)
Pages... 2


HTC One (2013) owners' discussion
Created by Dingbatt, last reply by wlfkfgkwlaktka on 24-May-2013 15:49 (1564 replies)
Pages... 103 104 105


Orcon, Is this for real or a scam??
Created by old3eyes, last reply by DarthKermit on 22-May-2013 19:12 (29 replies)
Pages... 2


Vodafone Naked Broadband Speeds (Auckland CBD)
Created by wscalioni, last reply by grkiwi on 20-May-2013 21:13 (14 replies)


Geekzone Jobs »
Most recent NZ jobs in technology:

Organisational Change Analyst
Posted 24-May-2013 19:28

Dedicated Java Developer/ Technical lead
Posted 24-May-2013 18:28

Account Manager - IT/Telco
Posted 24-May-2013 18:28

Commercial Java Developer
Posted 24-May-2013 18:28

Senior DB2 Database Administrator
Posted 24-May-2013 18:28

Technical BA
Posted 24-May-2013 18:28

OSS Systems Engineer
Posted 24-May-2013 18:28


Geekzone Live »
Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.

Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.

Alternatively, you can receive a daily email with Geekzone updates.