Trying to get one aggregate set from two different tables

  • I already have a solution to my problem, but I don't like it very much, and I'm wondering if there's another way I can make this more efficient.

    I have two tables.  Here's some really, really simplified versions of these tables (the tables actually have many other columns as well):

    PreviousCarrierForWC

    Id CHAR(20) - my primary key

    CarrierName VARCHAR(60)

    PreviousCarrierForBA

    Id CHAR(20) - my primary key

    txtPriorCarrier VARCHAR(60)

    Essentially CarrierName and txtPriorCarrier store the same type of data.  What I want is a list of all the distinct CarrierNames and a count of how many times they are in the table.

    For example, I have these data sets...

    PreviousCarrierForWC

    IdCarrierName
    12345678901234567890AO
    23456789012345678901AO
    34567890123456789012AAA
    45678901234567890123Something else

    PreviousCarrierForBA

    IdCarrierName
    12345678901234567890Test
    23456789012345678901Sample data
    34567890123456789012AAA
    45678901234567890123AO

    And I want the following dataset returned:

    Expected Result Set:

    CarrierNameTotal
    AO3
    AAA2
    Something else1
    Sample data1
    Test1

    I achieved this using a temp table:

    DECLARE @TempTable TABLE

     (CarrierName VARCHAR( 60 ))

    INSERT INTO @TempTable

     SELECT CarrierName

      FROM PreviousCarrierForWC

     UNION ALL

     SELECT txtPriorCarrier

      FROM PreviousCarrierForBA

    SELECT CarrierName, COUNT(*) AS Total

     FROM @TempTable

     GROUP BY CarrierName

     ORDER BY CarrierName, Total DESC

    DELETE @TempTable

    But what I really wanted was just one SELECT similar to the following...

    select distinct carriername, count(*) as Total

     from PreviousCarrierForWC

     group by CarrierName

    union

    select distinct txtpriorcarrier, count(*) as Total

     from PreviousCarrierForBA

     group by txtpriorcarrier

     order by carriername, Total DESC

    The problem is that this query returns a resultset like the following...

    CarrierNameTotal
    AO2
    AO1
    AAA1
    AAA1
    Something else1
    Sample data1
    Test1

    AO and AAA are repeated, and I actually want these totals to be added together.  If anyone has any suggestions, I'd be happy to hear them.

  • Do the UNION ALL in a derived table. Something like:

    SELECT CarrierName

        ,COUNT(*) AS Total

    FROM (

            SELECT CarrierName

            FROM PreviousCarrierForWC

            UNION ALL

            SELECT txtPriorCarrier

            FROM PreviousCarrierForBA

        ) D

    GROUP BY CarrierName

     

  • Oh!  I was trying to do something like this before, but I hadn't given the derived table an alias and I was getting errors.  I just thought this wasn't possible.  Thanks for that!

  • Hi ,

    Try This....Its working

    select txtPriorCarrier,count(*)  a from(

    select * from PreviousCarrierForBA

    union all

    select * from PreviousCarrierForwc)a

    group by txtPriorCarrier

    order by 2 desc

     

    Regards,

    Amit G.

Viewing 4 posts - 1 through 3 (of 3 total)

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