473,488 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Max Row Date

Hello,
I am reposting this from another group, because I had no responses.

I need to get the max row date from the following query. There is a
date field in rcompl.date_time. There can be several rows identical
except the date_time in which I need only the max of those rows. Thanks

select rcompl.*,fastener_database.dbo.fastener_db_working .*
FROM Rcompl LEFT OUTER JOIN fastener_database.dbo.fastener_db_working
ON
substring(Rcompl.hole_name,1,len(Rcompl.hole_name)-2) =
fastener_db_working.[Serial Number]
where len(hole_name)>2 and status<>'5' and program_id in (select
program_id from nc_programs where job_id =@job_id)

Aug 23 '06 #1
5 1784

Skip wrote:
I need to get the max row date from the following query. There is a
date field in rcompl.date_time. There can be several rows identical
except the date_time in which I need only the max of those rows. Thanks

select rcompl.*,fastener_database.dbo.fastener_db_working .*
FROM Rcompl LEFT OUTER JOIN fastener_database.dbo.fastener_db_working
ON
substring(Rcompl.hole_name,1,len(Rcompl.hole_name)-2) =
fastener_db_working.[Serial Number]
where len(hole_name)>2 and status<>'5' and program_id in (select
program_id from nc_programs where job_id =@job_id)
I'm assuming here that you've got a series of fields which are common,
with a varying date field.

Try removing the * field selectors and explicitly state the fields you
need -- this is good practice anyway. Then GROUP BY the other fields
and return the max(rcompl.date_time).

So, in a more simplistic example, you might have:

SELECT Title, Author, max(LastSale) as LastSale
FROM tbl_BookSales
GROUP BY Title, Author

This might return a list of books with their most recent sale date,
from a book shop database.

Get it working with a simple table like this, before you implement it
in your more complex query above. And don't use those SELECT *s!

Aug 23 '06 #2
Bobbo,
This has helped a little. Below is a
sample of data, problem is that the 'duplicate data' is for the
hole_name field only. But I need to include all the data. I have
simplified the query for the example.

Sample Data:
id hole_name Mtime
156140 2a000020x1 2006-08-02 19:18:34.000
156141 2a000021x1 2006-08-02 19:19:45.000
156142 2a000022x1 2006-08-02 19:28:54.000 'Do not to return this
record
156143 2a000022x1 2006-08-02 19:29:18.000 'Return this one only
156144 2a000023x1 2006-08-02 19:29:53.000
This works but I cannot include any additional fields:
select hole_name,max(date_time)as Mtime
FROM Rcompl
where program_id in (select program_id from nc_programs where job_id
='156')
group by hole_name
Result:
hole_name Mtime
2a000020x1 2006-08-02 19:18:34.000
2a000021x1 2006-08-02 19:19:45.000
2a000022x1 2006-08-02 19:29:18.000
2a000023x1 2006-08-02 19:29:53.000
So how do I get this to work with all the 28 fields.
Be glad to provide more info. Thanks for the help.

Bobbo wrote:
Skip wrote:
I need to get the max row date from the following query. There is a
date field in rcompl.date_time. There can be several rows identical
except the date_time in which I need only the max of those rows. Thanks

select rcompl.*,fastener_database.dbo.fastener_db_working .*
FROM Rcompl LEFT OUTER JOIN fastener_database.dbo.fastener_db_working
ON
substring(Rcompl.hole_name,1,len(Rcompl.hole_name)-2) =
fastener_db_working.[Serial Number]
where len(hole_name)>2 and status<>'5' and program_id in (select
program_id from nc_programs where job_id =@job_id)

I'm assuming here that you've got a series of fields which are common,
with a varying date field.

Try removing the * field selectors and explicitly state the fields you
need -- this is good practice anyway. Then GROUP BY the other fields
and return the max(rcompl.date_time).

So, in a more simplistic example, you might have:

SELECT Title, Author, max(LastSale) as LastSale
FROM tbl_BookSales
GROUP BY Title, Author

This might return a list of books with their most recent sale date,
from a book shop database.

Get it working with a simple table like this, before you implement it
in your more complex query above. And don't use those SELECT *s!
Aug 23 '06 #3

