SQL 中的 LISTAGG 正在返回具有空值的行
LISTAGG in SQL is returning a row with null values
我有 2 tables A 和 B,B 与 A 有外键关系,即 (b.detail_id = a.id)
我想对 B 中的其中一列应用 LISTAGG 查询。
SELECT LISTAGG(DISTINCT b.delivery_cadence, ',') WITHIN GROUP (ORDER BY b.delivery_cadence)
delivery_cadence, a.id FROM A a, B b WHERE b.detail_id = a.id AND a.id = 1236565;
上面的查询返回一行,所有值为空,但我不想要任何行。我怎样才能做到这一点?
如果不可能有任何替代解决方案。
a.id = 1236565 不存在于 A table.
假设您有这样的表:
create table a(id) as
(
select 1 from dual union all
select 2 from dual
);
create table b(detail_id, delivery_cadence) as
(
select 1, 'x' from dual union all
select 1, 'A' from dual union all
select 2, 'y' from dual
);
如果我理解得很好,你需要(在 ANSI 连接语法中):
SQL> SELECT LISTAGG(DISTINCT b.delivery_cadence, ',') WITHIN GROUP (ORDER BY b.delivery_cadence) delivery_cadence,
2 a.id
3 FROM A
4 inner join B ON b.detail_id = a.id
5 where a.id = 1236565
6 group by a.id;
no rows selected
对于表中存在的 ID
值,您得到:
SQL> SELECT LISTAGG(DISTINCT b.delivery_cadence, ',') WITHIN GROUP (ORDER BY b.delivery_cadence) delivery_cadence,
2 a.id
3 FROM A
4 inner join B ON b.detail_id = a.id
5 where a.id = 1
6 group by a.id;
DELIVERY_CADENCE ID
---------------- ----------
A,x 1
1 row selected.
只需添加 having count(b.delivery_cadence) > 0
例如
SELECT LISTAGG(DISTINCT b.delivery_cadence, ',') WITHIN GROUP (ORDER BY b.delivery_cadence)
delivery_cadence, a.id FROM A a, B b WHERE b.detail_id = a.id AND a.id = 1236565
HAVING COUNT(b.delivery_cadence) > 0
我有 2 tables A 和 B,B 与 A 有外键关系,即 (b.detail_id = a.id) 我想对 B 中的其中一列应用 LISTAGG 查询。
SELECT LISTAGG(DISTINCT b.delivery_cadence, ',') WITHIN GROUP (ORDER BY b.delivery_cadence)
delivery_cadence, a.id FROM A a, B b WHERE b.detail_id = a.id AND a.id = 1236565;
上面的查询返回一行,所有值为空,但我不想要任何行。我怎样才能做到这一点? 如果不可能有任何替代解决方案。
a.id = 1236565 不存在于 A table.
假设您有这样的表:
create table a(id) as
(
select 1 from dual union all
select 2 from dual
);
create table b(detail_id, delivery_cadence) as
(
select 1, 'x' from dual union all
select 1, 'A' from dual union all
select 2, 'y' from dual
);
如果我理解得很好,你需要(在 ANSI 连接语法中):
SQL> SELECT LISTAGG(DISTINCT b.delivery_cadence, ',') WITHIN GROUP (ORDER BY b.delivery_cadence) delivery_cadence,
2 a.id
3 FROM A
4 inner join B ON b.detail_id = a.id
5 where a.id = 1236565
6 group by a.id;
no rows selected
对于表中存在的 ID
值,您得到:
SQL> SELECT LISTAGG(DISTINCT b.delivery_cadence, ',') WITHIN GROUP (ORDER BY b.delivery_cadence) delivery_cadence,
2 a.id
3 FROM A
4 inner join B ON b.detail_id = a.id
5 where a.id = 1
6 group by a.id;
DELIVERY_CADENCE ID
---------------- ----------
A,x 1
1 row selected.
只需添加 having count(b.delivery_cadence) > 0
例如
SELECT LISTAGG(DISTINCT b.delivery_cadence, ',') WITHIN GROUP (ORDER BY b.delivery_cadence)
delivery_cadence, a.id FROM A a, B b WHERE b.detail_id = a.id AND a.id = 1236565
HAVING COUNT(b.delivery_cadence) > 0