2017-08-05 124 views
-3

這是一個簡單的查詢:SQL子查詢通過記錄

SELECT  fDATE, fRAIN 
FROM   tRAIN 
WHERE  (fRAIN >=5) 

檢索查詢可能是這樣的:

Query Result

現在,我想對應的時間越長,子集雨季,在這種情況下,日期爲10月9日至13日。有沒有辦法做到這一點,而不通過記錄集循環?

我感謝您的幫助。問候

+1

大小由計數確定連續幾天? –

+0

預期的結果是什麼? – Ali

+0

什麼是您的數據庫? – Turo

回答

0

我已經完成了任務,通過回答張貼在How to get count of consecutive dates

WITH Q4 AS (
SELECT Q3.fDate, Q3.RW 
FROM 
(SELECT 
RW=ROW_NUMBER() OVER(PARTITION BY GRP ORDER BY fDate) ,fDate 
FROM 
(
SELECT 
fDate, DATEDIFF(Day, '1900-01-01' , fDate)- ROW_NUMBER() OVER(ORDER BY 
fDate) AS GRP 
FROM (SELECT CONVERT(DATE,CONCAT(fYEAR , '/' , fMONTH , '/' ,fDAY),111) AS fDate, 
fRAIN 
FROM tRain 
WHERE (fRain >= _value AND fStation = '_string' AND fYear = _value) 
) Q1 
) Q2 
) Q3 
) 
SELECT (DATEADD(DAY,-Q4.RW+1,Q4.fDate)) AS Start_Date, Q4.fDate AS 
Final_Date, Q4.RW as Season_Length 
FROM Q4 
WHERE Q4.RW= (SELECT TOP 1 MAX(Q4.RW) FROM Q4) 

引導輸出看起來像:

QUERY RESULT

問候賽季