Click here to Skip to main content
15,891,184 members
Home / Discussions / Database
   

Database

 
GeneralRe: Sql Query Pin
Srinivas Uttareshwar17-Sep-13 1:38
Srinivas Uttareshwar17-Sep-13 1:38 
GeneralRe: Sql Query Pin
Richard Deeming17-Sep-13 1:44
mveRichard Deeming17-Sep-13 1:44 
GeneralRe: Sql Query Pin
Srinivas Uttareshwar17-Sep-13 1:50
Srinivas Uttareshwar17-Sep-13 1:50 
GeneralRe: Sql Query Pin
Richard Deeming17-Sep-13 1:53
mveRichard Deeming17-Sep-13 1:53 
GeneralRe: Sql Query Pin
Srinivas Uttareshwar17-Sep-13 1:56
Srinivas Uttareshwar17-Sep-13 1:56 
GeneralRe: Sql Query Pin
Richard Deeming17-Sep-13 2:03
mveRichard Deeming17-Sep-13 2:03 
GeneralRe: Sql Query Pin
Srinivas Uttareshwar17-Sep-13 2:09
Srinivas Uttareshwar17-Sep-13 2:09 
GeneralRe: Sql Query PinPopular
Richard Deeming17-Sep-13 2:42
mveRichard Deeming17-Sep-13 2:42 
So you essentially want one row per day?

SQL
DECLARE @SelectedStartDate date = '20130815';
DECLARE @SelectedEndDate date = '20130830';

WITH cteOverlappingWeeks As
(
    SELECT
        StartDate,
        MON,
        TUE,
        WED,
        THUR,
        FRI,
        SAT,
        SUN
    FROM
        dbo.Timesheet
    WHERE
        StartDate <= @SelectedEndDate
    And
        EndDate >= @SelectedStartDate
),
ctePivotedWeeks (WorkDay, Hours) As
(
    SELECT
        StartDate,
        MON
    FROM
        cteOverlappingWeeks

    UNION ALL

    SELECT
        DateAdd(day, 1, StartDate),
        TUE
    FROM
        cteOverlappingWeeks

    UNION ALL

    SELECT
        DateAdd(day, 2, StartDate),
        WED
    FROM
        cteOverlappingWeeks

    UNION ALL

    SELECT
        DateAdd(day, 3, StartDate),
        THUR
    FROM
        cteOverlappingWeeks

    UNION ALL

    SELECT
        DateAdd(day, 4, StartDate),
        FRI
    FROM
        cteOverlappingWeeks

    UNION ALL

    SELECT
        DateAdd(day, 5, StartDate),
        SAT
    FROM
        cteOverlappingWeeks

    UNION ALL

    SELECT
        DateAdd(day, 6, StartDate),
        SUN
    FROM
        cteOverlappingWeeks
)
SELECT
    WorkDay,
    Hours
FROM
    ctePivotedWeeks
WHERE
    WorkDay Between @SelectedStartDate And @SelectedEndDate
ORDER BY
    WorkDay
;


Example: http://www.sqlfiddle.com/#!3/b8a7a/1[^]



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Sql Query Pin
Simon_Whale17-Sep-13 3:15
Simon_Whale17-Sep-13 3:15 
GeneralRe: Sql Query Pin
Jörgen Andersson17-Sep-13 20:20
professionalJörgen Andersson17-Sep-13 20:20 
AnswerRe: Sql Query Pin
Mycroft Holmes17-Sep-13 12:57
professionalMycroft Holmes17-Sep-13 12:57 
Questionhow to check available cleaner Pin
Jassim Rahma16-Sep-13 9:28
Jassim Rahma16-Sep-13 9:28 
AnswerRe: check the end time also Pin
David Mujica16-Sep-13 9:43
David Mujica16-Sep-13 9:43 
GeneralRe: check the end time also Pin
Jassim Rahma16-Sep-13 9:46
Jassim Rahma16-Sep-13 9:46 
GeneralRe: check the end time also Pin
David Mujica16-Sep-13 10:03
David Mujica16-Sep-13 10:03 
GeneralRe: check the end time also Pin
Jassim Rahma30-Sep-13 23:03
Jassim Rahma30-Sep-13 23:03 
SuggestionRe: how to check available cleaner Pin
Richard Deeming16-Sep-13 10:58
mveRichard Deeming16-Sep-13 10:58 
QuestionTable UserId row population Pin
Member 914293616-Sep-13 9:21
Member 914293616-Sep-13 9:21 
AnswerRe: Table UserId row population Pin
Mycroft Holmes16-Sep-13 12:48
professionalMycroft Holmes16-Sep-13 12:48 
GeneralRe: Table UserId row population Pin
Member 914293616-Sep-13 15:08
Member 914293616-Sep-13 15:08 
QuestionMySQL - Can't create Visit table Pin
noislude15-Sep-13 4:42
noislude15-Sep-13 4:42 
AnswerRe: MySQL - Can't create Visit table Pin
PIEBALDconsult15-Sep-13 5:54
mvePIEBALDconsult15-Sep-13 5:54 
GeneralRe: MySQL - Can't create Visit table Pin
noislude15-Sep-13 6:15
noislude15-Sep-13 6:15 
GeneralRe: MySQL - Can't create Visit table Pin
Mycroft Holmes15-Sep-13 12:56
professionalMycroft Holmes15-Sep-13 12:56 
GeneralRe: MySQL - Can't create Visit table Pin
noislude15-Sep-13 13:01
noislude15-Sep-13 13:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.