我有
id | dvr
1 | 1,2,3
2 | 1,3,4
3 | 1,5,6,7,8
并希望
id | dvr
1 | 1
1 | 2
1 | 3
2 | 1
2 | 3
2 | 4
... and so on
我应该使用的最快查询是什么?
答案 0 :(得分:1)
制作如下的sql函数:
create Function [dbo].[fun_CSVToTable]
(
@LIST varchar(7000),
@Delimeter varchar(10)
)
RETURNS @RET1 TABLE (RESULT BIGINT)
AS
BEGIN
DECLARE @RET TABLE(RESULT BIGINT)
IF LTRIM(RTRIM(@LIST))='' RETURN
DECLARE @START BIGINT
DECLARE @LASTSTART BIGINT
SET @LASTSTART=0
SET @START=CHARINDEX(@Delimeter,@LIST,0)
IF @START=0
INSERT INTO @RET VALUES(SUBSTRING(@LIST,0,LEN(@LIST)+1))
WHILE(@START >0)
BEGIN
INSERT INTO @RET VALUES(SUBSTRING(@LIST,@LASTSTART,@START-@LASTSTART))
SET @LASTSTART=@START+1
SET @START=CHARINDEX(@Delimeter,@LIST,@START+1)
IF(@START=0)
INSERT INTO @RET VALUES(SUBSTRING(@LIST,@LASTSTART,LEN(@LIST)+1))
END
INSERT INTO @RET1 SELECT * FROM @RET
RETURN
END
答案 1 :(得分:0)
如果您正在运行postgresql,而dvr列是文本,您可以这样做:
select
id,
unnest(string_to_array(dvr,','))
from your_table;