Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 每班按类型获取项目计数_Sql_Ms Access - Fatal编程技术网

Sql 每班按类型获取项目计数

Sql 每班按类型获取项目计数,sql,ms-access,Sql,Ms Access,我很难得到一个标准的查询来完成它的工作…我肯定我遗漏了一些愚蠢的东西 我有一个表,其中有两列用于此查询-该表称为“calls”,我使用的两个字段是“shiftID”和“type”。 (类型是查找字段,将包含2个固定值中的1个) 我需要查询做的就是为每个班次id计算两个固定值发生的次数-因此结果如下所示: | shiftID | Type1 | Type2 | --------------------------- | 131011 | 5 | 2 | -------------

我很难得到一个标准的查询来完成它的工作…我肯定我遗漏了一些愚蠢的东西

我有一个表,其中有两列用于此查询-该表称为“calls”,我使用的两个字段是“shiftID”和“type”。 (类型是查找字段,将包含2个固定值中的1个)

我需要查询做的就是为每个班次id计算两个固定值发生的次数-因此结果如下所示:

| shiftID | Type1 | Type2 |
---------------------------
| 131011  |   5   |   2   |
---------------------------
| 131012  |   7   |   6   |
---------------------------
| 131013  |   1   |   3   |
---------------------------
ShiftID  Type
-------  -----------
131011   fixedvalue1
131012   fixedvalue1
131013   fixedvalue2
...      ...

任何帮助都将不胜感激-谢谢

试试这个:按shiftId从通话组中选择shiftId、sum(类型1)、sum(类型2)
如果我误解了您的问题,请评论…

以下是您如何达到预期结果的方法

假设数据集如下所示:

| shiftID | Type1 | Type2 |
---------------------------
| 131011  |   5   |   2   |
---------------------------
| 131012  |   7   |   6   |
---------------------------
| 131013  |   1   |   3   |
---------------------------
ShiftID  Type
-------  -----------
131011   fixedvalue1
131012   fixedvalue1
131013   fixedvalue2
...      ...
您可以先通过以下方式对其进行转换:

SELECT
  ShiftID,
  IIF(Type = 'fixedvalue1', 1, 0) AS Type1,
  IIF(Type = 'fixedvalue2', 1, 0) AS Type2
FROM calls
这将为您提供以下行集:

ShiftID  Type1  Type2
-------  -----  -----
131011   1      0
131012   1      0
131013   0      1
...      ...    ...
对于每个
ShiftID
现在有两列,
Type1
Type2
a 1在其中一列中为1在另一列中为0。如果按
ShiftID
对该结果集进行分组,并对两列进行聚合(求和),则将获得每个
ShiftID
的两个固定类型的计数:

SELECT
  ShiftID,
  SUM(IIF(Type = 'fixedvalue1', 1, 0)) AS Type1,
  SUM(IIF(Type = 'fixedvalue2', 1, 0)) AS Type2
FROM calls
GROUP BY ShiftID

最好的做法是展示一个你已经尝试过的例子。否则,我们将只是猜测,并可能导致您走上错误的道路。
类型
字段的两个可能固定值是什么?字段是数字数据类型还是文本数据类型?