Answered SQL Statement

  • Saturday, May 05, 2012 3:39 PM
     
     

    I suddenly find myself in a pinch...

    I need a SQL statement which does a couple of things.  A table contains the fields First_Name and Last_Name.  I need the statement to first combine the two and then count how many times the combination occurs in the results.  For me the first part seems simple:

    select Last_Name + ',' + ' ' + First_Name AS Full_Name FROM <table>

    What I am have a time getting done is Grouping by the results and then counting how many times Full_Name occurs.

    Oh yeah, I forgot...I am trying to do this in Excel 2002 VBA.

    Thank you in advance.

All Replies

  • Saturday, May 05, 2012 3:49 PM
     
      Has Code

    The SQL would look like this:

    SELECT [First_Name] & ' ' & [Last_Name] AS Full_Name, Count(*) AS N
    FROM [TableName]
    GROUP BY [First_Name] & ' ' & [Last_Name]

    You can use DAO or ADO to connect to a database, and opening a recordset on the above SQL string.

    Regards, Hans Vogelaar

    • Marked As Answer by GAMinTN Saturday, May 05, 2012 5:01 PM
    • Unmarked As Answer by GAMinTN Saturday, May 05, 2012 5:12 PM
    •  
  • Saturday, May 05, 2012 5:01 PM
     
     

    I knew there was someone out there who could answer this!

    Thank you very much!

  • Saturday, May 05, 2012 5:06 PM
     
     
    One last thing...How to get only those records which appear more than once?
  • Saturday, May 05, 2012 7:22 PM
     
      Has Code

    Use the following SQL:

    SELECT [First_Name] & ' ' & [Last_Name] AS Full_Name, Count(*) AS N
    FROM [TableName]
    GROUP BY [First_Name] & ' ' & [Last_Name]
    HAVING Count(*)>1


    Regards, Hans Vogelaar

    • Marked As Answer by GAMinTN Saturday, May 05, 2012 8:25 PM
    • Unmarked As Answer by GAMinTN Sunday, May 06, 2012 4:30 PM
    •  
  • Sunday, May 06, 2012 4:29 PM
     
     

    OK...so now the query is giving me fits!

    By itself, as written, it works fine but I need to to add another field to the results (Email Address) but the table has disappeared from the query build view (MS Excel) and I have tried everything I know to get this field added.

    The query should still combine First_Name and Last_Name, count the number of times the combination appears and return only those which appear more than once.  If possible I would like them in order of Full_Name, Email Address and Quantity > 1.

    I know this is SQL 101, but I am stumped.

  • Sunday, May 06, 2012 4:41 PM
     
     

    Try creating the query anew.

    Bringing in the email address could be a problem - what do you want to do if there are records with the same full name but different email addresses?


    Regards, Hans Vogelaar

  • Sunday, May 06, 2012 5:57 PM
     
     
    Thank you for responding...It would be permissible to duplicate the email address since there are those which share email addresses for some reason.
  • Sunday, May 06, 2012 5:59 PM
     
     
    Just re-read you question...It would be fine if same person has multiple email addresses.  Lord knows I have half a dozen myself...
  • Sunday, May 06, 2012 8:24 PM
     
     

    OK, but if you add the e-mail field to the query and group on it, you'll get a separate record for each full name - email address combination, say:

    Full_Name  Email                                  N
    John Doe    john.doe @ hotmail.com    1
    John Doe    john.doe @ gmaiil.com      3

    That might not be what you want. Or is it?


    Regards, Hans Vogelaar


  • Sunday, May 06, 2012 9:56 PM
     
     
    This would be acceptable.  I can compare it to the results of another query to make sure which one is correct.
  • Sunday, May 06, 2012 10:34 PM
     
     Answered Has Code

    Try this:

    SELECT [First_Name] & ' ' & [Last_Name] AS Full_Name, [EmailAddress], Count(*) AS N
    FROM [TableName]
    GROUP BY [First_Name] & ' ' & [Last_Name], [EmailAddress]
    HAVING Count(*)>1

    where EmailAddress is the name of the email field. If you prefer to have one record per full name, you could return an arbitrary email address:

    SELECT [First_Name] & ' ' & [Last_Name] AS Full_Name, 
    First([EmailAddress]) AS AnEmailAddress, Count(*) AS N
    FROM [TableName]
    GROUP BY [First_Name] & ' ' & [Last_Name]
    HAVING Count(*)>1


    Regards, Hans Vogelaar

    • Marked As Answer by GAMinTN Monday, May 07, 2012 12:22 AM
    •  
  • Monday, May 07, 2012 12:22 AM
     
     
    Thank you.  I promise not to bug you again on this.