首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

新手上路,SQL循环

2012-04-26 
新手上路,求助SQL循环SELECTcount(*)FROMtbl_AlarmHistorywhereyear(iResTime)2006andmonth(iResTime)1a

新手上路,求助SQL循环
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=1   and   iAlarmType=0
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=1   and   iAlarmType=1
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=1   and   iAlarmType=2
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=1   and   iAlarmType=3
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=2   and   iAlarmType=0
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=2   and   iAlarmType=1
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=2   and   iAlarmType=2
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=2   and   iAlarmType=3
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=3   and   iAlarmType=0
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=3   and   iAlarmType=1
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=3   and   iAlarmType=2
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=3   and   iAlarmType=3
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=4   and   iAlarmType=0
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=4   and   iAlarmType=1
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=4   and   iAlarmType=2
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=4   and   iAlarmType=3
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=5   and   iAlarmType=0
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=5   and   iAlarmType=1
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=5   and   iAlarmType=2
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=5   and   iAlarmType=3
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=6   and   iAlarmType=0
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=6   and   iAlarmType=1
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=6   and   iAlarmType=2
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=6   and   iAlarmType=3


SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=7   and   iAlarmType=0
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=7   and   iAlarmType=1
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=7   and   iAlarmType=2
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=7   and   iAlarmType=3
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=8   and   iAlarmType=0
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=8   and   iAlarmType=1
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=8   and   iAlarmType=2
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=8   and   iAlarmType=3
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=9   and   iAlarmType=0
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=9   and   iAlarmType=1
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=9   and   iAlarmType=2
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=9   and   iAlarmType=3
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=10   and   iAlarmType=0
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=10   and   iAlarmType=1
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=10   and   iAlarmType=2
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=10   and   iAlarmType=3
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=11   and   iAlarmType=0
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=11   and   iAlarmType=1
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=11   and   iAlarmType=2
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=11   and   iAlarmType=3
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=12   and   iAlarmType=0
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=12   and   iAlarmType=1
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=12   and   iAlarmType=2
SELECT   count(*)   FROM   tbl_AlarmHistory   where   year(iResTime)=2006   and   month(iResTime)=12   and   iAlarmType=3


上面是查询2006年1-12月告警类型分别为0.1.2.3的条数,哪位大虾帮忙能写的简单点吗?我用的是死办法,写了48条语句,小弟在此先谢谢了!

[解决办法]
这不是按年,月,类型分组吗?
试试
SELECT count(*) FROM tbl_AlarmHistory group by year(iResTime),month(iResTime),iAlarmType
[解决办法]
SELECT year(iResTime),month(iResTime),iAlarmType,count(*)
FROM tbl_AlarmHistory
group by year(iResTime),month(iResTime),iAlarmType
order by year(iResTime),month(iResTime)
[解决办法]
借鉴几位楼上朋友方法,写写:
SELECT year(iResTime),month(iResTime),iAlarmType,SUM(1)
FROM tbl_AlarmHistory
WHERE iAlarmType BETWEEN 0 AND 3
group by year(iResTime),month(iResTime),iAlarmType
order by year(iResTime),month(iResTime)

热点排行