Click here to Skip to main content
15,886,518 members
Home / Discussions / Database
   

Database

 
QuestionMySQL Database Tables Pin
sudevsu12-Jan-15 8:29
sudevsu12-Jan-15 8:29 
AnswerRe: MySQL Database Tables Pin
RUs12313-Jan-15 2:13
RUs12313-Jan-15 2:13 
GeneralRe: MySQL Database Tables Pin
sudevsu14-Jan-15 8:53
sudevsu14-Jan-15 8:53 
QuestionGrouping From & To continuous dates in SQL Server Pin
Ashokraja V7-Jan-15 17:34
Ashokraja V7-Jan-15 17:34 
AnswerRe: Grouping From & To continuous dates in SQL Server Pin
Mycroft Holmes7-Jan-15 20:58
professionalMycroft Holmes7-Jan-15 20:58 
GeneralRe: Grouping From & To continuous dates in SQL Server Pin
Ashokraja V7-Jan-15 21:13
Ashokraja V7-Jan-15 21:13 
GeneralRe: Grouping From & To continuous dates in SQL Server Pin
Mycroft Holmes7-Jan-15 21:20
professionalMycroft Holmes7-Jan-15 21:20 
AnswerRe: Grouping From & To continuous dates in SQL Server Pin
Richard Deeming8-Jan-15 2:55
mveRichard Deeming8-Jan-15 2:55 
There might be a better way to do this, but the obvious solution would be:
SQL
WITH cteStartingPoints As
(
    -- Find the rows with no row ending on the previous day:
    SELECT
        A.EmpCode,
        A.StartDate,
        ROW_NUMBER() OVER (ORDER BY A.StartDate) As RN
    FROM
        YourTable As A
    WHERE
        Not Exists
        (
            SELECT 1
            FROM YourTable As B
            WHERE B.EmpCode = A.EmpCode
            And B.EndDate = DateAdd(day, -1, A.StartDate)
        )
),
cteEndingPoints As
(
    -- Find the rows with no row starting on the next day:
    SELECT
        A.EmpCode,
        A.EndDate,
        ROW_NUMBER() OVER (ORDER BY A.StartDate) As RN
    FROM
        YourTable As A
    WHERE
        Not Exists
        (
            SELECT 1
            FROM YourTable As B
            WHERE B.EmpCode = A.EmpCode
            And B.StartDate = DateAdd(day, 1, A.EndDate)
        )
)
SELECT
    S.EmpCode,
    S.StartDate,
    E.EndDate
FROM
    cteStartingPoints As S
    INNER JOIN cteEndingPoints As E
    ON E.EmpCode = S.EmpCode
    And E.RN = S.RN
;

http://sqlfiddle.com/#!3/c1331/2[^]



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


GeneralRe: Grouping From & To continuous dates in SQL Server Pin
Mycroft Holmes8-Jan-15 12:02
professionalMycroft Holmes8-Jan-15 12:02 
GeneralRe: Grouping From & To continuous dates in SQL Server Pin
Ashokraja V12-Jan-15 18:24
Ashokraja V12-Jan-15 18:24 
GeneralRe: Grouping From & To continuous dates in SQL Server Pin
Mycroft Holmes12-Jan-15 19:00
professionalMycroft Holmes12-Jan-15 19:00 
GeneralRe: Grouping From & To continuous dates in SQL Server Pin
Jörgen Andersson12-Jan-15 21:30
professionalJörgen Andersson12-Jan-15 21:30 
GeneralRe: Grouping From & To continuous dates in SQL Server Pin
Ashokraja V19-Jan-15 0:23
Ashokraja V19-Jan-15 0:23 
AnswerRe: Grouping From & To continuous dates in SQL Server Pin
Umer Akram11-Jan-15 19:56
Umer Akram11-Jan-15 19:56 
QuestionLong running queries. Pin
sathish487-Jan-15 2:38
sathish487-Jan-15 2:38 
AnswerRe: Long running queries. Pin
ZurdoDev7-Jan-15 3:33
professionalZurdoDev7-Jan-15 3:33 
AnswerRe: Long running queries. Pin
Mycroft Holmes7-Jan-15 13:34
professionalMycroft Holmes7-Jan-15 13:34 
AnswerRe: Long running queries. Pin
Swinkaran7-Jan-15 16:26
professionalSwinkaran7-Jan-15 16:26 
QuestionYearly sales report Pin
pkfox5-Jan-15 20:26
professionalpkfox5-Jan-15 20:26 
AnswerRe: Yearly sales report Pin
Mycroft Holmes5-Jan-15 20:58
professionalMycroft Holmes5-Jan-15 20:58 
GeneralRe: Yearly sales report Pin
pkfox5-Jan-15 22:11
professionalpkfox5-Jan-15 22:11 
GeneralRe: Yearly sales report Pin
Umer Akram6-Jan-15 0:13
Umer Akram6-Jan-15 0:13 
GeneralRe: Yearly sales report Pin
pkfox6-Jan-15 6:39
professionalpkfox6-Jan-15 6:39 
GeneralRe: Yearly sales report Pin
pkfox10-Jan-15 22:20
professionalpkfox10-Jan-15 22:20 
GeneralRe: Yearly sales report Pin
Umer Akram11-Jan-15 19:18
Umer Akram11-Jan-15 19:18 

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.