Using a view in SQL Server 2000
Table1
:
id z1 z2 z3 z4 type
--------------------------------------
01A 300 400 300 400 01
2B 300 400 300 400 02
3C 500 300 400 300 01
04A 500 400 500 400 01
05B 400 300 400 300 02
06 150 200 200 150 03
....
Table2
:
type Formula1 Formula2
------------------------------------
01 ((Z1+Z2)/1000)*2 null
02 null ((Z3+Z4)/1000)*2
03 ((Z1+Z2)/1000)*2 ((Z3+Z4)/1000)*2
I want to select the table1
rows based on table2
formula:
Expected output
Table1
:
id z1 z2 z3 z4 type
-------------------------------------
01A 300 400 null null 01
2B null null 300 400 02
3C 500 300 null null 01
04A 500 400 null null 01
05B null null 400 300 02
06 150 200 200 150 03
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..?
Is this what you need? Query works in SQL Sever 2005, donno about 2000, please check:
select a.ID,
(CASE WHEN PATINDEX('%Z1%', ISNULL(b.Formula1,'')+ISNULL(b.Formula2,''))<>0 THEN a.Z1 ELSE NULL END) Z1,
(CASE WHEN PATINDEX('%Z2%', ISNULL(b.Formula1,'')+ISNULL(b.Formula2,''))<>0 THEN a.Z2 ELSE NULL END) Z2,
(CASE WHEN PATINDEX('%Z3%', ISNULL(b.Formula1,'')+ISNULL(b.Formula2,''))<>0 THEN a.Z3 ELSE NULL END) Z3,
(CASE WHEN PATINDEX('%Z4%', ISNULL(b.Formula1,'')+ISNULL(b.Formula2,''))<>0 THEN a.Z4 ELSE NULL END) Z4,
b.*
From TABLE1 a inner join TABLE2 b on a.type=b.type