Complex Query

Hello,

I need to construct a SQL 2008 table and associated query that can produce the following output
The table will contain a list of tasks; one of the fields will be a completion date.  When the query is run the query needs to be smart enough to work out the next 12 months from now and then populate the tasks to which month the completion date falls.  This in itself is quite complex, however to add a further layer of complexity it can’t be 1 row one date, all white spaces must be used.

Here is what the source table might look like

task_id      task      completion_date
1      Task 1      Mar
2      Task 2      May
3      Task 4      Mar
4      Task 5      Apr

For example instead of producing this:

Mar      Apr      May
Task 1              
       Task 5       
Task 4              
              Task 2

I need it to produce something like this:

Mar      Apr      May
Task 1      Task 5      Task 2
Task 4              
               
               

I have a completely free reign as to how the table can be structured in order to best achieve this outcome.
TIA
andyw27Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
This should be better  done in the front-end. Otherwise:

DECLARE @Sample TABLE
    (
      task_id INT ,
      task NVARCHAR(255) ,
      completion_date NVARCHAR(255)
    );

INSERT  INTO @Sample
VALUES  ( 1, 'Task 1', 'Mar' ),
        ( 2, 'Task 2', 'May' ),
        ( 3, 'Task 4', 'Mar' ),
        ( 4, 'Task 5', 'Apr' );

WITH    Ordered
          AS ( SELECT   S.task ,
                        S.completion_date ,
                        ROW_NUMBER() OVER ( PARTITION BY S.completion_date ORDER BY S.task_id ) AS RN
               FROM     @Sample S
             )
    SELECT  P.Mar ,
            P.Apr ,
            P.May
    FROM    Ordered O PIVOT ( MAX(O.task) FOR O.completion_date IN ( Mar, Apr, May ) ) P;

Open in new window

0
andyw27Author Commented:
Thanks I'll have a look.  I take it this is unable to cope with the dynamic elements, i.e. knowing what the next 12 months are, or displaying the months column even when there is no asscociated data, i.e. it should always display 12 columns
0
ste5anSenior DeveloperCommented:
Well, dynamic SQL is no miracle. But it's requirement wasn't clear from the op. E.g.

CREATE TABLE #Sample 
    (
      task_id INT ,
      task NVARCHAR(255) ,
      completion_date NVARCHAR(255)
    );

INSERT  INTO #Sample
VALUES  ( 1, 'Task 1', 'Mar' ),
        ( 2, 'Task 2', 'May' ),
        ( 3, 'Task 4', 'Mar' ),
        ( 4, 'Task 5', 'Apr' );

DECLARE @ColumnList NVARCHAR(MAX) = N'';
DECLARE @Sql NVARCHAR(MAX);
DECLARE @Template NVARCHAR(MAX) = N'
WITH    Ordered
          AS ( SELECT   S.task ,
                        S.completion_date ,
                        ROW_NUMBER() OVER ( PARTITION BY S.completion_date ORDER BY S.task_id ) AS RN
               FROM     #Sample S
             )
    SELECT  @ColumnList
    FROM    Ordered O PIVOT ( MAX(O.task) FOR O.completion_date IN ( @ColumnList ) ) P;
';

SELECT	@ColumnList += ', ' + S.completion_date
FROM	#Sample S
GROUP BY S.completion_date;

SET @Sql = REPLACE(@Template, '@ColumnList', STUFF(@ColumnList, 1, 2, ''));
EXECUTE ( @Sql );

DROP TABLE #Sample;

Open in new window


But still: It's often better done in the front-end.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
andyw27, do you still need help with this question?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.