Visual Studio 2008 中的多个下拉列表未正确过滤
Multiple Dropdown in Visual Studio 2008 not filtering correctly
我正在使用 Visual Basic 2008 SSRS,后端是 SQL Server 2008 来创建报告。我正在尝试创建 2 个参数化下拉列表,用户可以从中 select 过滤报告中的数据。出于某种原因,我似乎无法让两个参数都正确地只过滤一个过滤器。当我 运行 visual studio 2008 年的报告时,我选择 DROPDOWN1 等于 2,DROPDOWN2 等于 4。所以它应该只显示一行,但出于某种原因,似乎只有第二个过滤器有效。我可能只需要修改我的 sql 吗?任何帮助将不胜感激,谢谢。
**Table 1** **Table 2** **Table 3**
Key1 Key2 Data1 Key3 Data2 Data3
1 1 HI 2 GREEN WHITE
4 1 YOU 2 GREEN WHITE
2 HEY 3 RED BLACK
3 HI 1 ORANGE PURPLE
1 HEY 4 BLUE BLACK
4 HEY 4 BLUE PURPLE
**Current Result** **Desired**
Data1 Data2 Data3 Data1 Data2 Data3
HI ORANGE PURPLE HI ORANGE PURPLE
HEY BLUE PURPLE
**Parameters-** @DROPDOWN1- Available Values
Label Value
HIHEY 1
HI 2
**@DROPDOWN2- Avilable Values**
Label Value
GREENBLUE 3
PURPLE 4
Query Designer- SELECT L.Key1,
C.Data,
aa.Data2,
aa.Data3
FROM Table1 AS L INNER JOIN Table2 C ON L.Key1 = C.Key2
INNER JOIN Table3 aa on L.Key1 = aa.Key3
WHERE
((@DropDown1= 1 AND C.Data1 ='HI' OR C.Data1 ='HEY' )
OR
(@DropDown1= 2 AND C.Data1 ='HI'))
AND
((
(@DropDown2= 3 AND aa.Data2 IN ('GREEN', 'BLUE'))
OR
(@DropDown2= 4 AND aa.Data3 ='PURPLE')
))
不太清楚其中的逻辑,但从您想要的结果来看,您的 Where 条件中的 OR 子句需要用括号括起来。
((@DropDown1= 1 AND (C.Data1 ='HI' OR C.Data1 ='HEY') )
完整代码演示:
declare @table1 table (key1 int)
declare @table2 table (key2 int, data1 varchar(10))
declare @table3 table (key3 int, data2 varchar(10), data3 varchar(10))
insert into @table1 select 1 union all select 4
insert into @table2
values
(1, 'HI'),
(1, 'YOU'),
(2, 'HEY'),
(3, 'HI'),
(1, 'HEY'),
(4, 'HEY')
insert into @table3
values
(2, 'GREEN', 'WHITE'),
(2, 'GREEN', 'WHITE'),
(3, 'RED', 'BLACK'),
(1, 'ORANGE', 'PURPLE'),
(4, 'BLUE', 'BLACK'),
(4, 'BLUE', 'PURPLE')
select * from @table1
select * from @table2
select * from @table3
declare @DropDown1 int = 2, @DropDown2 int = 4
SELECT L.Key1, C.Data1,aa.Data2,aa.Data3
FROM @table1 AS L JOIN @table2 C ON L.Key1 = C.Key2
INNER JOIN @table3 aa on L.Key1 = aa.Key3
WHERE (
(@DropDown1= 1 AND (C.Data1 ='HI' OR C.Data1 ='HEY') )
OR
(@DropDown1= 2 AND C.Data1 ='HI'))
AND
((
(@DropDown2= 3 AND aa.Data2 IN ('GREEN', 'BLUE'))
OR
(@DropDown2= 4 AND aa.Data3 ='PURPLE')
))
我正在使用 Visual Basic 2008 SSRS,后端是 SQL Server 2008 来创建报告。我正在尝试创建 2 个参数化下拉列表,用户可以从中 select 过滤报告中的数据。出于某种原因,我似乎无法让两个参数都正确地只过滤一个过滤器。当我 运行 visual studio 2008 年的报告时,我选择 DROPDOWN1 等于 2,DROPDOWN2 等于 4。所以它应该只显示一行,但出于某种原因,似乎只有第二个过滤器有效。我可能只需要修改我的 sql 吗?任何帮助将不胜感激,谢谢。
**Table 1** **Table 2** **Table 3**
Key1 Key2 Data1 Key3 Data2 Data3
1 1 HI 2 GREEN WHITE
4 1 YOU 2 GREEN WHITE
2 HEY 3 RED BLACK
3 HI 1 ORANGE PURPLE
1 HEY 4 BLUE BLACK
4 HEY 4 BLUE PURPLE
**Current Result** **Desired**
Data1 Data2 Data3 Data1 Data2 Data3
HI ORANGE PURPLE HI ORANGE PURPLE
HEY BLUE PURPLE
**Parameters-** @DROPDOWN1- Available Values
Label Value
HIHEY 1
HI 2
**@DROPDOWN2- Avilable Values**
Label Value
GREENBLUE 3
PURPLE 4
Query Designer- SELECT L.Key1,
C.Data,
aa.Data2,
aa.Data3
FROM Table1 AS L INNER JOIN Table2 C ON L.Key1 = C.Key2
INNER JOIN Table3 aa on L.Key1 = aa.Key3
WHERE
((@DropDown1= 1 AND C.Data1 ='HI' OR C.Data1 ='HEY' )
OR
(@DropDown1= 2 AND C.Data1 ='HI'))
AND
((
(@DropDown2= 3 AND aa.Data2 IN ('GREEN', 'BLUE'))
OR
(@DropDown2= 4 AND aa.Data3 ='PURPLE')
))
不太清楚其中的逻辑,但从您想要的结果来看,您的 Where 条件中的 OR 子句需要用括号括起来。
((@DropDown1= 1 AND (C.Data1 ='HI' OR C.Data1 ='HEY') )
完整代码演示:
declare @table1 table (key1 int)
declare @table2 table (key2 int, data1 varchar(10))
declare @table3 table (key3 int, data2 varchar(10), data3 varchar(10))
insert into @table1 select 1 union all select 4
insert into @table2
values
(1, 'HI'),
(1, 'YOU'),
(2, 'HEY'),
(3, 'HI'),
(1, 'HEY'),
(4, 'HEY')
insert into @table3
values
(2, 'GREEN', 'WHITE'),
(2, 'GREEN', 'WHITE'),
(3, 'RED', 'BLACK'),
(1, 'ORANGE', 'PURPLE'),
(4, 'BLUE', 'BLACK'),
(4, 'BLUE', 'PURPLE')
select * from @table1
select * from @table2
select * from @table3
declare @DropDown1 int = 2, @DropDown2 int = 4
SELECT L.Key1, C.Data1,aa.Data2,aa.Data3
FROM @table1 AS L JOIN @table2 C ON L.Key1 = C.Key2
INNER JOIN @table3 aa on L.Key1 = aa.Key3
WHERE (
(@DropDown1= 1 AND (C.Data1 ='HI' OR C.Data1 ='HEY') )
OR
(@DropDown1= 2 AND C.Data1 ='HI'))
AND
((
(@DropDown2= 3 AND aa.Data2 IN ('GREEN', 'BLUE'))
OR
(@DropDown2= 4 AND aa.Data3 ='PURPLE')
))