简体   繁体   中英

i want max of multiple columns

Requirement is i need ID,Date and max value from A to I column. MAX value from columns A,B,C,D,E,F,G, I need SQL server or Synapse query

在此处输入图像描述

already tried

select max(A) from (values( A,B,C,D,E,F,G, I)) as A) as Z

this is working with sql but not with Azure synapse

If I understand correctly, you can use apply :

select t.*, v.max_val
from t cross apply
     (select max(v.col) as max_val
      from (values (a), (b), . . .) v(col)
     ) v;

Serverless SQL pools (formerly known as SQL on-demand) in Azure Synapse Analytics supports GREATEST so this type of query is easy in serverless, eg

SELECT id, dt, GREATEST ( a, b, c, d, e, f, g, h, i ) x
FROM yourTable

For dedicated SQL pools you can use UNPIVOT for the same effect:

SELECT id, dt, MAX(x) x
FROM yourTable
UNPIVOT ( x for y In ( a, b, c, d, e, f, g, h, i ) ) AS upvt
GROUP BY id, dt

GREATEST may get added at some point for dedicated SQL pools so it's probably worth checking every now and then.

Another thing you could do, as SparkSQL supports GREATEST is use an Azure Synapse notebook to quickly pull the data over from the dedicated SQL pool, process it and pop it back, say as part of a batch process. Here's a simple example using a Scala notebook.

Cell1:

// Get the table from the dedicated SQL pool into a dataframe
val df = spark.read.synapsesql("someSQLPool.dbo.greatestWorking")

// Save the dataframe as a temp view to make it available to SparkSQL
df.createOrReplaceTempView("greatestWorking")

Cell 2:

// Write the result to a new data frame to be saved back into the dedicated SQL pool
val result = spark.sql("SELECT id, dt, GREATEST ( a, b, c, d, e, f, g, h, i ) AS x FROM greatestWorking") 

result.write.synapsesql("someSQLPool.dbo.result", Constants.INTERNAL)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM