Original: http://www.jb51.net/article/28753.htm
http://xuzhihong1987.blog.163.com/blog/static/26731587200981744956366/
The SQL query to be implemented is primitive:
Ask to query from the first table to get the second table-style data, after the Internet query can write the following sql:
Copy CodeThe code is as follows:
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
Now analyze the SQL for a moment:
The total used points are: OUTER apply,for XML AUTO. Since there is no deep research on SQL Server, record
OUTER Apply is a query method supported by SQL2005, similar to the connection query, is the two query results splicing, but the peculiar is that the use of OUTER apply should be able to use in the query after the previous results obtained.
Such as:
Copy CodeThe code is as follows:
SELECT * FROM
(SELECT * from Userino) A
Cross joins (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
The first paragraph of SQL is obviously wrong, there are two reasons: 1. Cross joins are inherently unconditional, 2. SQL Server will get the following error:
The multi-part identifier "A.username" could not be bound.
You might say that conditional join queries are not written in this way, and should be written as a second SQL, which is actually the same as in the third SQL using the outer apply implementation.
But Outer apply can also achieve the following effect
Copy CodeThe code is as follows:
SELECT * FROM
(SELECT * from Userino) A
OUTER APPLY (select [value] = a.username+ ' test ') B
I'm afraid the direct use of join is a bit of a hassle, the above example may not make sense, in fact, SQL2005 proposed that the Apply connection method is mainly to use the results of query statements executed in the connection query
In addition to "OUTER apply", SQL Server also has cross APPLY, the difference is mainly in the processing of null values
The FOR XML auto is primarily used to return SQL query results directly to XML statements, for XML in addition to AUTO and raw and explicit, see "Super Simple: Using FOR XML auto control XML output"
In the article just started the SQL text, is to use the above two features, first use outer apply to achieve similar to the use of Userpart to group the effect, to filter out the user in each Userpart, and then because the filtered results are multiple lines, so use for XML to stitch multiple rows of data into XML, and finally two to split the XML ....
In conclusion, it feels that this way of realization is quite unique, and I have learned some features in SQL Server, and share with you
Pagination in SQL multi-table query, comprehensive instance analysis of field combination