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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:28:36+00:00 2026-06-03T13:28:36+00:00

Some help with some T-SQL would be most appreciated. I have the following four

  • 0

Some help with some T-SQL would be most appreciated.

I have the following four tables:

  • Item (Id)
  • ItemVersion (Id, FK many-to-one Item.Id)
  • ItemVersionStatusLog (LogDate, FK many-to-one ItemVersion.Id, FK many-to-one StatusType.Id)
  • StatusType (Id, Alias)

I’ve only listed the columns appropriate to this question.

The root entity is an Item. For each Item, there is one or more ItemVersion entries. For each ItemVersion entry, there is one or more ItemVersionStatusLog entries with a date and a reference to a StatusType (e.g. created, updated, disabled).

I’d like to summarise the “most recent status” of each Item by creating an aggregate view in a new table (ItemStatus) that I’ll backfill and then keep updated when data changes. The aggregation should give me the maximum-dated entry in the log table, for each Item and StatusType pair. So that I have a snapshot of, for each StatusType, I can get the most recent ItemVersion for an Item.

Another way of putting it is procedurally:

For each Item
- For each StatusType
- - List the ItemVersion Id with the maximum date from ItemVersionStatusLog given the correct StatusType

My target columns for the aggregate view or table are:

Item Id, ItemVersion Id, Date (from ItemVersionStatus), StatusType Id

While I could do this reasonably well with the use of UDF’s, if possible I’d love to do this in a single SQL statement. My primary target is SQL Server 2008, but also SQL Server Compact 4 without much modification, so relying on UDF’s isn’t a great option but any help is appreciated 🙂

Update – some example data

Item:
Id
--
1
2

ItemVersion:

Id  | ItemId | Name
----------
1   | 1      | Apple
2   | 1      | Orange
3   | 1      | Plum
4   | 2      | Petrol
5   | 2      | Diesel
6   | 2      | LPG

StatusType:

Id  | Alias
-----------
1   | Created
2   | Approved
3   | Published
4   | Deleted

ItemVersionStatusLog:

Id  | ItemVersionId | StatusTypeId | Date
------------------------------------------
1   | 1             | 1            | 2012-01-01 00:00
2   | 1             | 4            | 2012-01-01 00:05
3   | 2             | 1            | 2012-01-01 00:10
4   | 2             | 3            | 2012-01-01 00:15
5   | 3             | 1            | 2012-01-01 00:20
6   | 3             | 3            | 2012-01-01 00:25

In this case the expected result for Item 1 would be:

ItemStatus

ItemId | ItemVersionId | Date             | StatusTypeId
--------------------------------------------------------
1      | 3             | 2012-01-01 00:20 | 1
1      | 3             | 2012-01-01 00:25 | 3
1      | 1             | 2012-01-01 00:05 | 4
  • 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-03T13:28:37+00:00Added an answer on June 3, 2026 at 1:28 pm
    With MostRecentStatus As
        (
        Select ItemVersionId, StatusTypeId, [Date]
            , Row_Number() Over ( Partition By StatusTypeId Order By [Date] Desc ) As Rnk
        From ItemVersionStatusLog As IVSL
        )
    Select IV.ItemId, M.ItemVersionId, M.[Date], M.StatusTypeId
    From MostRecentStatus As M
      Join ItemVersion As IV
        On IV.Id = M.ItemVersionId
    Where Rnk = 1
    

    SQL Fiddle Version

    A version that does not use a CTE:

    Select IV.ItemId, IVSL.ItemVersionId, IVSL.[Date], IVSL.StatusTypeId
    From ItemVersionStatusLog As IVSL
      Join (
          Select  IVSL2.StatusTypeId, Max([Date]) As [Date]
          From ItemVersionStatusLog As IVSL2
          Group By IVSL2.StatusTypeId
          ) As Z
        On Z.StatusTypeId = IVSL.StatusTypeId
          And Z.[Date] = IVSL.[Date]
      Join ItemVersion As IV
        On IV.Id = IVSL.ItemVersionId
    

    SQL Fiddle Version

    One catch with the above solution is that it does not allow for multiple entries for the same status on the same date and time. If we can assume that in the case of such a tie, that the last ItemVersionStatusLog.Id value is to be used, then we would adjust like so:

    Select IV.ItemId, IVSL.ItemVersionId, IVSL.[Date], IVSL.StatusTypeId
    From ItemVersionStatusLog As IVSL
      Join (
          Select IVSL1.StatusTypeId, IVSL1.[Date], Max(IVSL1.Id) As Id
          From ItemVersionStatusLog As IVSL1
            Join (
                Select  IVSL2.StatusTypeId, Max([Date]) As [Date]
                From ItemVersionStatusLog As IVSL2
                Group By IVSL2.StatusTypeId
                ) As Z
              On Z.StatusTypeId = IVSL1.StatusTypeId
                And Z.[Date] = IVSL1.[Date]
          Group By IVSL1.StatusTypeId, IVSL1.[Date]
          ) As MostRecentStatus
        On MostRecentStatus.Id = IVSL.Id
      Join ItemVersion As IV
        On IV.Id = IVSL.ItemVersionId
    

    SQL Fiddle Version

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

Sidebar

Related Questions

I would appreciate some help on creating the proper SQL to retrieve only one
I am needing some help with SQL syntax. Say I have a members table,
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
I need some help with SQL Query. I am trying to select all records
I'm new to SQL and could use some help in creating a database schema
I'm just starting to learn T-SQL and could use some help in understanding what's
I am new to SQL Server 2008, and I need some help in query
Need some help to solve this. I have a gridview and inside the gridview
I am not an expert on SQL, so I am asking here some help
I have the following tables customers cust_id cust_name 1 a company 2 a company

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.