我有一个简单的MySQL,可以根据WHERE和HAVING子句返回分组数据。
SELECT Platform, `Test Procedure`, `Order Number`
FROM Stage
WHERE (`StartTime` BETWEEN "2018-4-23" AND DATE_ADD("2018-4-24",INTERVAL 1 DAY))
GROUP BY `Order Number`
HAVING (Stage.Platform = "Kingston") AND (Stage.`Test Procedure` = "Calibration and test")
我需要计算群组数量,而不是每个群组中的记录数量。
是否可以在SQL语句中使用,或者我是否可以通过ASPX.CS方面更好地尝试访问它?
答案 0 :(得分:0)
加入另一个获取所需计数的查询。
SELECT Platform, `Test Procedure`, `Order Number`, `Order Count`
FROM Stage
CROSS JOIN (
SELECT COUNT(DISTINCT `Order Number`) AS `Order Count`
FROM Stage
WHERE `StartTime` BETWEEN "2018-4-23" AND DATE_ADD("2018-4-24",INTERVAL 1 DAY)
AND Stage.Platform = "Kingston" AND Stage.`Test Procedure` = "Calibration and test"
) AS x
WHERE (`StartTime` BETWEEN "2018-4-23" AND DATE_ADD("2018-4-24",INTERVAL 1 DAY))
AND (Stage.Platform = "Kingston") AND (Stage.`Test Procedure` = "Calibration and test")
GROUP BY `Order Number`
似乎没有任何理由将Platform
和Test Procedure
测试放在HAVING
而不是WHERE
中。 HAVING
应该用于SELECT
列表中计算的列,而不是来自表行的值。