在sql server中的select*表查询中显示拆分结果
我正在使用下面的语句-如何获得预期的结果在sql server中的select*表查询中显示拆分结果,sql,sql-server,Sql,Sql Server,我正在使用下面的语句-如何获得预期的结果 select t1.StudentName, t1.Email, s.items Module from Student t1 outer apply dbo.Split(t1.Module, ',') s 当前 StudentName | Email | Module S1 | s1@uni.com | Math S1 | s1@uni.com | English S1 | s1
select t1.StudentName, t1.Email, s.items Module
from Student t1
outer apply dbo.Split(t1.Module, ',') s
当前
StudentName | Email | Module
S1 | s1@uni.com | Math
S1 | s1@uni.com | English
S1 | s1@uni.com | Science
预期的
StudentName | Email | Module
| Math
S1 | s1@uni.com | English
| Science
注意:使用SQL Server 2017基于预期结果,您需要
案例
表达式:
select (case when s.items = 'English' then t1.StudentName end) as StudentName ,
(case when s.items = 'English' then t1.Email end) as Email,
s.items Module
from Student t1 outer apply
dbo.Split(t1.Module, ',') s;
With cte as (select t1.StudentName, t1.Email, s.value, row_number() OVER (PARTITION BY StudentName,email ORDER BY StudentName) rn
from Student t1
outer apply STRING_SPLIT(t1.Module, ',') s)
select StudentName,email, value from (select case when c1.rn = c2.rn and c1.rn = 1 then c1.StudentName
else null
end as StudentName,
case when c1.rn = c2.rn and c1.rn = 1 then c1.email
else null
end as email,
case when c1.value = c2.value then c1.value
else null
end as value
from cte c1 join cte c2
on c1.StudentName = c2.StudentName
and c1.email = c2.email)t
where t.value is not null
如果您希望特定行应填充
StudentName&Email
,而不考虑模块
,则可以使用行号() 您可以使用如下查询(可能需要更改idx
列以匹配拆分函数):
SQL小提琴:
如果要垂直对齐输出,可以尝试下一个示例。请注意,使用STRING\u SPLIT()
时,输出行的顺序可能是任意的,并且不能保证顺序与输入字符串中的子字符串的顺序匹配
表:
CREATE TABLE Data (
StudentName varchar(100),
Email varchar(100),
Module varchar(100)
)
INSERT INTO Data
(StudentName, Email, Module)
VALUES
('S1', 's1@uni.com', 'Math,English,Science'),
('S2', 's1@uni.com', 'Math,English,Science,Grammar,Sports'),
('S3', 's1@uni.com', 'Math,English,Science,Sports'),
('S4', 's1@uni.com', 'Math')
声明:
SELECT
CASE WHEN (s.Cnt / 2 + 1) = s.Rn THEN d.StudentName END AS StudentName,
CASE WHEN (s.Cnt / 2 + 1) = s.Rn THEN d.Email END AS Email,
s.Module
FROM Data d
OUTER APPLY (
SELECT
[value] AS Module,
COUNT(*) OVER (ORDER BY (SELECT NULL)) AS Cnt,
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS Rn
FROM STRING_SPLIT(d.Module, ',')
) s
结果:
-----------------------------------
StudentName Email Module
-----------------------------------
Math
S1 s1@uni.com English
Science
Math
English
S2 s1@uni.com Science
Grammar
Sports
Math
English
S3 s1@uni.com Science
Sports
S4 s1@uni.com Math
我在SQL Server 2017中尝试过这一点
With cte as (select t1.StudentName, t1.Email, s.value, row_number() OVER (PARTITION BY StudentName,email ORDER BY StudentName) rn
from Student t1
outer apply STRING_SPLIT(t1.Module, ',') s)
select StudentName,email, value from (select case when c1.rn = c2.rn and c1.rn = 1 then c1.StudentName
else null
end as StudentName,
case when c1.rn = c2.rn and c1.rn = 1 then c1.email
else null
end as email,
case when c1.value = c2.value then c1.value
else null
end as value
from cte c1 join cte c2
on c1.StudentName = c2.StudentName
and c1.email = c2.email)t
where t.value is not null
有没有一种没有硬编码的方法?谢谢你的回答