Solved

nested/sub query in sql

Posted on 2013-11-07
2
295 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
2 Comments
 
LVL 11

Accepted Solution

by:
Louis01 earned 500 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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

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