在 GROUP BY 查询中将值聚合到表类型中
假设您有一个表(在 Oracle 中):
CREATE TABLE CUSTOMER
(
customer_id NUMBER,
gender CHAR(1)
);
并假设您有一个表类型:
CREATE TYPE NUMBER_TABLE_TYPE AS TABLE OF NUMBER;
是否可以编写一个 GROUP BY 查询,以便对于每个组,该组的主键字段存储在NUMBER_TABLE_TYPE
?例如:
DECLARE
CURSOR gender_cursor IS
SELECT
gender,
/* The CUSTOMER_IDS column will be of type NUMBER_TABLE_TYPE: */
SOME_MAGICAL_AGGREGATE_FUNCTION(customer_id) AS customer_ids
FROM
CUSTOMER
GROUP BY
gender;
customer_ids NUMBER_TABLE_TYPE;
BEGIN
FOR gender IN gender_cursor LOOP
customer_ids := gender.customer_ids;
FOR i IN customer_ids.FIRST .. customer_ids.LAST LOOP
dbms_output.put_line(customer_ids(i));
END LOOP;
END LOOP;
END;
我知道这应该通过使用两个游标来完成(一个用于性别组,另一个用于查询每个组的客户)。我只是好奇这是否可以做到。 :)
Suppose you have a table (in Oracle):
CREATE TABLE CUSTOMER
(
customer_id NUMBER,
gender CHAR(1)
);
And suppose you have a table type:
CREATE TYPE NUMBER_TABLE_TYPE AS TABLE OF NUMBER;
Is it possible to write a GROUP BY
query such that, for each group, that group's primary key fields are stored in a NUMBER_TABLE_TYPE
? For example:
DECLARE
CURSOR gender_cursor IS
SELECT
gender,
/* The CUSTOMER_IDS column will be of type NUMBER_TABLE_TYPE: */
SOME_MAGICAL_AGGREGATE_FUNCTION(customer_id) AS customer_ids
FROM
CUSTOMER
GROUP BY
gender;
customer_ids NUMBER_TABLE_TYPE;
BEGIN
FOR gender IN gender_cursor LOOP
customer_ids := gender.customer_ids;
FOR i IN customer_ids.FIRST .. customer_ids.LAST LOOP
dbms_output.put_line(customer_ids(i));
END LOOP;
END LOOP;
END;
I know that this should be accomplished by having two cursors (one for the gender group, and another that queries each group's customers). I am just curious to see if this can be done. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两种方法可以实现这一点。
首先,通过分析来获取每行中该性别的所有记录的计数。
其次,更接近您的要求
Two ways to accomplish this.
Firstly, with analytics to get the count of all the records for that gender on each row.
Secondly, and closer to your request