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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:32:45+00:00 2026-05-19T15:32:45+00:00

I have a table full of events. I’ve been asked to create an aggregate

  • 0

I have a table full of events. I’ve been asked to create an aggregate table of sessions; one session may have several events. Sessions are identified by having the same arrival time. e.g. (This is a simplification, I’m not typing out actual timestamps):

EventID  ArrivalTime  StartTime EndTime StaffID
1        0945         0950      0955    John
2        0945         0955      1000    Barb

Might be turned into something like:

ArrivalTime StartTime EndTime StaffID
0945        0950      1000    ???

With use of MIN(StartTime) and MAX(EndTime) to keep it to a single row.

The problem I’m running into, as the question marks above indicate, is getting a single staff ID – which staff member it is doesn’t matter too much, but I need someone. If it were just a string, as I’ve shown above, it could be done with MIN(StaffID), but the thing that’s doing my head in is that I’m required to look up StaffID in the Staff table and pull out the GUID that’s associated with the short code that’s in my table. And GUID’s don’t like functions like MIN(). Also, just to make matters worse, it’s feasible that the StaffID column in the Events table is NULL, so I have to stick with left joins or similar.

Someone suggested a subquery, but apparently my brain refuses to accept this on a Friday and can’t see how to get that to work.

As a baseline, here’s something along the lines of my current query:

SELECT NEWID() AS SessionID,
e.ArrivalTime,
MIN(e.StartTime),
MAX(e.EndTime),
s.StaffGUID
FROM Events e LEFT JOIN Staff s ON e.StaffID = s.StaffID
GROUP BY e.ArrivalTime, s.StaffGUID

The problem is that if two different staff members are in the list, the session will show up twice. Any ideas?

  • 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-19T15:32:46+00:00Added an answer on May 19, 2026 at 3:32 pm

    There’s the correlated subselect option (2000+ for using TOP):

      SELECT NEWID() AS SessionID,
             e.ArrivalTime,
             MIN(e.StartTime),
             MAX(e.EndTime),
             (SELECT TOP 1 s.StaffGUID
                FROM STAFF s
               WHERE s.staffid = e.staffid) AS staffguid
        FROM EVENTS e
    GROUP BY e.arrivaltime, e.staffguid, staffguid
    

    …or the derived table/inline view (2005+ for using ROW_NUMBER):

      SELECT NEWID() AS SessionID,
             e.ArrivalTime,
             MIN(e.StartTime),
             MAX(e.EndTime),
             s.staffguid
         FROM EVENTS e
    LEFT JOIN (SELECT t.staffid,
                      t.staffguid,
                      ROW_NUMBER() OVER (PARTITION BY t.staffid) AS rank
                 FROM STAFF t) s ON s.staffid = e.staffid
                                AND s.rank = 1
     GROUP BY e.arrivaltime, s.staffguid
    

    My preference is for the derived table – correlated subqueries tend not to perform as well.

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

Sidebar

Related Questions

I have a table full of data, where one column is full of different
I have a table full of attendance data for students, we are migrating to
I have a table full of items that each have a unique ItemID .
I have a table full of longitude/ latitude pairs in decimal format (e.g., -41.547,
I have a table full of Dealers along with their latitude and longitude. I
I have a table full of bugs. The BugTitle is the page erroring and
I have a table full of cells and i would like to get on
I have a table full of items and prices for said items. I would
I have a table full of id's (tID) which are created with a w
I have a Table full of players. Each of these players can have a

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.