Link to home
Start Free TrialLog in
Avatar of Member_2_7966563
Member_2_7966563

asked on

Histogram with varying widths

In an MS Access query, Field1 could contain an integer from 1 to 50000. I need help to create a function, or preferably a formula to convert this into a bracket. In the table below, the letter describes the bracket name (the output of the function or formula) and the integer range describes the range of Field1. For example, if Field1 = 9, then the formula should yield the letter F.

A_  0 - 1
B_  1 - 2
C_  2 - 4
D_  4 - 6
E_  6 - 8
F_  8 - 10
H_ 10 - 12
I_ 12 - 15
K_ 15 - 20
L_ 20 - 25
M_ 25 - 30
N_ 30 - 40
O_ 40 - 50
P_ 50 - 100
R_ 100 - 200
S_ 200 - 400
T_ 400 - 800
U_ 800 - 1,600
V_ 1,600 - 3,200
W_ 3,200 - 6,400
X_ 6,400 - 12,800
Y_ > 12,800
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Well its a bit confusing...if the value for example is 8 which Letter is matched E or F ?
I have prepared a small sample ...take a look at Query1
Altering the ranges in the Ranges table will allow to define the start of range and end of range.
Database15.accdb
Agree with John regarding your ranges, although I would use the values you have with criteria like:

SomeValue >= MinRange and SomeValue < MaxRange

To do this in a query, you would do something like:
SELECT table1.Field1, table1.Field2, table1.Somevalue, tblRange.RngLetter
FROM table1, tblRange
WHERE table1.SomeValue >= tblRange.MinRange
AND table1.SomeValue<NZ(tblRange.MaxRange, 32767)

Open in new window

another way to do this, although you cannot do this in the query designer would be:
SELECT table1.Field1, table1.Field2, table1.Somevalue, tblRange.RngLetter
FROM table1
LEFT JOIN tblRange ON table1.SomeValue >= tblRange.MinRange AND table1.SomeValue<NZ(tblRange.MaxRange, 32767)

Open in new window

The advantage of this second version is that you can include all values in table1, and the RngLetter value will simply be NULL if the value in the SomeValue field does not fall within one of the date ranges.

PS, I used 32767 in the NZ function because that is the upper value of an integer (smallint) data field.  That may not apply for you, in which case you can use a much larger number.
That's easy using Select-Case in a tiny function. Then no fancy SQL is needed:

Select *, LevelLetter([Field1]) As Bracket
From YourTable

Open in new window

and the function:

Public Function LevelLetter(ByVal Value As Long) As String

    Dim Letter  As String
    
    Select Case Value
        Case Is <= 1
            Letter = "A"
        Case Is <= 2
            Letter = "B"
        Case Is <= 4
            Letter = "C"
        Case Is <= 6
            Letter = "D"
        Case Is <= 8
            Letter = "E"
        Case Is <= 10
            Letter = "F"
        Case Is <= 12
            Letter = "H"
        Case Is <= 15
            Letter = "I"
        Case Is <= 20
            Letter = "K"
        Case Is <= 25
            Letter = "L"
        Case Is <= 30
            Letter = "M"
        Case Is <= 40
            Letter = "N"
        Case Is <= 50
            Letter = "O"
        Case Is <= 100
            Letter = "P"
        Case Is <= 200
            Letter = "R"
        Case Is <= 400
            Letter = "S"
        Case Is <= 800
            Letter = "T"
        Case Is <= 1600
            Letter = "U"
        Case Is <= 3200
            Letter = "V"
        Case Is <= 6400
            Letter = "W"
        Case Is <= 12800
            Letter = "X"
        Case Else
            Letter = "Y"
    End Select
    
    LevelLetter = Letter
    
End Function

Open in new window

I would really like to see a case with more Ranges...like what 1000 different ranges == 2000+ lines of static code...
You don't have that many letters.
Semi true....you can always have combinations of Letters...like AA,AV,BDA...etc
Well, you can imagine many many scenarios.
I have chosen to focus on this specific question.
Try this:
SELECT myData.ID, myData.Field1, DLookUp("Letter","myRanges","myRanges.RangeStart <= " & [Field1] & " AND " & "myRanges.RangeEnd > " & [Field1]) AS Letter
FROM myData;

Open in new window

Histogram.accdb
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.