Solved

SQL query with join

Posted on 2011-09-20
11
325 Views
Last Modified: 2012-05-12
Hi,
Any dvise on how to create joins in the query below will be appreciated

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

Cheers
0
Comment
Question by:RIAS
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 11

Expert Comment

by:rajvja
ID: 36567968
Hi,

   You can include 'NO Liability' also in the IN clause because you are checking with OR condition with the same column.

This is simple brackets magic.

What are your requirements. If we know that refactoring the query will be easy
0
 

Author Comment

by:RIAS
ID: 36567981
Hi,
Thanks for the reply.
Is it possible for you to restructure the query.I am not very good in sql.

Many Thanks
0
 

Author Comment

by:RIAS
ID: 36567983
Assume there will be more joins required.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 11

Expert Comment

by:rajvja
ID: 36567994
Select *  From 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)
or PENSIONER_DETAIL.Pdpencomm  > dateadd(y,9-20-2011,-1)
or TRANSFER_OUT_HISTORY > dateadd(y,9-20-2011,-1)
0
 
LVL 42

Expert Comment

by:dqmq
ID: 36568001
Join to what?  Please rephrase your question so that we know what you are trying to accomplish.

Also, when you combine "or" and "and" in the where clause, please use parenthesis to explicitly express the order of operations.
0
 
LVL 42

Expert Comment

by:dqmq
ID: 36568384
A "join" is a way to combine rows from two tables.  While you can join a table to itself, I don't think that is what you have in mind.  For us to illustrate a join, we need to know what other tables are involved and what columns form the relationship between them.

Here's an example of joining PersonalDetails to (hypothetical) Employee:


Select PersonalDetails.*, Employee.Name
From Personal_Details  
inner join Employee on Personal_Details.EmployeeID = Employee.EmployeeID

0
 
LVL 1

Expert Comment

by:FreelanzWithUs
ID: 36571766
I dont think you require a join statement here as you are referring only to one table.

you can just combine CURRStatus in one instead of an "OR"

Select *  From 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)
or PENSIONER_DETAIL.Pdpencomm  > dateadd(y,9-20-2011,-1)
or TRANSFER_OUT_HISTORY > dateadd(y,9-20-2011,-1)
0
 

Author Comment

by:RIAS
ID: 36572443
Hi,
THanks for the reply.
But here I am referring to 4 tables


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
 
LVL 42

Assisted Solution

by:dqmq
dqmq earned 250 total points
ID: 36574922
OK, then...the join syntax looks like this.  Note, you need to supply the column names that the tables are joined on.  Also, you need a date column from the history table in you where clause.


Select *  From Personal_Details P
inner join DeathBenefit B ON B.ID_COLUMN = P.ID_COLUMN
inner join Pensioner_detail R ON R.ID_COLUMN = P.ID_COLUMN
inner join Transfer_Out_History H ON H.ID_COLUMN = P.ID_COLUMN
Where  
((P.CURRStatus in ('Active','PreservedPensioner','Pensioner','Widow','Child','No Liability'))
and  (P.Dateleft > dateadd(y,9-20-2011,-1)))
or B.dbdate > dateadd(y,9-20-2011,-1)  
or R.pdpencomm  > dateadd(y,9-20-2011,-1)
or H.SOMECOLUMN > dateadd(y,9-20-2011,-1)
0
 
LVL 1

Accepted Solution

by:
FreelanzWithUs earned 250 total points
ID: 36575077
Are these tables related to each other, if the primary key is present inPersonal_Details and the same is reffered in all other tables then replace the "id" below with the appropriate Primary Key Name and it should solve your problem

select * from Personal_Details a inner join
DEATHBENEFIT b on a.id=b.id inner join
PENSIONER_DETAIL c on c.id=a.id inner join
TRANSFER_OUT_HISTORY d on d.id=a.id
where (a.CURRStatus in ('Active','PreservedPensioner','Pensioner','Widow','Child','No Liability'))
and (a.Dateleft > dateadd(y,9-20-2011,-1))
or  b.dbdate > dateadd(y,9-20-2011,-1)  
or c.pdpencomm  > dateadd(y,9-20-2011,-1)
or d.SOMECOLUMN > dateadd(y,9-20-2011,-1)
0
 

Author Closing Comment

by:RIAS
ID: 36579018
Cheers mates!!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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