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 8112249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:25:28+00:00 2026-06-06T02:25:28+00:00

Firstly, my SQL knowledge is little rusty. I am trying to generate a report

  • 0

Firstly, my SQL knowledge is little rusty. I am trying to generate a report of reviews each patient has gone through for a time period. A review is done as part of a Doctors’ round. The following are the corresponding tables with relevant columns:

Patients: (id, name)

Rounds: (id, patient_id, date)

Reviews: (id, round_id, review)

The report should look like the following:

Patient     |     Reviews
_________________________

Patient 1   |   2
_________________________ 
Patient 2   |   1
_________________________ 
Patient 3   |   0
_________________________

I tried the following SQL statement:

SELECT 
    p.name as patient, 
    COUNT(r.round_id) as reviews
FROM 
    patients as p
    JOIN rounds as ro ON p.id = ro.patient_id
    JOIN reviews as r ON ro.id = r.round_id
WHERE 
    r.review_date between '2012-02-01' AND '2012-02-29'
GROUP BY 
    p.name

But, the above query only returns rows where reviews count is > 1. I want it to return even if the count is 0.

  • 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-06T02:25:30+00:00Added an answer on June 6, 2026 at 2:25 am

    The simplest way to Join the tables, and to include instances where there is no match in one of those tables, is to use a LEFT OUTER JOIN. This will match all records to the left, regardless of whether a match was found on the right side of the JOIN.

    Since your r.review_date is in your WHERE clause, no matches can occur unless there is a review between those dates. So to include instances where there is no review, you must allow for that in your WHERE clause by adding “OR r.review_date IS NULL” as below. You may also want to consider filtering on the round.date field instead, so that you are only looking at instances where there were valid rounds performed within that time frame. ie. “WHERE ro.date between ‘2012-02-01’ AND ‘2012-02-29′”

    eg.

    SELECT
        p.name as patient, 
    COUNT(r.round_id) as reviews
    FROM 
        patients as p
        JOIN rounds as ro ON p.id = ro.patient_id
        LEFT OUTER JOIN reviews as r ON ro.id = r.round_id
    WHERE 
        r.review_date between '2012-02-01' AND '2012-02-29' OR r.review_date IS NULL
    GROUP BY 
        p.name
    

    Note: If you want to report records without any rounds, you will also have to make the first JOIN a LEFT OUTER JOIN as well.

    FROM 
        patients as p
        LEFT OUTER JOIN rounds as ro ON p.id = ro.patient_id
        LEFT OUTER JOIN reviews as r ON ro.id = r.round_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to commit a transaction to my Sql Server 2008 database - firstly
Firstly, I should point out I don't have much knowledge on SQL Server indexes.
Firstly, let me start by saying that I haven't ever directly used SQL views.
This is a bit of a two part question, firstly the SQL string Dim
Firstly anyone else trying to get onto forum.hibernate.org. I have been trying for a
Firstly I'm a complete newbie when it comes to SQL Server. I have five
Firstly, I'm rather new to SQL and I've run into a roadblock. I'm using
Firstly, let me say that I know that http://sphinxsearch.com/docs/current.html#conf-sql-attr-string says strings can only be
Firstly, I've read through the posts with matching names, and I've tried integrating some
I have a database table which has three relevant fields, firstly a user id,

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.