我正在使用Sql Server 2012,下面是示例行
Number Date
-------------- --------------------
145 2017-04-02
17345 2017-04-02
16345 2017-04-03
178345 2017-04-03
12745 2017-04-04
16734 2017-04-04
12764 2017-04-05
12367 2017-04-05
使用此结果集,我要求列“Number”的Min,Max和Avg值,使用此值我有意获取date列的值。
结果集应如下所示: -
MinNumber Date
145 2017-04-02
MaxNumber date
178345 2017-04-03
AVGNumber date
16345 2017-04-03
然后我应该如何选择“日期”和“'min(Number)”这样请为我建议预期结果集的解决方案。
先谢谢。
答案 0 :(得分:0)
如下:
SELECT MAX(Number) AS MaxNumber, Date FROM TableName GROUP BY Date LIMIT 1;
SELECT MIN(Number) AS MinNumber, Date FROM TableName GROUP BY Date LIMIT 1;
SELECT AVG(Number) AS AVGNumber, Date FROM TableName GROUP BY Date LIMIT 1;