Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8745435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:00:47+00:00 2026-06-13T12:00:47+00:00

I have data in table as bellow SERVICE_ID DATE_SEQ DAY_SEQ 101004 2012-10-18 1 101004

  • 0

I have data in table as bellow

SERVICE_ID  DATE_SEQ    DAY_SEQ
101004           2012-10-18       1
101004           2012-10-19       2
101001           2012-10-20       3
101004           2012-10-21       4

I want query generate date show as range with date {From-to} it will group by Service id and date is sequence,if final it show as
output:

**Service_id**      **From-To**                **Date**
101004      2012-10-18 - 2012-10-19         1-2
101001      2012-10-20 - 2012-10-20         3-3
101004      2012-10-21 - 2012-10-21         4-4
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:00:48+00:00Added an answer on June 13, 2026 at 12:00 pm

    try this

    DECLARE @TB AS TABLE(
      SERVICE_ID INTEGER,
      DATE_SEQ SMALLDATETIME,
      DAY_SEQ NVARCHAR(10),
      FARE_SUP_ID INT
    )
    
    INSERT INTO @TB VALUES
      (101004,'2012-10-18 00:00:00',1,267),
      (101001,'2012-10-19 00:00:00',2,171),
      (101004,'2012-10-20 00:00:00',3,267),
      (101004,'2012-10-21 00:00:00',4,171),
      (101001,'2012-10-22 00:00:00',5,171),
      (101001,'2012-10-23 00:00:00',6,171),
      (101004,'2012-10-24 00:00:00',7,267)
    
    SELECT * fROM @TB
    
    ;WITH TB1 AS (
    
    
    SELECT 
      T1.SERVICE_ID,
      MIN(T1.DATE_SEQ) AS CHECK_IN, 
      MAX(T2.DATE_SEQ) AS CHECK_OUT,
      MIN (T1.DAY_SEQ) + ' - ' + MAX(T2.DAY_SEQ) AS DAY_SEQ,
      MAX(T1.FARE_SUP_ID) AS FARE_SUP_ID,
      1 AS DIFF
      FROM @TB T1 INNER JOIN @TB T2
        ON T1.SERVICE_ID = T2.SERVICE_ID
      WHERE DATEDIFF(DY, T1.DATE_SEQ ,T2.DATE_SEQ) = 1
      GROUP BY 
        T1.SERVICE_ID
    
    UNION ALL
    
    SELECT  T3.SERVICE_ID,T3.DATE_SEQ AS CHECK_IN ,T4.DATE_SEQ AS CHECK_OUT 
      ,T3.DAY_SEQ,T3.FARE_SUP_ID,DATEDIFF(DY, T3.DATE_SEQ ,T4.DATE_SEQ)  AS DIFF
      FROM @TB T3 INNER JOIN @TB T4
        ON T3.SERVICE_ID = T4.SERVICE_ID
      WHERE DATEDIFF(DY, T3.DATE_SEQ ,T4.DATE_SEQ) = 0
        AND 
        T3.DATE_SEQ NOT IN (
          SELECT T1.DATE_SEQ
            FROM @TB T1 INNER JOIN @TB T2
              ON T1.SERVICE_ID = T2.SERVICE_ID
            WHERE DATEDIFF(DY, T1.DATE_SEQ ,T2.DATE_SEQ) = 1
        )
        AND 
          T4.DATE_SEQ NOT IN (
            SELECT T2.DATE_SEQ
              FROM @TB T1 INNER JOIN @TB T2
                ON T1.SERVICE_ID = T2.SERVICE_ID
              WHERE DATEDIFF(DY, T1.DATE_SEQ ,T2.DATE_SEQ) = 1
          )
      )
    SELECT 
      *
      FROM TB1
      ORDER BY DAY_SEQ
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that has data like below(sorted by date) COL_A | COL_B
I have this table services , with data as follows: service_id bepro_id service_name 1
I do have a table data as shown below: <td> <label for=title>Title : </label>
I have a piece of matlab code below which reads data from a table.
I have a table part with a few demo data as below in Oracle
I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work
I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I have a data table with many rows and columns. How can I display
I have table data, select * from tbltaxamount ; id | taxname | taxinfoid
I have written a service To delete data from a table when data inputed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.