0

SQLでは、このようなことを達成する方法はありますか? これは、SQL Server Report Builder で作成されたレポートに基づいており、ユーザーは複数のテキスト値を 1 つのレポート パラメーターとして指定できます。レポートのクエリは、ユーザーが選択したすべての値を取得し、それらを 1 つの変数に格納します。ユーザーが指定したすべての値に関連付けられているレコードのみをクエリで返す方法が必要です。

-- Assume there's a table of Elements with thousands of entries.  
-- Now we declare a list of properties for those Elements to be associated with.

create table #masterTable (
    ElementId int, Text varchar(10)
)

insert into #masterTable (ElementId, Text) values (1, 'Red');
insert into #masterTable (ElementId, Text) values (1, 'Coarse');
insert into #masterTable (ElementId, Text) values (1, 'Dense');
insert into #masterTable (ElementId, Text) values (2, 'Red');
insert into #masterTable (ElementId, Text) values (2, 'Smooth');
insert into #masterTable (ElementId, Text) values (2, 'Hollow');

-- Element 1 is Red, Coarse, and Dense.  Element 2 is Red, Smooth, and Hollow.  
-- The real table is actually much much larger than this; this is just an example.

-- This is me trying to replicate how SQL Server Report Builder treats 
-- report parameters in its queries.  The user selects one, some, all, 
-- or no properties from a list. The written query treats the user's   
-- selections as a single variable called @Properties.
-- Example scenario 1: User only wants to see Elements that are BOTH Red and Dense.
select e.* 
from Elements e 
where (@Properties) --ideally a set containing only Red and Dense
in
(select Text from #masterTable where ElementId = e.Id) --ideally a set containing only Red, Coarse, and Dense
--Both Red and Dense are within Element 1's properties (Red, Coarse, Dense), so Element 1 gets returned, but not Element 2.

-- Example scenario 2: User only wants to see Elements that are BOTH Red and Hollow.  
select e.* from Elements e where
(@Properties) --ideally a set containing only Red and Hollow
in
(select Text from #masterTable where ElementId = e.Id)
--Both Red and Hollow are within Element 2's properties (Red, Smooth, Hollow), so Element 2 gets returned, but not Element 1.


--Example Scenario 3: User only picked the Red option.
select e.* from Elements e where
(@Properties) --ideally a set containing only Red
in
(select Text from #masterTable where ElementId = e.Id)
--Red is within both Element 1 and Element 2's properties, so both Element 1 and Element 2 get returned.

SQL では "in" 比較の左側に複数の値を許可していないように見えるため、上記の構文は実際には機能しません。返されるエラー:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

私はここで正しい軌道に乗っていますか?例が長くてわかりにくい場合は申し訳ありません。

これが私が扱っている正確なコードです:

select p.*
from Products p
where
(
    (
        --user can search through gloves, bats, or both
        p.TypeId = 2 and 'Bat' in (@ProductTypes) 
        and 
        (
            (
                (@BatProperties) COLLATE DATABASE_DEFAULT in 
                (
                    select props.Name from PropertyTypes props
                    inner join ProductProperties pp on props.Id = pp.TypeId
                    where pp.ProductId = p.Id
                )
            --still want query to run when no properties are selected
            ) or not exists(select * from @BatProperties)
        )
    )
    or
    (
        p.TypeId = 1 and 'Glove' in (@ProductTypes) --user can search through gloves, bats, or both
        and
        (
            (
                (@GloveProperties) COLLATE DATABASE_DEFAULT in 
                (
                    select props.Name from PropertyTypes props
                    inner join ProductProperties pp on props.Id = pp.TypeId
                    where pp.ProductId = p.Id
                )
            --still want query to run when no properties are selected
            ) or not exists(select * from @GloveProperties)
        )
    )
)
4

1 に答える 1