I have two tables that can be seen in accompanying image.
Table A contains Department, Month and Average.
Table B contains Month, Year, RangeStart, RangeEnd and Colour.
If you look at the screen shot of Table B, you will see for each Month you have a Green, Yellow, Orange and Red value. You also have a range.
What I need.........
I need a new column on Table A named 'Colour'. In this column, I need either Green, Yellow, Orange or Red. The deciding factor on which colour is assigned to the month will be the 'Average' column.
For example:
DepartmentA for May's Average is equal to 0.96
Upon referencing Table B, I can see that line 8, 0.75+
will be the range this fits into. Therefore Red is the colour I want placed in table A next to Mays average.
I have left RangeEnd for the highest range per month as NULL as it is basically 75+
, anything greater than 0.75
slots in here.
Can anyone point me in the right direction that is not too time consuming.
To enable the range join optimization in a SQL query, you can use a range join hint to specify the bin size. The hint must contain the relation name of one of the joined relations and the numeric bin size parameter. The relation name can be a table, a view, or a subquery.
Explanation : Outer Join is to be used between two tables A and B when the resultant table needs rows from A and B that matches the condition and rows from A that does not match the condition. 1.
A conditional column join is a fancy way to let us join to a single column and to two (or more) columns in a single query. We can accomplish this by using a case statement in the on clause of our join. A case statement allows us to test multiple conditions (like an if/else if/else) to produce a single value.
The plus sign is Oracle syntax for an outer join. There isn't a minus operator for joins. An outer join means return all rows from one table. Also return the rows from the outer joined where there's a match on the join key. If there's no matching row, return null.
you can use it straight forward:
select *
from table a
join table b
on a.month = b.month
and a.average between b.rangestart and isnull(b.rangeend,10000) -- 100000 = greater than max value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With