Solved

Combine two sql queries

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

821 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