MS Access SQL - 具有2个示例表的聚合计数

时间:2016-08-22 03:56:12

标签: sql ms-access aggregate-functions

我有一种情况需要从MS Access中的2表进行查询。 我是这个SQL访问的新手。

Table: UserInfo
===================
| UserID | Gender | 
===================
|  K01   |   M    |
|  K02   |   M    |
|  K03   |   F    |
|  K04   |   M    |
===================

Table: OrderInfo
===================================
| OrderID  |  Type    | UserID_FK |
===================================
|   1      |  Food    |    K01    |
|   2      |  Food    |    K01    |
|   3      |  Toolkit |    K02    |
|   4      |  Food    |    K04    |
|   5      |  Toolkit |    K03    |
===================================

问题是: 我想查询结果产生这个表,我怎么能在MS Access中做到这一点? 我在想我应该做一个子查询,但我不知道该怎么做。

Table: Summary
================================================================
| UserID | Gender |  CountOfToolkit | CountOfFood | TotalCount |
================================================================
|  K01   |   M    |       0         |      2      |     2      |
|  K02   |   M    |       1         |      0      |     1      |
|  K03   |   F    |       1         |      0      |     1      |
|  K04   |   M    |       0         |      1      |     1      |
================================================================

2 个答案:

答案 0 :(得分:2)

首先,Type是MS Access中的保留字。 你应该避免使用保留字。 话虽如此,试试这个:

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    if ((gestureRecognizer is UIPinchGestureRecognizer || gestureRecognizer is UIPanGestureRecognizer)
         && (otherGestureRecognizer is UIPinchGestureRecognizer || otherGestureRecognizer is UIPanGestureRecognizer))  {
        return false
    }
    return true
}

答案 1 :(得分:1)

此交叉表查询不依赖于只有Food& Toolkit作为您的类型 - 它将计算您添加的任何新类型。

围绕NZ的{​​{1}}确保显示0值,而Count(sType)确保它仍然被视为数字而不是文本。 您可以使用CLNG

Count(sType) AS CountOfType