[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

SQL query with join

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
RIAS
Asked:
RIAS
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
rajvjaCommented:
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
 
RIASAuthor Commented:
Hi,
Thanks for the reply.
Is it possible for you to restructure the query.I am not very good in sql.

Many Thanks
0
 
RIASAuthor Commented:
Assume there will be more joins required.
0
Technology Partners: 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!

 
rajvjaCommented:
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
 
dqmqCommented:
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
 
dqmqCommented:
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
 
FreelanzWithUsCommented:
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
 
RIASAuthor Commented:
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
 
dqmqCommented:
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
 
FreelanzWithUsCommented:
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
 
RIASAuthor Commented:
Cheers mates!!
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now