Solved

SQL query with join

Posted on 2011-09-20
11
291 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
 
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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now