如何对每个月+总数进行拆分并获得一笔金额?

时间:2016-12-12 10:04:38

标签: sql sql-server

- 已编辑 -

我的表格中有一些员工数据 sal (薪水)/ ID 名称 -

[{
   "cpnSeqNbr": 0,
   "cpnStatusCd": 10,
   "cmpgnId": 37524,
   "cpnSkuNbr": 49579,
   "redeemEndDt": "        "
}]

我实现了每 ID 名称的行, 每个月的总和列 sal +所有列的总和。
(P.s. months显示从四月开始)

+----+------+-------+-------+------+
| id | name | head  | month | sal  |
+----+------+-------+-------+------+
| 1  | A    | basic | jan   | 1000 |
+----+------+-------+-------+------+
| 1  | A    | hra   | jan   | 500  |
+----+------+-------+-------+------+
| 1  | A    | basic | feb   | 1000 |
+----+------+-------+-------+------+
| 1  | A    | hra   | feb   | 500  |
+----+------+-------+-------+------+
| 1  | A    | basic | mar   | 1000 |
+----+------+-------+-------+------+
| 1  | A    | hra   | mar   | 500  |
+----+------+-------+-------+------+
| 1  | A    | basic | apr   | 1000 |
+----+------+-------+-------+------+
| 1  | A    | hra   | apr   | 500  |
+----+------+-------+-------+------+
| 1  | A    | basic | may   | 1000 |
+----+------+-------+-------+------+
| 1  | A    | hra   | may   | 500  |
+----+------+-------+-------+------+
| 1  | A    | basic | june  | 1000 |
+----+------+-------+-------+------+
| 1  | A    | hra   | june  | 500  |
+----+------+-------+-------+------+
| 1  | A    | basic | july  | 1000 |
+----+------+-------+-------+------+
| 1  | A    | hra   | july  | 500  |
+----+------+-------+-------+------+
| 1  | A    | basic | aug   | 1000 |
+----+------+-------+-------+------+
| 1  | A    | hra   | aug   | 500  |
+----+------+-------+-------+------+
| 1  | A    | basic | sep   | 1000 |
+----+------+-------+-------+------+
| 1  | A    | hra   | sep   | 500  |
+----+------+-------+-------+------+
| 1  | A    | basic | oct   | 1000 |
+----+------+-------+-------+------+
| 1  | A    | hra   | oct   | 500  |
+----+------+-------+-------+------+
| 1  | A    | basic | nov   | 1000 |
+----+------+-------+-------+------+
| 1  | A    | hra   | nov   | 500  |
+----+------+-------+-------+------+
| 1  | A    | basic | dec   | 1000 |
+----+------+-------+-------+------+
| 1  | A    | hra   | dec   | 500  |
+----+------+-------+-------+------+
|    |      |       |       |      |
+----+------+-------+-------+------+
| 2  | B    | basic | mar   | 2000 |
+----+------+-------+-------+------+
| 2  | B    | hra   | mar   | 500  |
+----+------+-------+-------+------+
| 2  | B    | basic | apr   | 2000 |
+----+------+-------+-------+------+
| 2  | B    | hra   | apr   | 500  |
+----+------+-------+-------+------+
| 2  | B    | basic | may   | 2000 |
+----+------+-------+-------+------+
| 2  | B    | hra   | may   | 500  |
+----+------+-------+-------+------+
| 2  | B    | basic | june  | 2000 |
+----+------+-------+-------+------+
| 2  | B    | hra   | june  | 500  |
+----+------+-------+-------+------+
| 2  | B    | basic | july  | 2000 |
+----+------+-------+-------+------+
| 2  | B    | hra   | july  | 500  |
+----+------+-------+-------+------+
| 2  | B    | basic | aug   | 2000 |
+----+------+-------+-------+------+
| 2  | B    | hra   | aug   | 500  |
+----+------+-------+-------+------+
| 2  | B    | basic | sep   | 2000 |
+----+------+-------+-------+------+
| 2  | B    | hra   | sep   | 500  |
+----+------+-------+-------+------+
| 2  | B    | basic | oct   | 2000 |
+----+------+-------+-------+------+
| 2  | B    | hra   | oct   | 500  |
+----+------+-------+-------+------+
| 2  | B    | basic | nov   | 2000 |
+----+------+-------+-------+------+
| 2  | B    | hra   | nov   | 500  |
+----+------+-------+-------+------+
| 2  | B    | basic | dec   | 2000 |
+----+------+-------+-------+------+
| 2  | B    | hra   | dec   | 500  |
+----+------+-------+-------+------+
|    |      |       |       |      |
+----+------+-------+-------+------+
| 3  | C    | basic | jan   | 5000 |
+----+------+-------+-------+------+
| 3  | C    | hra   | jan   | 500  |
+----+------+-------+-------+------+
| 3  | C    | basic | feb   | 5000 |
+----+------+-------+-------+------+
| 3  | C    | hra   | feb   | 500  |
+----+------+-------+-------+------+
| 3  | C    | basic | mar   | 5000 |
+----+------+-------+-------+------+
| 3  | C    | hra   | mar   | 500  |
+----+------+-------+-------+------+
| 3  | C    | basic | apr   | 5000 |
+----+------+-------+-------+------+
| 3  | C    | hra   | apr   | 500  |
+----+------+-------+-------+------+
| 3  | C    | basic | may   | 5000 |
+----+------+-------+-------+------+
| 3  | C    | hra   | may   | 500  |
+----+------+-------+-------+------+
| 3  | C    | basic | june  | 5000 |
+----+------+-------+-------+------+
| 3  | C    | hra   | june  | 500  |
+----+------+-------+-------+------+
| 3  | C    | basic | july  | 5000 |
+----+------+-------+-------+------+
| 3  | C    | hra   | july  | 500  |
+----+------+-------+-------+------+
| 3  | C    | basic | aug   | 5000 |
+----+------+-------+-------+------+
| 3  | C    | hra   | aug   | 500  |
+----+------+-------+-------+------+
| 3  | C    | basic | sep   | 5000 |
+----+------+-------+-------+------+
| 3  | C    | hra   | sep   | 500  |
+----+------+-------+-------+------+

1 个答案:

答案 0 :(得分:0)

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">

<select>
<option ng-repeat="item in items" ng-click="clicked()"     ng-if="item.area=='India'">{{item.name}}</option>
</select>

</div>
select      id,name,head
           ,apr,may,june,july,aug,sep,oct,nov,dec,jan,feb,mar
           ,apr+may+june+july+aug+sep+oct+nov+dec+jan+feb+mar   as total

from       (select      id
                       ,name
                       ,head

                       ,coalesce (apr  ,0)    as apr
                       ,coalesce (may  ,0)    as may
                       ,coalesce (june ,0)    as june
                       ,coalesce (july ,0)    as july
                       ,coalesce (aug  ,0)    as aug
                       ,coalesce (sep  ,0)    as sep
                       ,coalesce (oct  ,0)    as oct
                       ,coalesce (nov  ,0)    as nov
                       ,coalesce (dec  ,0)    as dec
                       ,coalesce (jan  ,0)    as jan
                       ,coalesce (feb  ,0)    as feb
                       ,coalesce (mar  ,0)    as mar

            from        mytable pivot (sum(sal) for month in
                            (apr,may,june,july,aug,sep,oct,nov,dec,jan,feb,mar)) p      
            ) p
相关问题