2015-10-21 50 views
0

原始表:一些條件SQL計數

Initialdate OriginalTime Gate1 Destindate DestinationTime Gate2 
20151001 00   A  20151001 00    B 
20151001 00   A  20151001 00    B 
20151001 00   A  20151001 00    C 
20151001 01   A  20151001 01    A 
20151001 01   A  20151001 01    B 
20151001 01   A  20151001 01    B 
20151001 01   A  20151001 01    B 
20151001 01   B  20151001 01    A 

如何獲得表如下:

Initialdate OriginalTime Gate1 Destindate DestinationTime Gate2 Freq 
20151001 00   A  20151001 00    B  2 
20151001 00   A  20151001 00    C  1 
20151001 01   A  20151001 01    A  1 
20151001 01   A  20151001 01    B  3 
20151001 01   B  20151001 01    A  1 

想到這麼多天來解決這個問題,但我不能處理它。

+1

你可以發佈你試過嗎? –

+0

一個簡單的'COUNT'將解決這個問題:'SELECT *, \t頻率= COUNT(*)FROM TBL GROUP BY \t InitialDate,OriginalTime,GATE1, \t DestinDate,DestinationTime,Gate2' –

+0

我認爲它是一個複雜的問題,所以我試圖使用IF THEN ELSE來做到這一點。但沒有達到目標。 – Tiger93

回答

0

這樣做。

SELECT Initialdate, OriginalTime, Gate1, 
     Destindate, DestinationTime, Gate2, 
     COUNT(1) as Freq 
FROM TABLE1 
GROUP BY Initialdate, OriginalTime, Gate1, 
     Destindate, DestinationTime, Gate2 
+0

非常感謝。^^ – Tiger93

0

可以在SQL2005 +使用Window功能:

SELECT DISTINCT *, 
     COUNT(*) OVER(PARTITION BY Initialdate, OriginalTime, Gate1, Destindate, DestinationTime, Gate2) AS Freq 
FROM Your_Table