Solved

Build sql query

Posted on 2011-09-21
5
259 Views
Last Modified: 2012-05-12
Hi,
I have a spec for sql query which involves 4 tables.Any help is appreciated.All bold words are tables

Select *
where  [Status_history.Status] at '21-Sept-2011'  = “No Liability”
and  [Personal_details.Dateleft], [Death_benefit.Dbdate], [Pensioner_Details.Pdpencomm] or [Pensioner_Details.Topaid]  later than ('21-Sept-2011' less 1 Year)

Cheers

0
Comment
Question by:RIAS
  • 2
  • 2
5 Comments
 
LVL 33

Expert Comment

by:Norie
Comment Utility
Are these tables linked in any way?

What other fields are there?

For example is there a date field in the 'Status_history' table that can be used in the first criteria?

If there was you could use something like this:

WHERE [Status_history].[DateField] = #21 Sept 2011# AND [Status_history].[Status] = 'No Liability'
0
 
LVL 8

Accepted Solution

by:
gena17 earned 500 total points
Comment Utility
I assume Status_history and Personal_details are connected with a foreign key.
In this case it will be something like:

select *
from Personal_details
join (select UserId, LastDate = max(StatusDate) from Status_history where StatusDate < '2011-09-21'  group by UserId) as LastStatusDates on LastStatusDates.UserId = Personal_details.UserId
join Status_history on Status_history.UserId = Personal_details.UserId and Status_history.StatusDate = LasstStatusDates.LastDate
where
Status_history.Status = 'No Liability'
and Personal_details.Dateleft > dateadd(year, -1, '2011-09-21')
and .........
0
 

Author Comment

by:RIAS
Comment Utility
The foreign key is :'REFNO'
Cheers
0
 
LVL 8

Expert Comment

by:gena17
Comment Utility
Ok, so you just need to replace UserId with REFNO
0
 

Author Closing Comment

by:RIAS
Comment Utility
Cheers mate
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

744 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

15 Experts available now in Live!

Get 1:1 Help Now