473,769 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Return the last row only

1 New Member
Hello!

I'm new to SQL and i've searched everywhere for the answer to this question but I can't seem to find the anwer anywhere. My question is.....

If there are multiple rows for a specific filed, is there a way in SQL to only return the last row based on that specific field.

For example, I wrote a query that returns a list of patients who have a specific lab type. If a patient has had several labs taken, they show up in the result set several times (for each lab date). I only want to return the most recent lab date.

Here is my query........

SELECT id, resulted_test_d esc, result_value, units, update_date

FROM dbo.lab_result LEFT OUTER JOIN
dbo.mpi ON dbo.lab_result. blind_key = dbo.mpi.chart LEFT OUTER JOIN
dbo.mpi_xref ON dbo.mpi.chart = dbo.mpi_xref.bl ind_key

WHERE resulted_test_d esc = 'Hemoglobin'

Any help would be greatly appreciated.

Thanks!

Amedeo
Jul 5 '07 #1
1 3841
ck9663
2,878 Recognized Expert Specialist
Hello!

I'm new to SQL and i've searched everywhere for the answer to this question but I can't seem to find the anwer anywhere. My question is.....

If there are multiple rows for a specific filed, is there a way in SQL to only return the last row based on that specific field.

For example, I wrote a query that returns a list of patients who have a specific lab type. If a patient has had several labs taken, they show up in the result set several times (for each lab date). I only want to return the most recent lab date.

Here is my query........

SELECT id, resulted_test_d esc, result_value, units, update_date

FROM dbo.lab_result LEFT OUTER JOIN
dbo.mpi ON dbo.lab_result. blind_key = dbo.mpi.chart LEFT OUTER JOIN
dbo.mpi_xref ON dbo.mpi.chart = dbo.mpi_xref.bl ind_key

WHERE resulted_test_d esc = 'Hemoglobin'

Any help would be greatly appreciated.

Thanks!

Amedeo

sort it in descending order based on your criteria then try this query:

select top 1 from....
order by [criteria] desc
Jul 5 '07 #2

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

Similar topics

10
3099
by: Don | last post by:
I want the server-side php script to return a browser page that is essentially a copy of the original client page that contained the <form> which referenced the php script in the first place. However, the php script will need to change a couple html lines in that returned page. I sure could you some help on how to go about this. Thanks in advance, Don ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
8
4812
by: Adrian Parker | last post by:
Hi. I would like to query a database, given several where clauses to refine my search, and return the value of one single field in the database. eg: I have a table that lists teachers. Their first name, last name, age, unique teacher number, etc is in the file. I want to return the unique teacher number, for example, of the teacher whose first name is Jane and last name is Doe. How does one do this?
94
13908
by: John Bailo | last post by:
The c# *return* statement has been bothering me the past few months. I don't like the fact that you can have different code paths in a method and have multiple return statements. To me, it would be more orthogonal if a method could only have one return statement. --
2
3419
by: M.Stanley | last post by:
Hi, I have a problem..I'm doing a specific query where I'm joining fields from a table with appednded data (there are duplicate records, except for the date/time), and another query. I want the results of the new query to return ALL of the matches, and only the latest record when it finds duplicates. Performing a return top values doesn't work because it doesn't return all of the matchs, using LAST in the date field criteria only...
12
4131
by: Jose Fernandez | last post by:
Hello. I'm building a web service and I get this error. NEWS.News.CoverNews(string)': not all code paths return a value This is the WebMethod public SqlDataReader CoverNews(string Sport) {
15
25904
by: Andrew Brampton | last post by:
Hi, This may sound a odd question, but I wanted to know how you return a list of data from a function. These are some of the ways I know how, and I was wondering which method you normally use. This is more of a best practices question rather than a technical one. 1) Return a list instance ie std::list myFunction() { std::list list(); return list;
11
2773
by: randomtalk | last post by:
hi, i have the following recursive function (simplified to demonstrate the problem): >>> def reTest(bool): .... result = .... if not bool: .... reTest(True) .... else: .... print "YAHHH" .... result =
2
1626
by: wevans | last post by:
Somehow I need to only return 1 record if there's two or more in the tables. I'm using this to create a mailing list labels so I don't want to print two or more labels for the same person. I want to find duplicate First and Last Name. Like Tim Wolf appears twice. I just need one label for him. But I need to print a label for Tina Wolf, and so on. Here's my code so far, only reason I have code is cause access did it for me. I couldn't type...
4
3936
by: z060053 | last post by:
#include <iostream> using namespace std; template <class T>class list{ protected: typedef struct node{ node* prev; T* data; node* next; }; node* first;
0
9589
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10049
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3565
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.