如何从表中的同一列“计数”两个不同的条件?

时间:2019-06-19 18:22:47

标签: sql ms-access

我正在尝试编写一个按天分组的简单查询,在该列中填充当天的“有效”和“运行中”点数,在另一列中填充所有“有效”点数。因此结果列将为:“日”,“运行号”,“总计”。它从具有“ TheDate”,“ Running”和“ ValidPoint”列的表中读取。

我可以通过更改“ FROM”语句以适合该列的特定条件来分别创建“ RunNum”和“ TotalNum”列。但是,我是SQL的完整入门者,我需要一些有关如何从一张表中读取两个不同条件的指导。

这是我为创建RunNum编写的内容:

SELECT TheDate AS Day, count(Running) AS RunNum
FROM Source_Table
WHERE Running = "Runtime" AND ValidPoint = "yes"
GROUP BY TheDate;

这将创建TotalNum:

SELECT TheDate AS Day, count(Running) AS TotalNum
FROM Source_Table
WHERE ValidPoint = "yes"
GROUP BY TheDate;

我尝试使用this post中概述的SUM(CASE WHEN ...)方法,但是当我尝试运行它时,它始终抛出语法错误消息:

SELECT TheDate As Day, SUM(CASE WHEN Running = "Runtime" THEN 1 ELSE 0 END) AS RunNum, COUNT(Running) AS TotalNum
FROM Source_Table
WHERE ValidPoint = "yes"
GROUP BY TheDate;

我怀疑我将需要在from语句中使用表“别名”,但是到目前为止我所读到的所有内容都令人困惑。在正确的方向上进行进一步研究的任何帮助或指导将不胜感激。感谢您抽出宝贵的时间阅读本文!

1 个答案:

答案 0 :(得分:2)

在MS Access中,条件聚合使用{{ Form::select('nombre_dep', $departamentos) }} 而不是 public void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterForNavigation<ProjectNavigation>(); // This line throws containerRegistry.RegisterSingleton<IProjectService, ProjectService>();}

IIF()