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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:46:03+00:00 2026-05-13T23:46:03+00:00

Alright, this problem is a little complicated, so bear with me. I have a

  • 0

Alright, this problem is a little complicated, so bear with me.

I have a table full of data. One of the table columns is an EntryDate. There can be multiple entries per day. However, I want to select all rows that are the latest entry on their respective days, and I want to select all the columns of said table.

One of the columns is a unique identifier column, but it is not the primary key (I have no idea why it’s there; this is a pretty old system). For purposes of demonstration, say the table looks like this:

create table ExampleTable (
    ID int identity(1,1) not null,
    PersonID int not null,
    StoreID int not null,
    Data1 int not null,
    Data2 int not null,
    EntryDate datetime not null
)

The primary key is on PersonID and StoreID, which logically defines uniqueness.

Now, like I said, I want to select all the rows that are the latest entries on that particular day (for each Person-Store combination). This is pretty easy:

--Figure 1
select PersonID, StoreID, max(EntryDate)
from ExampleTable
group by PersonID, StoreID, dbo.dayof(EntryDate)

Where dbo.dayof() is a simple function that strips the time component from a datetime. However, doing this loses the rest of the columns! I can’t simply include the other columns, because then I’d have to group by them, which would produce the wrong results (especially since ID is unique).

I have found a dirty hack that will do what I want, but there must be a better way — here’s my current solution:

select
    cast(null as int) as ID,
    PersonID,
    StoreID,
    cast(null as int) as Data1,
    cast(null as int) as Data2,
    max(EntryDate) as EntryDate
into #StagingTable
from ExampleTable
group by PersonID, StoreID, dbo.dayof(EntryDate)

update Target set
    ID = Source.ID,
    Data1 = Source.Data1,
    Data2 = Source.Data2,
from #StagingTable as Target
inner join ExampleTable as Source
    on Source.PersonID = Target.PersonID
   and Source.StoreID = Target.StoreID
   and Source.EntryDate = Target.EntryDate

This gets me the correct data in #StagingTable but, well, look at it! Creating a table with null values, then doing an update to get the values back — surely there’s a better way to do this? A single statement that will get me all the values the first time?

It is my belief that the correct join on that original select (Figure 1) would do the trick, like a self-join or something… but how do you do that with the group by clause? I cannot find the right syntax to make the query execute.

I am pretty new with SQL, so it’s likely that I’m missing something obvious. Any suggestions?

(Working in T-SQL, if it makes any difference)

  • 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-13T23:46:04+00:00Added an answer on May 13, 2026 at 11:46 pm

    There is no really “elegant” way. When you have group Group By queries like this, you’re going to have either sub-queries or temporary tables.

    This will work:

    Select ID, A.PersonID, A.StoreID, Data1, Data2, A.EntryDate
    From ExampleTable As A
    Inner Join
        (select PersonID, StoreID, max(EntryDate) As EntryDate
        from ExampleTable
        group by PersonID, StoreID, dbo.dayof(EntryDate)) As B
      On ExampleTable.PersonID = B.PersonID 
        And ExampleTable.StoreID = B.StoreID 
        And ExampleTable.EntryDate = B.EntryDate
    

    You should not be too down on the solution you came up with though. Using temporary tables never looks elegant, but it is efficient; I would not be surprised if your original two-step solution is actually faster than my one-step solution. (you’ll have to test to know for sure.)

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

Sidebar

Related Questions

Alright guys, I'm sure there is a simple solution to this problem. I have
Alright, this one (3a; sample problem with provided answer) has got me scratching my
Alright, some of you might have noticed I've been working on this problem off
Alright so I have this C++ image capturing class. I was wondering if I
Alright. I have a query that looks like this: SELECT SUM(`order_items`.`quantity`) as `count`, `menu_items`.`name`
Alright, so I have a query that looks like this: SELECT `orders`.*, GROUP_CONCAT( CONCAT(
Alright this probably is the worst error I have found ever. I have two
Alright guys, I really hurt my brain over this one and I'm curious if
Alright I'm a bit baffled by this one. Changing an unrelated int property on
Alright well I recently got into normalizing my database for this little side project

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.