我有一个SQL Server表 Cascading ,结构如下:
+----+--------+----------------+ +----------------+-----------+
| ID | NodeID | ActivationTime | | ActivationTime | NodeCount |
+----+--------+----------------+ +----------------+-----------+
| 1 | 448 | 1 | | 1 | 1 |
| 2 | 195 | 2 | | 2 | 3 |
| 3 | 504 | 2 | | 3 | 6 |
| 4 | 609 | 2 | | ... | ... |
| 5 | 15 | 3 | | ... | ... |
| 6 | 31 | 3 | => | ... | ... |
| 7 | 56 | 3 | | ... | ... |
| 8 | 461 | 3 | | ... | ... |
| 9 | 585 | 3 | | ... | ... |
| 10 | 345 | 3 | | ... | ... |
| .. | ... | ... | | ... | ... |
+----+--------+----------------+ +----------------+-----------+
我需要计算NodeID出现在具有DISTINCT ActivationTime的表中的次数。例如,ActivationTime 1处有1个节点,ActivationTime 2处有3个节点,ActivationTime 3处有6个节点,依此类推。所需的结果显示在右侧。
我尝试了以下不成功的查询:
SELECT DISTINCT(ActivationTime) FROM CASCADING WHERE ActivationTIme IN
(SELECT DISTINCT(COUNT(ActivationTime)) AS NodeCount FROM CASCADING
GROUP BY ActivationTime)
答案 0 :(得分:2)
使用COUNT(DISTINCT NodeID)和GROUP BY ActivationTime
{{1}}
答案 1 :(得分:0)
SELECT COUNT(NodeCount)AS Count,ActivationTime FROM CASCADING GROUP BY ActivationTime
答案 2 :(得分:0)
通过以下方式对我有用:
select activationtime, count(*) from cascading group by activationtime;