3 table Join Question

  • I have three tables (Tom, Dick and Harry)

    Tom

    NameLaps
    A1
    B1
    C1

    Dick

    NameLaps
    C1
    D1

    Harry

    NameLaps
    C1
    D1
    E1
    F1

    I want to obtain a result with a count of laps for each name.

    NameLaps
    A1
    B1
    C3
    D2
    E1
    F1

    The tables for Tom, Dick and Harry are quite large, so I would prefer not to iterate through each row. Can someone advise me on how to construct the query?

    Thanks in advance.

  • Select Name, Sum(Laps) As TotalLaps

    From

    (

      Select Name, Laps

      From Tom

      Union All

      Select Name, Laps

      From Dick

      Union All

      Select Name, Laps

      From Harry

    ) vtable

  • Thanks for the quick reply. I tried your suggestion, but I received an error:

    "Column 'vtable.Name' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.

    Any Suggestions?

  • Select Name, Sum(Laps) As TotalLaps

    From

    (

    Select Name, Laps

    From Tom

    Union All

    Select Name, Laps

    From Dick

    Union All

    Select Name, Laps

    From Harry

    ) vtable

    GROUP BY vtable.Name

  • Doh. Thanks Remi.

    Cerebral cortex system error: Please install more caffeine and try the operation again.

  • worked like a charm.

    Big Thanks to Remi and PW.

  • Everybody needs a little cefeine at this hour on a friday.

    HTH

    Brain shut off sequence has begun. Happy week-end.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply