更新多個變量我有一個table
與金額的總和每個工作日(星期日至星期六)。表結構如下。在單個查詢
我需要將這些表值分配到的參數。對於E.g:我需要將rdate '2015-11-15'
的總和,即324
賦值給變量@sundayval,374賦給變量@mondayval等...
如何在單個更新查詢中執行此操作。我已經嘗試過使用Case語句, 但是它只給變量@saturdayval賦值。
感謝您的幫助。
更新多個變量我有一個table
與金額的總和每個工作日(星期日至星期六)。表結構如下。在單個查詢
我需要將這些表值分配到的參數。對於E.g:我需要將rdate '2015-11-15'
的總和,即324
賦值給變量@sundayval,374賦給變量@mondayval等...
如何在單個更新查詢中執行此操作。我已經嘗試過使用Case語句, 但是它只給變量@saturdayval賦值。
感謝您的幫助。
好 - 讓我們做這樣說:在else
情況下被設置爲回報本身,因此每個變量基本上聚集的聚結。注意:我現在沒有任何方法來測試它。 :)
SELECT
@sundayval = case when DATEPART(weekday, rdate) = 1 then sum else @sundayval end
, @mondayval = case when DATEPART(weekday, rdate) = 2 then sum else @mondayval end
, @tuesdayval = case when DATEPART(weekday, rdate) = 3 then sum else @tuesdayval end
, @wednesdayval = case when DATEPART(weekday, rdate) = 4 then sum else @wednesdayval end
, @thursdayval = case when DATEPART(weekday, rdate) = 5 then sum else @thursdayval end
, @fridayval = case when DATEPART(weekday, rdate) = 6 then sum else @fridayval end
, @saturdayval = case when DATEPART(weekday, rdate) = 7 then sum else @saturdayval end
FROM TABLE
因此,它處理一行,比如說,對於星期二,並給@ tuesdayval一個值並將所有其他變量設置爲null,然後處理第二行,例如星期五,並給@ fridayval一個值和所有其他變量(*包括* '@ tuesdayval')都設置爲null。我建議在這裏需要一些聚集。 –
像@Damien_The_Unbeliever說,它給我的只有saturdayval即最後一天的總和。同我曾嘗試 – bmsqldev
已更新的答案。聚合是....有點。 –
我不知道單UPDATE
語句是否會做到這一點。我已爲每個Variable
分配值,如下所示,
SELECT @SunTotal = [sum]
FROM [table]
where rdate = @Startdate
SELECT @MonTotal = [sum]
FROM [table]
where rdate = DATEADD(DAY,1,@Startdate)
SO ON...
我很想知道我的更新答案是否能正常工作...... :) –
這樣做的工作。它不依賴於任何particulat DATEFIRST
設置 - 它,而不是使用任意選擇週日(我挑了今年5月17日)(我通常稱其爲「已知良好」的日期,因爲它具有我們正在尋找的屬性,在這種情況下,本週的天權):
declare @t table ([sum] int not null,rdate datetime2 not null)
insert into @t([sum],rdate) values
(324,'20151115'),
(374,'20151116'),
(424,'20151117'),
(474,'20151118'),
(524,'20151119'),
(574,'20151120'),
(624,'20151121')
declare @sundayval int
declare @mondayval int
declare @tuesdayval int
declare @wednesdayval int
declare @thursdayval int
declare @fridayval int
declare @saturdayval int
select
@sundayval = SUM(CASE WHEN DATEPART(weekday,rdate) = DATEPART(weekday,'20150517') THEN [sum] END),
@mondayval = SUM(CASE WHEN DATEPART(weekday,rdate) = DATEPART(weekday,'20150518') THEN [sum] END),
@tuesdayval = SUM(CASE WHEN DATEPART(weekday,rdate) = DATEPART(weekday,'20150519') THEN [sum] END),
@wednesdayval = SUM(CASE WHEN DATEPART(weekday,rdate) = DATEPART(weekday,'20150520') THEN [sum] END),
@thursdayval = SUM(CASE WHEN DATEPART(weekday,rdate) = DATEPART(weekday,'20150521') THEN [sum] END),
@fridayval = SUM(CASE WHEN DATEPART(weekday,rdate) = DATEPART(weekday,'20150522') THEN [sum] END),
@saturdayval = SUM(CASE WHEN DATEPART(weekday,rdate) = DATEPART(weekday,'20150523') THEN [sum] END)
from @t
select @sundayval,@mondayval,@tuesdayval,@wednesdayval,@thursdayval,@fridayval,@saturdayval
結果:
----------- ----------- ----------- ----------- ----------- ----------- -----------
324 374 424 474 524 574 624
你知道,時間提前,具體日期要存儲這些值? –
一旦這些變量被賦值,你將如何處理這些變量?一般來說,最好儘可能長時間地將數據保存在表格數據結構中(最好是直到數據退出SQL Server和其他事務(報表生成器,應用程序等)接管數據結構的責任處理 –
像@Damien_The_Unbeliever說,它給了我星期六的總和,也就是最後一天。和我試過的一樣 – bmsqldev