October 8, 2012 at 12:35 pm
[font="Courier New"]
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
[/font]
October 8, 2012 at 2:19 pm
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
January 6, 2013 at 12:49 am
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.
January 6, 2013 at 11:30 pm
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
Change is inevitable... Change for the better is not.
January 7, 2013 at 7:46 am
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
January 7, 2013 at 7:46 am
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
January 24, 2013 at 2:01 am
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?
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply