PLSQL 10g-创建一个字符串列表

时间:2016-03-14 17:13:26

标签: sql oracle plsql oracle10g

我一直在研究这个问题。我在下面有一个数据集,我需要将所需的输出放在字符串列表中。如果有重复的产品,我希望它在单独的行上。下面给我的是所有的客户和产品在一条线上。我想如果有重复项在副线上复制。如果有任何问题,请告诉我。

With CustomerData as (
select
ROW_NUMBER() OVER (PARTITION BY customerid ORDER BY Product) rn,
COUNT(*) OVER (PARTITION BY customerid ) cnt
from Customers)
select 
ltrim(sys_connect_by_path(customerid,','),','),
ltrim(sys_connect_by_path(Product,','),',') AS Product,
from CustomerData 
where rn = cnt 
start with rn = 1 
connect by prior customerid = customerid
and prior rn = rn1 


customerid | CustomerName | Product | date 
1            Bob            9         3-14-2016
1            Bob            10        3-14-2016       
1            Bob            9         3-12-2016
2            Brad           1         3-14-2016
2            Brad           3         3-14-2016
3            Sam            1         3-14-2016
3            Sam            1         3-12-2016
3            Sam            5         3-14-2016

期望输出

customerid   CustomerName      Product   
1, 1         BOB, BOB         9, 10
1,           BOB              9
2, 2         Brad, Brad       1, 3
3, 3         Sam, Sam         1, 5
3            Sam              1

2 个答案:

答案 0 :(得分:1)

在Oracle 10上,您应该能够使用wmsys.wm_concat函数,但要注意返回数据类型是CLOB而不是varchar:

WITH the_Data as (
select 1 cust_id,           'Bob' cust_name,           9 prod_id,        to_Date('3-14-2016','mm-dd-yyyy') order_dt from dual union all
select 1,            'Bob',            10,        to_Date('3-14-2016','mm-dd-yyyy') order_dt from dual union all       
select 1,            'Bob',            9,        to_Date('3-12-2016','mm-dd-yyyy') order_dt from dual union all
select 2,            'Brad',           1,        to_Date('3-14-2016','mm-dd-yyyy') order_dt from dual union all
select 2,            'Brad',           3,        to_Date('3-14-2016','mm-dd-yyyy') order_dt from dual union all
select 3,            'Sam',            1,        to_Date('3-14-2016','mm-dd-yyyy') order_dt from dual union all
select 3,            'Sam',            1,        to_Date(' 3-12-2016','mm-dd-yyyy') order_dt from dual union all
select 3,            'Sam',            5,        to_Date('3-14-2016','mm-dd-yyyy') order_dt from dual 
)
select wm_concat(cust_id)
      ,wm_concat(cust_name)
      ,wm_concat(prod_id) 
 from the_Data
group by order_dt, cust_id
order by cust_id, order_Dt  

WM_CONCAT(CUST_ID),WM_CONCAT(CUST_NAME),WM_CONCAT(PROD_ID)
1                 Bob                  9
1,1               Bob,Bob              9,10
2,2               Brad,Brad            1,3
3                 Sam                  1
3,3               Sam,Sam              1,5

如果您无权访问(它不是受支持的功能,也不是一直安装),那么您可以实现Tom Kyte's STRAGG user-defined analytic as shown here

除此之外,您还可以选择进行XML处理:

WITH the_Data as (
select 1 cust_id,           'Bob' cust_name,           9 prod_id,        to_Date('3-14-2016','mm-dd-yyyy') order_dt from dual union all
select 1,            'Bob',            10,        to_Date('3-14-2016','mm-dd-yyyy') order_dt from dual union all       
select 1,            'Bob',            9,        to_Date('3-12-2016','mm-dd-yyyy') order_dt from dual union all
select 2,            'Brad',           1,        to_Date('3-14-2016','mm-dd-yyyy') order_dt from dual union all
select 2,            'Brad',           3,        to_Date('3-14-2016','mm-dd-yyyy') order_dt from dual union all
select 3,            'Sam',            1,        to_Date('3-14-2016','mm-dd-yyyy') order_dt from dual union all
select 3,            'Sam',            1,        to_Date(' 3-12-2016','mm-dd-yyyy') order_dt from dual union all
select 3,            'Sam',            5,        to_Date('3-14-2016','mm-dd-yyyy') order_dt from dual 
)
select rtrim(xmlagg(xmlelement(e,cust_id,',').extract('//text()')),',') cus_ids
      ,rtrim(xmlagg(xmlelement(e,cust_name,',').extract('//text()')),',') cus_names
      ,rtrim(xmlagg(xmlelement(e,prod_id,',').extract('//text()')),',') prod_ids
 from the_Data
group by order_dt, cust_id
order by cust_id, order_Dt ;

cus_ids,      cus_names,    prod_ids    
1             Bob           9
1,1           Bob,Bob       9,10
2,2           Brad,Brad     1,3
3             Sam           1
3,3           Sam,Sam       1,5

答案 1 :(得分:1)

不使用未记录的(且不受支持的)WMSYS.WM_CONCAT函数(在所有系统上都不可用且not available at all on Oracle 12c),您可以使用集合和COLLECT聚合函数来执行此操作。 / p>

Oracle安装程序

CREATE OR REPLACE TYPE VARCHAR2s_Table AS TABLE OF VARCHAR2(4000);
/

CREATE OR REPLACE FUNCTION concatStrings(
  Strs VARCHAR2s_Table,
  delim VARCHAR2 DEFAULT ','
) RETURN CLOB
AS
  out_string CLOB;
BEGIN
  FOR i IN 1 .. Strs.COUNT LOOP
    out_string := out_string || CASE WHEN i = 1 THEN '' ELSE delim END || Strs(i);
  END LOOP;
  RETURN out_string;
END;
/

<强>查询

SELECT concatStrings( CAST( COLLECT( TO_CHAR( cust_id ) ) AS VARCHAR2s_Table ) ) AS cust_ids,
       concatStrings( CAST( COLLECT( cust_name ) AS VARCHAR2s_Table ) ) AS cust_names,
       concatStrings( CAST( COLLECT( TO_CHAR( prod_id ) ) AS VARCHAR2s_Table ) ) AS prod_ids
FROM   table_name
GROUP BY cust_id, order_dt;

<强>输出

CUST_IDS              CUST_NAMES            PROD_IDS
--------------------- --------------------- ---------------------
1                     Bob                   9
1,1                   Bob,Bob               9,10
2,2                   Brad,Brad             1,3  
3                     Sam                   1
3,3                   Sam,Sam               1,5