每个月的数据都会持续到12月。我希望得到一个新的列作为“总和”,这将总结到上个月。手段,现在是八月。因此,总和应该放到7月,如下面“总和”栏中的第一个条目所示。
Jan| Feb| Mar| Apr| May| Jun |Jul |Aug |Sum
21 | 28 | 26 | 31 | 54 | 67 |38 |29 |265
11 | 44 | 66 | 7 | 88 | 54 |90 |74 |
13 | 45 | 26 | 38 | 36 | 39 |67 |49 |
76 | 35 | 67 | 23 | 76 | 54 |35 |59 |
答案 0 :(得分:2)
由于月份已关闭,我更喜欢这个简单的解决方案:
SELECT *,
CASE WHEN DATEPART(MONTH, @date)<=1 THEN 0 ELSE Jan END +
CASE WHEN DATEPART(MONTH, @date)<=2 THEN 0 ELSE Feb END +
CASE WHEN DATEPART(MONTH, @date)<=3 THEN 0 ELSE Mar END +
CASE WHEN DATEPART(MONTH, @date)<=4 THEN 0 ELSE Apr END +
CASE WHEN DATEPART(MONTH, @date)<=5 THEN 0 ELSE May END +
CASE WHEN DATEPART(MONTH, @date)<=6 THEN 0 ELSE Jun END +
CASE WHEN DATEPART(MONTH, @date)<=7 THEN 0 ELSE Jul END +
CASE WHEN DATEPART(MONTH, @date)<=8 THEN 0 ELSE Aug END +
CASE WHEN DATEPART(MONTH, @date)<=9 THEN 0 ELSE Sep END Sum
--Append 3 more months to DECEMBER
FROM Months
答案 1 :(得分:0)
SELECT
Jan,Feb,Mar,Apr, and so on...
,COALESCE(Jan,0) + COALESCE(Feb,0) + COALESCE(Mar,0) + COALESCE(Apr,0)..(till Dec) AS "Sum"
FROM MyTable
如果行entrie为NULL,则SUM将为NULL,因此COALESCE-Function将把NULL变成ZERO
答案 2 :(得分:0)
public function rules() {
return [
'num_of_devices' => 'nullable | integer'
];
}