标签列以逗号分隔,我需要将其拆分为不同的行,如下所示。我在论坛上看到了多个链接,但大多数功能组合在SAP HANA中都不起作用。任何帮助都将受到高度赞赏。
MY_TABLE:
+-----+--------------+------------+-------------+
| id | parent_title | account_id | tags |
+-----+--------------+------------+-------------+
| 647 | title999 | 64 | 361,381,388 |
| 646 | title998 | 64 | 361,376,388 |
+-----+--------------+------------+-------------+
Required_Table
+-----+--------------+------------+------+
| 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 |
+-----+--------------+------------+------+
答案 0 :(得分:0)
这将有效,需要创建2个临时表。将价值单独存储为Bill Suggested总是一个好主意。我假设你有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
答案 1 :(得分:0)
尝试
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 | parent_title | account_id |标签|
| 647 | title999 | 64 | 361,381,388 |
| 646 | title998 | 64 | 361,376,388 |