【问题标题】:use of group by clause in oracle apex在 oracle apex 中使用 group by 子句
【发布时间】:2018-12-10 12:50:46
【问题描述】:

首先,我是 Oracle 新手,这就是为什么我的问题对于有经验的开发人员来说可能很有趣。提前抱歉。

问题。我有一个包含 3 个列(ID、名称、组名)的表测试

表格

ID | Name | Group_name
1  | name1 | group 1 
2  | name2 | group 1  
3  | name3 | group  1 
4  | name4 | group 2
5  | name5 | group 2

我想在 oracle apex 中创建一个选择列表。

-选择-

(Group 1)
Name 1
Name 2
Name 3
(Group 2)
Name 4
Name 5

其中Group1Group2仅用于显示,不可选择。

我试过了:

SELECT NAME, ID
FROM TEST
GROUP BY GROUP_NAME

给出错误:

不是按表达式分组

【问题讨论】:

标签: sql oracle oracle-apex


【解决方案1】:

首先,您的查询不是有效的 SQL。这在任何环境中都不起作用,无论是 Apex 还是 sqlplus。但这并不重要,因为...

其次,由于您希望显示组名而不是可选择的,因此您尝试生成的 HTML 将类似于:

<select>
    <optgroup label="Group 1">
        <option>Name 1</option>
        <option>Name 2</option>
        <option>Name 3</option>
    </optgroup>
    ....

Apex Select 项目类型不支持&lt;optgroup&gt;(除非他们在最近的版本中添加了该功能)。

我认为您的选择是编写一个插件,但这是一个相当高级的主题,或者使用其他人已经制作的插件。 Select2 Apex Plugin 将适用于您的目的。请参阅该页面下方的“选项分组”。

【讨论】:

    【解决方案2】:

    据我所知,在 native Apex 中,您无法影响某些选择列表元素是否不可选择(这是您想要的组名称)。

    但是,您可以创建一个选择列表查询来显示您想要的内容。这是一个例子:

    SQL> with test (id, name, group_name) as
      2    (select 1, 'name1', 'group 1' from dual union all
      3     select 6, 'name2', 'group 1' from dual union all
      4     select 9, 'name3', 'group 1' from dual union all
      5     --
      6     select 2, 'name4', 'group 2' from dual union all
      7     select 4, 'name5', 'group 2' from dual
      8    ),
      9  inter as
     10    (-- Group names; ID is a negative value to make sure it is displayed first
     11     select -row_number() over (order by group_name) id,
     12       '(' || initcap(group_name) ||')' name, group_name
     13     from test
     14     group by group_name
     15     union
     16     -- members of the group
     17     select id, name, group_name
     18     from test
     19    )
     20  select name display_value,
     21         id   return_value
     22  from inter
     23  order by group_name, id;
    
    DISPLAY_V RETURN_VALUE
    --------- ------------
    (Group 1)           -1
    name1                1
    name2                6
    name3                9
    (Group 2)           -2
    name4                2
    name5                4
    
    7 rows selected.
    
    SQL>
    

    【讨论】:

      猜你喜欢
      • 2012-04-20
      • 2021-08-15
      • 2022-01-13
      • 2023-04-04
      • 1970-01-01
      • 2011-09-25
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      相关资源
      最近更新 更多