简体   繁体   English

计算选择语句中的位置和位置

[英]count where and if, within a select statement

Hi I am trying to write a stacked bar chart in mysql, and first I need to get the query right, and need some help. 嗨,我正在尝试在mysql中编写堆积的条形图,首先我需要正确查询,并且需要一些帮助。

Currently I have: 目前我有:

SELECT
COUNT(TargetDate) AS Planned,
COUNT(StartDate) AS Completed,
MONTHNAME(TargetDate) AS `Month`,
StartDate,
AssessmentStatus
FROM AuditCycleAssessments
WHERE (Year(TargetDate) = Year(now()))
GROUP BY MONTHNAME(TargetDate)
ORDER BY Month(TargetDate)

which works just fine and looks like screenshot 效果很好,看起来像截图

However I need to change the 3rd line to include its own where statement, something like... 但是我需要更改第三行以包括其自己的where语句,例如...

  (COUNT(StartDate) if AssessmentStatus=3) AS Completed,

So number 1, can someone help me achieve this. 因此,第一,有人可以帮助我实现这一目标。

Thanks 谢谢

check how to write IF But here you can not write if you should use CASE 检查如何编写IF,但是如果要使用CASE,则无法写

SELECT
COUNT(TargetDate) AS Planned,
COUNT(CASE WHEN AssessmentStatus=3 THEN StartDate END) AS Completed,
MONTHNAME(TargetDate) AS `Month`,
StartDate,
AssessmentStatus
FROM AuditCycleAssessments
WHERE (Year(TargetDate) = Year(now()))
GROUP BY MONTHNAME(TargetDate)
ORDER BY Month(TargetDate)

Do a conditional COUNT with a CASE statement 使用CASE语句执行条件COUNT

SELECT
    COUNT(TargetDate) AS Planned,
    COUNT(CASE WHEN AssessmentStatus=3 THEN StartDate END) AS Completed,
    MONTHNAME(TargetDate) AS `Month`,
    StartDate,
    AssessmentStatus
FROM AuditCycleAssessments
WHERE (Year(TargetDate) = Year(now()))
GROUP BY MONTHNAME(TargetDate)
ORDER BY Month(TargetDate)

类似于以下内容的内容可能会为您服务,未经您的情况测试,但它应该可以工作。

Sum(IIf([AssessmentStatus]=3 AND [StartDate] NOT IS NULL,1,0)) AS Completed,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM