November 26, 2011 at 4:29 pm
Hi all
I am trying to come up with a Script that lists the dates individually in each month in a column format.
Currently i have written a function that returns a table and returns the dates in the given month in rows. Can someone please suggest an idea for that to appear in a column.
E.g. All days in the month of November. Currently the function returns it as -
27/11/2011
28/11/2011
29/11/2011
30/11/2011
What i need to is return it as
27/11/2011 28/11/2011 29/11/2011 30/11/2011
Any help is appreciated
thanks
vani
November 26, 2011 at 7:11 pm
The only thing I can suggest is, have you looked at the use of the PIVOT statement? I would suggest reading:
November 27, 2011 at 2:46 am
I'd rather go with the classic CrossTab approach (see related links in my signature).
The code is not only easier to read and to expand (e.g. to include "nested" pivots), it's also supposed to be faster than the PIVOT solution.
For your scenario you might want to go one step further and use the DynamicCrossTab method with month and year as input parameter.
However, the most elegant solution would be using a frontend (e.g. Reporting Services) to pivot the data since it's usually a presentation requirement. Hence it should be done at the presentation layer. 😉
November 27, 2011 at 8:57 am
vani_r14 (11/26/2011)
Hi allI am trying to come up with a Script that lists the dates individually in each month in a column format.
Currently i have written a function that returns a table and returns the dates in the given month in rows. Can someone please suggest an idea for that to appear in a column.
E.g. All days in the month of November. Currently the function returns it as -
27/11/2011
28/11/2011
29/11/2011
30/11/2011
What i need to is return it as
27/11/2011 28/11/2011 29/11/2011 30/11/2011
Any help is appreciated
thanks
vani
First, to show what a "Pivot" and "Cross Tab" is and why some folks (especially me) thinl that "Cross Tab" is better for such things (including some performance testing), please see the following article.
http://www.sqlservercentral.com/articles/T-SQL/63681/
For how to do it dynamically so that you don't need to make code changes every month, please the the following "follow up" article.
http://www.sqlservercentral.com/articles/Crosstab/65048/
C'mon back for additional information if you still have problems after reading those two articles (teach a person to fish).
--Jeff Moden
Change is inevitable... Change for the better is not.
November 27, 2011 at 9:53 pm
Hi Jeff
Thank you so much for your help and the post.
I am half way through it now 🙂
Vani
November 28, 2011 at 7:50 pm
Hi all
Thank you for your help Jeff. I have got that working now.
I have two tables now (one with client details) and one with the dates. There is nothing in common between these two tables and i need to come up with a SS that has all client details and the dates listed as heading. any suggestions
e.g.
Client details 01/11/2011 02/11/2011 03/11/2011 04/11/2011 .... 30/11/2011
x some value some value some value some value some value
y some value some value some value some value some value
z some value some value some value some value some value
thanks again
Vani
November 28, 2011 at 9:05 pm
vani_r14 (11/28/2011)
There is nothing in common between these two tables
The Client Details table MUST have something in it that can be referred to by date or can be joined to a different table that has some form of date or your task is simply not possible.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 28, 2011 at 9:38 pm
Hi Jeff
Thank you for your help :). I got it working.
I created a table with the client details, joined it with the temp table with the dates using an id
Thanks
Vani
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply