2013-10-04 75 views
-1

我相信我有一個困難的查詢撰寫。T-SQL查詢選擇當前,上一週或下週

可以說下面的圖片是結果集的一部分。

enter image description here

我需要寫一個query如果需要的話,這將使目前的工作日(週一至週五)和以前的周和下一週工作日(基於前端部分參數(link button click))

因此,可以說今天是4,那麼它將返回我30, 1, 2, 3, 4

下面是表結構。歡迎任何改進此方法的建議。

enter image description here

我在Gridview最終輸出會是這樣的。

enter image description here

+0

如果今天是第5或第6?在這些情況下,你如何界定「本週」? –

+0

如果它的第五或第六屆然後顯示下週...像'7th,8th,9th 10th,11th' – user2322507

+0

所以你需要一個窗口包含一組平日,包含給定的一天是正確的嗎? – Rikalous

回答

1
DECLARE @CurrentDate SMALLDATETIME; -- Or DATE 

SET @CurrentDate = '20131004' 

SELECT DATEADD(DAY, (DATEDIFF(DAY, 0, @CurrentDate)/7) * 7, 0) AS FirstDayOfTheWeek, 
     DATEADD(DAY, (DATEDIFF(DAY, 0, @CurrentDate)/7) * 7 + 4, 0) AS LastDayOfTheWeek 

結果:

FirstDayOfTheWeek  LastDayOfTheWeek 
----------------------- ----------------------- 
2013-09-30 00:00:00.000 2013-10-04 00:00:00.000 

星期一和星期五之間的所有天:

DECLARE @CurrentDate DATE; 
DECLARE @WeekNum SMALLINT; 

SET @CurrentDate = '20131004' 
SET @WeekNum = +1; -- -1 Previous WK, 0 Current WK, +1 Next WK 

SELECT DATEADD(DAY, dof.DayNum, fdow.FirstDayOfTheWeek) AS DayAsDateTime 
FROM (VALUES (DATEADD(DAY, (DATEDIFF(DAY, 0, @CurrentDate)/7) * 7 + @WeekNum*7, 0))) fdow(FirstDayOfTheWeek) 
CROSS JOIN (VALUES (0), (1), (2), (3), (4)) dof(DayNum) 

/* 
DayAsDateTime 
----------------------- 
2013-10-07 00:00:00.000 
2013-10-08 00:00:00.000 
2013-10-09 00:00:00.000 
2013-10-10 00:00:00.000 
2013-10-11 00:00:00.000 
*/ 

SELECT * 
FROM 
(
SELECT DATEADD(DAY, dof.DayNum, fdow.FirstDayOfTheWeek) AS DayAsDateTime, dof.DayNum 
FROM (VALUES (DATEADD(DAY, (DATEDIFF(DAY, 0, @CurrentDate)/7) * 7 + @WeekNum*7, 0))) fdow(FirstDayOfTheWeek) 
CROSS JOIN (VALUES (0), (1), (2), (3), (4)) dof(DayNum) 
) src 
PIVOT(MAX(DayAsDateTime) FOR DayNum IN ([0], [1], [2], [3], [4])) pvt 

/* 
0      1      2      3      4 
----------------------- ----------------------- ----------------------- ----------------------- ----------------------- 
2013-10-07 00:00:00.000 2013-10-08 00:00:00.000 2013-10-09 00:00:00.000 2013-10-10 00:00:00.000 2013-10-11 00:00:00.000 
*/ 
+0

上週和下週的選擇情況如何?那中間的3天呢? –

+0

@Bogdan ......這不是我真正想要的。 – user2322507

+0

@ user2322507我更新了我的答案。 –

3

這裏是我最初是如何解釋的問題:給我的日期本週,以及可選的上一週和下一週。我的解決方案會有一樣的,不管您是否已經有一個完整的日期(奇怪的是,沒有DATE列)的表:

-- these are your unmentioned parameters 
DECLARE @include_previous_week BIT = 1, @include_next_week BIT = 1; 


SET DATEFIRST 1; -- Monday 

DECLARE @today DATE = SYSDATETIME(); 
DECLARE @wd SMALLINT = DATEPART(WEEKDAY, @today); 
SET @wd -= CASE WHEN @wd < 6 THEN 0 ELSE 7 END; 

;WITH n AS 
(
    SELECT TOP (5) rn = ROW_NUMBER() OVER (ORDER BY [object_id]) 
    FROM sys.all_objects ORDER BY [object_id] 
) 
SELECT the_date = DATEADD(DAY, [email protected], @today) FROM n 
UNION ALL 
SELECT DATEADD(DAY, [email protected], @today) FROM n 
WHERE @include_previous_week = 1 
UNION ALL 
SELECT DATEADD(DAY, [email protected], @today) FROM n 
WHERE @include_next_week = 1 
ORDER BY the_date; 
+0

@aaron ....讓我說我的上一個按鈕點擊,它顯示了上週的數據。如果再次點擊上一個按鈕,它會再拖一週嗎? – user2322507

+0

@ user2322507您的按鈕點擊將不得不保持狀態並將當前(或選定)日期傳入。上面的代碼僅使用今天的日期作爲參數。 –

+0

+1 ...我得到你的觀點 – user2322507