Get the App
SLTechnology News&Howtos  ›  Database  › 

How to realize the maximum value of multiple fields according to the range in MSSQL

Shulou Source: shulou.com Published: 2022-05-31 19:18:25 10月02日 Update

This article is about how to achieve the maximum value of multiple fields according to the range in MSSQL, the editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.

-- > Title: generate test data-- > Author:wufeng4552-- > Date: 2009-09-21 15:08:41declare @ T table ([Col1] int, [Col2] int, [Col3] int, [Col4] int, [Col5] int, [Col6] int, [Col7] int) Insert @ Tselect 1020,0,30,40,50,50,60,50,0,52,85 union allselect 385 union allselect 387, 56pr 41pm21 1select [col1], max ([col2]) maxcolfrom (select [col1], [col2] from @ t union allselect [col1], [col3] from @ from] [col4] from @ t union all select [col1], [col5] from @ t union all select [col1], [col6] from @ t union all select [col1], [col7] from @ t) Twhere [col2] between 20 and 60-conditional restrictions group by [col1] / * col1 maxcol--1 602 603 56

(3 data columns are affected)

* /-- method 2select [col1], (select max ([col2]) from (select [col2] union all select [col3] union all select [col4] union all select [col5] union all select [col6] union all select [col7]) T where [col2] between 20 and 60) as maxcol--specify query range from @ tUnip * (3 data columns are affected) col1 maxcol--1 602 603 56 /

The above is how to achieve the maximum value of multiple fields according to the range in MSSQL. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Maximum field maximum value range method more knowledge article practical that is work meeting article look knowledge point industry details information channel follow part Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Xiaomi Shulou Technology Redmi Shulou Information