我正在使用MS SQL Server 2008中的旧表,需要创建view
以新系统需要的方式显示数据。这是遗产表。
id userid sport1 sport1level sport2 sport2level
----------------------------------------------------------------------------------------------------------------------------------
1 11 Baseball Varsity Baseball Recreational
2 22 Baseball,Basketball Varsity,Junior Varsity Baseball Varsity
3 33 Soccer Varsity Soccer,Track & Field Recreational,Intramural
4 44 null null Tennis Varsity
5 55 Volleyball Varsity null null
6 66 Baseball,Basketball Varsity,Varsity Soccer,Football Varsity,Varsity
7 77 Baseball,Basketball,Rowing Varsity,Varsity,Varsity Soccer,Football,Volleyball Varsity,Varsity,Recreational
这是我们正在寻找的结果:
id userid sport sportlevel1 sportlevel2
---------------------------------------------------------------------------------------
1_1 11 Baseball Varsity Recreational
2_1 22 Baseball Varsity Varsity
2_2 22 Basketball Junior Varsity null
3_1 33 Soccer Varsity Recreational
3_2 33 Track & Field null Intramural
4_1 44 Tennis null Varsity
5_1 55 Volleyball Varsity null
6_1 66 Baseball Varsity null
6_2 66 Basketball Varsity null
6_3 66 Soccer null Varsity
6_4 66 Football null Varsity
7_1 77 Baseball Varsity null
7_2 77 Basketball Varsity null
7_3 77 Rowing Varsity null
7_4 77 Soccer null Varsity
7_5 77 Football null Varsity
7_6 77 Volleyball null Recreational
需要注意的关键事项:
id
是int
,但在新表中不一定需要这种方式id
是{original id}_{incremental sport count per user}
的串联。对于用户选择的每项运动,{incremental sport count per user}
,sub id
,userid = 2
。例如:distinct
选择了2 {{1}}项运动:棒球和篮球,即使棒球属于两列。 如果我必须创建帮助函数或其他什么,请告诉我。
如果您有任何疑问或需要更多信息,请告知我们。
请不要试图问为什么它采用这种结构,或者尝试为新格式提供更好的结构。感谢
答案 0 :(得分:2)
不太优雅,但它确实起作用:
WITH
Data AS(
SELECT *
FROM (
VALUES ( 1, 11, 'Baseball ', 'Varsity ', 'Baseball ', 'Recreational ' )
, ( 2, 22, 'Baseball,Basketball', 'Varsity,Junior Varsity', 'Baseball ', 'Varsity ' )
, ( 3, 33, 'Soccer ', 'Varsity ', 'Soccer,Track & Field', 'Recreational,Intramural' )
, ( 4, 44, NULL, NULL, 'Tennis ', 'Varsity ' )
, ( 5, 55, 'Volleyball ', 'Varsity ', NULL, NULL )
, ( 6, 66, 'Baseball,Basketball', 'Varsity,Varsity ', 'Soccer,Football ', 'Varsity,Varsity ' )
, ( 7, 77, 'Baseball,Football,Rugby,Wrestling', 'Varsity,Varsity,Varsity,Junior Varsity', 'Rugby', 'Recreational' )
) AS T(id, userid, sport1, sport1level, sport2, sport2level)
),
SplitValues AS(
-- Substring logic is in the Anchor record ommited to prevent repetition of
-- code, therefor level 0 needs to be ignored
SELECT
id
, userid
, [level] = 0
, sport1 = sport1
, sport1level = sport1level
, sport2 = sport2
, sport2level = sport2level
, sport1Remainder = sport1
, sport1levelRemainder = sport1level
, sport2Remainder = sport2
, sport2levelRemainder = sport2level
FROM data
UNION ALL
SELECT
id
, userid
, [level] = [level] + 1
, sport1 = SUBSTRING(sport1Remainder, 1, ISNULL(NULLIF(CHARINDEX(',', sport1Remainder)- 1, -1), LEN(sport1Remainder)))
, sport1level = SUBSTRING(sport1levelRemainder, 1, ISNULL(NULLIF(CHARINDEX(',', sport1levelRemainder)- 1, -1), LEN(sport1levelRemainder)))
, sport2 = SUBSTRING(sport2Remainder, 1, ISNULL(NULLIF(CHARINDEX(',', sport2Remainder)- 1, -1), LEN(sport2Remainder)))
, sport2level = SUBSTRING(sport2levelRemainder, 1, ISNULL(NULLIF(CHARINDEX(',', sport2levelRemainder)- 1, -1), LEN(sport2levelRemainder)))
, sport1Remainder = SUBSTRING(sport1Remainder, NULLIF(CHARINDEX(',', sport1Remainder)+1, 1), LEN(sport1Remainder))
, sport1levelRemainder = SUBSTRING(sport1levelRemainder, NULLIF(CHARINDEX(',', sport1levelRemainder)+1, 1), LEN(sport1levelRemainder))
, sport2Remainder = SUBSTRING(sport2Remainder, NULLIF(CHARINDEX(',', sport2Remainder)+1, 1), LEN(sport2Remainder))
, sport2levelRemainder = SUBSTRING(sport2levelRemainder, NULLIF(CHARINDEX(',', sport2levelRemainder)+1, 1), LEN(sport2levelRemainder))
FROM SplitValues
WHERE sport1Remainder IS NOT NULL
OR sport2Remainder IS NOT NULL
),
SplitRowsWithDifferentSport AS(
SELECT id
, userid
, sport1
, sport1level
, sport1level2 = CASE WHEN sport1 = sport2 THEN sport2level END
FROM SplitValues
WHERE [level] <> 0
UNION ALL
SELECT id
, userid
, sport2
, null
, sport1level2 = sport2level
FROM SplitValues
WHERE ISNULL(sport1, '') <> sport2
AND [level] <> 0
)
SELECT id = CAST(S.id AS VARCHAR(max)) + '_' +
CAST(ROW_NUMBER() OVER (PARTITION BY S.userid ORDER BY s.id) AS VARCHAR(max))
, S.sport1
, sport1level1 = MAX(S.sport1level)
, sport1level2 = MAX(S.sport1level2)
FROM SplitRowsWithDifferentSport AS S
WHERE S.sport1 IS NOT NULL
GROUP BY S.ID, S.userid, S.sport1
ORDER BY id
编辑:更改了SplitValues CTE以允许在一列中进行多项运动。现在每行最多支持99项运动。如果您需要甚至更高,请添加OPTION(MAXRECURSION 0)
以完全没有限制。
EDIT2:添加了分组,以便在多行中摆脱相同的运动。
答案 1 :(得分:1)
您期望的输出未经过优化,因为它提供了一种在SportLevel2中生成空值的方法。您应该将每项运动和每个级别存储为一个单独的行,例如:
nid userid SportName SportLevel
1_1 11 Baseball Varsity
1_2 11 Baseball Recreational
2_1 22 Baseball Varsity
2_2 22 Baseball Varsity
3_1 33 Soccer Varsity
3_2 33 Soccer Recreational
3_3 33 Track & Field Recreation
4_2 44 Tennis Varsity
5_1 55 Volleyball Varsity
6_1 66 Baseball Varsity
6_2 66 Soccer Varsity
6_2 66 Basketball Varsity
6_3 66 Football Varsity
为实现这一目标,您可以按照以下方式使用CTE:
DECLARE @tmp TABLE(id INT IDENTITY(1,1), userid INT , sport1 VARCHAR(150), sport1level VARCHAR(150), sport2 VARCHAR(150), sport2level VARCHAR(150))
INSERT INTO @tmp (userid, sport1, sport1level, sport2, sport2level)
VALUES(11, 'Baseball', 'Varsity', 'Baseball', 'Recreational'),
(22, 'Baseball,Basketball', 'Varsity,Junior Varsity', 'Baseball', 'Varsity'),
(33, 'Soccer', 'Varsity', 'Soccer,Track & Field', 'Recreational,Intramural'),
(44, null, null, 'Tennis', 'Varsity'),
(55, 'Volleyball', 'Varsity', null, null),
(66, 'Baseball,Basketball', 'Varsity,Varsity', 'Soccer,Football', 'Varsity,Varsity')
;WITH Sports AS
(
--1) initial value
-- a) no commas in sport1
SELECT id, userid, 1 AS sportid, sport1 AS SportName, sport1level AS SportLevel,
NULL AS SportNameRemainder, NULL AS SportLevelRemainder
FROM @tmp
WHERE CHARINDEX(',', sport1)=0 AND CHARINDEX(',', sport1level)=0
UNION ALL
-- b) no commas in sport2
SELECT id, userid, 2 AS sportid, sport2 AS SportName, sport2level AS SportLevel,
NULL AS SportNameRemainder, NULL AS SportLevelRemainder
FROM @tmp
WHERE CHARINDEX(',', sport2)=0 AND CHARINDEX(',', sport2level)=0
UNION ALL
-- c) commas in sport1
SELECT id, userid, 1 AS sportid, LEFT(sport1, CHARINDEX(',', sport1)-1) AS SportName, LEFT(sport1level , CHARINDEX(',', sport1level)-1) AS SportLevel,
RIGHT(sport1, LEN(sport1) - CHARINDEX(',', sport1)) AS SportNameRemainder, LEFT(sport1level , LEN(sport1level) - CHARINDEX(',', sport1level)) AS SportLevelRemainder
FROM @tmp
WHERE CHARINDEX(',', sport1)>0 AND CHARINDEX(',', sport1level)>0
UNION ALL
-- d) commas in sport2
SELECT id, userid, 2 AS sportid, LEFT(sport2, CHARINDEX(',', sport2)-1) AS SportName, LEFT(sport2level , CHARINDEX(',', sport2level)-1) AS SportLevel,
RIGHT(sport2, LEN(sport2) - CHARINDEX(',', sport2)) AS SportNameRemainder, LEFT(sport2level , LEN(sport2level) - CHARINDEX(',', sport2level)) AS SportLevelRemainder
FROM @tmp
WHERE CHARINDEX(',', sport2)>0 AND CHARINDEX(',', sport2level)>0
UNION ALL
--2) recursive part
SELECT id, userid, sportid +1 AS sportid, SportNameRemainder AS SportName, SportLevelRemainder AS SportLevel, NULL AS SportNameRemainder, NULL AS SportLevelRemainder
FROM Sports
WHERE CHARINDEX(',', SportNameRemainder)=0 AND CHARINDEX(',', SportLevelRemainder)=0
)
SELECT CONCAT(CONVERT(VARCHAR(5), id), '_', CONVERT(VARCHAR(5), sportid)) AS nid, userid, SportName, SportLevel
FROM Sports
ORDER BY id, userid, sportid
随意根据您的需要进行更改。
注意:我建议将 SportLevel 中的字符串值替换为其数值,并且不会将 id 与 SportLevel 连接,例如:Varsity可能有值1,Recreational - 2等。 SportName 应该使用相同的逻辑。可能需要连接2个表中的数据。如果您需要帮助,请致电;)
答案 2 :(得分:1)
试试这个:
WITH Data
AS ( SELECT *
FROM ( VALUES
( 1, 11, 'Baseball', 'Varsity', 'Baseball', 'Recreational'),
( 2, 22, 'Baseball,Basketball', 'Varsity,Junior Varsity', 'Baseball', 'Varsity'),
( 3, 33, 'Soccer', 'Varsity', 'Soccer,Track & Field', 'Recreational,Intramural'),
( 4, 44, NULL , NULL , 'Tennis', 'Varsity'),
( 5, 55, 'Volleyball', 'Varsity', NULL , NULL ),
( 6, 66, 'Baseball,Basketball', 'Varsity,Varsity', 'Soccer,Football', 'Varsity,Varsity'),
( 7, 77, 'Baseball,Basketball,Rowing', 'Varsity,Varsity,Varsity', 'Soccer,Football,Volleyball', 'Varsity,Varsity,Recreational') )
AS T ( id, userid, sport1, sportlevel1, sport2, sportlevel2 )
),
Tally
AS ( SELECT n = 1
UNION ALL
SELECT n + 1
FROM Tally
WHERE n <= 100
),
Sprt1
AS ( SELECT ROW_NUMBER() OVER ( PARTITION BY Id, userid ORDER BY Id, userid , T.n ) AS RN ,
id ,
userid ,
CASE WHEN CHARINDEX(',', sport1) = 0 THEN sport1
ELSE SUBSTRING(sport1, T.n,
CHARINDEX(',',
SUBSTRING(sport1 + ',',
t.n, 100)) - 1)
END AS sport1
FROM data AS d
JOIN Tally AS T ON COALESCE(LEN(d.sport1), 1) >= t.n
AND SUBSTRING(','
+ COALESCE(d.sport1,
''), n, 1) = ','
),
lvl1
AS ( SELECT ROW_NUMBER() OVER ( PARTITION BY Id, userid ORDER BY Id, userid , T.n ) AS RN ,
id ,
userid ,
CASE WHEN CHARINDEX(',', sportlevel1) = 0
THEN sportlevel1
ELSE SUBSTRING(sportlevel1, T.n,
CHARINDEX(',',
SUBSTRING(sportlevel1
+ ',', t.n, 100))
- 1)
END AS sportlevel1
FROM data AS d
JOIN Tally AS T ON COALESCE(LEN(d.sportlevel1), 1) >= t.n
AND SUBSTRING(','
+ COALESCE(d.sportlevel1,
''), n, 1) = ','
),
sprt2
AS ( SELECT ROW_NUMBER() OVER ( PARTITION BY Id, userid ORDER BY Id, userid , T.n ) AS RN ,
id ,
userid ,
CASE WHEN CHARINDEX(',', sport2) = 0 THEN sport2
ELSE SUBSTRING(sport2, T.n,
CHARINDEX(',',
SUBSTRING(sport2 + ',',
t.n, 100)) - 1)
END AS sport2
FROM data AS d
JOIN Tally AS T ON COALESCE(LEN(d.sport2), 1) >= t.n
AND SUBSTRING(','
+ COALESCE(d.sport2,
''), n, 1) = ','
),
lvl2
AS ( SELECT ROW_NUMBER() OVER ( PARTITION BY Id, userid ORDER BY Id, userid , T.n ) AS RN ,
id ,
userid ,
CASE WHEN CHARINDEX(',', sportlevel2) = 0
THEN sportlevel2
ELSE SUBSTRING(sportlevel2, T.n,
CHARINDEX(',',
SUBSTRING(sportlevel2
+ ',', t.n, 100))
- 1)
END AS sportlevel2
FROM data AS d
JOIN Tally AS T ON COALESCE(LEN(d.sportlevel2), 1) >= t.n
AND SUBSTRING(','
+ COALESCE(d.sportlevel2,
''), n, 1) = ','
),
final
AS ( SELECT COALESCE(sprt1.RN, lvl1.RN, sprt2.rn, lvl2.rn) AS RN ,
COALESCE(sprt1.id, lvl1.id, sprt2.id, lvl2.id) AS id ,
COALESCE(sprt1.userid, lvl1.userid, sprt2.userid,
lvl2.userid) AS userid ,
sprt1.sport1 ,
lvl1.sportlevel1 ,
sprt2.sport2 ,
lvl2.sportlevel2
FROM sprt1
FULL JOIN lvl1 ON sprt1.id = lvl1.id
AND sprt1.RN = lvl1.RN
FULL JOIN sprt2 ON COALESCE(sprt1.id, lvl1.id) = sprt2.id
AND COALESCE(sprt1.RN, lvl1.RN) = sprt2.RN
FULL JOIN lvl2 ON COALESCE(sprt1.id, lvl1.id, sprt2.id) = lvl2.id
AND COALESCE(sprt1.RN, lvl1.RN,
sprt2.rn) = lvl2.RN
)
SELECT CONVERT(VARCHAR, id) + '_'
+ CONVERT(VARCHAR, ROW_NUMBER() OVER ( PARTITION BY id ORDER BY userid , part , sport )) AS id ,
userid ,
sport ,
sportlevel1 ,
sportlevel2
FROM ( SELECT 1 part ,
id ,
userid ,
sport1 AS sport ,
sportlevel1 ,
CASE WHEN sport1 = sport2 THEN sportlevel2
ELSE NULL
END sportlevel2
FROM final
WHERE sport1 IS NOT NULL
UNION ALL
SELECT 2 ,
id ,
userid ,
sport2 ,
NULL ,
sportlevel2
FROM final
WHERE sport2 IS NOT NULL
AND sport2 != COALESCE(sport1, '')
) FinalDataset
OPTION ( MAXRECURSION 1000 )
输出
--Create demo temp table for testing
IF OBJECT_ID('Tempdb..#Data') IS NOT NULL
DROP TABLE #Data
SELECT *
INTO #Data
FROM ( VALUES ( 1, 11, 'Baseball', 'Varsity', 'Baseball', 'Recreational'),
( 2, 22, 'Baseball,Basketball', 'Varsity,Junior Varsity', 'Baseball', 'Varsity'),
( 3, 33, 'Soccer', 'Varsity', 'Soccer,Track & Field', 'Recreational,Intramural'),
( 4, 44, NULL , NULL , 'Tennis', 'Varsity'),
( 5, 55, 'Volleyball', 'Varsity', NULL , NULL ),
( 6, 66, 'Baseball,Basketball', 'Varsity,Varsity', 'Soccer,Football', 'Varsity,Varsity'),
( 7, 77, 'Baseball,Basketball,Rowing', 'Varsity,Varsity,Varsity', 'Soccer,Football,Volleyball', 'Varsity,Varsity,Recreational') )
AS T ( id, userid, sport1, sportlevel1, sport2, sportlevel2 );
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--- CODE BELOW CAN BE USED THRO STORED PROCEDURE, VIEW DOES NOT ALLOW TEMP TABLE USAGE ---
----------------------------------------------------------------------------------------------------
--Create temp table with sequence num
IF OBJECT_ID('Tempdb..#Tally') IS NOT NULL --<<<~this code not required in Stored procedure, can be deleted
DROP TABLE #Tally --<<<~this code not required in Stored procedure, can be deleted
CREATE TABLE #Tally ( N INT PRIMARY KEY )
DECLARE @i INT = 1
WHILE @i < 1000
BEGIN
INSERT INTO #Tally
SELECT @i
SET @i = @i + 1
END
----------------------------------------------------------------------------------------------------
--split sport2 field and create temp table for final result
IF OBJECT_ID('Tempdb..#sprt1') IS NOT NULL
DROP TABLE #sprt1
SELECT ROW_NUMBER() OVER ( PARTITION BY Id, userid ORDER BY Id, userid , T.n ) AS RN ,
id ,
userid ,
CASE WHEN CHARINDEX(',', sport1) = 0 THEN sport1
ELSE SUBSTRING(sport1, T.n,
CHARINDEX(',', SUBSTRING(sport1 + ',', t.n, 100))
- 1)
END AS sport1
INTO #sprt1
FROM #data AS d
JOIN #Tally AS T ON COALESCE(LEN(d.sport1), 1) >= t.n
AND SUBSTRING(',' + COALESCE(d.sport1, ''), n, 1) = ','
----------------------------------------------------------------------------------------------------
--split sportlevel1 field and create temp table for final result
IF OBJECT_ID('Tempdb..#lvl1') IS NOT NULL
DROP TABLE #lvl1
SELECT ROW_NUMBER() OVER ( PARTITION BY Id, userid ORDER BY Id, userid , T.n ) AS RN ,
id ,
userid ,
CASE WHEN CHARINDEX(',', sportlevel1) = 0 THEN sportlevel1
ELSE SUBSTRING(sportlevel1, T.n,
CHARINDEX(',',
SUBSTRING(sportlevel1 + ',', t.n, 100))
- 1)
END AS sportlevel1
INTO #lvl1
FROM #data AS d
JOIN #Tally AS T ON COALESCE(LEN(d.sportlevel1), 1) >= t.n
AND SUBSTRING(',' + COALESCE(d.sportlevel1, ''), n,
1) = ','
----------------------------------------------------------------------------------------------------
--split sport2 field and create temp table for final result
IF OBJECT_ID('Tempdb..#sprt2') IS NOT NULL
DROP TABLE #sprt2
SELECT ROW_NUMBER() OVER ( PARTITION BY Id, userid ORDER BY Id, userid , T.n ) AS RN ,
id ,
userid ,
CASE WHEN CHARINDEX(',', sport2) = 0 THEN sport2
ELSE SUBSTRING(sport2, T.n,
CHARINDEX(',', SUBSTRING(sport2 + ',', t.n, 100))
- 1)
END AS sport2
INTO #sprt2
FROM #data AS d
JOIN #Tally AS T ON COALESCE(LEN(d.sport2), 1) >= t.n
AND SUBSTRING(',' + COALESCE(d.sport2, ''), n, 1) = ','
----------------------------------------------------------------------------------------------------
--split sportlevel2 field and create temp table for final result
IF OBJECT_ID('Tempdb..#lvl2') IS NOT NULL
DROP TABLE #lvl2
SELECT ROW_NUMBER() OVER ( PARTITION BY Id, userid ORDER BY Id, userid , T.n ) AS RN ,
id ,
userid ,
CASE WHEN CHARINDEX(',', sportlevel2) = 0 THEN sportlevel2
ELSE SUBSTRING(sportlevel2, T.n,
CHARINDEX(',',
SUBSTRING(sportlevel2 + ',', t.n, 100))
- 1)
END AS sportlevel2
INTO #lvl2
FROM #data AS d
JOIN #Tally AS T ON COALESCE(LEN(d.sportlevel2), 1) >= t.n
AND SUBSTRING(',' + COALESCE(d.sportlevel2, ''), n,
1) = ','
----------------------------------------------------------------------------------------------------
--final data set
IF OBJECT_ID('Tempdb..#Final') IS NOT NULL
DROP TABLE #Final
SELECT COALESCE(S1.RN, L1.RN, S2.rn, L2.rn) AS RN ,
COALESCE(S1.id, L1.id, S2.id, L2.id) AS id ,
COALESCE(S1.userid, L1.userid, S2.userid, L2.userid) AS userid ,
S1.sport1 ,
L1.sportlevel1 ,
S2.sport2 ,
L2.sportlevel2
INTO #Final
FROM #sprt1 AS S1
FULL JOIN #lvl1 AS L1 ON S1.id = L1.id
AND S1.RN = L1.RN
FULL JOIN #sprt2 AS S2 ON COALESCE(S1.id, L1.id) = S2.id
AND COALESCE(S1.RN, L1.RN) = S2.RN
FULL JOIN #lvl2 AS L2 ON COALESCE(S1.id, L1.id, S2.id) = L2.id
AND COALESCE(S1.RN, L1.RN, S2.rn) = L2.RN
----------------------------------------------------------------------------------------------------
--Final output query
SELECT CONVERT(VARCHAR, id) + '_'
+ CONVERT(VARCHAR, ROW_NUMBER() OVER ( PARTITION BY id ORDER BY userid , part , sport )) AS id ,
userid ,
sport ,
sportlevel1 ,
sportlevel2
FROM ( SELECT 1 part ,
id ,
userid ,
sport1 AS sport ,
sportlevel1 ,
CASE WHEN sport1 = sport2 THEN sportlevel2
ELSE NULL
END sportlevel2
FROM #final
WHERE sport1 IS NOT NULL
UNION ALL
SELECT 2 ,
id ,
userid ,
sport2 ,
NULL ,
sportlevel2
FROM #final
WHERE sport2 IS NOT NULL
AND sport2 != COALESCE(sport1, '')
) FinalDataset
答案 3 :(得分:0)
以下方法使用CTE,但它们不是递归的。输出正是您想要的,可以处理CSV字段中的任意数量的值。我使用了基于SQLCLR的字符串拆分器,它可以在SQL#库中免费获得(我写过,但2个String_Split函数是免费版本)。如果您愿意,可以使用另一个拆分器,但只需确保它不是标量T-SQL UDF。
请注意,对于“新”ID
字段,而不是将有效的INT
转换为VARCHAR
,以便可以将其与{{1}的唯一体育#连接起来1}},我把它变成了DECIMAL并将独特的运动#放在小数点的右边。这应该比字符串字段更快地过滤和排序。如果有人可以进行超过9项运动,请将UserID
更改为/ 10.0
。
/ 100.0