Solved

Query multiple tables (joins) for entries, but detecting missing entries

Posted on 2014-09-05
7
135 Views
Last Modified: 2014-09-05
I have a reporting system which our students are required to utilize.  I need to run a report off the database (which I developed) that links to an external database (our Student Information System).

My database doesn't use names, just student IDs.  I need to query the SIS for the student's first and last name.  Below is the query I am using -- which is working to give me all of the report entries and whether or not they were on-time or late.  What I am not getting are the missing entries.  We need to no if a student did not report at all for that week.  I feel like I'm close, but I'm not sure what I'm doing wrong.  I will include some sample data and the SQL query.

Query
WITH students (StudentID) AS
(
	SELECT n.soc_sec AS [StudentID]
	FROM [LINKEDSERVER].[SIS].[dbo].[name] n INNER JOIN [LINKEDSERVER].[SIS].[dbo].[nmcrs] c
		ON n.soc_sec = c.soc_sec
	WHERE c.course='REQU0007' AND c.enrollcode='EN' AND c.sch_yr='201415' AND c.semester='1'
),

reportstd (Report, ReportPeriod, LateOn) AS
(
	SELECT CONVERT(varchar(10),s.reportOpen,102) AS [Report],
		   CONVERT(varchar(10),s.reportBegin,102) + ' - ' + 
	       CONVERT(varchar(10),s.reportEnd,102) AS [ReportPeriod],
		   s.lateOn AS [LateOn]
	FROM reports s
	WHERE s.reportOpen BETWEEN '08/01/2014' AND GETDATE()
)

SELECT nm.soc_sec AS [StudentID],
	   nm.last_name AS [LastName],
	   nm.first_name AS [FirstName],
	   s.ReportPeriod,
	   s.Report,
	   [Status] = CASE
			WHEN r.submitted > s.LateOn THEN 'LATE'
			WHEN r.submitted < s.LateOn THEN 'ON TIME'
			WHEN r.submitted IS NULL THEN 'NO REPORT'
			END
FROM [LINKEDSERVER].[SIS].[dbo].[name] nm LEFT JOIN Reported r
	ON nm.soc_sec = r.student LEFT JOIN reportstd s
	ON r.csgTerm = s.Report
WHERE (r.student IN (SELECT StudentID FROM students) OR
	  r.student NOT IN (SELECT StudentID FROM students)) AND
	  ReportPeriod IS NOT NULL AND
	  Report IS NOT NULL
ORDER BY LastName, FirstName, s.Report

Open in new window


