+ Reply to Thread
Results 1 to 5 of 5

How to count unique records?

  1. #1
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    782

    How to count unique records?

    As I remember, some syntax is not allowed in this forum. I will add some "-" below.

    I can use S-QL to pull data from MS S-QL database to excel sheet.

    If I use the query S-ELECT ColumnO, ColumnP, ColumnQ, ColumnX, ColumnY, ColumnZ FROM Table_name WHERE ColumnO = 100 AND ColumnP = 10 AND ColumnQ = 30 , I will get something like below screenshot.

    I know how to put the screenshot table into excel sheet, via VBA. But this is not the final output yet. From there, I will need to calculate # of unique records, the answer is 3. I highlight them in 3 different colors. All columns have same data.

    Note: You may wonder why the output table (screen table) contains duplicate rows, because in the query, I only select 6 columns, and there are many other columns.

    All I want to get is the output number 3 , how should I modify the query to include something like COUNT/DISTINCT ? So there is no need to import raw data into excel sheet. It is possible that output result is 0

    Otherwise, I will run a VBA loop through ColumnX/ColumnY/ColumnZ and count the unique records.
    Attached Images Attached Images
    Last edited by VAer; 11-04-2024 at 06:41 PM.

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,564

    Re: How to count unique records?

    Hi VAer,

    It's been a while since I wrote anything in SQL but this worked in an Access Db I quickly created:

    SELECT COUNT(*) AS Unique_Count FROM (SELECT DISTINCT ColumnO, ColumnP, ColumnQ, ColumnX, ColumnY, ColumnZ FROM Table_name) AS Temp;

    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    782

    Re: How to count unique records?

    @Trebor76

    Thanks, but it also needs to include this information WHERE ColumnO = 100 AND ColumnP = 10 AND ColumnQ = 30

    Should I write it as
    SELECT COUNT(*) AS Unique_Count FROM (SELECT DISTINCT ColumnO, ColumnP, ColumnQ, ColumnX, ColumnY, ColumnZ FROM Table_name WHERE ColumnO = 100 AND ColumnP = 10 AND ColumnQ = 30) AS Temp

    Maybe I don't need Unique_Count and Temp, shall I?
    SELECT COUNT(*) FROM (SELECT DISTINCT ColumnO, ColumnP, ColumnQ, ColumnX, ColumnY, ColumnZ FROM Table_name WHERE ColumnO = 100 AND ColumnP = 10 AND ColumnQ = 30)

    In VBA, can I assign it to variable directly?

    Dim NumOfRecord As Integer
    NumOfRecord = SELECT COUNT(*) FROM (SELECT DISTINCT ColumnO, ColumnP, ColumnQ, ColumnX, ColumnY, ColumnZ FROM Table_name WHERE ColumnO = 100 AND ColumnP = 10 AND ColumnQ = 30)

    Edit: here is more detailed code. How can I get the final output number? I have the SELECT statement, but how to pass the value to a variable NumOfRecord ?

    Please Login or Register  to view this content.
    Last edited by VAer; 11-04-2024 at 08:24 PM.

  4. #4
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    782

    Re: How to count unique records?

    @Trebor76

    Thanks, the code works.

    I just need CopuFromRecordset to pass the output result.

  5. #5
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,564

    Re: How to count unique records?

    Glad you got it sorted and thanks for the rep 😎

+ 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. Help with formula to count unique records
    By Alison913 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-08-2024, 06:26 PM
  2. [SOLVED] Count unique IDs that have multiple records
    By scubadiver007 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 09-07-2021, 04:13 AM
  3. Count Unique records
    By guido167 in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 06-23-2013, 03:08 PM
  4. Count unique records in a column
    By bcn1988 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-20-2013, 11:51 AM
  5. Calculating Unique records row count
    By plk in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-05-2012, 04:26 PM
  6. Excel 2007 : Count Unique Records
    By stevetothink in forum Excel General
    Replies: 2
    Last Post: 11-04-2011, 04:22 PM
  7. [SOLVED] Count the number of unique records
    By [email protected] in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 03-08-2006, 03:35 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