Results 1 to 17 of 17

Thread: Multiple count - single table, fixed criteria [RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Resolved Multiple count - single table, fixed criteria [RESOLVED]

    hi,

    i need to look at a number of fields within a table, and count where it equals Zero.

    e.g.:
    Code:
    MyTable1
                   Field1     Field2     Field3     Field4
    Record1       5            0           5           1
    Record1       0            0           5           1
    Record3       1            5           0           0
    Record4       0            1           1           5
    so my query should retun:

    Field1 2
    Field2 2
    Field3 1
    Field4 1

    I had done this previously using Dcount, but its really slow.

    Done a search on here, and there are a couple of suggestions, but they dont seem to work for my situation.

    any suggestions appreciated

    TIA
    Last edited by Br1an_g; Apr 12th, 2005 at 05:23 AM. Reason: resolved post
    if you fail to plan, you plan to fail

  2. #2
    Junior Member llkhoutx's Avatar
    Join Date
    Jan 2005
    Location
    Houston, TX
    Posts
    19

    Re: Multiple count - single table, fixed criteria

    The SQL for such a query is

    SELECT Count(MyTable1.Field1) AS CountOfField1, Count(MyTable1.Field2) AS CountOfField2, Count(MyTable1.Field3) AS CountOfField3, Count(MyTable1.Field4) AS CountOfField4, Count(MyTable1.Field5) AS CountOfField5
    FROM MyTable1;

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Multiple count - single table, fixed criteria

    I think the poster wants to return only where the fields are zero.
    VB Code:
    1. SELECT Count(MyTable1.Field1) AS CountOfField1, Count(MyTable1.Field2) AS CountOfField2, Count(MyTable1.Field3) AS CountOfField3, Count(MyTable1.Field4) AS CountOfField4, Count(MyTable1.Field5) AS CountOfField5
    2. FROM MyTable1
    3. WHERE Count(MyTable1.Field1) <> 0 AND Count(MyTable1.Field2) <> 0 AND Count(MyTable1.Field3) <> 0;
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: Multiple count - single table, fixed criteria

    Quote Originally Posted by RobDog888
    I think the poster wants to return only where the fields are zero.
    VB Code:
    1. SELECT Count(MyTable1.Field1) AS CountOfField1, Count(MyTable1.Field2) AS CountOfField2, Count(MyTable1.Field3) AS CountOfField3, Count(MyTable1.Field4) AS CountOfField4, Count(MyTable1.Field5) AS CountOfField5
    2. FROM MyTable1
    3. WHERE Count(MyTable1.Field1) <> 0 AND Count(MyTable1.Field2) <> 0 AND Count(MyTable1.Field3) <> 0;
    Yes, where the field itself is equal to zero.
    wont your statment above return the counts, where it has counted more than 0.

    e.g.:
    Code:
    SELECT Count(MyTable1.Field1) AS CountOfField1
    FROM MyTable1
    WHERE MyTable1.Field1 = 0;
    the above works, but it means having to repeat this statement for each of the fields in the table (about 20)

    is it not possible to do this for multiple fields in the one statement?

    also, once i have ran the SQL statement, whats the best way to get the value into the control on the form?
    e.g.
    Code:
    dim strSQL as String
    dim dbs as Database
    dim rst as Recordset
    dim VarRecords as Variant
    
    set dbs = CurrentDb
    
    strSQL = "SELECT Count(MyTable1.Field1) AS CountOfField1 " _
    & " FROM MyTable1 " _
    & " WHERE MyTable1.Field1 = 0;"
    
    set rst = dbs.OpenRecordset(strSQL)
    
    'the next bit im not sure of
    varRecords = rst.GetRows(rst.RecordCount)
    
    txtTextBox = varRecords(0, 0)
    is there a better way? and can i reference the 'CountOfField1' directly?

    thanks
    if you fail to plan, you plan to fail

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Multiple count - single table, fixed criteria

    Didnt you see my sql code statement? It has a where clause to restrict the records to the ones ... oops I did it backwards.
    Here is the reversed statement.

    VB Code:
    1. SELECT Count(MyTable1.Field1) AS CountOfField1, Count(MyTable1.Field2) AS CountOfField2, Count(MyTable1.Field3) AS CountOfField3, Count(MyTable1.Field4) AS CountOfField4, Count(MyTable1.Field5) AS CountOfField5
    2. FROM MyTable1
    3. WHERE Count(MyTable1.Field1) = 0 AND Count(MyTable1.Field2) = 0 AND Count(MyTable1.Field3) = 0;
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: Multiple count - single table, fixed criteria

    RobDogg,

    thanks for your reply.

    doesnt your SQL statement return where the count of the field equals zero?

    i want it to return where the actual field value = 0, not the count of that field.
    if you fail to plan, you plan to fail

  7. #7
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Multiple count - single table, fixed criteria

    Code:
    Select 
      Count(qrySubZero.zF1) as ZeroF1
      Count(qrySubZero.zF2) as ZeroF2
      Count(qrySubZero.zF3) as ZeroF3
      Count(qrySubZero.zF4) as ZeroF4
      Count(qrySubZero.zF5) as ZeroF5
    FROM
    (
    SELECT 
      MyTable1.Field1=0 AS zF1,
      MyTable1.Field2=0 AS zF2,
      MyTable1.Field3=0 AS zF3,
      MyTable1.Field4=0 AS zF4,
      MyTable1.Field5=0 AS zF5
    FROM 
      MyTable1
    ) As qrySubZero
    Might be slightly different but thats the basic idea.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Multiple count - single table, fixed criteria

    Oops, sorry about that. I was thinking that count is an aggregate function so I had to use the same
    in the where clause. Doh! Just take the count out in the where clause.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: Multiple count - single table, fixed criteria

    Quote Originally Posted by Ecniv
    Code:
    Select 
      Count(qrySubZero.zF1) as ZeroF1
      Count(qrySubZero.zF2) as ZeroF2
      Count(qrySubZero.zF3) as ZeroF3
      Count(qrySubZero.zF4) as ZeroF4
      Count(qrySubZero.zF5) as ZeroF5
    FROM
    (
    SELECT 
      MyTable1.Field1=0 AS zF1,
      MyTable1.Field2=0 AS zF2,
      MyTable1.Field3=0 AS zF3,
      MyTable1.Field4=0 AS zF4,
      MyTable1.Field5=0 AS zF5
    FROM 
      MyTable1
    ) As qrySubZero
    Might be slightly different but thats the basic idea.
    Ecniv,

    does this type of statement work in VBA?

    im getting an error when i try to implement it within my app:

    Code:
    strSQL = "Select Count(qryCountFails.CF1) As CountF1" _
            & " FROM (SELECT ServicingInputTable.[Correct Greeting]=0 AS CF1 " _
            & " FROM ServicingInputTable) As qryCountFails;"
    ive only used a single field to get it to work first.

    im getting:
    Code:
    Run-time error '3131'
    
    Syntax error in FROM clause
    RobDogg: thanks for your input, but if i remove the count from your criteria section, it counts where all of the fields = 0 together, not on an individual basis.

    i was thinking this would be a relatively simple task and now im thinking of leaving the Dcounts in place and letting them wait 5 minutes for the form to load...lol
    if you fail to plan, you plan to fail

  10. #10
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Multiple count - single table, fixed criteria

    Code:
    Select T1, count(T1) as cT1 From
    (Select Table1.Test1='aa' as T1 FROM Table1) as arySubT
    Group By T1
    This is a test one run in the querybuilder of Access... Appears to work are required. I think I forgot that the options of True/false would need to be represented... Perhaps.

    As to your code the Sql statement looks fine.
    Code:
    strSQL = "Select CF1, Count(qryCountFails.CF1) As CountF1 FROM"
    strSql = strSql & " (SELECT ServicingInputTable.[Correct Greeting]=0 AS"
    strSql = strSql & " CF1 FROM ServicingInputTable) As qryCountFails"
    strSql = strSql & " GROUP BY CF1"
    debug.print strSql
    So I'm not sure why it would error. So if you put the above code in it should print the sql statement to the immediates window. Copy this, and go to Access query builder (new query). Don't add tables and change the top left drop down to sql view. Paste in the statement and see if there is an error or it runs.

    If it runs then there is a problem between your program and the db (ADO connection perhaps?) If it fails, then there is a problem with the table/sql statement.

    Post up what happens please?

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: Multiple count - single table, fixed criteria

    Code:
    Select CF1, Count(qryCountFails.CF1) As CountF1 FROM
    (SELECT ServicingInputTable.[Correct Greeting]=0 AS
    CF1 FROM ServicingInputTable) As qryCountFails
    GROUP BY CF1;
    the above fails with the same error. This is in the access query builder SQL view.

    Syntax error in FROM clause, and highlights the select in bold

    if you fail to plan, you plan to fail

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: Multiple count - single table, fixed criteria

    after a bit of testing it seems that the above Sub-query does not work as it is an Access 97 database.

    i have tested the exact statement in an access 2002 front end and it works fine.

    thanks for all of your feedback.

    I will post the complete SQL when i get a chance to finish it..just for information.
    if you fail to plan, you plan to fail

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: Multiple count - single table, fixed criteria

    ok, spoke too soon

    Code:
    Select
    Count(qryCountFails.CF1) As CountF1,
    Count(qryCountFails.CF2) as CountF2,
    Count(qryCountFails.CF2) as CountF3
    FROM
    (
    SELECT 
    ServicingInputTable.[Correct Greeting]=0 AS CF1,
    ServicingInputTable.[Bank Jargon Avoided]=0 AS CF2,
    ServicingInputTable.[Positive Words]=0 AS CF3
    FROM
    ServicingInputTable
    ) As qryCountFails
    GROUP BY 
    CF1,
    CF2,
    CF3;
    ok, the sub query works fine, as long as each record does not have 0 in more than one of the criteria field. if it has 0 in more than one field, it counts these seperately.
    e.g.:
    Code:
                        Field1          Field2          Field3
    Record1            0                0                 1
    Record2            1                0                 1
    Record3            0                1                 1
    Record4            0                1                 1
    Record5            1                0                 0
    returns:
    Code:
                        CountF1      CountF2       CountF3
    Record1            2                2                 2
    Record2            1                1                 1
    Record3            1                1                 1
    Record4            1                1                 1
    its kind of hard to explain...it sort of works, but doesnt at the same time..
    if you fail to plan, you plan to fail

  14. #14
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Multiple count - single table, fixed criteria

    Yup hi sorry.
    (in that order)

    I thought a little more on it (it is working sort of, just not quite the way you want it to...)


    This is what I think you need (revised from the earlier try )
    For all records you need a count of:
    - field 1 is 0
    - field 2 is 0
    - field 3 is 0
    It doesn't matter if they are on the same line (which would be caught in the previous attempt).
    Is the following more like what you need?
    Code:
    Select
       qryCFails1.CF1,
       qryCFails2.CF2,
       qryCFails3.CF3
    FROM
    ((
    SELECT
       ServicingInputTable.[IDfield],
       ServicingInputTable.[Correct Greeting],
       Count(ServicingInputTable.[Correct Greeting]) AS CF1
    FROM
       ServicingInputTable.
    GROUP BY
       ServicingInputTable.[Correct Greeting]
    HAVING
       ServicingInputTable.[IDfield],
       ServicingInputTable.[Correct Greeting]=0
    ) AS qryCFails1 LEFT JOIN
    (
    SELECT
       ServicingInputTable.[IDfield],
       ServicingInputTable.[Bank Jargon Avoided],
       Count(ServicingInputTable.[Bank Jargon Avoided]) AS CF2
    FROM
       ServicingInputTable.
    GROUP BY
       ServicingInputTable.[IDfield],
       ServicingInputTable.[Bank Jargon Avoided]
    HAVING
       ServicingInputTable.[Bank Jargon Avoided]=0
    ) AS qryCFails2 ON qryCFails1.[IDfield]=qryCFails2.[IDfield])
    LEFT JOIN (
    SELECT
       ServicingInputTable.[IDfield],
       ServicingInputTable.[Positive Words],
       Count(ServicingInputTable.[Positive Words]) AS CF2
    FROM
       ServicingInputTable.
    GROUP BY
       ServicingInputTable.[IDfield],
       ServicingInputTable.[Positive Words]
    HAVING
       ServicingInputTable.[Positive Words]=0
    ) AS qryCFails3 ON qryCFails1.[IDField]=qryCFails3.[IDField]

    Thinking some more on it - not sure if that is going to give the right results either.. Perhaps gives you more of an idea to experiment with?

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  15. #15
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Multiple count - single table, fixed criteria

    Try the attached. As I should have made an example to start with.
    The queries you will be interested in are 4 and 5, but the others may help to see how I got there.

    Enjoy!
    Attached Files Attached Files

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: Multiple count - single table, fixed criteria

    thanks ecniv.

    the download isnt working for me, but will try it when i get home, could just be work ePolicy.

    will let you know how i get on

    if you fail to plan, you plan to fail

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: Multiple count - single table, fixed criteria

    Ecniv,

    query4 was just what i was looking for.

    all implemented and working correctly. and much quicker than the DCounts i had before.

    thanks
    if you fail to plan, you plan to fail

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