Solved

SQL query with join

Posted on 2011-09-20
11
338 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
[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
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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