Creating an SQL query with multiple counts and different criteria from one table

cardycakes

I have an Access DB I have coded in VBA which my team use to log all of our requests. I want to create a query which will pull multiple counts from a single table with different criteria. An example of the table follows:

    Table TLogging
    logTypeId - Int [ this is 0 - 5 ] 
    isResolved - Boolean [ this is stored as -1 for true, 0 for false ]

I want to create a query which will output something like the following:

    Resolved, LogType1, LogType2
    True      101       39
    False     49        104
    All       150       144

So I want:

    Count of LogType1 where isResolved = True
    Count of LogType1 where isResolved = False
    Count of LogType1 where isResolved = True OR isResolved = False

And the same for LogType2.

I have already searched SO and other forums, but found no solutions. Also, CASE WHEN does not work in Access VBA.

Thanks in advance for your help!

Jean Robert

You can start with something like this:

SELECT logTypeId, Count(logTypeId) AS nbTotal, -1 * Sum(isResolved) AS nbTrue, Count(logTypeId) + Sum(isResolved) As nbFalse
FROM TLogging
GROUP BY logTypeId;

It gives about the same result as you want, but transposed.

EDIT: or this, it's ugly but i think it gives exactly the result you want

SELECT 'True' As Resolved, t0.nb As LogType0, t1.nb As LogType1, t2.nb As LogType2, t3.nb As LogType3, t4.nb As LogType4, t5.nb As LogType5
FROM (Select -1 * Sum(isResolved) As nb From TLogging Where logTypeId = 0)  AS t0
    , (Select -1 * Sum(isResolved) As nb From TLogging Where logTypeId = 1)  AS t1
    , (Select -1 * Sum(isResolved) As nb From TLogging Where logTypeId = 2)  AS t2
    , (Select -1 * Sum(isResolved) As nb From TLogging Where logTypeId = 3)  AS t3
    , (Select -1 * Sum(isResolved) As nb From TLogging Where logTypeId = 4)  AS t4
    , (Select -1 * Sum(isResolved) As nb From TLogging Where logTypeId = 5)  AS t5
Union All
SELECT 'False' As Resolved, t0.nb As LogType0, t1.nb As LogType1, t2.nb As LogType2, t3.nb As LogType3, t4.nb As LogType4, t5.nb As LogType5
FROM (Select Count(*) + Sum(isResolved) As nb From TLogging Where logTypeId = 0)  AS t0
    , (Select Count(*) + Sum(isResolved) As nb From TLogging Where logTypeId = 1)  AS t1
    , (Select Count(*) + Sum(isResolved) As nb From TLogging Where logTypeId = 2)  AS t2
    , (Select Count(*) + Sum(isResolved) As nb From TLogging Where logTypeId = 3)  AS t3
    , (Select Count(*) + Sum(isResolved) As nb From TLogging Where logTypeId = 4)  AS t4
    , (Select Count(*) + Sum(isResolved) As nb From TLogging Where logTypeId = 5)  AS t5
Union All
SELECT 'All' As Resolved, t0.nb As LogType0, t1.nb As LogType1, t2.nb As LogType2, t3.nb As LogType3, t4.nb As LogType4, t5.nb As LogType5
FROM (Select Count(*) As nb From TLogging Where logTypeId = 0)  AS t0
    , (Select Count(*) As nb From TLogging Where logTypeId = 1)  AS t1
    , (Select Count(*) As nb From TLogging Where logTypeId = 2)  AS t2
    , (Select Count(*) As nb From TLogging Where logTypeId = 3)  AS t3
    , (Select Count(*) As nb From TLogging Where logTypeId = 4)  AS t4
    , (Select Count(*) As nb From TLogging Where logTypeId = 5)  AS t5
;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL - Displaying 3 different counts on one query

From Java

How to get multiple counts with one SQL query?

From Dev

create multiple counts in one SQL query

From Dev

Get One Normal Table and Counts From Another Table In One SQL Query (JOIN)

From Dev

Join multiple tables, select counts from different tables and group by one column in one query

From Dev

Creating a table from a query using a different tablespace (Oracle SQL)

From Dev

Need sql query to pull back data that meets several groups of criteria from same table in one query

From Dev

Print counts from multiple tables in one SQL

From Dev

Get a list of all rows from one table, that satisfies multiple criteria in two different tables

From Dev

Two counts in one query with opposing where criteria

From Dev

Multiple aggregate sums from different conditions in one sql query

From Dev

SQL Query with multiple criteria

From Dev

How to combine different counts in one SQL Server query

From Dev

SQL Query to count multiple values from one table into specific view

From Dev

Get values from multiple sql server table by one query

From Dev

SQL Server - multiple counts in one query results issue

From Dev

Multiple months counts in one query

From Dev

Multiple counts in a single SQL query

From Dev

Linq to SQL to return multiple counts from query not returning correct results

From Dev

SQL subgroups from one table in one query

From Dev

SQL query with JOIN involving two criteria from same table

From Dev

Creating Hibernate Criteria Query for Tables with Multiple Relations

From Dev

SQL query based on multiple criteria

From Dev

SQL query based on multiple criteria

From Dev

Extract one column from sql table and insert into another table as multiple records coupled with different values

From Dev

Query to join multiple columns from one table

From Dev

Merging multiple rows from one table into multiple colums in a SQL Query Result Set

From Dev

while creating a table select one column from another table sql

From Dev

How to do multiple counts / exists in one query?

Related Related

  1. 1

    SQL - Displaying 3 different counts on one query

  2. 2

    How to get multiple counts with one SQL query?

  3. 3

    create multiple counts in one SQL query

  4. 4

    Get One Normal Table and Counts From Another Table In One SQL Query (JOIN)

  5. 5

    Join multiple tables, select counts from different tables and group by one column in one query

  6. 6

    Creating a table from a query using a different tablespace (Oracle SQL)

  7. 7

    Need sql query to pull back data that meets several groups of criteria from same table in one query

  8. 8

    Print counts from multiple tables in one SQL

  9. 9

    Get a list of all rows from one table, that satisfies multiple criteria in two different tables

  10. 10

    Two counts in one query with opposing where criteria

  11. 11

    Multiple aggregate sums from different conditions in one sql query

  12. 12

    SQL Query with multiple criteria

  13. 13

    How to combine different counts in one SQL Server query

  14. 14

    SQL Query to count multiple values from one table into specific view

  15. 15

    Get values from multiple sql server table by one query

  16. 16

    SQL Server - multiple counts in one query results issue

  17. 17

    Multiple months counts in one query

  18. 18

    Multiple counts in a single SQL query

  19. 19

    Linq to SQL to return multiple counts from query not returning correct results

  20. 20

    SQL subgroups from one table in one query

  21. 21

    SQL query with JOIN involving two criteria from same table

  22. 22

    Creating Hibernate Criteria Query for Tables with Multiple Relations

  23. 23

    SQL query based on multiple criteria

  24. 24

    SQL query based on multiple criteria

  25. 25

    Extract one column from sql table and insert into another table as multiple records coupled with different values

  26. 26

    Query to join multiple columns from one table

  27. 27

    Merging multiple rows from one table into multiple colums in a SQL Query Result Set

  28. 28

    while creating a table select one column from another table sql

  29. 29

    How to do multiple counts / exists in one query?

HotTag

Archive