简体   繁体   English

如何在所需范围内的Sql Server中选择MIN和MAX

[英]How to select MIN and MAX in Sql Server within required range

I have to select aid from a sql server table having MIN and MAX p_year within the range ie in between 1990 and 2014 . 我必须从MINMAX p_year在范围内(即在19902014年之间)的sql server表中选择aid

I have tried this query but it gives error ie 我已经试过这个查询,但它给错误即

An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference. 除非聚集在HAVING子句或选择列表中包含的子查询中,并且聚集的列是外部引用,否则聚集可能不会出现在WHERE子句中。

SELECT aid
FROM   sub_aminer_paper
WHERE  Min (p_year) = (SELECT p_year
                       FROM   sub_aminer_paper
                       WHERE  p_year >= 1990)
       AND Max (p_year) = (SELECT p_year
                           FROM   sub_aminer_paper
                           WHERE  p_year <= 2014) 

The required output should the aid of those authors whose minimum p_year >= 1990 and maximum p_year <= 2014. 所需的输出应的aid这些作者其最小p_year> = 1990和最大p_year <= 2014。

SELECT aid
FROM sub_aminer_paper 
GROUP BY aid
HAVING MIN(p_year) >= 1990 AND MAX(p_year) <= 2014
   select aid from sub_aminer_paper 
   group by aid
   having min(p_year) >= 1990 and max(p_year) <= 2014

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

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