Excluding groups from a resultset using criteria in that resultset
Using SQL Server 2008.
We are given a Code, say 020286 that gives us a starting resultset.
Starting data:
Code L R G
020286 2 703 1
030383 3 6 0
031847 4 5 0
021932 7 10 0
022499 8 9 0
020068 229 610 1
020866 231 396 1
020524 232 241 0
030772 233 234 0
031787 235 236 0
031859 237 238 0
031947 239 240 0
020964 242 383 1
021215 253 342 1
030728 343 344 0
020990 345 346 0
022521 347 354 0
Now I want to exclude rows whose L is between L and R of any rows whose
G=1 (in the same resultset) excepting the given Code (essentially do "L
between L and R" for all G=1 except the given Code), while still keeping
all G=1. Expected results:
Code L R G
020286 2 703 1
030383 3 6 0
031847 4 5 0
021932 7 10 0
022499 8 9 0
020068 229 610 1
020866 231 396 1
020964 242 383 1
021215 253 342 1
030728 343 344 0
020990 345 346 0
022521 347 354 0
Here is a table var with starting data.
declare @t table (Code nvarchar(10),L int, R int, G int)
insert into @t (Code, L, R, G)
select '020286',2,703,1 union
select '030383',3,6,0 union
select '031847',4,5,0 union
select '021932',7,10,0 union
select '022499',8,9,0 union
select '020068',229,610,1 union
select '020866',231,396,1 union
select '020524',232,241,0 union
select '030772',233,234,0 union
select '031787',235,236,0 union
select '031859',237,238,0 union
select '031947',239,240,0 union
select '020964',242,383,1 union
select '021215',253,342,1 union
select '030728',343,344,0 union
select '020990',345,346,0 union
select '022521',347,354,0
select * from @t
No comments:
Post a Comment