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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:34:12+00:00 2026-06-12T04:34:12+00:00

In SQL Server, I have the following data and I want to get the

  • 0

In SQL Server, I have the following data and I want to get the latest row for every week.

I have the following table. The WW (week number of the date) column doesn’t exist, just the date, I put it there to explain better the purpose of the query.

Number  Date          WW
392     2012-07-23    30
439     2012-07-24    30
735     2012-07-25    30
882     2012-07-26    30 *
193     2012-07-30    31
412     2012-07-31    31   
425     2012-08-01    31
748     2012-08-02    31
711     2012-08-03    31 *
757     2012-08-07    32
113     2012-08-08    32 *
444     2012-08-15    33 *

The desired result of the query should be

882     30 
711     31 
113     32 
444     33 

Basically what I want to get is the latest row of every week. I have found examples like this Get the records of last month in SQL server
to find just the latest one, but I don’t know how to extend them to get the list of results for every week. The rows can be stored in any date, for example maybe in a week there are no results, or in a week there are 5 rows and the next week 10 rows. I would really appreciate some hints for this.

  • 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-12T04:34:13+00:00Added an answer on June 12, 2026 at 4:34 am

    You should be able to use something like this:

    select t1.number, t2.ww
    from yourtable t1
    inner join
    (
        select max(date) mxdate, datepart(week, [Date]) ww
        from yourtable 
        group by datepart(week, [Date])
    ) t2
        on t1.date = t2.mxdate
        and datepart(week, t1.[Date]) = t2.ww;
    

    Or you can use CTE:

    ;with cte as
    (
      select number, datepart(week, [Date]) ww,
        row_number() over(partition by datepart(week, [Date]) 
                          order by date desc) rn
      from yourtable
    )
    select number, ww
    from cte
    where rn = 1
    

    See SQL Fiddle with Demo

    If you use the MAX() version of this and you have two numbers with the same date, then you will return two records. Using the row_number() version will only return one record that meets the criteria due to applying the row_number filter (See Demo)

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

Sidebar

Related Questions

i have the following table data in ms sql server table name category and
I have the following SQL Server stored procedure : BEGIN TRAN CREATE TABLE #TempTable
I have the following table variable in SQL Server 2005: DECLARE @results TABLE (id
I have the following table in SQL Server 2000: TABLE_NAME | COLUMN_NAME | TYPE_NAME
I have a following scenario in my SQL Server 2005 database. zipcodes table has
I have questionnaire data in, SQL Server 2008, that I want to transpose to
I have a table with a column named 'from'. I want to retrieve data
SQL Server 2008 Two tables: Table A has following data: RowA RowB RowC RowD
I have following tsql code in sql server 2008: declare @ID INT SET @ID
I'm using SQL Server 2005 and have the following T-SQL statement: DECLARE @MP VARCHAR(500)

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.