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


Translate rows in columns


Translate rows in columns

Author
Message
tudorn
tudorn
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 22

Hello,

I have the following TitleDetailsRequest table: see TitleDetailsRequest.jpg attachment.

I need to get an output of the EN_No and FR_No for all the Stores and for all the titles Title based on the latest RequestDate as follow: see TitleDetailsRequest_Output.jpg attachment.

The Title table have all the titles that need to be in the output, so if a title is not in the TitleDetailsRequest table (i.e. Title3) the output should just display "0". Currently there are 30 titles in the Title table but the users can add new titles so hardcoding the titles into columns will not be an option considering that the users can also add new titles.

Your help with regards to this matter will be greatly appreciated.

Thanks


Attachments
TitleDetailsRequest.JPG (22 views, 20.00 KB)
TitleDetailsRequest_Output.JPG (23 views, 18.00 KB)
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16652 Visits: 17029
Hi and welcome to SSC. What you have posted as desired output looks more like a report then the results of query. You have column group headers and column names. This type of thing is called a cross tab. It is generally more efficient (and far easier) to do this in the front end. This is very challenging to do in sql especially given the dynamic nature of what you are after. It can be done but it will take some considerable effort on your part.

There are two links in my signature about cross tabs. They will explain in great detail how to do this sort of thing.

If you are simply stumped after trying the methods laid out in there you should read the first link in my signature about best practices when posting questions. At that point you will need to post ddl (create table scripts), sample data (insert statements) and desired output based on your sample data.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
tudorn
tudorn
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 22
Hi Sean,
Sorry I did not replied to you earlier.
The two posts/links that you recommended were very good and helped me a lot to create the reports I needed.
Thank you very much.
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: 45371 Visits: 39936
I have to say that's one of the longer belated thank-you's I've seen in a while and I still think it's very cool that you came back to do so. Well done. Proof positive the later really is usually better than never.

--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
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16652 Visits: 17029
tudorn (1/6/2013)
Hi Sean,
Sorry I did not replied to you earlier.
The two posts/links that you recommended were very good and helped me a lot to create the reports I needed.
Thank you very much.


Glad you were able to get it fixed.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16652 Visits: 17029
Jeff Moden (1/6/2013)
I have to say that's one of the longer belated thank-you's I've seen in a while and I still think it's very cool that you came back to do so. Well done. Proof positive the later really is usually better than never.


+1

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
vinu512
vinu512
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1155 Visits: 1618
tudorn (1/6/2013)
Hi Sean,
Sorry I did not replied to you earlier.
The two posts/links that you recommended were very good and helped me a lot to create the reports I needed.
Thank you very much.


Hi tudorn,

Did you get it done exactly as it shows in the image that you uploaded???.....would like to see the code....would be of help to others as well...could you please post it?

Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden ;-)
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