473,544 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Return Record Matching Latest Date

9 New Member
Hi there,
I have a table, which has an order number, a comment & a date the comment was added.

Therefore you could have 3 or 4 comments against one order number. All I want to do is create a table via a query which takes my existing table and shows me the latest comment based on the date. So for example, if there are two comments for an order, one done today and one yesterday I want my query to show me only the comment done today.

I thought this would be easy, but when I use the MAX on the date on the query designer I am still getting two rows of data for some orders.

I just can't seem to get my head around what I am doing wrong.
Dec 28 '11 #1
4 1696
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Please post the SQL for the query concerned - we can't answer you without having the details of the tables and fields concerned in front of us.

-Stewart
Dec 28 '11 #2
NeoPa
32,563 Recognized Expert Moderator MVP
First of all let me warn you against duplicating the pre-existing data into a separate table. This is very rarely a good idea (See Database Normalisation and Table Structures). Instead, just use the SELECT query when that specific data is required.

Next, please check the new title of the thread. This is not only a valid title, but it also gives you clues as to what you're actually trying to do. Please remember in future to use a sensible title for any new question threads.

Now let's look at your actual issue. You're not looking to return the value of the maximum date, but actually you want the record associated with that maximum. This can be done in the following format :
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM   [YourTable] AS [YTo]
  3. WHERE  [CommentDate] In(SELECT Max([CommentDate])
  4.                         FROM   [YourTable] AS [YTi]
  5.                         WHERE  YTi.OrderNo = YTo.OrderNo)
Does that make sense?
Dec 28 '11 #3
BlackEyedPea
9 New Member
Thank you very much for your help, I've tried what you said above and it worked straight away.

Thanks again.
Dec 28 '11 #4
NeoPa
32,563 Recognized Expert Moderator MVP
I'm very pleased to help, and almost more pleased even to hear that what I knocked up worked first time. Very gratifying :-)
Dec 28 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
9618
by: jas | last post by:
> This is what I want to do with the data table below. > > I only want it to show one id for each issue. ( ie, 4001 only once, > 4002 only once, etc. But only the one with the latest date and time) > > Ie. 4001 should only be in the table once, with the latest date - > which should be : > 4001 09/12/2003 17:12:09 (as I only want to show...
2
7053
by: John | last post by:
I have a query that returns this recordset: Bob, 1/1/2000 Bob, 3/1/2001 Bob, 2/4/2004 Bob, 5/9/2002 I need the query to return this instead: Bob, 2/4/2004 So, it needs to return the most recent date in records that otherwise
1
1890
by: TGEAR | last post by:
I have a column which stores dates (datetime data type). I would like to fetch one data which is the most latest date among them. So if there are 04/01/06, 05/08/06, 05/12/06, 06/15/06, then 06/15/06 is the one I need to output. how can I write this in sql stmt? select datebegin from testtable where datebegin = ?????
8
2175
by: TGEAR | last post by:
I have an ItemSTDPriceHistory table as below and this is a child table of itemlookup table with one to many relationship. if exists (select * from dbo.sysobjects where id = object_id(N'.') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table . GO
20
14214
by: keri | last post by:
Hi, I am creating a table where I want to use the date as the primary key - and to automatically create a record for each working date (eg Mon to Fri) until 30 June 2007. Is this possible? I do not want my user to have to create a record for each date. Thanks,
6
7844
by: piyushdabomb | last post by:
Hi All, Currently, I have a SQL query where I need to grab the SECOND latest date by group. COMPONENTID COMPLETIONDATE CBT_HAZCOM 02/26/2007 15:17:00 CBT_HAZCOM 07/31/2007 21:23:00 QS 02/12/2007 13:51:00 SHEN158 02/12/2007 13:29:00
4
4692
by: ringer | last post by:
I have a db where each record is a certain task to be done. Each record includes the date the task should be completed. Some of the tasks are one time only but some are recurring. I have been having to create a new record for every task even the recurring ones, but I'd like to try to automate creation of the recurring tasks. What I'm trying to...
8
2099
by: helpmeplease213 | last post by:
Hello, I have a table that has five categories: Unit No, Date, P1, P2, P3. I have a query that only extracts data from the table that is over 40 for P1, P2 or P3 but I only want the data with the latest date for that particular Unit No to come up. Any Ideas?
2
7752
by: selvialagar | last post by:
i have a date field in a table..i want to retrieve the data with latest date...is there any query for that.....
1
2328
by: SunshineInTheRain | last post by:
I have a log table. I want to get record of latest date. Meaning if 1-May-2008 13:32:22 and 1-May-2008 2:22:21, it will retrieve 1-May-2008 13:32:22 as a result. how the statement should be? thaanks in advanced.
0
7447
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7792
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7400
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7732
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5947
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5314
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4935
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
684
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.