Important alert: (current site time 7/15/2013 8:08:38 PM EDT)
 

VB icon

Deeply Nested INNER JOINs

Email
Submitted on: 11/1/2000 12:44:10 PM
By: Tim Daniels 
Level: Beginner
User Rating: By 6 Users
Compatibility: SQL Server 7.0
Views: 67886
(About the author)
 
     To create a large working table from four disparate tables to demonstrate nesting INNER JOINS multiple times using multiple ON criteria and multiple WHERE criteria.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Deeply Nested INNER JOINs
-- Description:To create a large working table from four disparate tables to demonstrate nesting INNER JOINS multiple times using multiple ON criteria and multiple WHERE criteria.
-- By: Tim Daniels
--
-- Returns:Single table with all data in it
--
-- Assumes:This is specific to a set of tables, however the concept is not shown well in any documentation and took some time to figure out the nuances of it.
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=246&lngWId=5--for details.--**************************************

SELECT INSURED_RECORD.INSURED_ID,
EMPLOYMENT_HISTORY.EMP_HIST_SEQUENCE,
EMPLOYMENT_HISTORY.BANK_ID,
INSURED_PLAN.PLAN_CODE, 
INSURED_PLAN.PLAN_CODE_SEQ,
IR_FIRST_NAME,
IR_MIDDLE_INITIAL,
IR_LAST_NAME,
IR_CURRENT_AGE,
EH_DATE_HIRED, 
EH_LIFE_CLASS, 
EH_WEEKLY_SALARY,
EH_MONTHLY_SALARY, 
EH_ANNUAL_SALARY,
IP_COVG_CALC_CODE,
IP_PREMIUM_CALC_CODE_1,
IP_PREMIUM_CALC_CODE_2,
IPH_COVG_VOLUME, 
IPH_PREMIUM
FROMINSURED_RECORD INNER JOIN
 (EMPLOYMENT_HISTORY INNER JOIN
(INSURED_PLAN INNER JOIN 
 INSURED_PLAN_HISTORY
 ON (INSURED_PLAN.INSURED_ID = INSURED_PLAN_HISTORY.INSURED_ID)
AND (INSURED_PLAN.EMP_HIST_SEQUENCE =
INSURED_PLAN_HISTORY.EMP_HIST_SEQUENCE)
AND (INSURED_PLAN.BANK_ID = INSURED_PLAN_HISTORY.BANK_ID)
AND (INSURED_PLAN.PLAN_CODE = INSURED_PLAN_HISTORY.PLAN_CODE)
AND (INSURED_PLAN.PLAN_CODE_SEQ =
INSURED_PLAN_HISTORY.PLAN_CODE_SEQ))
 ON (EMPLOYMENT_HISTORY.INSURED_ID = INSURED_PLAN.INSURED_ID)
AND (EMPLOYMENT_HISTORY.EMP_HIST_SEQUENCE =
INSURED_PLAN.EMP_HIST_SEQUENCE)
AND (EMPLOYMENT_HISTORY.BANK_ID = INSURED_PLAN.BANK_ID))
ON (INSURED_RECORD.INSURED_ID = EMPLOYMENT_HISTORY.INSURED_ID)
WHERE (INSURED_PLAN.BANK_ID = 999999999)
 AND (EMPLOYMENT_HISTORY.BANK_ID = 999999999)
 AND (IPH_SUPERCEDED_DATE IS NULL)
 AND (IP_PLAN_STATUS <> 'T') 
 AND ((EH_STATUS = 'T' AND EH_DATE_TERMINATED > '10/15/2000')
OR (EH_STATUS <> 'T'))
ORDER BY INSURED_RECORD.INSURED_ID,
 INSURED_PLAN.PLAN_CODE,
 INSURED_PLAN.PLAN_CODE_SEQ


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

6/23/2002 1:47:25 AMWahid

How to query mysql database with are complex form same like: Text Field, List/Menu.
Thanks!
(If this comment was disrespectful, please report it.)

 
8/20/2003 9:39:51 PM

What if I wanted one of select values contained in the INNER JOIN to be a total returned from a COUNT() function on a different table?
(If this comment was disrespectful, please report it.)

 
10/18/2004 4:15:23 PMt0rus

SELECT COUNT(INSURED_PLAN.EMP_HIST_SEQUENCE)... FROM INSURED_RECORD INNER JOIN INSURED_PLAN...
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.