Solved

nested/sub query in sql

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

705 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