Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8664785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:19:36+00:00 2026-06-12T17:19:36+00:00

For me the scenario is hard to explain, but I would like to get

  • 0

For me the scenario is hard to explain, but I would like to get a list of users (actually “patients”) which are ready for a dismissal meeting.

THE SITUATION

I have a patient table:

PATIENT
id   (int)
name (varchar)

The patient follows a special process, where I have defined phases:

PHASE
id   (int)
name (varchar)

And I log all the phases of the patient:

STATUS
id         (int)
phase_id   (int)
patient_id (int)
timestamp  (datetime)

The term “ready for a meeting” means the meeting is planned. Such planned meetings are logged in a message table. The message for a planned meeting has a type “meeting_planned” and for example when the meeting is done, there is a new message “meeting_done”.

MESSAGE
id         (int)
patient_id (int)
type       (enum)
value      (varchar)
timestamp  (datetime)

MY QUERY

I am looking for a query now which I really cannot come up with. My logic is:

  1. Select all patients;
  2. Where the last status (based on timestamp) is from phase name “C” or “D”;
  3. Where there is no message with the type meeting_planned.

Ad 2: the patient can be either in one of the two phases. When a patient is in phase A, B, E or F then a meeting cannot be planned. A nurse may decide to plan the meeting already when patient is in phase C, but if that did not happened, it will occur at phase D (D always follows C, it is A>B>C>D>E>F).

Ad 3: there might be other messages, but never with this type. There is a unique constaint here on type+patient_id. If a meeting has been planned and has happened, there are two messages (type 1 = meeting_planned, type 2 = meeting_done). If there hasn’t been a meeting planned, the type meeting_planned does not occur, but other messages might be set already.

WHAT HAVE I TRIED

I came up with this query now:

SELECT p.id, p.name, ph.name

FROM patient p

LEFT JOIN status s  ON p.id=s.patient_id
LEFT JOIN phase ph  ON ph.id=s.phase_id
LEFT JOIN message m ON p.id=m.patient_id

WHERE (ph.name='C' OR ph.name='D')
AND (ph.name != 'E')
AND (m.type != 'meeting_planned' OR m.type IS NULL)

MY QUESTIONS

In above query I have two things I cannot manage:

  1. I have patients double in the list. Any patient in phase D also occurs as a patient in phase C (so double). I tried a SELECT DISTINCT but that didn’t work out
  2. I now exclude phases based on their name, but I would like to order phases on the timestamp from the status and only look at the last status from there

Ad 1: it looks now like this:

ID  NAME  PHASE
1   John  C
2   Jane  C
3   Tom   C
4   Pete  C
5   Anna  C
1   John  D
2   Jane  D
3   Tom   D

And obviously I want this:

ID  NAME  PHASE
4   Pete  C
5   Anna  C
1   John  D
2   Jane  D
3   Tom   D

I now I cannot use GROUP BY here because I then have to use a COUNT or similar like grouping functions.

Is there any SQL guru here to help me out?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:19:37+00:00Added an answer on June 12, 2026 at 5:19 pm
    SELECT p.id, p.name, max(ph.name) as phase
    
    FROM patient p
    
        LEFT JOIN status s  ON p.id=s.patient_id
        LEFT JOIN phase ph  ON ph.id=s.phase_id
        LEFT JOIN message m ON p.id=m.patient_id
    
    WHERE (ph.name='C' OR ph.name='D')
        AND (m.type != 'meeting_planned' OR m.type IS NULL)
        AND p.id not in (
            select p.id
            from 
                patient p
                inner join
                status s on p.id = s.patient_id
                inner join
                phase ph on ph.id = s.phase_id
            where ph.name > 'D'
            ) s
    group by p.id, p.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm finding it hard to get around my head how the specific scenario would
I have a scenario where I would like to filter some entity sets at
Actually it's quite hard to describe: I want to implement an algorithm which compares
I know this sounds backwards but I would like to be able to determine
It's hard to explain this but I've noticed that other apps have this so
Its hard to explain in word what I'm after but hopefully the code example
I have a scenario where I would like to add endpoints dynamically. I know
The problem I am having is a little hard to explain, but I will
Here is a hard one I have been trying to get it work. It
I was thinking about a typical problem that is very JIT-able, but hard to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.