473,394 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Help with an MS SQL Server Query to return the latest dates against each recordID.

The following SQL query :-

SELECT CardHolder.RecordID, History.GenTime, History.Link1
FROM History FULL OUTER JOIN
Card ON History.Param3 =
LTRIM(RTRIM(Card.CardNumber)) FULL OUTER JOIN
CardHolder ON Card.CardHolderID =
CardHolder.RecordID
WHERE (Card.Deleted = 0) AND (History.GenTime IS NOT NULL)
ORDER BY CardHolder.RecordID, History.GenTime DESC

returns :-

RecordID GenTime Link1

2 04/06/2004 15:30:00 123
2 01/06/2004 16:00:00 123
2 01/06/2004 08:00:00 110
1155 02/06/2004 11:30:00 123
1155 02/06/2004 08:00:00 110
3925 03/06/2004 09:00:00 123
3925 03/06/2004 08:00:00 110
4511 06/06/2004 11:30:00 123
4511 06/06/2004 10:30:00 110

Is there a way of modifying this query to just return the lastest
genTime for each RecordID ??? ie return just rows 1,4,6 & 8.

I assume it is something to do with MAX, but I can't seem to get my
head around it.

Any help, or pointers would be appreciated.

Oh, running query on Microsoft SQL Server 2000.

Regards
Dave
Jul 20 '05 #1
3 1645
Here's one possible method (untested). Note that I've changed the first of
your joins to INNER because your WHERE clause makes the FULL JOIN redundant.

SELECT CH.recordid, H.gentime, H.link1
FROM History AS H
JOIN Card AS C
ON H.param3 = LTRIM(RTRIM(C.cardnumber))
AND H.gentime =
(SELECT MAX(gentime)
FROM History
WHERE param3 = H.param3)
FULL OUTER JOIN CardHolder AS CH
ON C.cardholderid = CH.recordid
WHERE C.deleted = 0
AND H.gentime IS NOT NULL
ORDER BY cardholder.recordid, H.gentime DESC

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2


David,

Many many thanks for you help - that did the trick.

Simple now that I've seen how you've approached it.

Regards,

Dave

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
Hi

Having Card Holder in this seems to be redundant!

This will also assume that H.GenTime is unique..

SELECT C.CardHolderID, H.GenTime, H.Link1
FROM History H
FULL OUTER JOIN Card C ON H.Param3 = LTRIM(RTRIM(C.CardNumber))
WHERE C.Deleted = 0
AND H.GenTime IS NOT NULL
AND H.GenTime = ( SELECT MAX(I.GenTime) FROM History I WHERE I.Param3 =
H.Param3 )
ORDER BY C.CardHolderID, H.GenTime DESC

John

"DaveHayzen" <dh*****@ersltd.co.uk> wrote in message
news:f6**************************@posting.google.c om...
The following SQL query :-

SELECT CardHolder.RecordID, History.GenTime, History.Link1
FROM History FULL OUTER JOIN
Card ON History.Param3 =
LTRIM(RTRIM(Card.CardNumber)) FULL OUTER JOIN
CardHolder ON Card.CardHolderID =
CardHolder.RecordID
WHERE (Card.Deleted = 0) AND (History.GenTime IS NOT NULL)
ORDER BY CardHolder.RecordID, History.GenTime DESC

returns :-

RecordID GenTime Link1

2 04/06/2004 15:30:00 123
2 01/06/2004 16:00:00 123
2 01/06/2004 08:00:00 110
1155 02/06/2004 11:30:00 123
1155 02/06/2004 08:00:00 110
3925 03/06/2004 09:00:00 123
3925 03/06/2004 08:00:00 110
4511 06/06/2004 11:30:00 123
4511 06/06/2004 10:30:00 110

Is there a way of modifying this query to just return the lastest
genTime for each RecordID ??? ie return just rows 1,4,6 & 8.

I assume it is something to do with MAX, but I can't seem to get my
head around it.

Any help, or pointers would be appreciated.

Oh, running query on Microsoft SQL Server 2000.

Regards
Dave

Jul 20 '05 #4

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

Similar topics

12
by: Massa | last post by:
Hi all, I need some help on this one: I have a webpage with a form. This form submits data that goes to a mdb file (id (AutoNumber), name, email contry etc.). I would like to make another page...
6
by: paii | last post by:
I have a table that stores job milestone dates. The 2 milestones I am interested in are "Ship Date" TypeID 1 and "Revised Ship Date" TypeID 18. All jobs have TypeID 1 only some jobs have TypeID 18....
6
by: Megan | last post by:
Hi everybody- I'm trying to use a checkbox to control whether or not a date field in a query "Is Null" or "Is Not Null." I have 2 date fields: InDate and OutDate. If there is an OutDate, then...
2
by: Lior | last post by:
Hi, I have an ASP.NET website that crashes under heavy load. I use a SQL Server DB. I get around 5500 hits per day. I keep getting the timeout expieried connection pool error. Sometimes it even...
4
by: Will Chamberlain | last post by:
I am working on queries for a SQL database. I have much experience with Access and am trying to ween myself off of it. The query I am working on now is supposed to return a set of data like the...
1
by: uspensky | last post by:
kind of a tough one i think but any help is appreciated. please try to stay away from T-SQL... I have a table with products and closing dates for each of 4 quarters and annual for several years...
5
by: Darin L. Miller | last post by:
I'm not too good with advanced SQL queries, so please bear with me on this. I have a query with multiple joins that I am trying to get just the last 10 of each unique record (RecordID)...
0
by: georges the man | last post by:
The purpose: • Sorting and Searching • Numerical Analysis Design Specification You are to write a program called “StockAnalyser”. Your program will read a text file that contains historical...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.