Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Translate rows in columns Expand / Collapse
Author
Message
Posted Monday, October 8, 2012 12:35 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 2, 2013 9:29 PM
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



  Post Attachments 
TitleDetailsRequest.JPG (22 views, 20.71 KB)
TitleDetailsRequest_Output.JPG (23 views, 18.91 KB)
Post #1369996
Posted Monday, October 8, 2012 2:19 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:46 PM
Points: 13,436, Visits: 12,300
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)
Post #1370037
Posted Sunday, January 6, 2013 12:49 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 2, 2013 9:29 PM
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.
Post #1403311
Posted Sunday, January 6, 2013 11:30 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:07 PM
Points: 37,075, Visits: 31,632
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1403419
Posted Monday, January 7, 2013 7:46 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:46 PM
Points: 13,436, Visits: 12,300
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)
Post #1403614
Posted Monday, January 7, 2013 7:46 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:46 PM
Points: 13,436, Visits: 12,300
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)
Post #1403615
Posted Thursday, January 24, 2013 2:01 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, September 3, 2014 11:44 AM
Points: 1,127, Visits: 1,592
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
Post #1410990
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse