Solved

nested/sub query in sql

Posted on 2013-11-07
2
284 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
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 SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

758 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

21 Experts available now in Live!

Get 1:1 Help Now