Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Sequential Dates and Grouping

Sequential Dates and Grouping

Scheduled Pinned Locked Moved Database
learning
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Peet Schultz
    wrote on last edited by
    #1

    Hi All We have a table with a persons Employee number (Resource tag) , Date Worked and the Shift Worked on that date. Like So resource tag date worked shift type ------------ ------------------------------------------------------ -------------------------------------------------- 125197584 2004-07-14 00:00:00.000 Night 125197584 2004-07-15 00:00:00.000 Night 125197584 2004-07-16 00:00:00.000 Night 125197584 2004-07-17 00:00:00.000 Night 125197584 2004-07-18 00:00:00.000 Morning 125197584 2004-07-19 00:00:00.000 Morning 125197584 2004-07-20 00:00:00.000 Morning 125197584 2004-07-21 00:00:00.000 Morning 125197584 2004-07-22 00:00:00.000 Morning 125197584 2004-07-23 00:00:00.000 Morning 125197584 2004-07-24 00:00:00.000 Morning 125197584 2004-07-25 00:00:00.000 Night 125197584 2004-07-26 00:00:00.000 Night 125197584 2004-07-27 00:00:00.000 Night 125197584 2004-07-28 00:00:00.000 Night 125197584 2004-07-29 00:00:00.000 Night 125197584 2004-07-30 00:00:00.000 Night 125197584 2004-07-31 00:00:00.000 Night 125197584 2004-08-01 00:00:00.000 Morning 125197584 2004-08-02 00:00:00.000 Morning 125197584 2004-08-03 00:00:00.000 Morning 125197584 2004-08-04 00:00:00.000 Morning 125197584 2004-08-05 00:00:00.000 Afternoon 125197584 2004-08-06 00:00:00.000 Morning 125197584 2004-08-07 00:00:00.000 Morning 125197584 2004-08-08 00:00:00.000 Morning 125197584 2004-08-09 00:00:00.000 Morning 125197584 2004-08-10 00:00:00.000 Morning 125197584 2004-08-11 00:00:00.000

    M 1 Reply Last reply
    0
    • P Peet Schultz

      Hi All We have a table with a persons Employee number (Resource tag) , Date Worked and the Shift Worked on that date. Like So resource tag date worked shift type ------------ ------------------------------------------------------ -------------------------------------------------- 125197584 2004-07-14 00:00:00.000 Night 125197584 2004-07-15 00:00:00.000 Night 125197584 2004-07-16 00:00:00.000 Night 125197584 2004-07-17 00:00:00.000 Night 125197584 2004-07-18 00:00:00.000 Morning 125197584 2004-07-19 00:00:00.000 Morning 125197584 2004-07-20 00:00:00.000 Morning 125197584 2004-07-21 00:00:00.000 Morning 125197584 2004-07-22 00:00:00.000 Morning 125197584 2004-07-23 00:00:00.000 Morning 125197584 2004-07-24 00:00:00.000 Morning 125197584 2004-07-25 00:00:00.000 Night 125197584 2004-07-26 00:00:00.000 Night 125197584 2004-07-27 00:00:00.000 Night 125197584 2004-07-28 00:00:00.000 Night 125197584 2004-07-29 00:00:00.000 Night 125197584 2004-07-30 00:00:00.000 Night 125197584 2004-07-31 00:00:00.000 Night 125197584 2004-08-01 00:00:00.000 Morning 125197584 2004-08-02 00:00:00.000 Morning 125197584 2004-08-03 00:00:00.000 Morning 125197584 2004-08-04 00:00:00.000 Morning 125197584 2004-08-05 00:00:00.000 Afternoon 125197584 2004-08-06 00:00:00.000 Morning 125197584 2004-08-07 00:00:00.000 Morning 125197584 2004-08-08 00:00:00.000 Morning 125197584 2004-08-09 00:00:00.000 Morning 125197584 2004-08-10 00:00:00.000 Morning 125197584 2004-08-11 00:00:00.000

      M Offline
      M Offline
      Michael Potter
      wrote on last edited by
      #2

      You can try something like:

      SELECT
           wt.DateWorked
      FROM 
           WorkTable wt
      WHERE
           wt.ShiftType <>
                (SELECT
                     ShiftType
                 FROM
                     WorkTable
                 WHERE
                     ResourceTag = wt.ResourceTag AND
                     DateWorked = (SELECT 
                                       MAX(DateWorked)
                                   FROM 
                                       WorkTable
                                   WHERE
                                       ResourceTag = wt.ResourceTag AND
                                       DateWorked < wt.DateWorked))
      

      The table will need the following index to work quickly:

      CREATE INDEX ShiftTest ON WorkTable
      (
           ResourceTag,
           DateWorked
      )
      

      This assumes that no worker will work more than 1 shift per day.

      P 1 Reply Last reply
      0
      • M Michael Potter

        You can try something like:

        SELECT
             wt.DateWorked
        FROM 
             WorkTable wt
        WHERE
             wt.ShiftType <>
                  (SELECT
                       ShiftType
                   FROM
                       WorkTable
                   WHERE
                       ResourceTag = wt.ResourceTag AND
                       DateWorked = (SELECT 
                                         MAX(DateWorked)
                                     FROM 
                                         WorkTable
                                     WHERE
                                         ResourceTag = wt.ResourceTag AND
                                         DateWorked < wt.DateWorked))
        

        The table will need the following index to work quickly:

        CREATE INDEX ShiftTest ON WorkTable
        (
             ResourceTag,
             DateWorked
        )
        

        This assumes that no worker will work more than 1 shift per day.

        P Offline
        P Offline
        Peet Schultz
        wrote on last edited by
        #3

        Thanks This looks good, only problem is does not return the first record either. I do have a way of getting past that and will run a couple of benchmarks to check the speed of this against the left join Regards Peet YASP

        M 1 Reply Last reply
        0
        • P Peet Schultz

          Thanks This looks good, only problem is does not return the first record either. I do have a way of getting past that and will run a couple of benchmarks to check the speed of this against the left join Regards Peet YASP

          M Offline
          M Offline
          Michael Potter
          wrote on last edited by
          #4

          Didn't think about the first record. Just wrap the correlated Select in an ISNULL() that will return an invalid ShiftType:

          wt.ShiftType <> ISNULL((Select...),'')
          
          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups