将多行的字段连接成单个字段

时间:2017-05-22 22:21:55

标签: sql oracle exadata

障碍:我无法创造任何东西。没有GTT,PROC等我正在查询Exadata数据湖,我创建的任何脚本都将从Toad或SQL Developer执行。

我有一个返回3行的SELECT查询:

  • 日期
  • IDKey
  • EVENT

对于每个日期,IDKey,可能有十几个EVENT。示例:

Sub Tester()

    With ActiveSheet
        CountCcolor .Range("A1:A30"), .Range("A3"), .Range("A1"), .Range("C1")
    End With

End Sub



Sub CountCcolor(range_data As Range, criteria As Range, _
                log_page As Range, rngOutput As Range)

    Dim arrCounts(), resNum As Long
    Dim datax As Range
    Dim xcolor As Long
    Dim ycolor As Long

    'size output array for worst-case scenario (all purple rows)
    ReDim arrCounts(1 To range_data.Rows.Count, 1 To 2)

    xcolor = criteria.Interior.ColorIndex
    ycolor = log_page.Interior.ColorIndex
    resNum = 0

    For Each datax In range_data
        If datax.Interior.ColorIndex = ycolor Then

            resNum = resNum + 1
            arrCounts(resNum, 1) = datax.Row '<< log the header row
            arrCounts(resNum, 2) = 0

        ElseIf datax.Interior.ColorIndex = xcolor Then
            If resNum > 0 Then
                 'increment the count
                 arrCounts(resNum, 2) = arrCounts(resNum, 2) + 1
            End If
        End If
    Next datax

    'populate the counts to the worksheet
    rngOutput.Resize(resNum, 2).Value = arrCounts

End Sub

在给定的DATE,IDKey可以有多个EVENTS,每个都会生成一个新行。

目标:创建一个将DATE和IDKEY分组的行,将所有唯一的EVENT连接到一个字段中;

 DATE       IDKEY EVENT    
10/17/2016  300328  EVENT1    
10/17/2016  300328  EVENT3    
10/17/2016  300328  EVENT4    
10/17/2016  300440  EVENT1    
10/17/2016  300440  EVENT2    
10/17/2016  300440  EVENT5 

2 个答案:

答案 0 :(得分:1)

看起来像group by listagg

with demo (dt, id, event) as
   ( select date '2016-10-17', 300328, 'EVENT1' from dual union all
     select date '2016-10-17', 300328, 'EVENT3' from dual union all
     select date '2016-10-17', 300328, 'EVENT4' from dual union all
     select date '2016-10-17', 300440, 'EVENT1' from dual union all
     select date '2016-10-17', 300440, 'EVENT2' from dual union all
     select date '2016-10-17', 300440, 'EVENT5' from dual 
   )
select dt, id
     , listagg(event, '|') within group (order by event) as events
from   demo
group by dt, id;

DT                  ID EVENTS
----------- ---------- --------------------------------
17/10/2016      300328 EVENT1|EVENT3|EVENT4
17/10/2016      300440 EVENT1|EVENT2|EVENT5

如果有大量的事件,可能会出现问题。

答案 1 :(得分:1)

您可以使用&#34; listagg&#34;功能。请查看http://docs.oracle.com/database/122/SQLRF/LISTAGG.htm#SQLRF30030以进一步阅读

app.get("/", (req,res)
  .then(function(firstResult)) {
    //You can use result of first promise here
    return Promise.all([
      //Create array of get request here
      //To also return firstResult just add it in the Promise.All array
    ]);
  })
  .then(function(allResults){
    //You can use results of all the get requests created in the previous then()
  })
  .catch(function(error){
    //Deal with any error that happened
  });