Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Convert years


Convert years

Author
Message
hugo_17_23 19942
hugo_17_23 19942
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 11
Hi,
Can Anyone help me with this, please

I have a Table like this

Es Year January February March
E0003 2008 XXXXX XXXXX XXXXX
E0003 2009 XXXXY XXXXY XXXXY
E0004 2008 XXXYY XXXYY XXXYY
E0004 2009 ZZYYY ZZYYY ZZYYY

And I need to convert that table into this:

Es Jan_08 Jan_09 Feb_08 Feb_09 Mar_08 Mar_09
E0003 XXXXX XXXXY XXXXX XXXXY XXXXX XXXXY
E0004 XXXYY ZZYYY XXXYY ZZYYY XXXYY ZZYYY
Ray M
Ray M
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1491 Visits: 1076
What have you tried, and where are you getting stuck?

Please read this
http://www.sqlservercentral.com/articles/Best+Practices/61537/

Look up Case statement, and
group by Es
Or the Pivot Functions
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45119 Visits: 39918
hugo_17_23 19942 (10/18/2012)
Hi,
Can Anyone help me with this, please

I have a Table like this

Es Year January February March
E0003 2008 XXXXX XXXXX XXXXX
E0003 2009 XXXXY XXXXY XXXXY
E0004 2008 XXXYY XXXYY XXXYY
E0004 2009 ZZYYY ZZYYY ZZYYY

And I need to convert that table into this:

Es Jan_08 Jan_09 Feb_08 Feb_09 Mar_08 Mar_09
E0003 XXXXX XXXXY XXXXX XXXXY XXXXX XXXXY
E0004 XXXYY ZZYYY XXXYY ZZYYY XXXYY ZZYYY




You will need to "unpivot" or "uncrosstab" the data so that it's in a vertical format and then "repivot" or "recrosstab" to the desired layout.

For coded help, please see the first link in my signature line below.

As a sidebar, the original data is stored as if it were a spreadsheet, which is a denormalized form and causes a lot of the problems like the very problem you're currently having. My recommendation is that once you have "unpivoted" the data, you keep it that way.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8546 Visits: 18134
I'm sure this article will help you.
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8546 Visits: 18134
Jeff Moden (10/18/2012)

You will need to "unpivot" or "uncrosstab" the data so that it's in a vertical format and then "repivot" or "recrosstab" to the desired layout.

For coded help, please see the first link in my signature line below.

As a sidebar, the original data is stored as if it were a spreadsheet, which is a denormalized form and causes a lot of the problems like the very problem you're currently having. My recommendation is that once you have "unpivoted" the data, you keep it that way.


For this query, I'm sure he doesn't need to "unpivot" the data.
However, I support your recommendation on keep the data normalized.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45119 Visits: 39918
Luis Cazares (10/18/2012)
Jeff Moden (10/18/2012)

You will need to "unpivot" or "uncrosstab" the data so that it's in a vertical format and then "repivot" or "recrosstab" to the desired layout.

For coded help, please see the first link in my signature line below.

As a sidebar, the original data is stored as if it were a spreadsheet, which is a denormalized form and causes a lot of the problems like the very problem you're currently having. My recommendation is that once you have "unpivoted" the data, you keep it that way.


For this query, I'm sure he doesn't need to "unpivot" the data.
However, I support your recommendation on keep the data normalized.

For hard coded dates, I agree. But how often are hard coded dates going to be safe on the 1st of the next year?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search