【问题标题】:Expression that compares two Datasets with different amount of rows比较具有不同行数的两个数据集的表达式
【发布时间】:2023-04-02 11:36:01
【问题描述】:

我有两个数据集,

这两个数据集的行数不同。

我有两个 Tablix。每个 Tablix 都指向这两个不同的数据集

在一列中,我想逐行比较两个数据集。

所以我想要的想法是这样的:

这是我的两个 Tablix 和我的数据集,你可以看到学生号 10,15 和 23 在 Dataset2 上不存在。

当这种情况发生时,我想写一个 0。

如果我使用查找表达式,它会在比较每个数据集存在的行时给我一个错误:

=IIF(Fields!Student.Value <> Lookup(Fields!Student.Value,Fields!Student.Value,Fields!Student.Value,"Dataset1"),"0","1")

那个表达式会给我一个错误。

这个想法(我认为)是保留学生 21 并在数据集 1 上为每个学生进行比较,当他们相等时写入 1 和 0,而他们不相等但对于每一行。

我不能触摸数据集,我的意思是查询。

如果您需要更多信息,请告诉我。

编辑: 为了给你更多信息,我想要做的是复制我在我的网络应用程序中的代码在数据层中所做的事情:

            ReportsDataSet.MyDatasetRow drSchool = (ReportsDataSet.MyDatasetRow)repDset.MyDataset.Rows[0];
            string Student = drSchool.Student;
            bool firstone = true;
            bool hayApproved = false;
            bool hayNotApproved = false;
            bool hayNeedMoreStudy = false;
            bool hayFail = false;
            bool hayG5 = false;
            bool hayNC = false;

            if (repDset.MyDataset.Rows.Count > 0)
            {
                int Count = repDset.MyDataset.Rows.Count;

                for (int i = 0; i < Count; i++)
                {
                    ReportsDataSet.MyDatasetRow dr = (ReportsDataSet.MyDatasetRow)repDset.MyDataset.Rows[i];

                    if (dr.tipo.Trim() == "TOTAL" && firstone)
                    {
                        Student = dr.Student;
                        firstone = false;

                        switch (dr.categoria.Trim())
                        {
                            case "Approved":
                                hayApproved = true;
                                break;
                            case "NotApproved":
                                hayNotApproved = true;
                                break;
                            case "NeedMoreStudy":
                                hayNeedMoreStudy = true;
                                break;
                            case "Fail":
                                hayFail = true;
                                break;
                            case "G5":
                                hayG5 = true;
                                break;
                            case "N/C":
                                hayNC = true;
                                break;
                        }
                    }
                    else
                    {
                        if (dr.tipo.Trim() == "TOTAL" && dr.Student == Student)
                        {
                            switch (dr.categoria.Trim())
                            {
                                case "Approved":
                                    hayApproved = true;
                                    break;
                                case "NotApproved":
                                    hayNotApproved = true;
                                    break;
                                case "NeedMoreStudy":
                                    hayNeedMoreStudy = true;
                                    break;
                                case "Fail":
                                    hayFail = true;
                                    break;
                                case "G5":
                                    hayG5 = true;
                                    break;
                                case "N/C":
                                    hayNC = true;
                                    break;
                            }
                        }
                    }

                    if (dr.Student != Student || i == (Count-1) )
                    {
                        if (!hayApproved)
                        {
                            repDset.MyDataset.AddMyDatasetRow(Student, "Approved", "TOTAL", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0");
                            Alreadyfilled++;
                        }

                        if (!hayNotApproved)
                        {
                            repDset.MyDataset.AddMyDatasetRow(Student, "NotApproved", "TOTAL", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0");
                            Alreadyfilled++;
                        }

                        if (!hayNeedMoreStudy)
                        {
                            repDset.MyDataset.AddMyDatasetRow(Student, "NeedMoreStudy", "TOTAL", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0");
                            Alreadyfilled++;
                        }

                        if (!hayFail)
                        {
                            repDset.MyDataset.AddMyDatasetRow(Student, "Fail", "TOTAL", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0");
                            Alreadyfilled++;
                        }

                        if (!hayG5)
                        {
                            repDset.MyDataset.AddMyDatasetRow(Student, "G5", "TOTAL", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0");
                            Alreadyfilled++;
                        }

                        if (!hayNC)
                        {
                            repDset.MyDataset.AddMyDatasetRow(Student, "N/C", "TOTAL", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0");
                            Alreadyfilled++;
                        }

                        if (!hayApproved && !hayNotApproved && !hayNeedMoreStudy && !hayFail && !hayG5 && !hayNC)
                        {
                            repDset.MyDataset.AddMyDatasetRow(Student, "TOTAL", "TOTAL", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0");
                            Alreadyfilled++;
                        }

                        repDset.MyDataset.AcceptChanges();

                        hayApproved = false;
                        hayNotApproved = false;
                        hayNeedMoreStudy = false;
                        hayFail = false;
                        hayG5 = false;
                        hayNC = false;
                        firstone = true;

                        if (i != (Count - 1))
                        {
                            Student = dr.Student;
                            i--;
                        }
                    }
                }
            }

编辑 2:

让我给你更多信息:

正如您在最后一张图片中看到的,学生 11、16 等在数据集 2 上不存在,在这些情况下我必须输入“0”。 但是我对 Lookup 所做的是将 21 与 11,21 与 11 再次进行比较,22 与 11 进行比较,依此类推。这不是重点,重点是每次 Dataset1 中存在的 Student 不在 Dataset2 中时输入 0。

应该是这样的,我将Student的值保存在Dataset 2中,并在Dataset1上比较每个Student,当它们不相等时,输入0,相等时输入1。

也许是一个函数?将 Dataset1 的所有学生保存在一个列表中,将 Dataset2 的学生保存在另一个列表中,并在自定义代码中的 foreach 上进行比较?

【问题讨论】:

    标签: c# asp.net vba reporting-services ssrs-2008-r2


    【解决方案1】:

    如果没有匹配,Lookup() 函数将返回 Nothing。它不应该给你一个错误,但在这种情况下你可以什么都不检查并返回 0。我认为您只是在与 IIF 表达式中的错误内容进行比较。

    =IIF(
        IsNothing(Lookup(Fields!Student.Value, Fields!Student.Value, Fields!Student.Value, "Dataset1")),
        0,
        Lookup(Fields!Student.Value, Fields!Student.Value, Fields!Student.Value, "Dataset1")
    )
    

    除非您真的想在查找工作时只显示 1。在这种情况下:

    =IIF(
        IsNothing(Lookup(Fields!Student.Value, Fields!Student.Value, Fields!Student.Value, "Dataset1")),
        0,
        1
    )
    

    返回

    返回 Variant,如果没有匹配则返回 Nothing。

    Lookup Function (Report Builder and SSRS)


    演练

    我将执行此操作与您尝试完成的操作稍有不同,以便您可以看到查找函数发生了什么。

    首先,我创建了两个简单的数据集,每个数据集都包含一个 ID 和标签列表。第二个数据集只是第一个数据集的子集(有些行丢失,但 DataSet2 中的所有行都存在于 DataSet1 中)。

    数据集1

     id  |  label
    -----+-----------
      1  |  Student1-a
      2  |  Student2-a
      3  |  Student3-a
      4  |  Student4-a
    ...
     25  |  Student25-a
    

    数据集2

     id  |  label
    -----+-----------
      1  |  Student1-b
      5  |  Student5-b
     10  |  Student10-b
     12  |  Student12-b
    ...
     25  |  Student25-b
    

    然后我在报表上创建两个 tablix。两个 tablix 都将数据源设置为 DataSet1,因为其中包含我将检查 DataSet2 以使用查找的行。

    我将第一个 tablix 设置为仅在两列中显示 ID 和标签。第二个 tablix 显示 ID(来自 DataSet1)并使用表达式在 DataSet2 中查找该 ID,如果不存在则显示 0,如果存在则显示来自 dataset2 的标签。

    我使用的表达式是:

    =IIF(
        IsNothing(Lookup(Fields!id.Value, Fields!id.Value, Fields!label.Value, "DataSet2")),
        0,
        Lookup(Fields!id.Value, Fields!id.Value, Fields!label.Value, "DataSet2")
    )
    

    我得到的输出是这样的,请注意所有行如何出现在第二个 tablix 中(因为它提供了包含所有行的数据集),但是当行没有时,label 列显示为 0 t 存在于 dataset2 中。

    【讨论】:

    • 为此,我必须执行以下操作:=IIF(IsNothing(Lookup(Fields!Student.Value,Fields!Student.Value,Fields!Student.Value,"Dataset1"))=False,"0","1") As IsNothing 返回一个布尔值,但不是我要找的。请记住,查找将比较每个数据集上存在的行。如果该学生的行不存在,我想要做的是在该表达式中放置一个 0
    • 我忘了说这个有一个组,所以查找应该在这个组之外才能工作
    • 您知道是否可以将字段的值传递给我的自定义代码中的变量?
    • 您不需要使用自定义代码,但您可以将字段值传递给自定义函数。 (不过,这将是一个单独的 SO 问题。)通过查找功能如何工作的演练查看我的更新。我认为您缺少的一点是两个表都需要来自 DataSet1,因为它具有您在 DataSet2 中查找的所有行。您使用 Table2 中的 Lookup 函数来查找该行是否存在并显示您要查找的数据。
    猜你喜欢
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多