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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:36:42+00:00 2026-05-16T17:36:42+00:00

SQL newbie here :) Here are my tables if anyone’s interested. AHH, cant post

  • 0

SQL newbie here 🙂

Here are my tables if anyone’s interested.

AHH, cant post image yet
http://img832.imageshack.us/img832/442/72098588.jpg

What I’m trying to do is query the tblPatientStats table within a date interval (@StartDate, @EndDate)
and group them accordingly in a data grid on winforms.

So each row in tblPatientStats either have a RefDoctor or RefMode or both or none at all.

So the query should return a table with the Name of the patient from tblPatient, the RefMode from tblRefMode, the Name of the RefDoctor (Title + FirstName + lastName) and SessionDate from tblPatientStats

==> yfrog dot com/0yhi2dj

Here is my attempt so far.

INSERT @Final(Name, Doctor, Mode, SessionDate)
 SELECT DISTINCT (FirstNames + LastName) as Name, 
 (tblRefDoctor.RefDTitle + ' ' + tblRefDoctor.RefDFNames + ' ' + tblRefDoctor.RefDName) AS Doctor, 
 tblRefMode.RefMode AS Mode, SessionDate 

 FROM tblPatientStats, tblPatient
 left outer join tblRefDoctor on (RefDoctor = tblRefDoctor.RefDoctor)
 left outer join tblRefMode on (RefModeID = tblRefMode.RefModeID)
 WHERE
 tblPatientStats.RefDoctor IS NOT NULL or tblPatientStats.RefModeID IS NOT NULL
 AND 
 tblPatient.PatientID = tblPatientStats.PatientID
 AND tblPatientStats.SessionDate between @StartDate AND @EndDate 

What am I doing wrong? The query times out every single time, the tables are small, less than 10K records each.

Any help would be much appreciated.

  • 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-05-16T17:36:42+00:00Added an answer on May 16, 2026 at 5:36 pm

    I suspect the issue is because of the cartesian join on

    tblPatientStats, tblPatient
    

    Whilst there is a join condition in the where clause there is an issue with the precedence of the boolean operators. This is in order Not, And, Or so I think you need brackets around the ‘Or’ ed conditions.

    The WHERE condition on the original query with brackets applied to show the effective operator precedence is

     WHERE
     tblPatientStats.RefDoctor IS NOT NULL or 
    
    (tblPatientStats.RefModeID IS NOT NULL 
         AND tblPatient.PatientID = tblPatientStats.PatientID  
         AND tblPatientStats.SessionDate between @StartDate AND @EndDate)
    

    This is almost certainly not the desired semantics and will likely bring back too many rows.

    I’ve moved the join condition between tblPatientStats and tblPatient up into the JOIN clauses and added brackets to the Or ed conditions.

     FROM tblPatientStats
     inner join tblPatient on tblPatient.PatientID = tblPatientStats.PatientID
     left outer join tblRefDoctor on RefDoctor = tblRefDoctor.RefDoctor
     left outer join tblRefMode on RefModeID = tblRefMode.RefModeID
     WHERE
     (tblPatientStats.RefDoctor IS NOT NULL or tblPatientStats.RefModeID IS NOT NULL)
     AND tblPatientStats.SessionDate between @StartDate AND @EndDate 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is sort of SQL newbie question, I think, but here goes. I have
SQL Newbie here, and I'm having a hell of a time finding what should
Newbie SQL question here --> I've got an Occurrences table that contains a row
am a newbie to Oracle/PL SQL.I've 2 tables A and B. A has a
This is a sql newbie question. Basically, I want an extra column to return
I'm a newbie when it comes to SQL. When creating a stored procedure with
Just getting started with Linq to SQL so forgive the newbie question. I'm trying
I'm still a newbie to Adobe Air/Flex, and still fairly new with SQL. I've
I am a newbie to SQL. please help with this. I have the below
I'm a newbie with SQL... Now I want to display some instances of AddrDistances

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.