查找特定状态为零的范围

时间:2016-06-20 17:42:43

标签: sql sql-server-2012 range

我有两列的表,一个是int,另一个是varchar列

id   status
int  varchar(50)

状态栏有10个状态,让我们说status1,status2,status3,... status10

我想写一个查询来查找status7,status8,status9 count(*)为零的最大范围。

我从这个查询开始,

Select status, count(*) from table1 where id between 1 and 1000 group by status

Select status, count(*) from table1 where id between 1001 and 2000 group by status

此处当count为零时,不包括在内。可以通过一个好的单一查询来满足我的要求吗?

表数据,

Id  Status
 1   status1
 2   status2
 3   status3
 4   status4
 5   status5
 6   status6
 7   status7
 8   status8
 9   status9
 10  status10
 11  status1
 12   status2
 13   status3
 14   status4
 15   status5
 16   status9
 17   status2
 18   status7
 19   status3
 20   status5
 ......
 1000 status6 

我的第一个期望输出

当我使用15到20之间时,

 **Status      Count**
 status1       0
 status2       1
 status3       1
 status4       0
 status5       2
 status6       0
 status7       1
 status8       0
 status9       1
 status10      0

接下来如果可能的话,我想找到这3个状态8,9 10为零的范围。

1 个答案:

答案 0 :(得分:0)

你需要创建一个只有状态的静态表,然后像

一样加入它
select * into #data from (
select  1  id,   'status1'   [status] union 
select  2  id,   'status2'   [status] union 
select  3  id,   'status3'   [status] union 
select  4  id,   'status4'   [status] union 
select  5  id,   'status5'   [status] union 
select  6  id,   'status6'   [status] union 
select  7  id,   'status7'   [status] union 
select  8  id,   'status8'   [status] union 
select  9  id,   'status9'   [status] union 
select  10 id,   'status10'  [status] union 
select  11 id,   'status1'   [status] union 
select  12 id,   'status2'   [status] union 
select  13 id,   'status3'   [status] union 
select  14 id,   'status4'   [status] union 
select  15 id,   'status5'   [status] union 
select  16 id,   'status9'   [status] union 
select  17 id,   'status2'   [status] union 
select  18 id,   'status7'   [status] union 
select  19 id,   'status3'   [status] union 
select  20 id,   'status5'   [status]  
) x


select * into #table from (
select     'status1'     [status] union 
select     'status2'     [status] union 
select     'status3'     [status] union 
select     'status4'     [status] union 
select     'status5'     [status] union 
select     'status6'     [status] union 
select     'status7'     [status] union 
select     'status8'     [status] union 
select     'status9'     [status] union 
select     'status10'    [status] ) x

--=========================================gets the result for between 15 and 20 
select x.status , count(y.id)
from #table x 
left join #data y
on x.status = y.status
and id between 15 and 20 
group by x.status


--==============================================this should give you the ranges 


select *,row_number () over (order by id ) rowid
into #range
from #data 
where id not in (
select distinct 
 y.id
from #table x 
left join #data y
on x.status = y.status
where x.status in ( 'status8' , 'status9' , 'status10' )
)

select x.*
from #range x 
left join #range y
on x.rowid = y.rowid + 1 
where x.id - isnull(y.id,0) = 1