• The second problem I am having is more complex. I am trying to figure out 2 things.

    1) How to get the column headings (and totals)

    2) How to have a report use dynamic columns

    Below is a sample table I created:

    CREATE TABLE [dbo].[TimeSheetTest](

    [ID] [int] IDENTITY(1,1) NOT NULL,

    [EmployeeCode] [varchar](50) NOT NULL,

    [ProjectCode] [varchar](50) NOT NULL,

    [HoursWorked] [decimal](18, 2) NOT NULL,

    [DateWorked] [datetime] NOT NULL,

    CONSTRAINT [PK_TimeSheetTest] PRIMARY KEY CLUSTERED

    (

    [ID] ASC

    ) ON [PRIMARY]

    ) ON [PRIMARY]

    Then, add the following data:

    INSERT INTO TimeSheetTest

    SELECT 'Adam', 'P1', 8, '2009-01-01'

    UNION ALL SELECT 'Adam', 'P2', 1, '2009-01-01'

    UNION ALL SELECT 'Bob', 'P1', 5, '2009-01-01'

    UNION ALL SELECT 'Bob', 'P2', 3, '2009-01-01'

    UNION ALL SELECT 'Adam', 'P1', 8, '2009-01-03'

    UNION ALL SELECT 'Adam', 'P1', 8, '2009-01-05'

    UNION ALL SELECT 'Bob', 'P1', 7, '2009-01-05'

    UNION ALL SELECT 'Bob', 'P2', 1, '2009-01-05'

    Now, using the basic syntax for getting the xtab:

    exec UberCrosstab

    'EmployeeCode, ProjectCode',

    'DateWorked',

    'TimeSheetTest',

    'HoursWorked',

    'SUM'

    I get the following results (I dropped the time 12:00AM from below for readability):

    EmployeeCode ProjectCode Jan 1 2009 Jan 3, 2009 Jan 5, 2009

    Adam P1 8.00 8.00 8.00

    Bob P1 5.00 NULL 7.00

    Adam P2 1.00 NULL NULL

    Bob P2 3.00 NULL 1.00

    This part works as expected. However, I want to display Thu 01/01, Sat 01/03, Mon 01/05

    so, I created a field as follows:

    SELECT *, LEFT(DATENAME(dw, DateWorked), 3) + ' ' + CONVERT(VARCHAR(5), DateWorked, 1) AS Header

    FROM TimeSheetTest

    Of course (since I cannot get the (query) to work), I do the following:

    SELECT *, LEFT(DATENAME(dw, DateWorked), 3) + ' ' + CONVERT(VARCHAR(5), DateWorked, 1) AS Header

    INTO #temp

    FROM TimeSheetTest

    exec UberCrosstab

    'EmployeeCode, ProjectCode',

    'Header',

    '#temp',

    'HoursWorked',

    'SUM'

    drop table #temp

    Now, the table appears with headings of what I want (Mon 01/05, Sat 01/03, Thu 01/01), but the problem is they are sorted by the text (M in Monday comes before S - Saturday, etc).

    [highlight=#ffff11]

    Q. How do I sort it the way I want?

    [/highlight]

    I am also trying to get all the dates in a date range, not just those that have data. This is not a problem, other than the data comes out sorted by Fri, Mon, Sat, Sun, Thurs, Tues, Wed.

    SET NOCOUNT ON

    DECLARE @StartDateDATETIME

    DECLARE @EndDateDATETIME

    DECLARE @ctrINT

    DECLARE @diffINT

    DECLARE @days TABLE (

    WorkDay DATETIME PRIMARY KEY,

    WorkDayName VARCHAR(50),

    Counter INT

    )

    SELECT @StartDate = '2009-01-01'

    SELECT @EndDate = '2009-01-07'

    SELECT @diff = DATEDIFF(d, @StartDate, @EndDate)

    SELECT @ctr = 0

    WHILE @ctr <= @diff BEGIN

    INSERT INTO @days (

    WorkDay,

    WorkDayName,

    Counter

    ) VALUES (

    @StartDate + @ctr,

    LEFT(DATENAME(dw, @StartDate + @ctr), 3) + '. ' + LEFT(CONVERT(VARCHAR(8), @StartDate + @ctr, 1), 5),

    @ctr + 1

    )

    SET @ctr = @ctr + 1

    END

    SELECT *

    INTO #temp

    FROM @days d

    LEFT OUTER JOIN TimeSheetTest t

    ON d.WorkDay = t.DateWorked

    exec UberCrosstab

    'EmployeeCode, ProjectCode',

    'WorkDayName',

    '#temp',

    'HoursWorked',

    'SUM'

    drop table #temp

    Part 2 of the problem is to create a report that will have dynamic fields in the crosstab, and a total afterwards. I have not tried it, but I am assuming I can create another table variable or query that totals across for the period (in this case, one week) and join that with this data. That will get me the totals.

    The tricky part is the workdays. It might be 7. It might be 14 (paid every 2 weeks). It might be a little more if paid twice a month (1-15, 16-31 for some months, 16-28 or 29 for February, etc). It could be 28-31 days (depending again on the month).

    [highlight=#ffff11]

    Please tell me there is a way other than to create 31 columns and hide the ones that are not used.

    [/highlight]