我需要一个SSRS表达式来检查一个列中的值是否存在于另一个列中。

huangapple go评论56阅读模式
英文:

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) &gt; 0, &quot;True&quot;, &quot;False&quot;)<br>
&<br>
=IIF(InStr(Fields!Name.Value, Fields!Mngr.Value) &gt; 0, &quot;True&quot;, &quot;False&quot;)<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, &quot;DataSet2&quot;).Length &gt; 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() 表达式。

这将产生以下结果:

我需要一个SSRS表达式来检查一个列中的值是否存在于另一个列中。

英文:

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, &quot;DataSet1&quot;) Is Nothing
	, &quot;N&quot;
	, &quot;Y&quot;
	)

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 &quot;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 
(&#39;Dave&#39;, &#39;Bob&#39;), (&#39;Mary&#39;, &#39;Bob&#39;), (&#39;Bob&#39;, &#39;John&#39;), (&#39;John&#39;, NULL), (&#39;Anna&#39;, &#39;John&#39;)
SELECT * FROM @t

Then I just added a table and set the 3rd column to the LOOKUP() expression above

This gives the following result.

我需要一个SSRS表达式来检查一个列中的值是否存在于另一个列中。

huangapple
  • 本文由 发表于 2023年6月1日 16:32:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定