Oracle LISTAGG()用于多个属性?

时间:2015-06-23 12:46:42

标签: string oracle aggregate-functions string-concatenation listagg

我想知道下面描述的问题是否有更好的(即更快执行)解决方案。

步骤1)

create table t (k number, v1 number, v2 number);

insert into t values (1,1,1);
insert into t values (1,2,2);
insert into t values (1,2,3);
insert into t values (1,3,3);
insert into t values (1,4,3);  
insert into t values (2,7,8);
insert into t values (2,7,9);

步骤2)

我想返回以下数据集

(k, v1_list, v2_list)
1, (1,2,3,4), (1,2,3)
2, (7), (8,9)

步骤3)

我能够通过运行listagg()的多个组合来解决它,我想知道它是否可以用另一种方式实现。 在这个例子中,我处理2个属性,但实际上我必须为数百个列表运行类似的语句。如下所示的查询可能变得很简洁。我想知道是否可以在一个查询中完成,可能多次使用listagg()?

with q1 as (
 select distinct k, listagg (v1, ', ') within group (order by k) over (partition by k) v1_list
 from   t
 group  by k, v1),
q2 as (
 select distinct k, listagg (v2, ', ') within group (order by k) over (partition by k) v2_list
 from   t
 group  by k, v2)
--
select distinct t.k, v1_list, v2_list from t       
--
join   q1 on q1.k = t.k
join   q2 on q2.k = t.k

感谢您的建议,

- 卢卡斯

3 个答案:

答案 0 :(得分:1)

您可以尝试未记录的函数wm_concat

select k, wm_concat(distinct(v1)), wm_concat(distinct(v2))
from t
group by k

listagg

select k, listagg(v1, ', ') within group (order by k), listagg(v2, ', ') within group (order by k)
from (
    select distinct k, v1, null v2 from t  
    union all
    select distinct k, null v1, v2 from t
)
group by k

答案 1 :(得分:0)

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {

        //1. remove data from model
        data.removeAtIndex(indexPath.row) 

        //2. remove row from view
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 

        //3. custom method to update your view after removing
        updateView() 
    }
}

答案 2 :(得分:0)

另一种方式:

String AdsIds[]=new String[6];
String EnTitle[]=new String[6];
String AdsTemproryData[]=new String[6];
String BineryTemprory[]=new String[2];
public void sieve(String Hash){         
    AdsTemproryData=Hash.split("/");
    for(int i=0;i<=5;i++){
        BineryTemprory= AdsTemproryData[i].split("*");
        AdsIds[i]=BineryTemprory[0];
        EnTitle[i]=BineryTemprory[1];
    }

SQL Fiddle

或没有投射到odcinumberlist:

select t.k
,(select listagg(column_value,', ') within group (order by column_value)
  from table(v1)) v1 
,(select listagg(column_value,', ') within group (order by column_value)
  from table(v2)) v2 
from (select k
            ,cast(collect (distinct v1) as sys.odcinumberlist) v1
            ,cast(collect (distinct v2) as sys.odcinumberlist) v2
      from T t
      group by k) t