Sample Data
name.txt
reports.txt
reportstd.txt
students.txt
reported.txt
0
Comment
Question by:Shane Kahkola
[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
  • 4
  • 3
7 Comments
 
LVL 11

Expert Comment

by:Ray
ID: 40306301
Your Inner Join in the 'with' section is eliminating those students who did not report at all.  Change it to a left join to get all student records.

This section MAY also be eliminating non reporters.
ReportPeriod IS NOT NULL AND
        Report IS NOT NULL


Indispensable tool for understanding joins . . .
http://www.codeproject.com/KB/database/Visual_SQL_Joins/Visual_SQL_JOINS_orig.jpg
0
 
LVL 11

Accepted Solution

by:
Ray earned 500 total points
ID: 40306308
This might be a silly question, but why are you doing this part (pasted below)?  It is effectively a non condition since it allows for all r.students whether they have a student ID or not.  If you are building this in some other application and need a 'placeholder' where you may or may not add 'and's, might I suggest replacing that piece with "Where 1=1"


WHERE (r.student IN (SELECT StudentID FROM students) OR
        r.student NOT IN (SELECT StudentID FROM students))
0
 
LVL 3

Author Comment

by:Shane Kahkola
ID: 40306314
Thank you, Ray, I will try your suggestions, and read the chart you posted.

The reason I did that last part was a desperation attempt to get all the records.  I really could not reiterate to you why I did it -- mainly because it doesn't make sense to me any more.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:Shane Kahkola
ID: 40306595
I've requested that this question be closed as follows:

Accepted answer: 0 points for ccbbc_cs's comment #a40306314

for the following reason:

Ray, you were a God-send.

The chart and the questions led me in the right direction.  I finally did this, and got what I was looking for:

WITH students (StudentID,LastName,FirstName) AS
(
	SELECT n.soc_sec AS [StudentID],n.last_name AS [LastName],n.first_name AS [FirstName]
	FROM [LINKEDSERVER].[SIS].[dbo].[name] n INNER JOIN [LINKEDSERVER].[SIS].[dbo].[nmcrs] c
		ON n.soc_sec = c.soc_sec
	WHERE c.course='REQU0007' AND c.enrollcode='EN' AND c.sch_yr='201415' AND c.semester='1'
),

reportstd (Report, ReportPeriod, LateOn,ID) AS
(
	SELECT CONVERT(varchar(10),s.reportOpen,102) AS [Report],
		   CONVERT(varchar(10),s.reportBegin,102) + ' - ' + 
	       CONVERT(varchar(10),s.reportEnd,102) AS [ReportPeriod],
		   s.lateOn AS [LateOn],s.ID
	FROM reports s
	WHERE s.reportOpen BETWEEN '08/01/2014' AND GETDATE()
)

SELECT DISTINCT s.StudentID,
	   s.LastName,
	   s.FirstName,
	   [ReportPeriod] = CASE
				WHEN rs.ReportPeriod IS NULL THEN 'NEVER REPORTED'
				WHEN rs.ReportPeriod IS NOT NULL THEN rs.ReportPeriod
			     END,
	   [Report] = CASE
			WHEN rs.Report IS NULL THEN 'NEVER REPORTED'
			WHEN rs.Report IS NOT NULL THEN rs.Report
		       END,
	   rs.ID,
	   [Status] = CASE
			WHEN r.submitted > rs.LateOn THEN 'LATE'
			WHEN r.submitted < rs.LateOn THEN 'ON TIME'
			ELSE 'NO REPORT'
		      END
FROM reportstd rs LEFT JOIN Reported r
	ON rs.Report = r.csgTerm RIGHT JOIN students s 
	ON r.student = s.StudentID
ORDER BY LastName, FirstName, Report

Open in new window


This gave me the results I was looking to see.  Thanks again.
0
 
LVL 3

Author Closing Comment

by:Shane Kahkola
ID: 40306596
Ray, you were a God-send.

The chart and the questions led me in the right direction.  I finally did this, and got what I was looking for:

WITH students (StudentID,LastName,FirstName) AS
(
	SELECT n.soc_sec AS [StudentID],n.last_name AS [LastName],n.first_name AS [FirstName]
	FROM [LINKEDSERVER].[SIS].[dbo].[name] n INNER JOIN [LINKEDSERVER].[SIS].[dbo].[nmcrs] c
		ON n.soc_sec = c.soc_sec
	WHERE c.course='REQU0007' AND c.enrollcode='EN' AND c.sch_yr='201415' AND c.semester='1'
),

reportstd (Report, ReportPeriod, LateOn,ID) AS
(
	SELECT CONVERT(varchar(10),s.reportOpen,102) AS [Report],
		   CONVERT(varchar(10),s.reportBegin,102) + ' - ' + 
	       CONVERT(varchar(10),s.reportEnd,102) AS [ReportPeriod],
		   s.lateOn AS [LateOn],s.ID
	FROM reports s
	WHERE s.reportOpen BETWEEN '08/01/2014' AND GETDATE()
)

SELECT DISTINCT s.StudentID,
	   s.LastName,
	   s.FirstName,
	   [ReportPeriod] = CASE
				WHEN rs.ReportPeriod IS NULL THEN 'NEVER REPORTED'
				WHEN rs.ReportPeriod IS NOT NULL THEN rs.ReportPeriod
			     END,
	   [Report] = CASE
			WHEN rs.Report IS NULL THEN 'NEVER REPORTED'
			WHEN rs.Report IS NOT NULL THEN rs.Report
		       END,
	   rs.ID,
	   [Status] = CASE
			WHEN r.submitted > rs.LateOn THEN 'LATE'
			WHEN r.submitted < rs.LateOn THEN 'ON TIME'
			ELSE 'NO REPORT'
		      END
FROM reportstd rs LEFT JOIN Reported r
	ON rs.Report = r.csgTerm RIGHT JOIN students s 
	ON r.student = s.StudentID
ORDER BY LastName, FirstName, Report

Open in new window


This gave me the results I was looking to see.  Thanks again.
0
 
LVL 3

Author Comment

by:Shane Kahkola
ID: 40306603
I did something dumb here, but I don't know how to fix it.  I wanted to award you the points and I must have clicked on something wrong.
0
 
LVL 11

Expert Comment

by:Ray
ID: 40306819
Glad I could help in some way.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

691 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