MYSQL获取组的数量

时间:2018-04-26 07:32:15

标签: mysql asp.net

我有一个简单的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方面更好地尝试访问它?

1 个答案:

答案 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` 

似乎没有任何理由将PlatformTest Procedure测试放在HAVING而不是WHERE中。 HAVING应该用于SELECT列表中计算的列,而不是来自表行的值。