【问题标题】:Creating an SQL query with multiple counts and different criteria from one table从一个表中创建具有多个计数和不同条件的 SQL 查询
【发布时间】:2013-08-10 07:10:05
【问题描述】:

我有一个用 VBA 编码的 Access DB,我的团队用它来记录我们所有的请求。我想创建一个查询,该查询将从具有不同条件的单个表中提取多个计数。表格示例如下:

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

我想创建一个查询,它将输出如下内容:

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

所以我想要:

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

LogType2 也是如此。

我已经搜索过 SO 和其他论坛,但没有找到解决方案。此外,CASE WHEN 在 Access VBA 中不起作用。

提前感谢您的帮助!

【问题讨论】:

    标签: sql vb.net vba ms-access


    【解决方案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
    ;
    

    【讨论】:

    • 嗨,让。感谢您的回答,但这并不能满足我的需求,因为它只是将所有内容放入多个列中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 2017-02-24
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    相关资源
    最近更新 更多