Hi,
Could you please help me with the following:
I have 2 tables - [Coal_Samples] and [Coal Type] Which I have pasted below. I want to create a single result set that matches as closley as possible [Coal Type].Coal_Type to the interval between [Coal_Samples].mfrom and [Coal_Samples].mto. I would have [Coal_Samples] as the primary table using a left outer join.
The problem is that the intervals [Coal_Samples].mfrom do not match [Coal Type].mfrom and [Coal_Samples].mto does not match [Coal Type].mto. I want to match the [Coal_Samples] intervals ([Coal_Samples].mfrom-[Coal_Samples].mto) as closely as possible to the [Coal_Type].Coal_Type field.
I have been able to acheive this in excel using a VLOOKUP statement - but I want to know how to do this with and SQL Script without duplicating the rows based in [Coal_Samples].mfrom-[Coal_Samples].mto interval.
COAL SAMPLES TABLE | |||
ID | mFrom | mTo | SAMPLE NUMBER |
AD261 | 57 | 57.5 | SAMPLE_001 |
AD261 | 57.5 | 58 | SAMPLE_002 |
AD261 | 58 | 59 | SAMPLE_003 |
AD261 | 59 | 60 | SAMPLE_004 |
AD261 | 60 | 61 | SAMPLE_005 |
AD261 | 61 | 62 | SAMPLE_006 |
AD261 | 62 | 63 | SAMPLE_007 |
AD261 | 63 | 64 | SAMPLE_008 |
AD261 | 64 | 65 | SAMPLE_009 |
AD261 | 65 | 66 | SAMPLE_010 |
AD261 | 66 | 67 | SAMPLE_011 |
AD261 | 67 | 68 | SAMPLE_012 |
AD261 | 68 | 69 | SAMPLE_013 |
AD261 | 69 | 70 | SAMPLE_014 |
AD261 | 70 | 71 | SAMPLE_015 |
AD261 | 71 | 72 | SAMPLE_016 |
AD261 | 72 | 73 | SAMPLE_017 |
AD261 | 73 | 74 | SAMPLE_018 |
AD261 | 74 | 75 | SAMPLE_019 |
AD261 | 75 | 76 | SAMPLE_020 |
AD261 | 76 | 77 | SAMPLE_021 |
AD261 | 77 | 78 | SAMPLE_022 |
AD261 | 78 | 79 | SAMPLE_023 |
COAL TYPE TABLE | |||
ID | Type_mFrom | Type__mTo | COAL TYPE |
AD261 | 57 | 68.6 | BROWN COAL |
AD261 | 68.6 | 75 | GREY COAL |
AD261 | 75 | 78.2 | BLACK COAL |
AD261 | 78.2 | 79.2 | BLACK COAL |
Example RESULT SET | |||
ID | mFrom | mTo | DOMINANT_COAL TYPE |
AD261 | 57 | 57.5 | BROWN COAL |
AD261 | 57.5 | 58 | BROWN COAL |
AD261 | 58 | 59 | BROWN COAL |
AD261 | 59 | 60 | BROWN COAL |
AD261 | 60 | 61 | BROWN COAL |
AD261 | 61 | 62 | BROWN COAL |
AD261 | 62 | 63 | BROWN COAL |
AD261 | 63 | 64 | BROWN COAL |
AD261 | 64 | 65 | BROWN COAL |
AD261 | 65 | 66 | BROWN COAL |
AD261 | 66 | 67 | BROWN COAL |
AD261 | 67 | 68 | BROWN COAL |
AD261 | 68 | 69 | BROWN COAL |
AD261 | 69 | 70 | GREY COAL |
AD261 | 70 | 71 | GREY COAL |
AD261 | 71 | 72 | GREY COAL |
AD261 | 72 | 73 | GREY COAL |
AD261 | 73 | 74 | GREY COAL |
AD261 | 74 | 75 | GREY COAL |
AD261 | 75 | 76 | BLACK COAL |
AD261 | 75 | 76 | BLACK COAL |
AD261 | 76 | 77 | BLACK COAL |
AD261 | 77 | 78 | BLACK COAL |
AD261 | 78 | 79 | BLACK COAL |
Can you please help with the logic of this query?
Thanks very much.