0

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!

4

1 回答 1

1

你可以从这样的事情开始:

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

它给出了与您想要的结果大致相同的结果,但已转置。

编辑:或者这个,它很丑,但我认为它给出了你想要的结果

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
;
于 2013-08-08T14:25:01.827 回答