希望当年份更改时两个字段之间的每周差异

时间:2018-10-03 11:26:54

标签: sql-server

我正在创建以下查询来计算每周磁盘空间差异,但是当年份更改时,它将显示错误的数据,我该如何处理?

select T.*, [Weekly Diff] = T.FreeGB - T1.FreeGB, [Monthely Diff]= T.FreeGB - T2.FreeGB, T1.*,T2.* From 
(SELECT  *, [Week No]= datepart(WEEK,CreatedDate),[Month No] = datepart(MONTH,CreatedDate), [Year] =datepart(YEAR,CreatedDate) FROM  Disk_Space ) T 
LEFT JOIN  (SELECT  *
,[Week No]= datepart(WEEK,CreatedDate),[Month No] = datepart(MONTH,CreatedDate), [Year] =datepart(YEAR,CreatedDate) FROM  Disk_Space ) T1 
on T1.[Week
 No] = T.[Week No] - 1 and T.Year = T1.Year and T.ServerName =     BT1.ServerName and T.Drive = T1.Drive

1 个答案:

答案 0 :(得分:0)

通过采取以下措施解决了此问题

选择T。*,[每周差异] = T.FreeGB-T1.FreeGB,[每月差异] = T.FreeGB-T2.FreeGB 从 DISK_SPACE T
左联接磁盘_空格T1 在DATEDIFF(week,T.createddate,T1.createddate)= 1和DATEDIFF(day,T.createddate,T1.createddate)= 7上并且T.ServerName = T1.ServerName和T.Drive = T1.Drive