How to parse the aggregation between arbitrary columns of SQLServer
This article shows you how to parse SQL Server arbitrary aggregation between columns, concise and easy to understand, absolutely can make your eyes shine, through the detailed introduction of this article I hope you can gain something.
SQL max aggregation function can only be for the same column n row operation, if the n column operation, generally use case statement to judge, if the column is less easy to write, column more trouble.
/* Test Name: Use XML to find aggregation between arbitrary columns Test Function: Do min, max, sum and avg operations on column data of a table Operation Principle: After fields are merged into xml, do xquery query and aggregate after converting to rowset */
--Establish test environment declare @t table ( id smallint , a smallint , b smallint , c smallint , d smallint , e smallint , f smallint ) insert into @t select 1, 1, 2, 3, 4, 6, 7 union all select 2, 34, 45, 56, 54, 9, 6
--test statement select a.*, c.* from @t a outer apply( select doc=( select * from @t as doc where id= a. id for xml path ( '' ), type ) ) b outer apply( select min ( r) as minValue, max ( r) as maxValue, sum ( r) as sumValue, avg ( r) as avgValue from ( select cast ( cast ( d. n. query( 'text()' ) as varchar ( max )) as int ) as r from doc. nodes( '/a,b,c,d,e,f' ) D( n)) tt ) c
/* Test result id a b c d e f minValue maxValue sumValue avgValue --- 1 1 2 3 4 6 7 1 7 23 3 2 34 45 56 54 9 6 56 204 34 */
That's how to resolve aggregations between arbitrary columns in SQL Server. Have you learned anything or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.