Solved

Combine two sql queries

Posted on 2011-09-22
5
241 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

932 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

10 Experts available now in Live!

Get 1:1 Help Now