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 4270764
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:21:02+00:00 2026-05-21T07:21:02+00:00

I am working on a routing system for a transportation type similar to a

  • 0

I am working on a routing system for a transportation type similar to a bus routing system like below.
I have a view that gives me the output. I need to pivot over schedules where the number of schedules can be of a variable quantity.

My query should result in the output given below in the image. I tried using Case Statements but I had problems with the number of rows returned.

Here are the scripts to generate the table and the data for reference:

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ScheduleTest]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ScheduleTest](
[StationName] [nvarchar](255) NULL,
[ScheduleNumber] [nvarchar](255) NULL,
[ArrivalTime] [nvarchar](20) NULL,
[DepartureTime] [nvarchar](20) NULL
) ON [PRIMARY]
END
GO
  

--Insert Scripts For Schedule A
Insert into ScheduleTest Values ('Chicago, IL, (Union Station)', 'ScheduleA', NULL, '02:45')
Insert into ScheduleTest Values ('Chicago, IL, (DownTown)', 'ScheduleA', '02:55', '03:00')
Insert into ScheduleTest Values ('Benton, MI, Harbor', 'ScheduleA', '08:00', NULL) --Benton in this case 
--is final destination so departure time is null

-- Insert Scripts for Schedule B (Another Which runs in the morning)
Insert into ScheduleTest Values ('Chicago, IL, (Union Station)', 'ScheduleB', NULL, '06:00')
Insert into ScheduleTest Values ('Chicago, IL, (DownTown)', 'ScheduleB', '06:10', '06:15')
Insert into ScheduleTest Values ('Benton, IL, Harbor', 'ScheduleB', '11:00', NULL)

I don’t think I can use pivoting in Sql server 2005 since it needs some sort of aggregates to work on. I have nothing aggregate here.

  • 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-05-21T07:21:03+00:00Added an answer on May 21, 2026 at 7:21 am

    I got it from a different source… This works and thanks to one who suggested this answer

    IF OBJECT_ID( ‘tempdb..#ScheduleTest’) IS NOT NULL
    DROP TABLE #ScheduleTest
    GO

    IF OBJECT_ID( ‘tempdb..#tmp’) IS NOT NULL
    DROP TABLE #tmp
    GO

    CREATE TABLE #ScheduleTest(
    [StationName] nvarchar NULL,
    [ScheduleNumber] nvarchar NULL,
    [ArrivalTime] nvarchar NULL,
    [DepartureTime] nvarchar NULL
    ) ON [PRIMARY]

    GO

    –Insert Scripts For Schedule A
    Insert into #ScheduleTest Values (‘Chicago, IL, (Union Station)’, ‘ScheduleA’, NULL, ’02:45′)
    Insert into #ScheduleTest Values (‘Chicago, IL, (DownTown)’, ‘ScheduleA’, ’02:55′, ’03:00′)
    Insert into #ScheduleTest Values (‘Benton, IL, Harbor’, ‘ScheduleA’, ’08:00′, NULL) –Benton in this case
    –is final destination so departure time is null

    — Insert Scripts for Schedule B (Another Which runs in the morning)
    Insert into #ScheduleTest Values (‘Chicago, IL, (Union Station)’, ‘ScheduleB’, NULL, ’06:00′)
    Insert into #ScheduleTest Values (‘Chicago, IL, (DownTown)’, ‘ScheduleB’, ’06:10′, ’06:15′)
    Insert into #ScheduleTest Values (‘Benton, IL, Harbor’, ‘ScheduleB’, ’11:00′, NULL)

    SELECT
    StationName, ScheduleNumber, ArrivalTime AS TimeOfEvent, ‘Arrival’ AS [Arrival/Departure]
    INTO #tmp
    FROM
    #ScheduleTest
    WHERE
    ArrivalTime IS NOT NULL
    UNION ALL
    SELECT
    StationName, ScheduleNumber, DepartureTime AS TimeOfEvent, ‘Departure’ AS [Arrival/Departure]
    FROM
    #ScheduleTest
    WHERE
    DepartureTime IS NOT NULL

    — Due to the aggregation requirement of pivot, Only one Schedule/Station combination is allowed.
    — This is the query for the core data. Now, to dynamic this:
    SELECT
    StationName,
    ScheduleA,
    ScheduleB,
    [Arrival/Departure]
    FROM
    #tmp AS t
    PIVOT
    ( MAX(TimeOfEvent) FOR ScheduleNumber IN ( ScheduleA, ScheduleB)
    ) AS pvt

    DECLARE @sql nVARCHAR(MAX),
    @ScheduleNumber VARCHAR(50),
    @PivotInList nVARCHAR(MAX)

    SET @sql = ‘SELECT StationName, ‘
    SET @PivotInList = ‘ IN (‘

    DECLARE ScheduleCursor CURSOR FAST_FORWARD FOR
    SELECT DISTINCT ScheduleNumber FROM #tmp

    OPEN ScheduleCursor
    FETCH NEXT FROM ScheduleCursor INTO @ScheduleNumber

    WHILE @@FETCH_STATUS = 0
    BEGIN
    SET @sql = @sql + @ScheduleNumber + ‘, ‘
    SET @PivotInList = @PivotInList + ‘ ‘ + @ScheduleNumber + ‘,’

        FETCH NEXT FROM ScheduleCursor INTO @ScheduleNumber
    

    END

    CLOSE ScheduleCursor
    DEALLOCATE ScheduleCursor

    SET @PivotInList = LEFT( @PivotInList, LEN( @PivotInList) – 1 /Remove the extra comma-space/) + ‘)’
    PRINT @PivotInList

    SET @sql = @sql + ‘[Arrival/Departure] FROM #tmp AS t PIVOT ( MAX( TimeOfEvent) FOR ScheduleNumber’
    + @PivotInList + ‘) AS pvt’
    PRINT @sql

    EXEC sp_executeSQL @sql


    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web application working fine in IIS6. Its using routes using System.Web.Routing.RouteCollection.
My url routing is working properly inside Areas folder, that means i can go
I'm working on my ecommerce on kohana 3.2. i need to setup the routing
Good Day, I have a simple working routine in Perl that swaps two words:
I have a project I am working on that has a Order entity that
I have a C# web forms ASP.NET 4.0 web application that uses Routing for
I'm working on a Ruby on Rails application that used the Devise authentication system.
I have read all these articles about how to make system.web.routing work but all
Url Routing is not working on IIS 6. (using System.Web.Routing Namespace) If i am
I'm currently working on map routing system for pedestrian pathway and not sure does

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.