We help IT Professionals succeed at work.
Get Started

SQL join - one to many

Last Modified: 2012-05-11
I have two tables.  A simplified listing of the fields is shown below:

      CallTable            HistoryTable
      firstname            identity
      lastname            calldate
      phone            disposition

One record in the call table per customer (key=identity)
Multiple records in history table for the same identity.

I want to pull a list of call failures (any identity that did not have a record in the history table with disposition='success') and the last time the call was attempted.  This is my first attempt at building the query. (I obviously am not a SQL guru)

SELECT c.FirstName, c.LastName, MAX(h.calldate) , h.Disposition, h.identity
FROM  calltable AS c INNER JOIN
               HistoryTable AS h ON c.IDENTITY = h.identity
                   (SELECT DISTINCT identity
                    FROM   HistoryTable
                    WHERE (disposition = 'success')))
GROUP BY c.FirstName, c.LastName, h.disposition, h.identity

This is not working properly and i'm guessing it has to do with the fact that this is a one to many relationship.  How can this query be restructured so it works properly?
Watch Question
This problem has been solved!
Unlock 1 Answer and 12 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE