标签列以逗号分隔,我需要将其分成不同的行,如下所示。我在论坛上看到了多个链接,但大多数功能组合在 SAP HANA 中不起作用。任何帮助将不胜感激。
我的表:
+-----+--------------+------------+-------------+
| id | parent_title | account_id | tags |
+-----+--------------+------------+-------------+
| 647 | title999 | 64 | 361,381,388 |
| 646 | title998 | 64 | 361,376,388 |
+-----+--------------+------------+-------------+
必填_表
+-----+--------------+------------+------+
| id | parent_title | account_id | tags |
+-----+--------------+------------+------+
| 647 | title999 | 64 | 361 |
| 647 | title999 | 64 | 381 |
| 647 | title999 | 64 | 388 |
| 646 | title998 | 64 | 361 |
| 646 | title998 | 64 | 376 |
| 646 | title998 | 64 | 388 |
+-----+--------------+------------+------+
这可以工作,需要创建 2 个临时表。正如比尔建议的那样,单独存储值总是一个好主意。我假设您的字符串中有 3 位数字,后跟逗号并编译了此代码。这将适用于您给出的示例数据。
create table #temp
( id int, parent_title varchar(100), account_id int, tags varchar(max))
insert into #temp
values ( '647','title999','64', '361,381,388');
insert into #temp
values ( '647','title999','64', '361,376,388');
create table #temp2
( id int, parent_title varchar(100), account_id int, tags varchar(max));
insert #temp2 (id,parent_title,account_id,tags)
select id, parent_title,account_id, LEFT(tags,3) tags from #temp;
insert #temp2 (id,parent_title,account_id,tags)
select id, parent_title,account_id, right (tags,3) tags from #temp ;
insert #temp2 (id,parent_title,account_id,tags)
select id, parent_title,account_id, left( substring( tags,5,6),3) tags from #temp ;
select * from #temp2
drop table #temp ;
drop table #temp2
尝试
SELECT id, parent_title, account_id, STRING_AGG(tags,',' ORDER BY tags ) AS tags
from your_Table
group by id, parent_title, account_id
order by 1 desc
结果
| id |父标题 |帐户 ID | 标签 |
| 647 | 647标题999 | 64 | 361,381,388 |
| 646 | 646标题998 | 64 | 361,376,388 |
TABLES: MY_TABLE.
DATA: LT_SEP_VALUES TYPE TABLE OF STRING.
DATA: LT_REQUIRED_TABLE TYPE TABLE OF MY_TABLE. " Or you can create custom table type
LS_REQUIRED_TABLE TYPE MY_TABLE.
SELECT FROM MY_TABLE
FIELDS *
INTO TABLE @DATA(LT_MY_TABLE).
// Give condition if required
LT_MY_TABLE
并拆分字段 tags
的逗号分隔值,并将它们附加到您所需的本地表中。LOOP AT LT_MY_TABLE INTO DATA(LS_MY_TABLE).
CLEAR LS_REQUIRED_TABLE.
MOVE-CORRESPONDING LS_MY_TABLE TO LS_REQUIRED_TABLE.
REFRESH LT_SEP_VALUES.
SPLIT LS_MY_TABLE-TAGS AT ',' INTO LT_SEP_VALUES.
IF LT_SEP_VALUES IS NOT INITIAL.
CLEAR LS_REQUIRED_TABLE-TAGS.
LOOP AT LT_SEP_VALUES INTO DATA(LV_TAG).
LS_REQUIRED_TABLE-TAGS = LV_TAG.
APPEND LS_REQUIRED_TABLE TO LT_REQUIRED_TABLE.
ENDLOOP.
ENDIF.
ENDLOOP.
这只是一个获取和处理位的通用过程。