多个 SQL 脚本在报告中通过选项选择的数据集中。

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

Multiple sql scripts in dataset selected via option in report

问题

我有一个包含聚合数据图表等内容的SSRS报表,利用了多个数据源。我正在创建一个下钻功能,用于显示每个图表的个人级别数据,这样您就可以点击数据标签,然后查看底层的姓名等信息。

多年前,我能够创建一个个人级别的报表,其中包含了一些不同的“脚本”,以便您可以选择哪个脚本(一个数据集包含了一些不同的脚本)来驱动报表的输出。

进一步解释一下 - 在选择“下钻”操作以转到报表时,传递的一个“参数”将是数字1、2、3等,这将传递到个人级别的报表,并有效确定哪个脚本驱动了用户级别的报表。

因此,值为1将确定从数据集中使用脚本一来驱动报表。这将与值为2提供的脚本完全不同。

我不确定数据集的结构,但大致来说,我认为它的工作方式有点像一个Case语句,因此数据集可能是这样的:

case
when value = 1 then

select
from
where

when value = 2 then

select
from
where

等等...

我无法找到这个报表,并且一直在寻找如何完成这个操作的指导。任何帮助将不胜感激。

英文:

I have an SSRS report that includes aggregated data chart etc utilizing more than one data source. I am in the process of creating a drill down to display person level data for each of the charts so you click on the data label and then view the underlying names etc..

Years ago i was able to create a person level report that incorporated a few different 'scripts' as it were so that you could in effect choose which script (one dataset included a few different scripts) powered the output of the report.

To explain further - when selecting a 'drill through' action to go to report one of the 'parameters' passed would be a number 1,2.3 etc and this would be passed through to the person level report and effectively determone which script powered the user level report.

So a value of 1 would determine that script one fromt he dataset would be used to power the report. This would be totally different to the script that a value of 2 would provide.

Im not sure how the dataset was structured but roughly speaking I believe it worked a bit like a case statement so the dataset would be

case
when value = 1 then

select
from
where

when value = 2 then

select
from
where

etc..

I kind find this report and have struggled to find guidance how this would be done. Any help would be gratefully appreciated.

答案1

得分: 1

您可以按照您描述的方式执行此操作,但是脚本中的所有路径都必须以相同的结构返回给 SSRS,具有相同的列名和数据类型。

因此,您不能像这样做:

IF @option = 1 
   BEGIN
      SELECT FirstName, LastName from Employees WHERE EmpID = @emp
   END
IF @option = 2
   BEGIN
      SELECT StartDate, Salary, Department from Employees WHERE EmpID = @emp
   END

在上面的示例中,列的数量发生了变化,即使在第二个选项中只返回了 StartDate 和 Salary,数据类型也会不同。

或者,如果您的数据集很小,您可以返回键、值和数据类型的列表,然后报表使用 LOOKUP() 函数检索正确的行。

例如:

IF @option = 1 
   BEGIN
          SELECT 'FirstName' as FieldName, FirstName as FieldValue, 'text' as FieldType FROM Employees WHERE EmpID = @emp 
    UNION
          SELECT 'LastName', LastName, 'text' FROM Employees WHERE EmpID = @emp
       END
    IF @option = 2
       BEGIN
          SELECT 'StartDate' as FieldName, StartDate as FieldValue, 'date' as FieldType FROM Employees WHERE EmpID = @emp
          UNION
          SELECT 'Salary', Salary, 'money' FROM Employees WHERE EmpID = @emp
          UNION 
          SELECT 'Department', Department, 'text' FROM Employees WHERE EmpID = @emp
       END

这样,字段名称和数据类型始终保持一致。

在报表中,假设从查询返回的表格格式不是您想要的,您可以使用 LOOKUP() 函数在报表的每个文本框中选择正确的值,例如 =LOOKUP('Salary', Fields.FieldName.Value, Field.FieldValue.Value, "DataSet1")

您可以使用相同的方法获取字段类型,然后相应地设置文本框的格式属性。

英文:

You can do this as you described but all paths in your script must return the same structure to SSRS, the same column names and data types.

So you cannot do something like

IF @option = 1 
   BEGIN
      SELECT FirstName, LastName from Employees WHERE EmpID = @emp
   END
IF @option = 2
   BEGIN
      SELECT StartDate, Salary, Department from Employees WHERE EmpID = @emp
   END

In the above, the number of columns changes, and even if we only returned StartDate and Slaary in the 2nd option, the datatype would also be different.

Alternatively, if your dataset is small, you can return a list of keys, values and datatypes and the report use a LOOKUP() to retrieve the correct row.

e.g.

IF @option = 1 
   BEGIN
          SELECT 'FirstName' as FieldName, FirstName as FieldValue, 'text' as FieldType FROM Employees WHERE EmpID = @emp 
    UNION
          SELECT 'LastName', LastName, 'text' FROM Employees WHERE EmpID = @emp
       END
    IF @option = 2
       BEGIN
          SELECT 'StartDate' as FieldName, StartDate as FieldValue, 'date' as FieldType FROM Employees WHERE EmpID = @emp
          UNION
          SELECT 'Salary', Salary, 'money' FROM Employees WHERE EmpID = @emp
          UNION 
          SELECT 'Department', Department, 'text' FROM Employees WHERE EmpID = @emp
       END

This way the field names and datatypes are always the same.

In the report, assuming the table format returned from the query was not what you wanted, you would use LOOKUP() to choose the correct value in each textbox on your report e.g. =LOOKUP('Salary', Fields.FieldName.Value, Field.FieldValue.Value, "DataSet1")

You would do the same thing to get the field type and then set the textbox format property accordingly.

答案2

得分: 0

尝试在个人级别报告中创建一个名为 ScriptSelection 的参数,以捕获确定要使用的脚本的值。

然后修改个人级别报告中的数据集查询,根据 ScriptSelection 参数的值使用 CASE 语句或 IF 语句包含条件语句:

SELECT *
FROM (
    SELECT
        -- 来自脚本 1 的列
    FROM table1
    WHERE @ScriptSelection = 1

    UNION ALL

    SELECT
        -- 来自脚本 2 的列
    FROM table2
    WHERE @ScriptSelection = 2

    -- 为其他脚本添加更多 UNION ALL 部分
) AS data;

然后配置主报告中的钻取操作,以传递适当的值(1、2、3 等)给 ScriptSelection 参数以供个人级别报告使用。

在主报告中点击数据标签并触发钻取操作时,个人级别报告将接收所选参数值,并使用相应的脚本(数据集)来生成报告。

英文:

Try to create a parameter ScriptSelection in the person-level report to capture the value that determines the script to be used.

Then modify the dataset query in the person-level report to include a conditional statement based on the value of the ScriptSelection parameter using a CASE statement or an IF statement :

SELECT *
FROM (
    SELECT
        -- Columns from script 1
    FROM table1
    WHERE @ScriptSelection = 1

    UNION ALL

    SELECT
        -- Columns from script 2
    FROM table2
    WHERE @ScriptSelection = 2

    -- Add more UNION ALL sections for additional scripts
) AS data;

Then configure the drill-through action in the main report to pass the appropriate value (1, 2, 3, etc.) for the ScriptSelection parameter to the person-level report.

When clicking on a data label in the main report and trigger the drill-through action, the person-level report will receive the selected parameter value and use the corresponding script (dataset) to power the report.

huangapple
  • 本文由 发表于 2023年5月29日 22:17:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358106.html
匿名

发表评论

匿名网友

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

确定