views:

25

answers:

3

I have a table set up like this

Project   Category   Date       Hours
Proj1     test       8/2/2010   2
Proj1     test       8/3/2010   8
Proj1     test       8/4/2010   4
Proj1     test       8/5/2010   3
Proj1     test       8/6/2010   5

I want to develop a query where you can input a saturday(Week Ending) date and get a result like this

WeekEnding Project Category  SunHrs MonHrs TuesHrs WedHrs ThuHrs FriHrs SatHrs  
8/7/2010   Proj1   test      0      2      8       4      3      5      0

Thank you

+3  A: 

Couple of good articles to help:

http://www.devx.com/dbzone/Article/28165

http://technet.microsoft.com/en-us/library/ms177410.aspx

Ardman
Agreed, @Joshua won't the PIVOT command do exactly what you want?
Nathan Koop
+1  A: 

Here's one way. It looks kind of kludgey, but then pivots always look that way to me.

DECLARE @Saturday datetime

SET @Saturday = 'Aug 7, 2010'

SELECT
   @Saturday       WeekEnding
  ,Project
  ,Category
  ,isnull([1], 0)  SunHrs
  ,isnull([2], 0)  MonHrs
  ,isnull([3], 0)  TueHrs
  ,isnull([4], 0)  WedHrs
  ,isnull([5], 0)  ThuHrs
  ,isnull([6], 0)  FriHrs
  ,isnull([7], 0)  SatHrs
 from (select Project, Category, Datepart(dw, Date) DOW, Hours
        from MyTable
        --  Fixed bug from -7 to -6
        where Date between dateadd(dd, /*-7*/ -6, @Saturday) and @Saturday) Source
  pivot (max(Hours)
         for DOW in ([1],[2],[3],[4],[5],[6],[7]) ) as pvt

I used the following to set up data to test this with:

DROP TABLE MyTable
CREATE TABLE MyTable
 (
    Project    varchar(10)  not null
   ,Category   varchar(10)  not null
   ,Date       datetime     not null
   ,Hours      int          not null
 )

INSERT MyTable
 values 
('Proj1', 'test', '8/2/2010',  2 ),
('Proj1', 'test', '8/3/2010',  8 ),
('Proj1', 'test', '8/4/2010',  4 ),
('Proj1', 'test', '8/5/2010',  3 ),
('Proj1', 'test', '8/6/2010',  5 )
Philip Kelley
You need to check `@@datefirst` if using `datepart` with `dw` and if his datetimes have any time component other than midnight `between` won't count these as being on Saturday.
Martin Smith
thank you that works great
Joshua Slocum
@Marting, yeah, I meant to mention that there's lots of caveats when using day-of-week logic... but I forgot, since the data sample was so small.
Philip Kelley
I ran SELECT @@DATEFIRST; and it returned 7I have the app setup so they can only enter the date and not the time
Joshua Slocum
So long as you *know* that @@Datefirst will always return 7 anywhere and everywhere your code will run, your good. If you're paranoid, you have to code for the possibility that the value may vary from server to server.
Philip Kelley
where Date between dateadd(dd, -7, @Saturday)should that 7 be a six?
Joshua Slocum
Yep, that should be a -6. (My excuse is typical: insufficient testing data led to poor debugging.)
Philip Kelley
A: 

Here's basically the query you want but against a different table as it was quicker for me to do and the conversion should be more educational than just giving you a solution on a plate.

DECLARE @WeekEndingDate datetime
SET @WeekEndingDate = '20100821'

SELECT type, 
[6], [5], [4], [3], [2], [1], [0]
FROM
(
select type, DATEDIFF(day,modify_date,@WeekEndingDate) As D,create_date
 from sys.objects
where modify_date > (@WeekEndingDate-6) and 
 modify_date < (@WeekEndingDate+1)) AS SourceTable
PIVOT
(
MAX(create_date)
FOR D IN ([0], [1], [2], [3], [4], [5], [6])
) AS PivotTable
Martin Smith