要实现的SQL查询很原始:
要求从第一个表进行查询得到第二个表格式的数据,上网查询之后竟然能写出下面的SQL:
复制代码 代码如下:
select * from userino
SELECT * FROM(
SELECT DISTINCT userpart
FROM userino
)A
OUTER APPLY(
SELECT
[usernames]= replace(replace(replace((SELECT username as value FROM userino N
WHERE userpart = A.userpart order by n.username asc FOR XML AUTO),'"/><N value="','/')
,'<N value="',''),'"/>','')
)N
OUTER APPLY(
SELECT
[username_cns]= replace(replace(replace((SELECT username_cn as value FROM userino M
WHERE userpart = A.userpart order by m.username asc FOR XML AUTO),'"/><M value="','/')
,'<M value="',''),'"/>','')
)M
复制代码 代码如下:
select * from
(select * from userino) A
cross join (select username from userino
where username = A.username )B
select * from
(select * from userino) A
join (select username from userino ) B on a.username = b.username
select * from
(select * from userino) A
OUTER APPLY (select username from userino
where username = A.username ) B
复制代码 代码如下:
select * from
(select * from userino) A
OUTER APPLY (select [value] = a.username+'test' ) B
新闻热点
疑难解答