SQL JOIN to multiple records - bring back only the first

Posted on 2011-10-13
Last Modified: 2012-05-12
I need to join two tables to find the first instance in the second table, however as I am using Teradata I can't use temporary tables, table variables or recursion.

I have two tables - let's call them Customers and Purchases

Customers example
customerid   name
01       Andy Anderson
02       Bob Bobman
03       Chris Cross
04       David Daverton

Purchase example
productid   customerid   datepurchased
02               01                   01/01/2001
02              04                    02/02/2002
01              01                    05/05/2005
07               04                    07/07/2007
06               03                    04/04/2004

I need to left join customers to purchases to bring back details of their first purchase - i.e.
customerid    name     product_first_purchased   date_of_first_purchase
01              Andy Anderson    02      01/01/2001
02              Bob Bobman         NULL     NULL
03              Chris Cross          03        04/04/2004
04              David Daverton    02        02/02/2002

As I said above I can't use any form of temporary table or table variable - it needs to be done in one breath.



Question by:hungoveragain
    LVL 142

    Accepted Solution

    what is the db you are using?

    you can have this:
    select c.*, p.*
      from customers c
      left join purchases p
        on p.customerid = c.customerid
      and p.datepurchased = ( select min(f.datepurchased) from purchases f where f.customerid = c.customerid )

    Open in new window

    or with oracle/sql2005+:
    select sq.*
      from ( select c.*, p.*, row_number() over (partition by c.customerid order by p.purchasedate ) rn
      from customers c
      left join purchases p
        on p.customerid = c.customerid
    ) sq
    where sq.rn = 1 

    Open in new window


    Author Closing Comment



    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    755 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