在SQL Server中一次获取多个列中的最大值的结果 [英] Get result with maximum values in multiple columns at once in SQL Server

查看:112
本文介绍了在SQL Server中一次获取多个列中的最大值的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个包含3列的DATES表:colYear(YEAR),colMonth(MONTH),colDay(DAY).

Lets say we have a DATES table with 3 columns: colYear(YEAR), colMonth(MONTH), colDay(DAY).

行插入如下:

colYear colMonth colDay
   1       2        3   
   2       3        0   
   4       5        5   
   4       8        0    
   1      10       28

我需要根据3列找到最长日期.因此,在此示例中,正确答案将是: 最长为4年8个月0天(我们检查年,最大值为4s,此后我们检查月份最大为8,所以我们找到了结果).

I need to find maximum date based on 3 columns. So that in this example right answer will be: 4 years, 8 months and 0 days is maximum (we check years, max are 4s, after that we check months max is 8, so we've found our result).

有可能吗?如何使用sql实现此目标?

Is it possible? How to achieve this with sql?

推荐答案

使用顺序--

    SELECT * from DATES 
    ORDER BY colYear DESC, colMonth DESC, colDay DESC

根据您的数据库,您可以将行数限制为1.

Based on your DB you can limit the row count to 1.

因此对于oracle使用rownum = 1 而对于mysql,它将是LIMIT 1

so for oracle use rownum = 1 whereas for mysql it will be LIMIT 1

这篇关于在SQL Server中一次获取多个列中的最大值的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