2016-06-20 15 views
1

私が持っている二つの列、1つのint型と別のvarchar型の列を持つ表のステータス列が10のステータスは、言うことができますしてい検索範囲ゼロカウント

id status 
int varchar(50) 

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 

カウントがゼロで、その含まれておりません。良い単一のクエリで私の要件を満たすことができますか?

表データ、

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はゼロです。

+2

のようにそれに参加するだけにしてステータスを持っている静的なテーブルを作成する必要があります。 「status7、status8、status9 count(*)がゼロである最大範囲を見つけるためのクエリ」からは明らかではありません。どういう意味ですか。あなたの試みたSQLは、多くの手がかりを追加しません。 – JNevill

+0

「最大範囲」とは何ですか? –

+0

数値Xから無限大までが最大範囲のように見え、他の制約がありません。 –

答えて

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