2012-03-21 117 views
0

我有以下存儲過程,這將產生星期一至星期日,然後用一系列的創建臨時表「周」(開始和結束周):INNER JOIN碼計算值與SELECT語句

USE [test_staff] 
GO 

/****** Object: StoredProcedure [dbo].[sp_timesheets_all_staff_by_week_by_job_grouping_by_site] Script Date: 03/21/2012 09:04:49 ******/ 
SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 



CREATE PROCEDURE [dbo].[sp_timesheets_all_staff_by_week_by_job_grouping_by_site] 
    (
    @grouping_ref int, 
    @week_ref int 
    ) 
AS 

CREATE TABLE #WeeklyList 
(
    Start_Week date, 
    End_Week date, 
    week_ref int 
    --month_name date 
) 


DECLARE @REPORT_DATE DATETIME, @WEEK_BEGINING VARCHAR(10) 
SELECT @REPORT_DATE = '2011-01-19T00:00:00' 
--SELECT @REPORT_DATE = GETDATE() -- should grab the date now. 
SELECT @WEEK_BEGINING = 'MONDAY' 
IF @WEEK_BEGINING = 'MONDAY' 
SET DATEFIRST 1 
ELSE IF @WEEK_BEGINING = 'TUESDAY' 
SET DATEFIRST 2 
ELSE IF @WEEK_BEGINING = 'WEDNESDAY' 
SET DATEFIRST 3 
ELSE IF @WEEK_BEGINING = 'THURSDAY' 
SET DATEFIRST 4 
ELSE IF @WEEK_BEGINING = 'FRIDAY' 
SET DATEFIRST 5 
ELSE IF @WEEK_BEGINING = 'SATURDAY' 
SET DATEFIRST 6 
ELSE IF @WEEK_BEGINING = 'SUNDAY' 
SET DATEFIRST 7 
DECLARE @WEEK_START_DATE DATETIME, @WEEK_END_DATE DATETIME 
--GET THE WEEK START DATE 
SELECT @WEEK_START_DATE = @REPORT_DATE - (DATEPART(DW, @REPORT_DATE) - 1) 
--GET THE WEEK END DATE 
SELECT @WEEK_END_DATE = @REPORT_DATE + (7 - DATEPART(DW, @REPORT_DATE)) 
PRINT 'Week Start: ' + CONVERT(VARCHAR, @WEEK_START_DATE) 
PRINT 'Week End: ' + CONVERT(VARCHAR, @WEEK_END_DATE) 

DECLARE @Interval int = datediff(WEEK,getdate(),@WEEK_START_DATE)+1 

--SELECT [email protected]_START_DATE 
--, [email protected]_END_DATE 
--INTO #WeekList 

INSERT INTO #WeeklyList 

SELECT [email protected]_START_DATE, [email protected]_END_DATE 

WHILE @Interval <= 0 
    BEGIN 
    set @WEEK_START_DATE=DATEADD(WEEK,1,@WEEK_START_DATE) 
    set @WEEK_END_DATE=DATEADD(WEEK,1,@WEEK_END_DATE) 
    INSERT INTO #WeeklyList values (@WEEK_START_DATE,@WEEK_END_DATE) 
    SET @Interval += 1; 
END 

SELECT 
CONVERT(VARCHAR(11), Start_Week, 106) AS 'month_name', 
CONVERT(VARCHAR(11), End_Week, 106) AS 'End', 
DATEDIFF(DAY, 0, Start_Week)/7 AS week_ref -- create the unique week reference number 
--'VIEW' AS month_name 

FROM #WeeklyList 

在本節中,我創建了week_ref

DATEDIFF(DAY, 0, Start_Week)/7 AS week_ref -- create the unique week reference number 

然後我需要將它與這個選擇代碼結合:

