Solved

Build sql query

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

726 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