Solved

Sql question

Posted on 2014-12-18
7
128 Views
Last Modified: 2014-12-18
Hi Experts,

I have a table containing:

ID
PatientID
DateStarted
Subject

How do I get the ID of the latest day per patient, per subject?
I would prefer to have this in one query/view.
0
Comment
Question by:bfuchs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 22

Expert Comment

by:Flyster
ID: 40508406
Try this:

SELECT tblYourTable.[PatientID], Last(tblYourTable.[DateStarted]) AS LastOfDateStarted, Last(tblYourTable.[Subject]) AS LastOfSubject
FROM tblYourTable
GROUP BY tblYourTable.[PatientID];

Open in new window


Flyster
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40508432
try this query


SELECT A.*
 FROM TableX AS A
 INNER JOIN
 (SELECT PatientID, Max([DateStarted]) AS MaxOfDate, Subject
      FROM TableX  GROUP BY PatientID, Subject)  AS B
      ON A.[DateStarted]=B.MaxOfDate AND A.PatientID=B.PatientID AND A.Subject=B.Subject
0
 
LVL 4

Author Comment

by:bfuchs
ID: 40508484
@Ray,
It works great,
Just one question, if I want to exclude a subject where do I put it?

@Flyster,
I cant see how your example would work in my case, I need the ID of the record containing the highest date of patient per subject.

Thanks,
Ben
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 22

Expert Comment

by:Flyster
ID: 40508496
Sorry, missed that part. How about this:
SELECT Last(tblYourTable.ID) AS LastOfID, tblYourTable.PatientID, Last(tblYourTable.DateStarted) AS LastOfDateStarted, Last(tblYourTable.Subject) AS LastOfSubject
FROM tblYourTable
GROUP BY tblYourTable.PatientID

Open in new window


To exclude a particular subject, see the last line:
SELECT Last(tblYourTable.ID) AS LastOfID, tblYourTable.PatientID, Last(tblYourTable.DateStarted) AS LastOfDateStarted, Last(tblYourTable.Subject) AS LastOfSubject
FROM tblYourTable
GROUP BY tblYourTable.PatientID
HAVING (((Last(tblYourTable.Subject))<>"Enter_Your_Subject_Here"));

Open in new window

0
 
LVL 4

Author Comment

by:bfuchs
ID: 40508506
@Flyster,

Last(tblYourTable.ID) AS LastOfID..Group by PatientID would give me the highest ID per patient, not necessary the ID that belongs to the records containing the highest date.

Re excluding one subject, can you tell me how to modify Ray's suggestion to include this portion?

Thanks,
Ben
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 40508507
change "What Subject" with the subject you want to exclude


SELECT A.*
 FROM TableX AS A
 INNER JOIN
 (SELECT PatientID, Max([DateStarted]) AS MaxOfDate, Subject
      FROM TableX  
      Where Subject <> "What Subject"
      GROUP BY PatientID, Subject)  AS B
      ON A.[DateStarted]=B.MaxOfDate AND A.PatientID=B.PatientID AND A.Subject=B.Subject
0
 
LVL 4

Author Closing Comment

by:bfuchs
ID: 40508562
Excellent, Thank You!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question