如何在 SQL 子查询中使用 order by?

发布于 2024-12-20 09:28:10 字数 560 浏览 1 评论 0原文

我有一个名为 ConcatList 的聚合函数,它将值列表连接成单个逗号分隔值。

这是我正在尝试做的一个例子。我有两个表:员工和工作。 Job 是 Employee 的“孩子”。我需要获取员工列表,其中包括与员工相关的以逗号分隔的工作列表,并按工作名称排序。

我以为我可以这样做:

select em.EmployeeId,
       em.EmployeeName,
       (select ConcatList(jo.JobName)
          from Job jo
         where jo.EmployeeId = em.EmployeeId
         order by jo.JobName)
  from Employee em;

但是,这会在“order by”子句上返回以下错误:

ORA-00907: missing right parenthesis

如果我去掉“order by”子句,此 SQL 可以工作,但我需要对作业列表进行排序。

我怎样才能做到这一点?

I have an aggregate function called ConcatList that concatenates a list of values into a single comma separated value.

Here is an example of what I am trying to do. I have two tables: Employee and Job. Job is a "child" of Employee. I need to get a list of the employees that includes a comma-delimited list of the jobs related to the employee, sorted by the name of the job.

I thought I could do this:

select em.EmployeeId,
       em.EmployeeName,
       (select ConcatList(jo.JobName)
          from Job jo
         where jo.EmployeeId = em.EmployeeId
         order by jo.JobName)
  from Employee em;

However, this returns me the following error on the "order by" clause:

ORA-00907: missing right parenthesis

If I get rid of the "order by" clause, this SQL works, but I need to sort the list of jobs.

How can I make this work?

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

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

发布评论

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

评论(3

寄意 2024-12-27 09:28:10

您可以对连接的记录进行排序,然后聚合结果:

select EmployeeId,
       EmployeeName,
       ConcatList(JobName)   
from (
    select em.EmployeeId,
           em.EmployeeName,
           jo.JobName
      from Employee em 
      join Job jo
        on jo.EmployeeId = em.EmployeeId
     order by jo.JobName 
      )
group by EmployeeId,
         EmployeeName

You can sort the joined records and then aggregate the result:

select EmployeeId,
       EmployeeName,
       ConcatList(JobName)   
from (
    select em.EmployeeId,
           em.EmployeeName,
           jo.JobName
      from Employee em 
      join Job jo
        on jo.EmployeeId = em.EmployeeId
     order by jo.JobName 
      )
group by EmployeeId,
         EmployeeName
忘羡 2024-12-27 09:28:10

由于 ConcatList 是用户定义的聚合函数,因此修改 ConcatList 以对结果进行排序或创建 ConcatSortedList 似乎是有意义的对它聚合的数据进行排序的聚合函数。 Gary Myers 有一个用户定义聚合函数的示例,生成一个排序列表

您还可以在 Tim Hall 的网站上了解各种字符串聚合技术。如果必须生成排序列表,那么使用其中许多技术比尝试保证非排序用户定义聚合函数生成排序结果更容易。例如,采用 REF CURSOR 的通用函数相对而言是当需要排序时很容易使用,因为您只需向光标添加 ORDER BY 即可。

Since ConcatList is a user-defined aggregate function, it would seem to makes sense either to modify ConcatList to order the results or to create a ConcatSortedList aggregate function that sorts the data it aggregates. Gary Myers has an example of a user-defined aggregate function that produces a sorted list.

You can also go through the various string aggregation techniques on Tim Hall's site. If you have to produce a sorted list, using many of those techniques would be easier than trying to guarantee that a non-sorting user-defined aggregate function produces a sorted result. The generic function taking a REF CURSOR, for example, is relatively easy to use when sorting is required because you can just add an ORDER BY to the cursor.

葬﹪忆之殇 2024-12-27 09:28:10
select em.EmployeeId,
       em.EmployeeName,
       wm_concat(jo.JobName) over 
          (partion by jo.EmployeeID order by JobName) as JobList
  from Employee em
  INNER JOIN JOB JO on jo.EmployeeId = em.EmployeeId

我从中意识到 wm_concat 是一个分析的示例 --> http://www.tek-tips.com/viewthread.cfm?qid=1629662

Text2 = 随机文本

val = 分组

Select val, text2, wm_concat(text2) over (partition by val order by text2) as out_text2 
from B


Test results:
VAL     text2                    out_Text2
1   XXX010105                   (CLOB) XXX010105
1   something XXX010101 somet   (CLOB) XXX010105,...
2   yet another XXX010102 and   (CLOB) yet anothe...
4   XXX010103                   (CLOB) XXX010103
5   a                           (CLOB) a
5   b                           (CLOB) a,b
5   c                           (CLOB) a,b,c
5   x                           (CLOB) a,b,c,x
6   a                           (CLOB) a
6   g                           (CLOB) a,g
6   i                           (CLOB) a,g,i
6   n                           (CLOB) a,g,i,n
6   x                           (CLOB) a,g,i,n,x
select em.EmployeeId,
       em.EmployeeName,
       wm_concat(jo.JobName) over 
          (partion by jo.EmployeeID order by JobName) as JobList
  from Employee em
  INNER JOIN JOB JO on jo.EmployeeId = em.EmployeeId

Example from which I realized wm_concat is an analytic --> http://www.tek-tips.com/viewthread.cfm?qid=1629662

Text2 = random text

val = grouping

Select val, text2, wm_concat(text2) over (partition by val order by text2) as out_text2 
from B


Test results:
VAL     text2                    out_Text2
1   XXX010105                   (CLOB) XXX010105
1   something XXX010101 somet   (CLOB) XXX010105,...
2   yet another XXX010102 and   (CLOB) yet anothe...
4   XXX010103                   (CLOB) XXX010103
5   a                           (CLOB) a
5   b                           (CLOB) a,b
5   c                           (CLOB) a,b,c
5   x                           (CLOB) a,b,c,x
6   a                           (CLOB) a
6   g                           (CLOB) a,g
6   i                           (CLOB) a,g,i
6   n                           (CLOB) a,g,i,n
6   x                           (CLOB) a,g,i,n,x
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文