我在這裏有一個小開發挑戰。我已在SQL Server中下表CURR_RATES
:使用範圍,例如2013年8月1日至2013年8月31日需要存儲過程來查找缺失的間隙並插入行
SELECT *
FROM (VALUES
(1001300, 'USD', 13, '8/1/2013', 1.31),
(1001301, 'USD', 13, '8/2/2013', 1.32),
(1001302, 'USD', 13, '8/5/2013', 1.33),
(1001303, 'USD', 13, '8/6/2013', 1.34),
(1001304, 'USD', 13, '8/7/2013', 1.35),
(1001305, 'USD', 13, '8/8/2014', 1.31),
(1001306, 'USD', 13, '8/9/2013', 1.32),
(1001307, 'USD', 13, '8/12/2013', 1.33),
(1001308, 'USD', 13, '8/13/2013', 1.34),
(1001309, 'USD', 13, '8/14/2013', 1.35),
(1001310, 'USD', 13, '8/15/2013', 1.36),
(1001311, 'USD', 13, '8/16/2013', 1.37),
(1001312, 'USD', 13, '8/19/2013', 1.38),
(1001313, 'USD', 13, '8/20/2013', 1.38),
(1001314, 'USD', 13, '8/21/2013', 1.37),
(1001315, 'USD', 13, '8/22/2013', 1.36),
(1001316, 'USD', 13, '8/23/2013', 1.35),
(1001317, 'USD', 13, '8/26/2013', 1.34),
(1001318, 'USD', 13, '8/27/2013', 1.33),
(1001319, 'USD', 13, '8/28/2013', 1.31),
(1001320, 'USD', 13, '8/29/2013', 1.32),
(1001321, 'USD', 13, '8/30/2013', 1.33)) as t(DATA_ID, CURR, CODE, [DATE], [RATE])
而且,我需要找到哪些日子=到週六和週日(週末)。我可以用datename(dw,CURR_RATES.DATE)函數輕鬆做到這一點。
datename(dw, CURR_RATES.DATE) as weekday from CURR_RATES
DATA_ID CURR CODE DATE RATE Weekday
----------- ---- ----------- --------- ----- -----------
1001300 USD 13 8/1/2013 1.31 Thursday
1001301 USD 13 8/2/2013 1.32 Friday
1001302 USD 13 8/5/2013 1.33 Monday
1001303 USD 13 8/6/2013 1.34 Tuesday
1001304 USD 13 8/7/2013 1.35 Wednesday
1001305 USD 13 8/8/2014 1.31 Friday
1001306 USD 13 8/9/2013 1.32 Friday
1001307 USD 13 8/12/2013 1.33 Monday
1001308 USD 13 8/13/2013 1.34 Tuesday
1001309 USD 13 8/14/2013 1.35 Wednesday
1001310 USD 13 8/15/2013 1.36 Thursday
1001311 USD 13 8/16/2013 1.37 Friday
1001312 USD 13 8/19/2013 1.38 Monday
1001313 USD 13 8/20/2013 1.38 Tuesday
1001314 USD 13 8/21/2013 1.37 Wednesday
1001315 USD 13 8/22/2013 1.36 Thursday
1001316 USD 13 8/23/2013 1.35 Friday
1001317 USD 13 8/26/2013 1.34 Monday
1001318 USD 13 8/27/2013 1.33 Tuesday
1001319 USD 13 8/28/2013 1.31 Wednesday
1001320 USD 13 8/29/2013 1.32 Thursday
1001321 USD 13 8/30/2013 1.33 Friday
我需要做的是寫一個存儲過程,以尋找失蹤的週六和週日,並插入他們行設置欄速率等於前一週五的價值。該表可以包含其他月份的數據,所以我需要能夠指定一個範圍。另外,DATA_ID列需要包含新添加的行的值。對於第一個插入的行,DATA_ID列的值應該是最後一行的值DATA_ID +1 - 第二個插入的行應該是新插入的行的DATA_ID +1,等等....所以SP需要找出表中最後一行的DATA_ID。
希望這已經夠清楚了 - 希望瞭解有關如何實現此目的的任何反饋或意見。
非常感謝您提前。
R00ty
顯示你有什麼至今。 – RBarryYoung
_「等於上一個星期五_ _即使你錯過了一個星期天,但不是它相應的星期六_? – geomagas
嗨。感謝您的回覆!總會有周末失蹤。所以對於上面的表格,星期六和星期天需要插入1,34的值。然後在第二個星期五,星期六和星期天需要插入值爲1,32等等...... – R00ter