?
Solved

nested/sub query in sql

Posted on 2013-11-07
2
Medium Priority
?
299 Views
Last Modified: 2013-11-07
Hi I have two tables, one called vwApplicationCurrent and one called vwApplicationCategoryCurrent.  The tables are a one-many relationship based on Ref no.  There can be many Categories assigned to one Application.

I am trying to find the most recent Application Category (would be the row with the max CatAllocationDate) and return the Category Code along with other fields as shown below.

I am a newbie to SQL and am thinking this should be simple but just can't get it.   Here is the sample bit of data

TABLENAME vwApplicationCurrent            
RefNo      ApplicationDate      CurrentApplication
2063      31/10/2013      Yes
            
            
TABLENAME vwApplicationCategoryCurrent            
RefNo      CatCode      CatAllocationDate
2063      BN      25/08/2013
2063      NO      26/08/2013
2063      ME      27/08/2013


I am therefore looking to return the following from my view/sql statement:
RefNo       ApplicationDate    CatCode    CatAllocationDate    CurentApplication
2063         31/10/2013           ME               27/08/2013               Yes

Many thanks in advance.
0
Comment
Question by:deborahhowson00
[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 Comments
 
LVL 11

Accepted Solution

by:
Louis01 earned 2000 total points
ID: 39629650
select ac.RefNo, ac.ApplicationDate, acc1.CatCode, acc1.CatAllocationDate, ac.CurrentApplication
  from vwApplicationCurrent ac
        left outer join vwApplicationCategoryCurrent acc1
    on ac.RefNo = acc1.RefNo
   and acc1.CatAllocationDate = (select MAX(acc2.CatAllocationDate) 
                                  from vwApplicationCategoryCurrent acc2
                                 where acc1.RefNo = acc2.RefNo)

Open in new window

0
 

Author Closing Comment

by:deborahhowson00
ID: 39629741
Super thank you very much! :)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.
Suggested Courses

777 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