![]() ![]() Performance and compact code are essential. In SQL Server we can find the maximum or minimum value from different columns of the sameĭata type using different methods. ![]() When we need to compare values of more columns weĬreate a new one, because in SQL Server we can't create a function with a dynamic number of parameters. Or minimum from the given parameters - datetime, datetime, datetime),īut it's not a flexible solution, because it can be applied only to fixed number of columns with the same data typeĪnd we can only use this function for a specific task. ![]() We can also solve this task by creating a function, which finds the maximum The maximum or minimum: we can modify the code to find average, sum, etc. Is the most optimal also it has compact code and is a good choice for calculating the maximum from the columns of the same We can run these queries separately and see the execution time for the first query is minimal (if the queries run veryįast on your server, you can increase the rowcount in your testing table to have a more clearĬomparison of run times). In the actual execution plan we can see the Query Cost (relative to the batch) is 7% for the first query, 11% for the second,ġ6% for the third and 66% for the forth. ( SELECT tt.UpdateByApp1Date AS UpdateDate ( SELECT MAX(UpdateDate) AS LastUpdateDate SELECT ID, UpdateByApp3Date AS UpdateDate SELECT ID, UpdateByApp2Date AS UpdateDate SELECT ID, UpdateByApp1Date AS UpdateDate UNPIVOT ( UpdateDate FOR DateVal IN ( UpdateByApp1Date, UpdateByApp2Date, UpdateByApp3Date ) ) AS u SELECT ID, MAX(UpdateDate) AS LastUpdateDate FROM (VALUES (UpdateByApp1Date),(UpdateByApp2Date),(UpdateByApp3Date)) AS UpdateDate(LastUpdateDate)) ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |