2017-08-01 86 views
0

我有一個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) 

回答

2

使用COUNT(DISTINCT節點ID)和GROUP BY ActivationTime

SELECT ActivationTime, COUNT(DISTINCT NodeID) NumberOfID 
FROM 
    CASCADING 
GROUP BY ActivationTime 
+2

'COUNT(DISTINCT NodeID)' – Lamak

+0

是的,我修好了。 – DVT

0

SELECT COUNT(NodeCount)AS計數,ActivationTime FROM級聯 GROUP BY ActivationTime

0

它適用於我以下:

select activationtime, count(*) from cascading group by activationtime;