Report Builder 2.0 或 Oracle 字符串聚合

发布于 2024-08-23 13:28:48 字数 1068 浏览 4 评论 0原文

在 Report Builder 2.0 中,我尝试创建一个包含如下数据的表:

Person | StrID's
-------------------
Jim    | a, b, c, d
Mary   | h, k
Sue    | l, m, p, z

问题是我的 Oracle SQL 查询返回以下格式的数据:

Person | StrID
--------------
Jim    | a
Jim    | b
Jim    | c
Jim    | d
Mary   | h

等等。我无权在 Oracle 中使用 LISTAGG,如 本教程,我也不能使用 EXPLAIN PLAN 或告诉您我正在使用哪个版本的 Oracle,因为我必须通过非常有限的接口访问数据库。我研究了报表生成器的函数,例如Aggregate,但找不到将多个字符串值与聚合函数连接在一起的方法。如果我创建一个按人员分组的表,我会为每个 StrID 获得单独的行。使用 Join(Fields!StrID.Value, ", ") 会导致“#Error”显示在表格单元格中,我认为是因为 Fields!StrID.Value 不是实际上是一个多值字段,它是每个人的单个值。

谁能告诉我一种通过 SQL 或 Report Builder 2.0/Visual Basic 在一行中按人员显示关联 StrID 列表的方法?

编辑:我可以使用LAG函数< /a> 如果有人能想出一个创造性的解决方案来使用它。事实证明,我也可以将 PARTITION BY 与 LAG 函数结合使用。

In Report Builder 2.0, I'm trying to create a table with data like the following:

Person | StrID's
-------------------
Jim    | a, b, c, d
Mary   | h, k
Sue    | l, m, p, z

The problem is that my Oracle SQL query returns data in the following format:

Person | StrID
--------------
Jim    | a
Jim    | b
Jim    | c
Jim    | d
Mary   | h

And so on. I don't have access in Oracle to use LISTAGG as described in this tutorial, nor can I use EXPLAIN PLAN or tell you which version of Oracle I'm using because I have to access the database through a very limited interface. I've looked into Report Builder's functions like Aggregate but cannot find a way to concatenate multiple string values together with an aggregate function. If I create a table grouping by Person, I get separate rows for each of the StrID's. Using Join(Fields!StrID.Value, ", ") causes "#Error" to show up in the table cell, I assume because Fields!StrID.Value is not actually a multi-value field, it's a single value per Person.

Can anyone tell me a way of showing a list of the associated StrID's by Person in one row, either through SQL or Report Builder 2.0/Visual Basic?

Edit: I can use the LAG function if anyone can think of a creative solution using that. Turns out I can use PARTITION BY in conjunction with the LAG function, too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世界等同你 2024-08-30 13:28:48

我浏览过 Tom Kyte 网站?如果您搜索“pivot”,结果可能会有所帮助。

例如

http://asktom。 oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:124812348063

http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID :15151874723724

不幸的是,我无法访问报告 2.0(幸运的是(?)我从 6i 开始!),所以我无法确认这是否适用于您的环境。

I have had a look on Tom Kyte's site? If you search for "pivot" the results might be of some help.

e.g.

http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:124812348063

or

http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:15151874723724

Unfortunately I don't have access to reports 2.0 (Fortunately(?) I started with 6i!) so I can't confirm that this will work in your environment.

墨小墨 2024-08-30 13:28:48

在发现 LAG 函数起作用后,我想我也许能够获得一个 StrID 和上一行的 StrID,将它们连接在一起,然后以某种方式继续对 Person 的所有行执行此操作。我发现 这个 AskTom 问题 并能够调整它以与我的表格一起使用:

SELECT Person, 
       SUBSTR(
         MAX(list) KEEP (DENSE_RANK FIRST ORDER BY lev DESC),
         2
       ) AS StrIDs
FROM ( 
       SELECT Person,
              SYS_CONNECT_BY_PATH(StrID, ', ') AS list,
              LEVEL AS lev
       FROM (
              SELECT Person,
                     StrID,
                     LAG(StrID, 1)
                       OVER (PARTITION BY Person ORDER BY StrID) AS prev_id
              FROM my_table
              WHERE other_conditions = 'blah blah'
            )
       START WITH prev_id IS NULL
       CONNECT BY PRIOR StrID=prev_id
     )
GROUP BY Person
ORDER BY Person

我现在以我期望的格式获取数据,即:

Person StrIDs
jeff   a, b, c
linda  k, l, m

After finding out that the LAG function worked, I thought I might be able to get a StrID and the previous row's StrID, concatenate them together, and then somehow keep doing that for all the rows for a Person. I found this AskTom question and was able to adapt it to work with my tables:

SELECT Person, 
       SUBSTR(
         MAX(list) KEEP (DENSE_RANK FIRST ORDER BY lev DESC),
         2
       ) AS StrIDs
FROM ( 
       SELECT Person,
              SYS_CONNECT_BY_PATH(StrID, ', ') AS list,
              LEVEL AS lev
       FROM (
              SELECT Person,
                     StrID,
                     LAG(StrID, 1)
                       OVER (PARTITION BY Person ORDER BY StrID) AS prev_id
              FROM my_table
              WHERE other_conditions = 'blah blah'
            )
       START WITH prev_id IS NULL
       CONNECT BY PRIOR StrID=prev_id
     )
GROUP BY Person
ORDER BY Person

I now get data in the format I expected, namely:

Person StrIDs
jeff   a, b, c
linda  k, l, m
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文