英文:
I need an SSRS expression to check if a value in one column exists anywhere in another column
问题
我有一个员工数据库,其中包括一个“Names”列和一个“Mngr”列。我需要一个SSRS表达式,如果“Names”中的某个值存在于“Mngr”中的任何位置,则返回True(或“Y”)。这将显示特定人是否是经理。
我尝试过以下表达式:
=IIF(InStr(Fields!Mngr.Value, Fields!Name.Value) > 0, "True", "False")
和
=IIF(InStr(Fields!Name.Value, Fields!Mngr.Value) > 0, "True", "False")
但都不起作用。两者都返回False,而应该返回True(我交换了列,因为我无法完全理解表达式的语法)。
在研究之后,我有第二个数据集仅包含经理(“Mngrs”),并尝试了这个表达式:
=IIF(Not IsNothing(LookupSet(Fields!Name.Value, Fields!Mngr.Value, Fields!Mngrs.Value, "DataSet2").Length > 0, True, False)
但它甚至都不能运行。
所以我有点困惑。我相信一定有一个可以做到这一点的表达式 - 所以任何帮助将不胜感激。
英文:
I have an employee database, which includes a "Names" column, and a managers column ("Mngr").
I need an SSRS expression to return True (or "Y") if a value in "Names" exists anywhere in "Mngr".
This will show if a particular person is a manager.<br><br>
I have tried<br>
=IIF(InStr(Fields!Mngr.Value, Fields!Name.Value) > 0, "True", "False")
<br>
&<br>
=IIF(InStr(Fields!Name.Value, Fields!Mngr.Value) > 0, "True", "False")
<br>
but neither work. Both return False, where it should be True (I swapped the cols around as I couldn't quite understand the syntax of the expression)<br><br>
After researching, I have a 2nd DataSet that just holds managers ("Mngrs"), and tried this<br>
=IIF(Not IsNothing(LookupSet(Fields!Name.Value, Fields!Mngr.Value, Fields!Mngrs.Value, "DataSet2").Length > 0, True, False)
<br>
which wouldn't even run.
So I'm a bit stuck. I'm sure there must be an expression that can do this - so any help would be appreciated.
答案1
得分: 1
你不需要第二个数据集来完成这个任务,你需要使用 LOOKUP()
函数来处理整个数据集。你所展示的第一个表达式只会考虑上下文中的行,这可能只是表达式所在的行。
可以尝试这样做:
=IIF(
LOOKUP(Fields!Names.Value, Fields!Mngr.Value, Fields!Mngr.Value, "DataSet1") Is Nothing
, "N"
, "Y"
)
这个表达式会在整个 "DataSet1" 数据集中查找 Names
字段在 Mngr
字段中的匹配项。如果查找结果是 Nothing
,则表达式返回 "N",否则返回 "Y"。
作为一个快速测试,我生成了一些虚拟数据,并将其用作我的数据集查询:
DECLARE @t TABLE (Names varchar(20), Mngr varchar(20))
INSERT INTO @t VALUES
('Dave', 'Bob'), ('Mary', 'Bob'), ('Bob', 'John'), ('John', NULL), ('Anna', 'John')
SELECT * FROM @t
然后,我只需添加一个表,并将第三列设置为上述的 LOOKUP()
表达式。
这将产生以下结果:
英文:
You don't need a second dataset to do this, you need to use LOOKUP()
but across your entire dataset, the first expressions you showed will only take into account the the rows that are in context, which will probably be just the row where the expression sits.
try something like this
=IIF(
LOOKUP(Fields!Names.Value, Fields!Mngr.Value, Fields!Mngr.Value, "DataSet1") Is Nothing
, "N"
, "Y"
)
This just looks for the Names
field in the Mngr
field and returns the Mngr
field if it finds a match, across the whole of "DataSet1
". If
Nothing` is returned from the lookup, the expression returns "N" otherwise "Y".
As a quick test a generated some dummy data and used this as my dataset query
DECLARE @t TABLE (Names varchar(20), Mngr varchar(20))
INSERT INTO @t VALUES
('Dave', 'Bob'), ('Mary', 'Bob'), ('Bob', 'John'), ('John', NULL), ('Anna', 'John')
SELECT * FROM @t
Then I just added a table and set the 3rd column to the LOOKUP()
expression above
This gives the following result.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论