Solved

Build sql query

Posted on 2011-09-21
5
267 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
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 500 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

813 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

12 Experts available now in Live!

Get 1:1 Help Now