SQL Sum除了相邻月份的权重

时间:2015-07-31 07:29:33

标签: sql sql-server sum

我有下表(样本):

+----+--------+-------+------+------------+
| ID | WEIGHT | MONTH | YEAR | CATEGORYID |
+----+--------+-------+------+------------+
|  1 | 0.5    |     1 | 2014 | A          |
|  1 | 0.5    |     1 | 2014 | A          |
|  1 | 0.5    |     2 | 2014 | A          |
|  1 | 0.2    |     2 | 2014 | C          |
|  1 | 0.2    |     2 | 2014 | C          |
|  2 | 1.0    |     2 | 2014 | B          |
|  2 | 1.0    |     2 | 2014 | B          |
+----+--------+-------+------+------------+

我想要的输出就像这样(样本):

+----+--------+-------+------+------------+
| ID | WEIGHT | MONTH | YEAR | CATEGORYID |
+----+--------+-------+------+------------+
|  1 | 1.5    |     1 | 2014 | A          |
|  1 | 1.5    |     2 | 2014 | A          |
|  1 | 0.4    |     1 | 2014 | C          |
|  1 | 0.4    |     2 | 2014 | C          |
|  2 | 2.0    |     2 | 2014 | B          |
|  2 | 2.0    |     3 | 2014 | B          |
+----+--------+-------+------+------------+

因此,当月份休息时,我仍然希望将前一个月的重量加总到当前等等。我想总结一下特定ID的权重。类别ID。

2 个答案:

答案 0 :(得分:1)

希望这有效。

$('.myLinks').click(function (e) {
     e.preventDefault();
     document.getElementById('textTemp').value += $(this).text() + " ";
});

答案 1 :(得分:0)

试试这个

SELECT ID,
       Sum(s_weight)OVER(partition BY CATEGORYID, id),
       MONTH,
       YEAR,
       CATEGORYID
FROM   (SELECT ID,
               Sum(weight) AS s_weight,
               MONTH,
               YEAR,
               CATEGORYID
        FROM   Yourtable
        GROUP  BY ID,
                  MONTH,
                  YEAR,
                  CATEGORYID) a