Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Build sql query

Posted on 2011-09-21
5
Medium Priority
?
284 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 35

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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

604 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