Solved

Combine two sql queries

Posted on 2011-09-22
5
239 Views
Last Modified: 2012-06-27
Hi,
I have 2 queries ,any help on how to combine them will be appreciated
Specification for the query :

Where  [Status] at %Input_Date%  = “Active” or “Preserved Pensioner” or “Pensioner” or  “Widow(er)”  or “Child” ---is it the latest entry
OR where  [Status] at %Input_Date%  = “No Liability”
and  [Dateleft], [Dbdate], [Pdpencomm] or [Topaid]  later than (%Input_Date% less 1 Year)

Query1:

Select *  From Personal_Details where PERSONAL_DETAILS.CURRSTATUS = 'PRESERVED PENSIONER' and DateJS > datejc

Query2:

 select * from Personal_Details a inner join DEATH_BENEFIT b on a.REFNO=b.REFNO inner join PENSIONER_DETAIL c on c.REFNO=a.REFNO inner join TRANSFER_OUT_HISTORY d on d.REFNO=a.REFNO where (a.CURRStatus in ('Active','PreservedPensioner','Pensioner','Widow','Child','No Liability')) and (a.Dateleft > dateadd(y,17/06/2011,-1)) or  b.dbdate > dateadd(y,17/06/2011,-1)   or c.pdpencomm  > dateadd(y,17/06/2011,-1) or d.topaid > dateadd(y,17/06/2011,-1)
0
Comment
Question by:RIAS
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:lisa_mc
ID: 36579624
Try this I think its what you need


select * from Personal_Details a
inner join DEATH_BENEFIT b on
a.REFNO=b.REFNO 

inner join PENSIONER_DETAIL c on
c.REFNO=a.REFNO 

inner join TRANSFER_OUT_HISTORY d on 
d.REFNO=a.REFNO 

where (a.CURRStatus in ('Active','PreservedPensioner','Pensioner','Widow','Child')
or (a.CURRStatus = 'No Liability'
and ((a.Dateleft > dateadd(y,17/06/2011,-1)) or  (b.dbdate > dateadd(y,17/06/2011,-1))   
or (c.pdpencomm  > dateadd(y,17/06/2011,-1)) or (d.topaid > dateadd(y,17/06/2011,-1)))  )

Open in new window

0
 

Author Comment

by:RIAS
ID: 36579651
Hi,
Got error :
Msg 102, Level 15, State 1, Line 14
Incorrect syntax near ')'.


Thanks
0
 
LVL 3

Accepted Solution

by:
lisa_mc earned 500 total points
ID: 36579661
my apologies forgot one bracket

 select * from Personal_Details a
inner join DEATH_BENEFIT b on
a.REFNO=b.REFNO

inner join PENSIONER_DETAIL c on
c.REFNO=a.REFNO

inner join TRANSFER_OUT_HISTORY d on
d.REFNO=a.REFNO

where (a.CURRStatus in ('Active','PreservedPensioner','Pensioner','Widow','Child'))
or (a.CURRStatus = 'No Liability'
and ((a.Dateleft > dateadd(y,17/06/2011,-1)) or  (b.dbdate > dateadd(y,17/06/2011,-1))  
or (c.pdpencomm  > dateadd(y,17/06/2011,-1)) or (d.topaid > dateadd(y,17/06/2011,-1)))  )
0
 

Author Closing Comment

by:RIAS
ID: 36579689
Cheers mate
0
 
LVL 3

Expert Comment

by:lisa_mc
ID: 36579696
you are very welcome glad I could help
happy coding :-)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

746 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

12 Experts available now in Live!

Get 1:1 Help Now