日期:2014-05-19  浏览次数:20707 次

[求教]c#+asp.net简单统计问题
数据库内容(图1):
┏━━┳━━┓
┃   ID   ┃NUM   ┃
┣━━╋━━┫
┃   α     ┃     1   ┃  
┣━━╋━━┫
┃   α     ┃     9   ┃
┣━━╋━━┫
┃   α     ┃     0   ┃
┣━━╋━━┫
┃   β     ┃     5   ┃
┣━━╋━━┫
┃   β     ┃     6   ┃
┣━━╋━━┫
┃   β     ┃     2   ┃
┗━━┻━━┛

页面显示内容(图2):
┏━━┳━━┳━━┳━━┓
┃   ID   ┃0~3   ┃4~6   ┃7~9   ┃
┣━━╋━━╋━━╋━━┫
┃   α     ┃     2   ┃     0   ┃     1   ┃
┣━━╋━━╋━━╋━━┫
┃   β     ┃     1   ┃     2   ┃     0   ┃
┗━━┻━━┻━━┻━━┛

如(图1)数据库中有α、β两组数据,每组有3个整数值,我想请教如何在页面上显示如(图2)的结果,即统计出数据库中α、β两组数据里各值的所属范围,如α中0~3的数有2个、4~6的数有0个、7~9的数有1个,β中0~3的数有1个、4~6的数有2个、7~9的数有0个。

不知我的问题说清楚没,谢谢~!

------解决方案--------------------
try:
你的sql语句写成这样就可以了,或者用如下语句生产view,然后页面上读取view就可以了
select a.id,isnull(b.total,0),isnull(c.total,0),isnull(d.total,0)
from (select distinct id from table1)a
left join (select id,count(id) total from table1 where num between 0 and 3 group by id)b on a.id=b.id
left join (select id,count(id) total from table1 where num between 4 and 6 group by id)c on a.id=c.id
left join (select id,count(id) total from table1 where num between 7 and 9 group by id)d on a.id=d.id