473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Problem with select criteria with conditions

BACKGROUND
I was trying the figure out a query for my sisters optometrist office. I have two tables, one that contains customers demographics and another table that contains patient's examination.

The two tables are called Customers and Examination.
Customers contains 2494 records and Examination contains 3496 records.

Not all customes have examinations because some customers may come in to purchase glasses. The extra records in Examination are for customers that are repeat customers who come in for an exam every year.

Table: Customers
Column Name: Cust_ID, Cust_First, Cust_Last

Table: Examination
Column Name: Exam_ID, Exam_CustID, Exam_LastExam

NOW THE QUESTION REGARDING SQL
I want to create a view and select all 2494 records from Customers and check if the customer ID exists in Examination.
If it does exists, I want to choose the most recent record from Examination corresponding to that customer.
If it does not exists, I want the value to be NULL.

Current my code is:

Expand|Select|Wrap|Line Numbers
  1. SELECT     TOP (100) PERCENT business.person.pers_key, business.person.pers_firstname, business.person.pers_lastname, MAX(exam.examination.exam_date) AS Expr1
  2. FROM         business.person INNER JOIN exam.examination ON business.person.pers_key = exam.examination.exam_patientkey
  3. GROUP BY business.person.pers_key, business.person.pers_firstname, business.person.pers_lastname, business.person.pers_key
  4. ORDER BY business.person.pers_key
  5.  
When I run the query, I get 2098 records instead of 2494 records. It seems that if the customer does not have an examination, it does not return the record.

Any suggestions?

Thanks in advance
Dec 27 '07 #1
1 1447
ck9663
2,878 Expert 2GB
BACKGROUND
I was trying the figure out a query for my sisters optometrist office. I have two tables, one that contains customers demographics and another table that contains patient's examination.

The two tables are called Customers and Examination.
Customers contains 2494 records and Examination contains 3496 records.

Not all customes have examinations because some customers may come in to purchase glasses. The extra records in Examination are for customers that are repeat customers who come in for an exam every year.

Table: Customers
Column Name: Cust_ID, Cust_First, Cust_Last

Table: Examination
Column Name: Exam_ID, Exam_CustID, Exam_LastExam

NOW THE QUESTION REGARDING SQL
I want to create a view and select all 2494 records from Customers and check if the customer ID exists in Examination.
If it does exists, I want to choose the most recent record from Examination corresponding to that customer.
If it does not exists, I want the value to be NULL.

Current my code is:

Expand|Select|Wrap|Line Numbers
  1. SELECT     TOP (100) PERCENT business.person.pers_key, business.person.pers_firstname, business.person.pers_lastname, MAX(exam.examination.exam_date) AS Expr1
  2. FROM         business.person INNER JOIN exam.examination ON business.person.pers_key = exam.examination.exam_patientkey
  3. GROUP BY business.person.pers_key, business.person.pers_firstname, business.person.pers_lastname, business.person.pers_key
  4. ORDER BY business.person.pers_key
  5.  
When I run the query, I get 2098 records instead of 2494 records. It seems that if the customer does not have an examination, it does not return the record.

Any suggestions?

Thanks in advance

"INNER JOIN" returns record that are existing on the two tables being "joined". you might want to try "LEFT JOIN"
Dec 28 '07 #2

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

Similar topics

18
by: CJM | last post by:
I'm building a search function for one of my applications. The user has the option to enter a number criteria of criteria, but none are compulsary. I need to be able to build up a query string that...
1
by: antao | last post by:
I'm keeping in the database a log of all the sessions for my application. I'm trying to write a stored procedure that returns all the sessions that; the login contains a certain string, loggedin...
6
by: HKM | last post by:
Hello, I have a query engine that builds the SQL Query to obtain the recordSet. Following is an Exmaple Query that my QueryBuilder outputted SELECT * FROM BookInfo WHERE BookName LIKE...
10
by: MLH | last post by:
Suppose the following... Dim A as Date A=#7/24/2005# I wish to compare value of A against 2 other values: 1) 8/1/2005 2) 9/1/2005 Which is better and why... First:
2
by: Anandan | last post by:
Hi, In our Project we use Dataset to load the Grid with Values. We have some criteria to filter the values to be shown in the Grid. For that we used the SELECT command to filter the Same...
4
by: Aryan | last post by:
Hi, I am having problem with DataTable.Select() method. I am using ASP.NET 2.0. I have DataSet which reads data from XML file using DataSet.ReadXML(). Now this dataset has various datatable,...
2
by: Ravi Joshi | last post by:
The menu on my site works fine in IE6 and Firefox. In IE7, there is a problem with the menu: when you mouse over the various main catagories, the sub-catagories all appear to the right as they...
20
by: anthonyk | last post by:
Hi there, im hoping someone might be able to painlessly tell me what im incorrectly doing here/expecting. Im using Access 2007 I am creating a query to show ongoing tasks in a database. in...
1
by: Constantine AI | last post by:
Hi i am trying to get User input if data does not exist within a DLOOKUP table. I have gotten it to work for one record but not multiple, i have tried to incorporate my code into a loop procedure but...
2
by: rleepac | last post by:
I have a form with a tabbed control on it. I am trying to set certain criteria for some of the tabs to show only when info from other fields meet criteria. Specifically, I have two fields. Both...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.