新手上路,求助SQL循环

peng008007 2007-01-09 03:47:41
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条语句,小弟在此先谢谢了!
...全文
434 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Andy-W 2007-01-09
  • 打赏
  • 举报
回复
借鉴几位楼上朋友方法,写写:
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)
xiangyu120 2007-01-09
  • 打赏
  • 举报
回复
create table #t(id int , Result varchar(10),iResTime datetime,iAlarmType int )
insert into #t(id,result,iResTime,iAlarmType)values (1,'a','2006-01-01',1)
insert into #t(id,result,iResTime,iAlarmType)values (1,'b','2006-02-01',1)
insert into #t(id,result,iResTime,iAlarmType)values (1,'a','2006-03-01',1)
insert into #t(id,result,iResTime,iAlarmType)values (1,'a','2006-04-01',2)
insert into #t(id,result,iResTime,iAlarmType)values (1,'a','2006-05-01',2)
insert into #t(id,result,iResTime,iAlarmType)values (1,'a','2006-06-01',2)
insert into #t(id,result,iResTime,iAlarmType)values (1,'a','2006-07-01',2)
insert into #t(id,result,iResTime,iAlarmType)values (1,'a','2006-08-01',3)
insert into #t(id,result,iResTime,iAlarmType)values (1,'a','2006-01-01',1)
insert into #t(id,result,iResTime,iAlarmType)values (1,'a','2006-08-01',3)
insert into #t(id,result,iResTime,iAlarmType)values (1,'a','2006-08-01',3)
select * from #t
SELECT count(*),month(iResTime),iAlarmType FROM #t where year(iResTime)=2006
group by month(iResTime),iAlarmType
order by month(iResTime)
drop table #t


rookie_one 2007-01-09
  • 打赏
  • 举报
回复

上面的有各弟兄说的对阿

SELECT count(*)
FROM tbl_AlarmHistory
group by datepart(yy,iResTime),datepart(mm,iResTime)
and iAlarmType IN (1,2,3)--如果是字符类型的就'1','2','3'
akuzou 2007-01-09
  • 打赏
  • 举报
回复
SELECT year(iResTime),month(iResTime),iAlarmType,count(*)
FROM tbl_AlarmHistory
group by year(iResTime),month(iResTime),iAlarmType
order by year(iResTime),month(iResTime)
peng008007 2007-01-09
  • 打赏
  • 举报
回复
2楼的,查出来了,但是数据却不知道是几月的什么类型
ALLEN625314 2007-01-09
  • 打赏
  • 举报
回复

关注
akuzou 2007-01-09
  • 打赏
  • 举报
回复
这不是按年,月,类型分组吗?
试试
SELECT count(*) FROM tbl_AlarmHistory group by year(iResTime),month(iResTime),iAlarmType
caixia615 2007-01-09
  • 打赏
  • 举报
回复
用动态语句执行..

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