使用先验连接连接字段

发布于 2024-11-15 17:25:10 字数 741 浏览 0 评论 0原文

有下表my_tabe

M01 |   1
M01 |   2
M02 |   1

我想查询它以获得:

M01 |   1,2
M02 |   1

我设法使用以下查询接近:

with my_tabe as
(
    select 'M01' as scycle, '1' as sdate from dual union
    select 'M01' as scycle, '2' as sdate from dual union
    select 'M02' as scycle, '1' as sdate from dual
)
SELECT scycle, ltrim(sys_connect_by_path(sdate, ','), ',')
FROM
(
    select scycle, sdate, rownum rn
    from my_tabe
    order by 1 desc
)
START WITH rn = 1
CONNECT BY PRIOR rn = rn - 1

Yielding:

SCYCLE      |   RES
M02         |   1,2,1
M01         |   1,2

这是错误的。看起来我已经很接近了,但恐怕我不知道下一步是什么......

有什么建议吗?

Having the following table my_tabe:

M01 |   1
M01 |   2
M02 |   1

I want to query over it in order to obtain:

M01 |   1,2
M02 |   1

I managed to get close using the following query:

with my_tabe as
(
    select 'M01' as scycle, '1' as sdate from dual union
    select 'M01' as scycle, '2' as sdate from dual union
    select 'M02' as scycle, '1' as sdate from dual
)
SELECT scycle, ltrim(sys_connect_by_path(sdate, ','), ',')
FROM
(
    select scycle, sdate, rownum rn
    from my_tabe
    order by 1 desc
)
START WITH rn = 1
CONNECT BY PRIOR rn = rn - 1

Yielding:

SCYCLE      |   RES
M02         |   1,2,1
M01         |   1,2

Which is wrong. It's seems I'm close, but I'm afraid I don't what's the next step...

Any tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆蝶 2024-11-22 17:25:10

您需要将 connect by 限制为相同的 scycle 值,并计算匹配数并对其进行过滤,以避免看到中间结果。

with my_tabe as
(
    select 'M01' as scycle, '1' as sdate from dual union
    select 'M01' as scycle, '2' as sdate from dual union
    select 'M02' as scycle, '1' as sdate from dual
)
select scycle, ltrim(sys_connect_by_path(sdate, ','), ',')
from
(
    select distinct sdate,
        scycle,
        count(1) over (partition by scycle) as cnt,
        row_number() over (partition by scycle order by sdate) as rn
    from my_tabe
)
where rn = cnt
start with rn = 1
connect by prior rn + 1 = rn
and prior scycle = scycle
/

SCYCLE LTRIM(SYS_CONNECT_BY_PATH(SDATE,','),',')
------ -----------------------------------------
M01    1,2
M02    1

如果您使用的是 11g,则可以使用内置的 改为 LISTAGG 函数:

with my_tabe as
(
    select 'M01' as scycle, '1' as sdate from dual union
    select 'M01' as scycle, '2' as sdate from dual union
    select 'M02' as scycle, '1' as sdate from dual
)
select scycle, listagg (sdate, ',') 
within group (order by sdate) res
from my_tabe
group by scycle
/ 

两种方法(以及其他方法)均显示此处

You need to restrict your connect by to the same scycle value, and also count the number of matches and filter on that to avoid seeing intermediate results.

with my_tabe as
(
    select 'M01' as scycle, '1' as sdate from dual union
    select 'M01' as scycle, '2' as sdate from dual union
    select 'M02' as scycle, '1' as sdate from dual
)
select scycle, ltrim(sys_connect_by_path(sdate, ','), ',')
from
(
    select distinct sdate,
        scycle,
        count(1) over (partition by scycle) as cnt,
        row_number() over (partition by scycle order by sdate) as rn
    from my_tabe
)
where rn = cnt
start with rn = 1
connect by prior rn + 1 = rn
and prior scycle = scycle
/

SCYCLE LTRIM(SYS_CONNECT_BY_PATH(SDATE,','),',')
------ -----------------------------------------
M01    1,2
M02    1

If you're on 11g you can use the built-in LISTAGG function instead:

with my_tabe as
(
    select 'M01' as scycle, '1' as sdate from dual union
    select 'M01' as scycle, '2' as sdate from dual union
    select 'M02' as scycle, '1' as sdate from dual
)
select scycle, listagg (sdate, ',') 
within group (order by sdate) res
from my_tabe
group by scycle
/ 

Both approaches (and others) are shown here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文