简体   繁体   English

SQL仅返回最大值

[英]SQL return max value only

I have done some basic queries using max but this one has a little more to it. 我已经使用max完成了一些基本查询,但是这个查询还有更多内容。 I am using SSMS 2012. This would also need to include a group or distinct. 我正在使用SSMS2012。这也需要包括一个组或一个不同的组。

Select statement - 选择语句-

select A.YEAR1, A.PERIODID 
from GPSTJ..SY40100 A
where A.CLOSED = 0 and A.PERIODID <> 0 and A.series = 5

This returns the following results - 这将返回以下结果-

YEAR1    PERIODID
2015     12
2015     12
2016     1
2016     1
2016     2
2016     2

I only want it to return 1 row, with the maximum value in the year column first, and then the maximum Period ID - 我只希望它返回1行,并首先在year列中返回最大值,然后返回最大Period ID-

YEAR1    PERIODID
2016     2

Thanks 谢谢

Just ordering with TOP (1) 只需订购TOP (1)

select TOP (1) A.YEAR1, A.PERIODID 
from GPSTJ..SY40100 A
where A.CLOSED = 0 and A.PERIODID <> 0 and A.series = 5
ORDER BY A.YEAR1 DESC, A.PERIODID DESC

You would use MAX() so use SELECT MAX(YEAR1), PERIODID FROM table; 您将使用MAX()因此请使用SELECT MAX(YEAR1), PERIODID FROM table;

This will select the highest value in the column. 这将在列中选择最大值。

你可以尝试having条款, group by一年,时间ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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