+ Reply to Thread
Results 1 to 14 of 14

Counting DB records

  1. #1
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    273

    Counting DB records

    Note that I am leaving off the Select in the below Queries because Sucuri blocks the post if it contains the word Select in the below queries.

    The below sql query returns a record count of 1 no matter what the value of X
    Count(*) FROM tablename WHERE fieldname=X

    This below sql query returns the correct record count of 66
    * FROM tablename WHERE fieldname = X

    Why do I get different record counts?

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,873

    Re: Counting DB records

    I believe the correct sequence for SQL and Count is as follows
    PHP Code: 
    SE LECT COUNT(column_name)
    FROM table_name
    WHERE condition

    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    273

    Re: Counting DB records

    Still returns 1 no matter what condition is specified. If I use
    S lect fieldname FROM tabename WHREE fieldname = x

    then the correct number of records are returned but if I use count(fieldname) then I always I get a record count of 1
    returned.

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,873

    Re: Counting DB records

    Is your fieldname actually "fieldname"? If not change it to your actual field name.

  5. #5
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    273

    Re: Counting DB records

    Yes, I know that. the field name is DietID and it returns a record count of 1. If I use

    S LECT DietID FROM Recipes WHERE DietID = X, it returns a record count of 1 no matter what the X is since apparently the fieldname is the same as the condition search field. If I use another field such as

    S LECT Name FROM Recipes WHERE DietID = X, it returns the correct record count

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,873

    Re: Counting DB records

    I believe that it should read

    PHP Code: 
    S LECT Count(DietIDFROM Recipes WHERE DietID 

  7. #7
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,755

    Re: Counting DB records

    This thread is the one that needs moving back - is this an Excel or Access query? Sorry - it got moved with all the others.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  8. #8
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    273

    Re: Counting DB records

    Same with Count (DietID) or DietID..record count is 1 no matter what X is. If I use another field such as Name, it returns the correct record count of the number of records in the Recipe Table that has a DietID = X

  9. #9
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,929

    Re: Counting DB records

    I have moved this to the appropriate forum
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  10. #10
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    273

    Re: Counting DB records

    Same with Count (DietID) or DietID..record count is 1 no matter what X is. If I use another field such as Name, it returns the correct record count of the number of records in the Recipe Table that has a DietID = X

  11. #11
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Counting DB records

    I suspect that it's because DietId is null, a count of nulls is 0 - the other fields are not null.

  12. #12
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    273

    Re: Counting DB records

    The Recipes table has many DietID's equal to X under the DietID field in the Recipe Table. "S LECT Name FROM Recipes WHERE DietID = X" it returns the correct record count.

  13. #13
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Counting DB records

    Is that your actual SQL?

    How are you calling this?

    What's is DietID a Nullable field?

    What database are you using?
    Last edited by Kyle123; 11-07-2017 at 11:44 AM.

  14. #14
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    273

    Re: Counting DB records

    Yes, those are the actual SQL's and I'm calling it frrom excel vba using the recordset object. As I said, the below three sql's return the values indicated.

    S LECT DietID FROM Recipes WHERE DietID = X ' returns a record count of 1 no matter what the X is since apparently the fieldname is the same as the condition search field.

    S LECT Count(DietID) FROM Recipes WHERE DietID = X ' returns a record count of 1 no matter what the X is since apparently the fieldname is the same as the condition search field.

    S LECT Name FROM Recipes WHERE DietID = X, it returns the correct record count

    Note that my access database has several fields in the Recipes table of which Name and DietID are two fields.

+ 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. counting records
    By tkaplan in forum Excel General
    Replies: 6
    Last Post: 05-27-2021, 08:28 AM
  2. [SOLVED] help in counting records
    By civram1982 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-26-2014, 02:28 AM
  3. Replies: 3
    Last Post: 01-06-2012, 11:16 PM
  4. Counting all the records in activesheet
    By usr123 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 02-01-2010, 09:50 AM
  5. Counting Records
    By benstead in forum Excel General
    Replies: 2
    Last Post: 06-30-2008, 02:38 PM
  6. [SOLVED] counting records
    By Panagiotis Marantos in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-14-2006, 06:00 AM
  7. counting unique records
    By ceemo in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-23-2005, 03:52 PM
  8. Counting records
    By Slim Jim in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-12-2005, 04:06 PM

Tags for this Thread

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