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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:24:44+00:00 2026-06-17T13:24:44+00:00

I have this item table stored in SQL Server database: ID_Item | ID_Sale |

  • 0

I have this item table stored in SQL Server database:

ID_Item   | ID_Sale  | ID_Product |
1         | 200      | 2314       |
2         | 200      | 3894       |
3         | 200      | 4949       |
4         | 100      | 9389       |
5         | 100      | 9499       |
6         | 300      | 9388       |

And I need get the last record of the “ID_Sale” column. Those records as follow:

ID_Item   | ID_Sale  | ID_Product |
3         | 200      | 4949       |
5         | 100      | 9499       |
6         | 300      | 9388       |

I am trying this code, but it fetches only one record:

Select Top 1 * From Itens 
Where ID_Sale = 200
Order by ID_Item Desc

How to get the results iterating over the entire table?

  • 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-06-17T13:24:45+00:00Added an answer on June 17, 2026 at 1:24 pm
    SELECT ID_Item, ID_Sale, ID_Product 
    FROM Tbl
    WHERE Id_Item IN
             (
                 SELECT Max(Id_Item) Id
                 FROM Tbl
                 WHERE [DateTime] BETWEEN @StartDate AND @EndDate
                 GROUP BY Id_Sale
              )
    

    If you use SQL SERVER 2005+

    ;WITH CTE
    AS
    (
       SELECT ID_Item, ID_Sale, ID_Product, ROW_NOUMBER() OVER (PARTITION BY ID_Sale ORDER BY ID_Item DESC) RN
       FROM Tbl
       WHERE [DateTime] BETWEEN @StartDate AND @EndDate
    )
    SELECT ID_Item, ID_Sale, ID_Product
    FROM CTE
    WHERE RN = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this table in my database Item Cat1 Cat2 -------------------- Aaa Red
In our inventory database (SQL Server 2008 std edition) we have a table (called
A my SQL database has an Item table. Each item can have many alternate
I have a SQL Server database designed like this : TableParameter Id (int, PRIMARY
I have these scenario: //This class maps to the Item table in the database
i have this three tables. Table: Item Columns: ItemID, Title, Content, NoChange (Date) Table:
I have a bill of materials table that is set up like this: item
we have a portal that have SQL server 2005 database that contain about 1750
I have an existing SQL Server database, where I store data from large specific
I have this sql update statement which updates a remote table. Is there any

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.