device_id device_status device_address now_date
00001 故障 A区 2012-03-01
00001 故障 A区 2012-03-02
00001 故障 A区 2012-03-03
00002 故障 A区 2012-03-01
00002 故障 A区 2012-03-02
00002 无故障 A区 2012-03-03
00003 故障 B区 2012-03-02
00003 故障 B区 2012-03-03
00003 故障 B区 2012-03-04
00004 故障 B区 2012-03-02
00004 无故障 B区 2012-03-03
00004 故障 B区 2012-03-04
------------------------------------------------------------
返回一个 以小区 和最后一次 日期 统计的表
Fault_Count device_address last_date
1 A区 2012-03-03
2 B区 2012-03-04
------------------------------------------------------------
表值函数 或查询语句都可以
00001 故障 A区 2012-03-01
00001 故障 A区 2012-03-02
00001 故障 A区 2012-03-03
00002 故障 A区 2012-03-01
00002 故障 A区 2012-03-02
00002 无故障 A区 2012-03-03
00003 故障 B区 2012-03-02
00003 故障 B区 2012-03-03
00003 故障 B区 2012-03-04
00004 故障 B区 2012-03-02
00004 无故障 B区 2012-03-03
00004 故障 B区 2012-03-04
------------------------------------------------------------
返回一个 以小区 和最后一次 日期 统计的表
Fault_Count device_address last_date
1 A区 2012-03-03
2 B区 2012-03-04
------------------------------------------------------------
表值函数 或查询语句都可以
解决方案 »
- MSSQL_2008分区表的问题
- 急求 数值 排序问题!!! 谢谢
- 关于用sql语句打开excel的问题
- 关于分数查询系统的数据库建设问题
- CPU占用100%,Audit Logout占用资源的问题怎么解决?
- 如何求一个日期型字段的平均值?
- 高手请进
- [急]MSSQL恢复问题
- 如何实现下方地SQL...急....(100分)
- 存储过程中调用如下语句 execute immediate 'create global temporary table test (a char(1))' 提示权限不够,为什么?
- 查询过滤重复问题,如何可以过滤重复时,取ID较大的值?
- SQL中的系统存数过程SP_EXECUTESQL的用法和意思,求详细
from T
where device_status = '故障'
group by device_address
from tb
group by device_address
from T
where device_status = '故障'
group by device_address
insert into tb values('00001', '故障' , 'A区', '2012-03-01')
insert into tb values('00001', '故障' , 'A区', '2012-03-02')
insert into tb values('00001', '故障' , 'A区', '2012-03-03')
insert into tb values('00002', '故障' , 'A区', '2012-03-01')
insert into tb values('00002', '故障' , 'A区', '2012-03-02')
insert into tb values('00002', '无故障', 'A区', '2012-03-03')
insert into tb values('00003', '故障' , 'B区', '2012-03-02')
insert into tb values('00003', '故障' , 'B区', '2012-03-03')
insert into tb values('00003', '故障' , 'B区', '2012-03-04')
insert into tb values('00004', '故障' , 'B区', '2012-03-02')
insert into tb values('00004', '无故障', 'B区', '2012-03-03')
insert into tb values('00004', '故障' , 'B区', '2012-03-04')
goselect t.* from tb t where device_status = '故障' and not exists (select 1 from tb where device_status = '故障' and device_address = t.device_address and (now_date > t.now_date or (now_date = t.now_date and device_id > t.device_id)) ) order by t.device_address
/*
device_id device_status device_address now_date
---------- ------------- -------------- ------------------------------------------------------
00001 故障 A区 2012-03-03 00:00:00.000
00004 故障 B区 2012-03-04 00:00:00.000(所影响的行数为 2 行)
*/
select (select count(1) from
(
select device_address , max(now_date) now_date from tb where device_status = '故障' group by device_address
) n where now_date < m.now_date) + 1 Fault_Count , m.* from
(
select device_address , max(now_date) now_date from tb where device_status = '故障' group by device_address
) m
/*
Fault_Count device_address now_date
----------- -------------- ------------------------------------------------------
1 A区 2012-03-03 00:00:00.000
2 B区 2012-03-04 00:00:00.000(所影响的行数为 2 行)*/drop table tb
select '00001','故障','A区',' 2012-03-01'
union all select '00001','故障','A区','2012-03-02'
union all select '00001','故障','A区','2012-03-03'
union all select '00002','故障','A区','2012-03-01'
union all select '00002','故障','A区','2012-03-02'
union all select '00002','无故障','A区','2012-03-03'
union all select '00003','故障','B区','2012-03-02'
union all select '00003','故障','B区','2012-03-03'
union all select '00003','故障','B区','2012-03-04'
union all select '00004','故障','B区','2012-03-02'
union all select '00004','无故障','B区','2012-03-03'
union all select '00004','故障','B区','2012-03-04'
Select count(*)as Fault_Count,device_address,max(now_date) as last_date
from test where device_status='故障' group by device_address
查询语句是对的 但是速度很慢啊 我这张表里 现在只有 7w多条记录,跑这个查询语句用了 两秒多,有没有更好的办法?
select row_number() over(order by now_date) Fault_Count , m.* from
(
select device_address , max(now_date) now_date from tb where device_status = '故障' group by device_address
) m
order by Fault_Count
--1.创建临时表
create table #TB(device_id varchar(10),device_status varchar(10),device_address varchar(10),now_date datetime)
insert into #TB values('00001', '故障' , 'A区', '2012-03-01')
insert into #TB values('00001', '故障' , 'A区', '2012-03-02')
insert into #TB values('00001', '故障' , 'A区', '2012-03-03')
insert into #TB values('00002', '故障' , 'A区', '2012-03-01')
insert into #TB values('00002', '故障' , 'A区', '2012-03-02')
insert into #TB values('00002', '无故障', 'A区', '2012-03-03')
insert into #TB values('00003', '故障' , 'B区', '2012-03-02')
insert into #TB values('00003', '故障' , 'B区', '2012-03-03')
insert into #TB values('00003', '故障' , 'B区', '2012-03-04')
insert into #TB values('00004', '故障' , 'B区', '2012-03-04')
insert into #TB values('00004', '无故障', 'B区', '2012-03-04')
insert into #TB values('00004', '故障' , 'B区', '2012-03-04')
go
--2.查询with a as (
select count(device_status) as Fail_counts,device_address,now_date from #TB where device_status='故障' group by device_address,now_date
)
,b as (
select device_address,last_date=max(now_date) from #TB where device_status='故障' group by device_address,device_status
)select a.Fail_counts,a.device_address,now_date as last_date from a
join b on a.now_date=b.last_date and a.device_address=b.device_address--3.删除临时表
truncate table #TB
drop table #TB 这个。
--1.创建临时表
create table #TB(device_id varchar(10),device_status varchar(10),device_address varchar(10),now_date datetime)
insert into #TB values('00001', '故障' , 'A区', '2012-03-01')
insert into #TB values('00001', '故障' , 'A区', '2012-03-02')
insert into #TB values('00001', '故障' , 'A区', '2012-03-03')
insert into #TB values('00002', '故障' , 'A区', '2012-03-01')
insert into #TB values('00002', '故障' , 'A区', '2012-03-02')
insert into #TB values('00002', '无故障', 'A区', '2012-03-03')
insert into #TB values('00003', '故障' , 'B区', '2012-03-02')
insert into #TB values('00003', '故障' , 'B区', '2012-03-03')
insert into #TB values('00003', '故障' , 'B区', '2012-03-04')
insert into #TB values('00004', '故障' , 'B区', '2012-03-04')
insert into #TB values('00004', '无故障', 'B区', '2012-03-04')
insert into #TB values('00004', '故障' , 'B区', '2012-03-04')
go
--2.查询with a as (
select count(device_status) as Fail_counts,device_address,now_date from #TB where device_status='故障' group by device_address,now_date
)
,b as (
select device_address,last_date=max(now_date) from #TB where device_status='故障' group by device_address,device_status
)select a.Fail_counts,a.device_address,now_date as last_date from a
join b on a.now_date=b.last_date and a.device_address=b.device_address--3.删除临时表
truncate table #TB
drop table #TB
这个。第一列不是编号 而是故障数量