DECLARE @YearString char(3) = CONVERT(char(3), SUBSTRING(CONVERT(char(5), @week_ref), 1, 3)) 
    DECLARE @MonthString char(2) = CONVERT(char(2), SUBSTRING(CONVERT(char(5), @week_ref), 4, 2)) 

    --Convert: 
    DECLARE @Year int = CONVERT(int, @YearString) + 1200 
    DECLARE @Month int = CONVERT(int, @MonthString) 

**--THIS FILTERS THE REPORT**  
    SELECT  ts.staff_member_ref, sm.common_name, sm.department_name, DATENAME(MONTH, ts.start_dtm) + ' ' + DATENAME(YEAR, ts.start_dtm) AS month_name, 
          ts.timesheet_cat_ref, cat.desc_long AS timesheet_cat_desc, grps.grouping_ref, grps.description AS grouping_desc, ts.task_ref, tsks.task_code, 
          tsks.description AS task_desc, ts.site_ref, sits.description AS site_desc, ts.site_ref AS Expr1, 
          CASE WHEN ts .status = 0 THEN 'Pending' WHEN ts .status = 1 THEN 'Booked' WHEN ts .status = 2 THEN 'Approved' ELSE 'Invalid Status' END AS site_status, 
          ts.booked_time AS booked_time_sum, 

    start_dtm, CONVERT(varchar(20), start_dtm, 108) + ' ' + CONVERT(varchar(20), start_dtm, 103) AS start_dtm_text, booked_time, 
    end_dtm, CONVERT(varchar(20), end_dtm, 108) + ' ' + CONVERT(varchar(20), end_dtm, 103) AS end_dtm_text 


    FROM  timesheets AS ts INNER JOIN 
          timesheet_categories AS cat ON ts.timesheet_cat_ref = cat.timesheet_cat_ref INNER JOIN 
          timesheet_tasks AS tsks ON ts.task_ref = tsks.task_ref INNER JOIN 
          timesheet_task_groupings AS grps ON tsks.grouping_ref = grps.grouping_ref INNER JOIN 
          timesheet_sites AS sits ON ts.site_ref = sits.site_ref INNER JOIN 
          vw_staff_members AS sm ON ts.staff_member_ref = sm.staff_member_ref 



    WHERE  (ts.status IN (1, 2)) AND (cat.is_leave_category = 0) 


    GROUP BY ts.staff_member_ref, sm.common_name, sm.department_name, DATENAME(MONTH, ts.start_dtm), DATENAME(YEAR, ts.start_dtm), ts.timesheet_cat_ref, 
          cat.desc_long, grps.grouping_ref, grps.description, ts.status, ts.booked_time, ts.task_ref, tsks.task_code, tsks.description, ts.site_ref, sits.description, ts.start_dtm, 
          ts.end_dtm 
    ORDER BY sm.common_name, timesheet_cat_desc, tsks.task_code, site_desc 

    DROP TABLE #WeeklyList 

    GO 

我想將week_ref傳遞給SELECT語句(請參閱comment - THIS FILTERS THE REPORT),但是問題是week_ref不是通過代碼派生的有效列。

任何想法?

回答

0

只是執行INNER JOIN你weeklylist臨時表上需要過濾的日期開始。

0

我不確定我是否真的瞭解問題,但在您發佈的代碼中發現了2個問題。 你做的#WeeklyList臨時表的插入,缺失在我假設你想成爲你的變量@week_ref

II改變這個選定的值一列,我進入決賽桌的點選擇我沒有。

希望這是解決你的問題

+0

謝謝,這個語句後:WHERE(ts.status IN(1,2))AND(cat.is_leave_category = 0)我要寫信week_ref = @ week_ref – 2012-03-21 11:47:48

+0

問題是變量week_ref心不是公認 – 2012-03-21 11:54:05

+0

對不起但我不能複製錯誤,我添加了「AND week_ref = @ week_ref」部分,並且我沒有創建sp的錯誤,我只有「無效的對象名稱'時間表'」的錯誤,因爲我沒有該表。你能發佈實際的錯誤信息嗎? – Cenas 2012-03-23 11:55:47