gpt4 book ai didi

sql - Excel 聚合函数

转载 作者:行者123 更新时间:2023-12-02 01:58:25 28 4
gpt4 key购买 nike

我有一个包含列的 Excel 文件,

C_IP
SESSION_ID
CS_USER_AGENT
CS_URI_STEM
CS_URI_QUERY
WEB_LINK

由于 Oracle (11g) 中允许的字符串大小限制,我无法聚合上述属性。我尝试使用用户定义的聚合函数。我想聚合“WEB_LINK”列,并按 C_IP 分组。在 Excel 中可以做到这一点吗?

我尝试使用的 SQL 查询是,

CREATE TABLE WEBLOG_AGG AS
SELECT C_IP,
tab_to_string(CAST(COLLECT(WEB_LINK) AS T_VARCHAR2_TAB)) AS WEBLINKS
FROM WEBLOG_SESSION
GROUP BY C_IP;

最佳答案

我想执行连接到 clob 比编写 VBA 代码更容易。

15:34:36 SYSTEM@dwal> create table t1 ( group_col number, value varchar2(1 byte) );

Table created.

Elapsed: 00:00:00.10
15:34:38 SYSTEM@dwal> insert into t1
15:35:34 2 select 1, decode(mod(rownum,5), 0,0,1) from dual connect by rownum <= 4001
15:36:20 3 union all
15:36:22 4 select 2, decode(mod(rownum,5), 0,0,1) from dual connect by rownum <= 4001
15:36:27 5 ;

8002 rows created.

Elapsed: 00:00:00.05
15:36:28 SYSTEM@dwal> commit;

Commit complete.

Elapsed: 00:00:00.02
15:36:31 SYSTEM@dwal> create type t_varchar2_tab is table of varchar2(1);
15:37:11 2 /

Type created.

Elapsed: 00:00:00.50
15:38:15 SYSTEM@dwal> ed
Wrote file S:\tools\buffer.sql

1 create function tab_to_str(tab in t_varchar2_tab) return clob
2 as
3 result clob;
4 begin
5 for i in tab.first .. tab.last loop
6 result := result || tab(i);
7 end loop;
8 return result;
9* end;
15:38:46 SYSTEM@dwal> /

Function created.

Elapsed: 00:00:00.19
15:46:01 SYSTEM@dwal> select group_col
15:46:04 2 ,length(tab_to_str(cast(collect(value) as t_varchar2_tab))) len
15:46:10 3 ,substr(tab_to_str(cast(collect(value) as t_varchar2_tab)), 1, 20) val
15:46:12 4 from t1 group by group_col;

GROUP_COL LEN VAL
---------- ------ --------------------
1 4001 11011110111101111011
2 4001 11011110111101111011

Elapsed: 00:00:01.13

关于sql - Excel 聚合函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13944939/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com