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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:38:37+00:00 2026-05-15T09:38:37+00:00

I’m having some trouble coming up with a query for what I am trying

  • 0

I’m having some trouble coming up with a query for what I am trying to do.

I’ve got a table we’ll call ‘Movements’ with the following columns:

RecID(Key), Element(f-key), Time(datetime), Room(int)

The table is holding a history of Movements for the Elements. One record contains the element the record is for, the time of the recorded location, and the room it was in at that time.

What I would like are all records that indicate that an Element entered a room. That would mean the first (by time) entry for any element in a series of movements for that element in the same room.

The input is a room number and a time. IE, I would like all of the records indicating that any Element entered room X after time Y.

The closest I came was this

Select Element, min(Time)
from Movements
where Time > Y and Room = x
group by Element

This will only give me one room entry record per Element though (If the Element has entered the room X twice since time Y I’ll only get the first one back) Any ideas? Let me know if I have not explained this clearly.

I’m using MS SQLServer 2005.

  • 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-15T09:38:38+00:00Added an answer on May 15, 2026 at 9:38 am

    This sounds like a typical min per group and in SQL Server can be solved by using ROW_NUMBER:

    SELECT Element, Time
    FROM (
        SELECT
            Element, Time, 
            ROW_NUMBER() OVER(PARTITION BY Element ORDER BY Time) AS rn
        FROM Movements
        WHERE Room = 1 AND Time > '2010-06-01 00:30:00'
    ) T1
    WHERE rn = 1
    

    Example result:

    Element  Time                   
    1        2010-06-01 02:00:00.000
    2        2010-06-01 03:00:00.000
    

    Test data:

    CREATE TABLE Movements (RecID INT NOT NULL, Element INT NOT NULL, Time DATETIME NOT NULL, Room INT NOT NULL);
    INSERT INTO Movements (RecID, Element, Time, Room) VALUES
    (1, 1, '2010-06-01 00:00:00', 1),
    (2, 1, '2010-06-01 01:00:00', 2),
    (3, 1, '2010-06-01 02:00:00', 1),
    (4, 2, '2010-06-01 03:00:00', 1),
    (5, 2, '2010-06-01 04:00:00', 2),
    (6, 1, '2010-06-01 05:00:00', 3),
    (7, 2, '2010-06-01 06:00:00', 2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from

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.