2017-04-26 82 views
0

我有一個這樣的SQL存儲過程的查詢結果(見下文)與日期函數別名SQL列字段

EmpID FirstName Dept  JoinDate Code1  Code2 Code3 Code4 
01  aaa  service  2017-02-11  0  2  3  5 
02  bbb  customerCare 2010-01-23  1  4  7  9 
03  ccc  Receptionist 2009-12-20  2  5  1  8 

所有這些字段從目前存在的表通過用條件接合服用。(我創建了一個選擇存儲過程來執行每月的這第一個工作日)

我的要求是,我想要替換所有的代碼字段的開始日期。例如,

如果我在2017年3月1日執行此存儲過程,結果應該是這樣的

EmpID FirstName Dept  JoinDate 2016-12-01 2017-01-01 2017-02-01 2017-03-01     
01  aaa  service  2017-02-11 0  2  3  5 
02  bbb  customerCare 2010-01-23 1  4  7  9 
03  ccc  Receptionist 2009-12-20 2  5  1  8 

例2: 如果我執行4月1日2017年這個存儲過程,結果應該是像這樣

*

EmpID FirstName Dept JoinDate  2017-01-01 2017-02-01 2017-03-01 2017-04-01 
    01 aaa  service 2017-02-11   0  2   3   5 
    02 bbb  customerCare 2010-01-23 1  4   7   9 
    03 ccc  Receptionist 2009-12-20 2  5   1   8 

*

這menas,

  • 碼4場別名應該是當月的第一天

  • CODE3場別名應該是當月-1

  • 碼2場別名的第一天應該是本月的第一天-2

  • code1字段別名應該是本月的第一天-3

我怎樣才能做到這一點?我正在使用SQL服務器2014. 我無法找到如何將字段名稱與我們在sql server中連接多個表的日期進行別名。 請幫幫我。感謝您花時間閱讀我的問題,並非常感謝您的幫助。

+0

你可能不得不使用動態SQL與PIVOT到一個月行變爲列。 – EMUEVIL

+0

只需要動態查詢 – DhruvJoshi

+0

@DhruvJoshi你是對的。你可以使用DATEADD來獲取值。 – EMUEVIL

回答

2

裝上去像下面爲您的存儲過程。這將得到當前日期,找到本月的第一個月,以及前3個月。只需在你自己的桌子上。您必須將其餘查詢放入Dynamic SQL @query字符串以及非代碼列中。讓我知道你是否需要任何幫助。

DECLARE @code4 date; SET @code4 = CAST(DATEADD(DAY,(DATEPART(DAY, GETDATE()-1)) * -1, GETDATE()) AS date) 
DECLARE @code3 date; SET @code3 = DATEADD(MM, -1, @code4) 
DECLARE @code2 date; SET @code2 = DATEADD(MM, -2, @code4) 
DECLARE @code1 date; SET @code1 = DATEADD(MM, -3, @code4) 

DECLARE @query NVARCHAR(MAX); 
DECLARE @selects NVARCHAR(MAX); 
set @selects = 'code1 AS [' + CAST(@code1 as nvarchar) + '], code2 AS [' + CAST(@code2 as nvarchar) + '], code3 AS [' + CAST(@code3 as nvarchar) + '], code4 AS [' + CAST(@code4 as nvarchar) + ']' 
set @query = 'SELECT ' + @selects + ' FROM [dbo].[myTable]' 

EXEC(@query) 
+0

非常感謝你..我會嘗試這一點,並讓你叩頭 – Anj

+0

它完美..Thank你非常非常EMUEVIL – Anj

+0

@Anj太好了!聽到那個消息很開心。 – EMUEVIL

1
IF Object_ID('tempdb..#temp') IS NOT NULL 
Drop table #temp 

;WITH cte(EmpID,FirstName, Dept,JoinDate,Code1, Code2,Code3,Code4) 
AS 
(
SELECT 01, 'aaa', 'service'  ,'2017-02-11',0, 2, 3, 5 union all 
SELECT 02, 'bbb', 'customerCare' , '2010-01-23',1, 4, 7, 9 union all 
SELECT 03, 'ccc', 'Receptionist' , '2009-12-20',2, 5, 1, 8 
) 
SELECT * INTO #temp FROM cte 


DECLARE @Currentmonth DATE='2017-03-01',--Change date like 2017-04-01,2017-03-01 
@AllDAtes VARCHAR(100), 
@Lastmonth DATE,  
@LastBeforemonth DATE, 
@LastBeforemonth2 DATE, 
@query NVARCHAR(MAX), 
@selects NVARCHAR(MAX); 

DECLARE @Lastmonths TABLE (Currentmonth DATE,Lastmonth DATE,LastBeforemonth DATE,LastBeforemonth2 DATE) 
INSERT INTO @Lastmonths 

SELECT @Currentmonth AS Currentmonth, 
    DATEADD(month, -1, DATEADD(day, 1 - day(@Currentmonth), @Currentmonth))AS Lastmonth, 
    DATEADD(month, -2, DATEADD(day, 1 - day(@Currentmonth), @Currentmonth))AS LastBeforemonth, 
    DATEADD(month, -3, DATEADD(day, 1 - day(@Currentmonth), @Currentmonth))AS LastBeforemonth2 

SET @[email protected] 
SELECT @Lastmonth= Lastmonth ,@LastBeforemonth= LastBeforemonth,@LastBeforemonth2=LastBeforemonth2 FROM @Lastmonths 

SET @selects = 'code1 AS [' + CAST(@LastBeforemonth2 as nvarchar) + '], code2 AS [' + CAST(@LastBeforemonth as nvarchar) + '], code3 AS [' + CAST(@Lastmonth as nvarchar) + '], code4 AS [' + CAST(@Currentmonth as nvarchar) + ']' 

SET @query = 'SELECT EmpID,FirstName, Dept,JoinDate , ' + @selects + ' FROM #temp' 

EXEC (@query) 
+0

@Anji嘗試通過改變月份數字我的查詢關鍵詞,比如4,3你會得到列名動態如您所料 – 2017-04-26 15:43:23

+0

非常感謝你。我作者Srini會嘗試這個 – Anj

+0

謝謝你非常非常作者Srini ......我花了一些代碼,你和emuevil ..非常感謝..這是我想要的方式 – Anj