Stupid question

  • Thread starter Thread starter Nikolay Petrov
  • Start date Start date
N

Nikolay Petrov

I junt can't figures this out. Can somebody help.

I have list of numbers
00000001
00000002
00000003
00000004
00000005
00000006
00000007
00000008
and so on
I want to define some range like
1-500
501-1000
1001-1500
1501-2000
....
What I need is how to find which number in which range belongs.
Can someone help?

TIA
 
School homework?

for a number t, t belongs to the range m..n if t >= m and t <= n!
 
I need the ranges to be generate dynamilcy, but in the way I have showed in
previews post
1-500
501-1000
1001-1500
 
The question doesn't make sense.

Nikolay Petrov said:
I need the ranges to be generate dynamilcy, but in the way I have showed in
previews post
1-500
501-1000
1001-1500
 
Robin Tucker said:

I need the ranges to be generate dynamilcy, but in the way I have showed in
previews post
1-500
501-1000
1001-1500

Robin gave you an answer that will work; it's even almost in
pseudo-code. Use your brain and translate it into real code.
--
auric underscore underscore at hotmail dot com
*****
- What the hell is *that*?
- It's my iMac. Cool, huh?
- Does Willy Wonka know you took this out of his office?
 
Nikolay,
You can use a Select Case if you have fixed ranges, otherwise I would
probably use an IntegerRange class.

http://www.martinfowler.com/ap2/range.html

Something like:

Dim range1 As New IntegerRange(1, 500)
Dim range2 As New IntegerRange(501, 1000)
Dim range3 As New IntegerRange(1001, 1500)
Dim range4 As New IntegerRange(1501, 2000)

Dim value As Integer = 100

If range1.Contains(value) Then
' do stuff for 1-500
ElseIf range2.Contains(value) Then
' do stuff for 501-1000
ElseIf range3.Contains(value) Then
' do stuff for 1001-1500
ElseIf range4.Contains(value) Then
' do stuff for 1501-2000
End If

Where range1, range2, range3, range4 are individually part of another
abstraction (class) that could work polymophically based on an overridable
"Action" method, I would then put this other abstraction into a collection
and simply iterate the collection looking for a specific range & call the
"Action" method of the object that was found... Note the "Action" method
could simply be a Delegate, then this second abstraction would simply need
to be an IntegerRange & Delegate pair.


Public Structure IntegerRange

Private ReadOnly m_start As Integer
Private ReadOnly m_finish As Integer

Public Sub New(ByVal start As Integer, ByVal finish As Integer)
If start > finish Then Throw New ArgumentException("Start cannot be
after finish")
m_start = start
m_finish = finish
End Sub

Public ReadOnly Property Start() As Integer
Get
Return m_start
End Get
End Property

Public ReadOnly Property Finish() As Integer
Get
Return m_finish
End Get
End Property

Public ReadOnly Property Duration() As Integer
Get
Return m_finish - m_start
End Get
End Property

Public Function Contains(ByVal value As Integer) As Boolean
Return (m_start <= value AndAlso value <= m_finish)
End Function

End Structure

Hope this helps
Jay
 
Here's an easy set-based solution

use tempdb
go
set nocount on
create table t1 (col1 int not null)
insert t1 values (1)
insert t1 values (3)
insert t1 values (6)
insert t1 values (6)
insert t1 values (8)
insert t1 values (9)
insert t1 values (10)
insert t1 values (11)
insert t1 values (12)
insert t1 values (25)
insert t1 values (30)
insert t1 values (95)
insert t1 values (350)


create table t2 (lowerRange int not null)
insert t2 values (0)
insert t2 values (5)
insert t2 values (10)
insert t2 values (15)
insert t2 values (20)
insert t2 values (25)
insert t2 values (30)


select c.lowerRange, c.upperRange, count(d.col1) as Cnt
from (
select a.lowerRange + 1 as LowerRange, min(b.lowerRange) as UpperRange
from t2 a
left join t2 b
on a.lowerRange < b.lowerRange
group by a.lowerRange
) c
left join t1 d
on d.col1 >= c.lowerRange and d.col1 <= coalesce(c.UpperRange, 999)
group by c.lowerRange, c.upperRange

drop table t1
drop table t2



hth,
Eric
 
The question doesn't make sense.

He couldn't explain it in the VB6 group either.... (Or at least not WHY he
wanted to do it.)
 
I have managed to do it by myself.
However thank you all for your time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top