Using a view in SQL Server 2000
Table1:
id z1 z2 z3 z4 type
--------------------------------------
01A 300 400 300 400 01
2B 300 400 300 500 02
3C 700 600 400 300 01
04A 500 400 800 900 01
05B 400 300 400 300 02
06 150 200 200 150 03
....
Table2:
type value1 value2
------------------------------------
01 0 300
01 301 500
02 0 200
02 201 400
03
.....
I want to select the table1 rows based on table2 range:
Combination for table1- max(Z1, Z2) and max(Z3, Z4)
If Max(z1,z2) range is less than or equal to table2 max(value2) where table1.type = table2.type
If Max(z1, z2) range is less than or equal to table2 max(value2) where table1.type = table2.type
If z1, Z2 range less than or equal to table2 then i want to display the z1, z2 rows otherwise null
If z3, z4 range less than or equal to table2 then i want to display the z3, z4 rows otherwise null
Expected output
Table1:
id z1 z2 z3 z4 type
--------------------------------------
01A 300 400 300 400 01 ' `Both (z1, z2), (z3, z4) rows are matching with table2 for type 01`
2B 300 400 null null 02 ' (z1, z2) are matching, (z3, z4) rows are not matching with table2 for type 02
3C null null 400 300 01 ' (z1, z2) rows are not matching, (z3, z4) rows are matching with table2 for type 01
04A 500 400 null null 01 ' (z1, z2) rows are matching, (z3, z4) rows are not matching with table2 for type 01
05B 400 300 400 300 02 ' Both (z1, z2), (z3, z4) rows are matching with table2 for type 02
....
Currently I’m using a view, I don’t want to change to stored procedure because most of the report are using this view.
How to do it in sql..?
This seems to give the expected results. (I’d have preferred to use a CTE, but you specified 2000).
Sample data:
Query:
Result:
I’m not sure what the relevance of your question title was, the verbiage in the middle is quite confusing, and I made a guess at the final
WHEREclause since the06row seems to disappear.