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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:13:55+00:00 2026-05-26T13:13:55+00:00

I am trying to get 2 rows from a table at one shot. The

  • 0

I am trying to get 2 rows from a table at one shot. The one with the minimum datetime (today – 7) of last week and the one which is the latest (today).
My table:

|id  |dataIn |dataOut|date                   |MachineId                            |
-----+-------+-------+-----------------------+-------------------------------------+
|1   |5006   |58     |2011-10-25 09:03:17.000|7B788EE88E-6527-4CB4-AA4D-01B7F4048559  
|2   |1200   |130    |2011-10-26 12:45:43.000|7B788EE88E-6527-4CB4-AA4D-01B7F4048559 
        ...  
|124 |1350   |480    |2011-10-29 13:29:04.000|7B788EE88E-6527-4CB4-AA4D-01B7F4048559  
|125 |8005   |560    |2011-10-31 21:18:35.000|7B788EE88E-6527-4CB4-AA4D-01B7F4048559  

I can select the data from last week with:

SELECT 
dbo.myDatabase.Date AS [date], dbo.myDatabase.dataIn AS [in], 
dbo.myDatabase.dataOut AS [out] 
FROM 
dbo.myDatabase WHERE 
Date >=dateadd(day,datediff(day,0,GetDate())- 7,0) 
AND 
dbo.myDatabase.MachineId = '7B788EE88E-6527-4CB4-AA4D-01B7F4048559' 

but I would only like row 1 AND 125 because those are the rows used for my calculations.
So my question is:
How do I select the 2 rows (with MIN and MAX date) from within the results of the previous query?

  • 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-26T13:13:55+00:00Added an answer on May 26, 2026 at 1:13 pm

    you can use this:

    select * from dbo.myDatabase 
    where 
        ([Date] = (select max([Date]) from /* your query */ ) or 
        [Date] = (select min([Date]) from /* your query */ ))
        and MachineId = '7B788EE88E-6527-4CB4-AA4D-01B7F4048559' -- or any other id
    

    Edit: since it’s entirely possible that two machines have the same date value, the query should be updated to also include a MachineId filter in the where clause. I updated the query to show this.

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

Sidebar

Related Questions

I'm trying to do a query to filter on rows from one table and
I am trying to get data from one row in a table into three
I am trying to get the rows in my table that resides in a
In SQL Server, I am trying to get the rows from the two tables
I'm trying to use an SQL insert statement to migrate rows from a table
In one statement I'm trying to group rows of one table by joining to
I'm trying to retrieve books from one table and left join the chapters table.
I'm trying to get data from one database and put it in the format
I am trying to select some fields from one table and insert them into
I am trying to get all the rows that exist in allData but not

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.