任何人都可以帮我解决这个问题。在表格中,我现在有这样的数据。
如何将具有分隔符TTBFA-TTBFB-TTBFC-TTBFD的列节点拆分为4行,其他列相同。
加州地区GAXAEB 102,520,000 18.71 4 8/30/2014
加州地区TTBFA 92,160,000 23.33 3 9/13/2014
加州地区TTBFB 92,160,000 23.33 3 9/13/2014
加州地区TTBFC 92,160,000 23.33 3 9/13/2014
加州地区TTBFD 92,160,000 23.33 3 9/13/2014
列NODES的值不总是5个字符,可能如下所示
提前致谢
答案 0 :(得分:6)
INSTR函数通常意味着你正在运行TD14 +。
还有STRTOK功能,更好地使用它而不是SUBSTRING(INSTR)。
而不是最多15个UNION ALL,你也可以交叉连接到一个带数字的表:
SELECT region_name, STRTOK(nodes, '-', i) AS x
FROM table
CROSS JOIN
( -- better don't use sys_calendar.CALENDAR as there are no statistics on day_of_calendar
SELECT day_of_calendar AS i
FROM sys_calendar.CALENDAR
WHERE i <= 15
) AS dt
WHERE x IS NOT NULL
您也可以在TD14中使用STRTOK_SPLIT_TO_TABLE:
SELECT *
FROM table AS t1
JOIN
(
SELECT *
FROM TABLE (STRTOK_SPLIT_TO_TABLE(table.division, table.nodes, '-')
RETURNS (division VARCHAR(30) CHARACTER SET UNICODE
,tokennum INTEGER
,token VARCHAR(30) CHARACTER SET UNICODE)
) AS dt
) AS t2
ON t1.division = t2.division
希望这是用于数据清理而不是日常使用......
答案 1 :(得分:1)
你可以使用(无论你的最大节点数是多少)UNION ALL
语句和SUBSTRING
与INSTR一起用于节点的可能位置
尝试类似:
SELECT region_name, nodes AS node,
sgspeed, sgutil, portCount, WeekendingDate
FROM t
WHERE instr(nodes,'-') = 0
UNION ALL
SELECT region_name, SUBSTRING(nodes FROM instr(nodes,'-',1,1) +1 FOR instr(nodes,'-',1,2)-1) AS node,
sgspeed, sgutil, portCount, WeekendingDate
FROM t
WHERE instr(nodes,'-') > 0
UNION ALL
SELECT region_name, SUBSTRING(nodes FROM instr(nodes,'-',1,2) +1 FOR instr(nodes,'-',1,3)-1) AS node,
sgspeed, sgutil, portCount, WeekendingDate
FROM t
WHERE instr(nodes,'-',1,2) > 0
UNION ALL
SELECT region_name, SUBSTRING(nodes FROM instr(nodes,'-',1,3) +1 FOR instr(nodes,'-',1,4)-1) AS node,
sgspeed, sgutil, portCount, WeekendingDate
FROM t
WHERE instr(nodes,'-',1,3) > 0
...
答案 2 :(得分:0)
这很好。
select
REGION_NAME,
case when POSITION('-' IN Nodes) = 0 then NODES else SUBSTRING(Nodes,0,POSITION('-' IN Nodes)) end as node,
SgSpeed,
SgUtil,
PortCount,
WeekEndingDate
FROM table
UNION
select
REGION_NAME,
SUBSTRING(nodes FROM instr(nodes,'-',1,1)+1 for instr(nodes,'-',1,1)-1) AS node,
SgSpeed,
SgUtil,
PortCount,
WeekEndingDate
FROM table
WHERE instr(nodes,'-') > 0
UNION
select
REGION_NAME,
SUBSTRING(nodes FROM instr(nodes,'-',1,2)+1 for instr(nodes,'-',1,1)-1) AS node,
SgSpeed,
SgUtil,
PortCount,
WeekEndingDate
FROM table
WHERE instr(nodes,'-') > 0
UNION
select
REGION_NAME,
SUBSTRING(nodes FROM instr(nodes,'-',1,3)+1 for instr(nodes,'-',1,1)-1) AS node,
SgSpeed,
SgUtil,
PortCount,
WeekEndingDate
FROM table
WHERE instr(nodes,'-') > 0