读书人

SQL语句 多数据查询有关问题

发布时间: 2013-04-12 18:33:12 作者: rapoo

SQL语句 ,多数据查询问题
现在有 100 台机器 ,每台机器有四种状态 1.正在使用 2.未用 3.维护 4.其他

问题:现在要 根据时间查询 在某一天中 四种状态的机器各有多少 (每台机器每天只有一种状态)


我知道 select count(*) from table where 机器ID =“ID” and 状态 ="正在使用";
但是我100台机器 就得写100条查询吗 ? 有没有批量操作的啊 ,把 这些数据查出来 ,或者 插入到一个数据表里 的方法啊??


求大神帮忙 急 在线等!!!

(每台机器有唯一ID )

[解决办法]

select 机器id, 状态, count(1) from TB where <添加你想要的条件> group by 机器id, 状态

[解决办法]

select 机器id
,sum(case 状态 when '正在使用' then 1 else 0 end) as 正在使用
,sum(case 状态 when '未用' then 1 else 0 end) as 未用
,sum(case 状态 when '维护' then 1 else 0 end) as 维护
,sum(case 状态 when '其他' then 1 else 0 end) as 其他
from TB where convert(varchar(10),时间列,23)='2013-01-23' group by 机器id


[解决办法]
select case when 状态 ='正在使用' then sum(1) when 状态='未用' then sum(1) when 状态='维护' then sum(1) else sum(1) end 
from table

读书人网 >SQL Server

热点推荐