我有一個SQL Server表級聯用以下結構使用COUNT和DISTINCT:在同一個SQL Server查詢
+----+--------+----------------+ +----------------+-----------+
| 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 | | ... | ... |
| .. | ... | ... | | ... | ... |
+----+--------+----------------+ +----------------+-----------+
我需要計算的時間節點ID是出現在表中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)
'COUNT(DISTINCT NodeID)' – Lamak
是的,我修好了。 – DVT