Results 1 to 6 of 6

Thread: Select unique records with JOIN

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289

    Select unique records with JOIN

    Hi

    I'm trying to select some rows but keep getting double the number of records because of duplicates for each record in table calltypes. Here's what I have:

    SELECT DISTINCT TA.GroupName, SUM(TB.Customer_Call_Cost), COUNT(TB.ID)
    FROM CallTypes AS TA INNER JOIN VoiceTable AS TB ON TA.GroupName=TB.Customer_Call_Group
    WHERE TB.Customer_Index=1 And TB.Date_Time_Called>=#5/1/2009# And TB.Date_Time_Called<=#5/31/2009 23:59:59#
    GROUP BY TA.GroupName
    ORDER BY TA.GroupName;

    It returns this:

    GroupName Expr1001 Expr1002
    International 76.89 140
    Mobile 33.32 252
    National 3.51 64
    Other 0.15 8

    When it should return this:

    GroupName Expr1001 Expr1002
    International 76.89 70
    Mobile 33.32 126
    National 3.51 32
    Other 0.15 4

    I'd be grateful for help. Thanks.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Select unique records with JOIN

    What happens if you add the keyword DISTINCT there?
    Code:
    SELECT DISTINCT TA.GroupName, SUM(TB.Customer_Call_Cost), COUNT(DISTINCT TB.ID)
    FROM CallTypes AS TA INNER JOIN VoiceTable AS TB ON TA.GroupName=TB.Customer_Call_Group
    WHERE TB.Customer_Index=1 And TB.Date_Time_Called>=#5/1/2009# And TB.Date_Time_Called<=#5/31/2009 23:59:59#
    GROUP BY TA.GroupName
    ORDER BY TA.GroupName;
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289

    Re: Select unique records with JOIN

    Thanks. It might work in other DBs than Access but in Access I get:

    Syntax error missing operator in query expression 'count(distinct tb.id)'

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Select unique records with JOIN

    okk.. So you are using Ms-Access.

    The DISTINCT keyword is actually not required when a GROUP BY is there. What happens if you remove it?
    sql Code:
    1. SELECT TA.GroupName, SUM(TB.Customer_Call_Cost), COUNT(TB.ID)
    2. FROM CallTypes AS TA
    3.     INNER JOIN VoiceTable AS TB ON TA.GroupName=TB.Customer_Call_Group
    4. WHERE TB.Customer_Index = 1
    5.     And TB.Date_Time_Called >= #5/1/2009#
    6.     And TB.Date_Time_Called <= #5/31/2009 23:59:59#
    7. GROUP BY TA.GroupName
    8. ORDER BY TA.GroupName
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289

    Re: Select unique records with JOIN

    Hi

    I think I've fixed it. Results are ok now:

    Code:
    SELECT DISTINCT TB.GroupName, SUM(TA.Customer_Call_Cost), COUNT(TA.ID)
    FROM VoiceAutoCRCustomerInv AS TA INNER JOIN (select distinct groupname from calltypes group by groupname having count(*) > 1)  AS TB ON TB.GroupName=TA.Customer_Call_Group
    WHERE TA.Customer_Index=1 And TA.Date_Time_Called>=#5/1/2009# And TA.Date_Time_Called<=#5/31/2009 23:59:59#
    GROUP BY TB.GroupName
    ORDER BY TB.GroupName;

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Europe
    Posts
    289

    Re: Select unique records with JOIN

    i

    Pradeep, just tried your sql but gives same incorrect results.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width