+ Reply to Thread
Results 1 to 4 of 4

Writing a SELECT query to return only records with duplicate values in one column

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Writing a SELECT query to return only records with duplicate values in one column

    I know I am still learning SQL but I can't work out what I am doing wrong.

    I have a table (tbcCrcFile). One of the columns (crcStrm) does not contain unique values. I want to return any records where their crcStrm value is not unique.

    Below are all my attempts which I tried to adapt from googling. What do I need to change the query to, and why?

    Attempt #1
    PHP Code: 
    10:15:21 AM: -2147467259 [Microsoft][ODBC Excel DriverCannot have aggregate function in WHERE clause (COUNT(tblA.crcStrm)>1).
    10:15:21 AMSELECT tblA.* FROM [tbcCrcFile$] tblA WHERE (COUNT(tblA.crcStrm) > 1
    Attempt #2
    PHP Code: 
    10:16:38 AM: -2147217900 [Microsoft][ODBC Excel DriverSyntax error (missing operatorin query expression 'tblA.crcStrm WHERE (COUNT(tblA.crcStrm) > 1)'.
    10:16:38 AMSELECT tblA.* FROM [tbcCrcFile$] tblA GROUP BY tblA.crcStrm WHERE (COUNT(tblA.crcStrm) > 1
    Attempt #3
    PHP Code: 
    10:19:00 AM: -2147217900 [Microsoft][ODBC Excel DriverSyntax error (missing operatorin query expression 'tblA.crcStrm WHERE COUNT(*) > 1'.
    10:19:00 AMSELECT tblA.*, COUNT(*) FROM [tbcCrcFile$] tblA GROUP BY tblA.crcStrm WHERE COUNT(*) > 
    Attempt #4
    PHP Code: 
    10:24:43 AM: -2147217900 [Microsoft][ODBC Excel DriverCannot group on fields selected with '*' (tblA).
    10:24:43 AMSELECT tblA.*, COUNT(*) FROM [tbcCrcFile$] tblA GROUP BY tblA.crcStrm HAVING COUNT(*) > 
    Last edited by mc84excel; 03-26-2015 at 10:26 PM.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Writing a SELECT query to return only records with duplicate values in one column

    maybe

    SELECT T.* FROM [tbcCrcFile$] T WHERE T.crcStrm = (SELECT TS.crcStrm FROM [tbcCrcFile$] TS GROUP BY TS.crcStrm HAVING (((Count(TS.crcStrm))>1)));
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Writing a SELECT query to return only records with duplicate values in one column

    Quote Originally Posted by JosephP View Post
    maybe

    SELECT T.* FROM [tbcCrcFile$] T WHERE T.crcStrm = (SELECT TS.crcStrm FROM [tbcCrcFile$] TS GROUP BY TS.crcStrm HAVING (((Count(TS.crcStrm))>1)));
    Good to hear from you again Josie

    I tried your suggestion and got an error ( -2147467259 [Microsoft][ODBC Excel Driver] At most one record can be returned by this subquery. ) But I was sure that you were pointing me in the right direction so after googling the error message, I changed the query by one word 'IN' (i.e.
    Please Login or Register  to view this content.
    )

    Then it worked perfectly.

    I would never have solved this without your help. Thank you. And +1

  4. #4
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Writing a SELECT query to return only records with duplicate values in one column

    Thanks again Josie. I had a need for something like this just now in a SQL Server query and I remembered this. Worked perfectly.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Having trouble writing a SELECT query
    By mc84excel in forum Access Tables & Databases
    Replies: 15
    Last Post: 03-23-2015, 08:19 PM
  2. Need a query for select & replace the column values in vba
    By kavitha.v in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-23-2014, 05:54 AM
  3. [SOLVED] Return Column Headers based on row rank with duplicate values in row
    By carlwin in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-31-2013, 12:24 AM
  4. Connection but no records writing VBA query to retrieve data from SQL Server 2012
    By sportsguy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-01-2012, 03:20 PM
  5. Find records and return values to new column
    By ironj32 in forum Excel General
    Replies: 4
    Last Post: 11-20-2009, 11:55 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1