?
Solved

sql query with 4 tables

Posted on 2011-09-21
8
Medium Priority
?
268 Views
Last Modified: 2012-05-12
Hi,

Here I am referring to 4 tables any help with joins is appreciated.


Select *  From Personal_Details 'table Personal_Details
Where  
((Personal_Details.CURRStatus in ('Active','PreservedPensioner','Pensioner','Widow','Child','No Liability'))
and  (Personal_Details .Dateleft > dateadd(y,9-20-2011,-1)))
or DEATHBENEFIT Dbdate > dateadd(y,9-20-2011,-1) 'table DEATHBENEFIT
or PENSIONER_DETAIL.Pdpencomm  > dateadd(y,9-20-2011,-1)' table PENSIONER_DETAIL
or TRANSFER_OUT_HISTORY > dateadd(y,9-20-2011,-1) 'table TRANSFER_OUT_HISTORY

Cheers
0
Comment
Question by:RIAS
[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
  • 4
8 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 36574223
something like this?

SELECT *
  FROM personal_details
      JOIN deathbenefit
          ON ((personal_details.currstatus IN
                   ('Active', 'PreservedPensioner', 'Pensioner', 'Widow', 'Child', 'No Liability'))
              AND (personal_details.dateleft > dateadd(y, 9 - 20 - 2011, -1)))
             OR deathbenefit.dbdate > dateadd(y, 9 - 20 - 2011, -1)
      JOIN pensioner_detail
          ON pensioner_detail.pdpencomm > dateadd(y, 9 - 20 - 2011, -1)
      JOIN transfer_out_history
          ON transfer_out_history > dateadd(y, 9 - 20 - 2011, -1)
0
 

Author Comment

by:RIAS
ID: 36574369
Thanks for the reply but the query loops infinitely.

Cheers
0
 

Author Comment

by:RIAS
ID: 36574397
Hi,
This is a spec, all marked in bold are tables:

Where  [Personal_details.currStatus] at '21-Sept-2011'  = “Active” or “Preserved Pensioner” or “Pensioner” or  “Widow(er)”  or “Child”
OR where  [Personal_details.Status] at '21-Sept-2011'   = “No Liability”
and  [Personal_details.Dateleft], [death_benefit..Dbdate], [pensioner_detail.Pdpencomm] or [Transfer_out_history.Topaid]  later than (%Input_Date% less 1 Year)

Cheers
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 74

Expert Comment

by:sdstuber
ID: 36574438
>>> query loops infinitely.

how does that query "loop"  ?

it might take a long time, but it can't loop

can you give some sample data and expected results?
0
 

Author Comment

by:RIAS
ID: 36574526
Hi,
The query resulted  in 213000 rows.There was only one number duplicated 213000 times and also it needed to be cancelled as it was executing even after 213000 .

Cheers
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 36574756
perhaps your join conditions aren't restrictive enough
 so you're seeing the cartesian explosion of extra rows
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 36574858
do you have any columns between the tables that should be linked?

for example....

if you have 100 rows in personal_details that meet your status and date criteria

and if you have 100 rows in transfer_out_history that meet your date criteria

then your results will have 10000 rows because nothing above has information restricting the relationships between the two tables.

So,  every row in each table will be joined to every row in the other table.

Same with the other two tables.  


If each table had a mere 100 rows matching their respective criteria above,
  that would yield 100000000  rows total in the output

0
 

Author Closing Comment

by:RIAS
ID: 36813697
Cheers mate
0

Featured Post

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

765 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