0

SQL Server 2000 でのビューの使用

表1:

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
....

表 2:

type  value1         value2
------------------------------------    
01    0      300
01    301    500          
02    0      200
02    201    400
03
.....

table2 範囲に基づいて table1 行を選択したい:

テーブル1の組み合わせ-max(Z1, Z2) and max(Z3, Z4)

Max(z1,z2) 範囲が table2 以下の場合 max(value2) こ​​こで table1.type = table2.type Max(z1, z2) 範囲が table2 以下の場合 max(value2) こ​​こで table1.type = table2.type

z1、Z2 の範囲が table2 以下の場合、z1、z2 の行を表示します。それ以外の場合は null z3、z4 の範囲が table2 以下の場合、z3、z4 の行を表示します。それ以外の場合は null

期待される出力

表1:

 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

....

現在、私はビューを使用しています。ほとんどのレポートがこのビューを使用しているため、ストアド プロシージャに変更したくありません。

SQLでそれを行う方法..?

4

1 に答える 1

1

これにより、期待どおりの結果が得られるようです。(私は CTE を使用したかったのですが、2000 を指定しました)。

サンプルデータ:

declare @T1 table (Id varchar(3) not null,z1 int not null,z2 int not null,z3 int not null,z4 int not null,type char(2) not null)
insert into @T1 (id,z1,z2,z3,z4,type) values
('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')

declare @T2 table (type char(2) not null,value1 int not null,value2 int not null)
insert into @T2 (type,value1,value2) values
('01',0,300),
('01',301,500),
('02',0,200),
('02',201,400)

クエリ:

select
    t1.Id,
    CASE WHEN t2.type is not null then z1 END as z1,
    CASE WHEN t2.type is not null then z2 END as z2,
    CASE WHEN t3.type is not null then z3 END as z3,
    CASE WHEN t3.type is not null then z4 END as z4,
    t1.type
from
    @T1 t1
        left join
    (select type,MAX(value2) as val2 from @T2 group by type) t2
        on
            t1.type = t2.type and
            t1.z1 <= t2.val2 and
            t1.z2 <= t2.val2
        left join
    (select type,MAX(value2) as val2 from @T2 group by type) t3
        on
            t1.type = t3.type and
            t1.z3 <= t3.val2 and
            t1.z4 <= t3.val2
where
    t2.type is not null or
    t3.type is not null

結果:

Id   z1          z2          z3          z4          type
---- ----------- ----------- ----------- ----------- ----
01A  300         400         300         400         01
2B   300         400         NULL        NULL        02
3C   NULL        NULL        400         300         01
04A  500         400         NULL        NULL        01
05B  400         300         400         300         02

あなたの質問のタイトルの関連性が何であるかはわかりません。真ん中の言い回しはかなり混乱しています。行が消えているように見えるWHEREので、最後の節を推測しました。06

于 2012-11-07T07:39:46.187 に答える