Solved

SQL query with join

Posted on 2011-09-20
11
331 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Display field if column exists 7 35
SQL Syntax 6 41
php hashing methods 3 16
Insert multiple records into a table 4 15
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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…
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 …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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