管理数据表中的记录

时间:2016-06-08 05:34:44

标签: asp.net sql-server c#-4.0 datatable

我有数据区,有数据区名,进度百分比和计数。

District      Progress %      Count

Amritsar      100            2
Amritsar      89             3
Amritsar       0             1
Amritsar       24            5
Barnala        70            2
Barnala        55            3
Mohali         15            5
Mohali         100           3
Mohali         30            5

依旧......

我想在数据表中显示数据,其中记录的数量来自特定进度%Range

District     0%      1-25%      26-50%    51-75%     76-99%      100%

Amritsar     1        5          0          0          3          2
Barnala      0        0          0          5          0          0
Mohali       0        5          5          0          0          3

如此......

请介绍如何执行此操作。感谢..

1 个答案:

答案 0 :(得分:2)

使用SQL,您可以使用条件聚合

来完成
SELECT District,
       COUNT(CASE WHEN Progress = 0 THEN 1 END) AS '0%',
       COUNT(CASE WHEN Progress BETWEEN 1 AND 25 THEN 1 END) AS '1-25%',
       COUNT(CASE WHEN Progress BETWEEN 26 AND 50 THEN 1 END) AS '26-50%',
       COUNT(CASE WHEN Progress BETWEEN 51 AND 75 THEN 1 END) AS '51-75%',
       COUNT(CASE WHEN Progress BETWEEN 76 AND 99 THEN 1 END) AS '76-99%',
       COUNT(CASE WHEN Progress = 100 THEN 1 END) AS '100%'
FROM mytable
GROUP BY District