Skip wrote:
Sample Data:
id hole_name Mtime
156140 2a000020x1 2006-08-02 19:18:34.000
156141 2a000021x1 2006-08-02 19:19:45.000
156142 2a000022x1 2006-08-02 19:28:54.000 'Do not to return this
record
156143 2a000022x1 2006-08-02 19:29:18.000 'Return this one only
156144 2a000023x1 2006-08-02 19:29:53.000

This works but I cannot include any additional fields:
Ah, I get you. Try doing the max() and GROUP thing as a subquery and
link it back out to your larger result set, something like this:

select r1.[id], r1.hole_name, r1.mtime, etc...
from rcompl r1
where mtime = (select max(mtime) from rcompl r2 where r1.hole_name =
r2.hole_name)

Or alternatively this (someone else might be able to advise which of
these is better):

select r1.[id], r1.hole_name, r1.mtime, etc...
from rcompl r1
join (select hole_name, max(mtime) from rcompl group by hole_name) r2
on (r1.hole_name = r2.hole_name and r1.mtime = r2.mtime)

Aug 23 '06 #4
Thanks! Works great

Bobbo wrote:
Skip wrote:
Sample Data:
id hole_name Mtime
156140 2a000020x1 2006-08-02 19:18:34.000
156141 2a000021x1 2006-08-02 19:19:45.000
156142 2a000022x1 2006-08-02 19:28:54.000 'Do not to return this
record
156143 2a000022x1 2006-08-02 19:29:18.000 'Return this one only
156144 2a000023x1 2006-08-02 19:29:53.000

This works but I cannot include any additional fields:

Ah, I get you. Try doing the max() and GROUP thing as a subquery and
link it back out to your larger result set, something like this:

select r1.[id], r1.hole_name, r1.mtime, etc...
from rcompl r1
where mtime = (select max(mtime) from rcompl r2 where r1.hole_name =
r2.hole_name)

Or alternatively this (someone else might be able to advise which of
these is better):

select r1.[id], r1.hole_name, r1.mtime, etc...
from rcompl r1
join (select hole_name, max(mtime) from rcompl group by hole_name) r2
on (r1.hole_name = r2.hole_name and r1.mtime = r2.mtime)
Aug 23 '06 #5
Hi Skip,
You may like to try this

Select * from YourTable A
Where Not Exists(Select 1 from YourTable B where A.hole_id=B.hole_id
and A.date_time>B.date_time)

Returning those rows of hole_id which have only max date_time.

I hope this helps

With Warm regards
Jatinder Singh
http://jatindersingh.blogspot.com
http://sqloracle.tripod.com

Aug 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5197
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
4
5340
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
2
4128
by: Tjerk | last post by:
Hello all, I have the script below to change an image depending on the date upto january it worked fine but then it just stopped working does anybody have an idea how I can make it work again or...
9
2620
by: Thomas R. Hummel | last post by:
Hello, I am importing data that lists rates for particular coverages for a particular period of time. Unfortunately, the data source isn't very clean. I've come up with some rules that I think...
30
3637
by: Dr John Stockton | last post by:
It has appeared that ancient sources give a method for Numeric Date Validation that involves numerous tests to determine month length; versions are often posted by incomers here. That sort of code...
3
2558
by: captain | last post by:
Below is the sql for data with same date need to extract + or - 5 days data of same date also. How to also get data of + and - days related to same date. SELECT IM.Area, IM.Location,...
1
2796
by: Liz Malcolm | last post by:
Hello and TIA. I have a DE form with an option group that if daily is selected todays date is used for start and end date, if weekly is selected Monday - Friday is used. I am trying to add a...
7
31790
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
12
29418
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
3
2337
by: JJ | last post by:
Here's the code. $link="http://xbox360cheat.org"; $close_date=$_POST; #last content change check if ($close_date == 0) $close_date = date("Y-m-d H:m:s", mktime(12, 0, 0, date("m"), date...
1
6847
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...
0
7352
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...
0
5445
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,...
1
4875
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...
0
4565
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...
0
3078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.