Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse the aggregation between arbitrary columns of SQLServer

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report