?
Solved

Build sql query

Posted on 2011-09-21
5
Medium Priority
?
279 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
[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
  • 2
  • 2
5 Comments
 
LVL 34

Expert Comment

by:Norie
ID: 36574687
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 2000 total points
ID: 36575411
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
ID: 36576539
The foreign key is :'REFNO'
Cheers
0
 
LVL 8

Expert Comment

by:gena17
ID: 36580260
Ok, so you just need to replace UserId with REFNO
0
 

Author Closing Comment

by:RIAS
ID: 36813694
Cheers mate
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

771 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