Oracle SQL分组依据:当列具有特殊值时拆分组

Oracle SQL分组依据:当列具有特殊值时拆分组,oracle,Oracle,我有一个包含以下列的表: SerialNr Date Processstep Comment -------- ---------- ----------- ---------- 2300 01.01.2018 Step 01 Comment01 2300 02.01.2018 Step 02 Comment02 2300 03.01.2018 Step 03 Comment03 2

我有一个包含以下列的表:

SerialNr  Date        Processstep  Comment  
--------  ----------  -----------  ----------
    2300  01.01.2018  Step 01      Comment01  
    2300  02.01.2018  Step 02      Comment02  
    2300  03.01.2018  Step 03      Comment03  
    2300  04.01.2018  Finished     Comment04  
    2300  05.01.2018  Step 04      Comment05  
    2300  06.01.2018  Step 05      Comment06  
    2300  07.01.2018  Finished     Comment07  
    2302  01.01.2018  Step 01      Comment08  
    2302  02.01.2018  Step 02      Comment09  
    2302  03.01.2018  Step 03      Comment10  
    2302  04.01.2018  Finished     Comment11  
    2302  05.01.2018  Step 04      Comment12  
    2302  06.01.2018  Step 05      Comment13  
    2302  07.01.2018  Finished     Comment14  
我现在想得到一个包含以下信息的表:

SerialNr  Processstep                          Comment  
--------  -----------------------------------  -------------------------------------------
    2300  Step 01, Step 02, Step 03, Finished  Comment01, Comment02, Comment03, Comment 04  
    2300  Step 04, Step 05, Finished           Comment05, Comment06, Comment07  
    2302  Step 01, Step 02, Step 03, Finished  Comment08, Comment09, Comment10, Comment 11  
    2302  Step 04, Step 05, Finished           Comment12, Comment13, Comment14
因此,我希望对
SerialNr
进行分组,但同时也希望在Processstep
完成的位置拆分此组

有什么简单的方法吗?到目前为止,我只能按
SerienNr
进行分组。我不知道如何在Processstep
完成时拆分组:

SELECT 
SerienNr, 
listagg(Processstep, ' - ') within group (order by Date),
listagg(Comment, ' - ') within group (order by Date)
FROM table
GROUP BY SerienNr

您需要有另一列,它允许您根据“完成”行进行分组


从该表中,您可以再次应用相同的查询,但您需要根据SerienNr、grp_id进行分组,您需要有另一个列,该列允许您根据“finish”行进行分组


从这个表中,您可以再次应用相同的查询,除了按SerienNr、grp\u id进行分组之外。我更改了一些标识符:您的数据库中肯定没有名为
table
的表。不能有名为
comment
(它是保留字)的列,也不能有名为
date
的列。将来,请显示实际名称-这对任何人提供此类非法名称进行测试都没有帮助

另外-您的LISTAGG使用
-
,但是您使用
显示输出-我不知道您如何能够做到这一点

无论如何:

select   serialnr, listagg(processstep, ', ') within group (order by dt) as processstep,
                   listagg(comm, ', ')        within group (order by dt) as comm
from     (
           select serialnr, dt, processstep, comm,
                  count(case processstep when 'Finished' then 1 end)
                        over (partition by serialnr order by dt desc) as grp
           from   tbl
         )
group by serialnr, grp
order by serialnr, min(dt)  --  If needed
;

我更改了一些标识符:您的数据库中肯定没有名为
table
的表。不能有名为
comment
(它是保留字)的列,也不能有名为
date
的列。将来,请显示实际名称-这对任何人提供此类非法名称进行测试都没有帮助

另外-您的LISTAGG使用
-
,但是您使用
显示输出-我不知道您如何能够做到这一点

无论如何:

select   serialnr, listagg(processstep, ', ') within group (order by dt) as processstep,
                   listagg(comm, ', ')        within group (order by dt) as comm
from     (
           select serialnr, dt, processstep, comm,
                  count(case processstep when 'Finished' then 1 end)
                        over (partition by serialnr order by dt desc) as grp
           from   tbl
         )
group by serialnr, grp
order by serialnr, min(dt)  --  If needed
;

抱歉搞混了!您的解决方案非常有效,非常感谢。我只想弄明白它为什么会起作用。我还没有使用过“over(partitionby…)”功能。你有什么资源可以让我了解更多吗?@Christoph-如果你有时间学习分析函数,你会发现你可以做很多你一直想做的事情。最好从文档和一些示例开始,我会看一看。是的,我已经有了一些如何改进现有SQL查询的想法。我不知道到底有多少可能!抱歉搞混了!您的解决方案非常有效,非常感谢。我只想弄明白它为什么会起作用。我还没有使用过“over(partitionby…)”功能。你有什么资源可以让我了解更多吗?@Christoph-如果你有时间学习分析函数,你会发现你可以做很多你一直想做的事情。最好从文档和一些示例开始,我会看一看。是的,我已经有了一些如何改进现有SQL查询的想法。我不知道到底有多少可能!非常感谢。我不知道“结束”功能-我会仔细看看。谢谢!我不知道“over”函数——我会仔细看看。