2

这基本上就是我想要实现的目标;

SELECT 
COUNT(DISTINCT No) 
FROM 
Table 
WHERE 
(Type = 1 AND COUNT(DISTINCT No) > Num) 
OR (Type = 2 AND COUNT(DISTINCT No) > Num) 
GROUP BY
Week

实现这一目标的最佳方法是什么?

谢谢

4

4 回答 4

2

任何聚合比较都需要在 Group By 之后进入 HAVING 子句。请记住,根据您放置非聚合条件的位置,您可能会得到不同的结果,因为您将汇总通过 WHERE 包含/排除的内容。

IE

SELECT COUNT(DISTINCT No) 
FROM Table 
WHERE Type = 1
GROUP BY Week
HAVING COUNT(DISTINCT No) > Num

可能不同于

SELECT COUNT(DISTINCT No) 
FROM Table 
GROUP BY Week
HAVING COUNT(DISTINCT No) > Num
AND Type = 1
于 2013-05-10T08:56:36.427 回答
1
SELECT 
COUNT(DISTINCT No) 
FROM 
Table 
WHERE Type = 1 OR Type = 2
GROUP BY
Week
Having COUNT(DISTINCT No) > Num

这是可能的答案。

于 2013-05-10T08:56:05.877 回答
0

用这个:

SELECT 
COUNT(DISTINCT No) as num1
FROM TableName
WHERE Type IN (1,2)  
GROUP BY Week
HAVING num1 > Num
于 2013-05-10T08:56:35.510 回答
0
  SELECT Week, Type, COUNT(DISTINCT No) WeekTally
    FROM   [Table]
   WHERE  Type in (1,2)
GROUP BY Week, Type
  HAVING COUNT(DISTINCT No) > @Num
ORDER BY Week, Type;

如果您按周分组,您肯定想查看计数是针对哪一周的?!

于 2013-05-10T08:58:37.050 回答