SQL 服务器查询 - 最小值、最大值、平均值聚合问题
SQL Server query - Min, Max, Avg aggregates issue
我想从数据库中获取一些统计数据(最小值、最大值、平均值),但我目前遇到了问题。我认为语法不正确,但我不确定。我得不到预期的结果。
这里是 table;
name period b c
-----------------------------
KONYA July - 2014 7 3
KONYA July - 2014 8 5
KONYA July - 2014 9 9
KARAMAN July - 2014 5 2
KARAMAN July - 2014 4 5
LYON July - 2014 5 1
LYON July - 2014 6 9
LYON July - 2014 7 7
这是我所期望的;
name period max(b) min(b) max(c) min(c) ...
--------------------------------------------------------------
KONYA July - 2014 9 7 9 3
KARAMAN July - 2014 5 4 5 2
LYON July - 2014 7 5 9 1
还有我的 sql 查询;
SELECT
name,
max(b), max(c), min(b), min(c)
FROM
tablename
GROUP BY
name, b, c;
你能帮帮我吗?
提前致谢。
从 group by
中删除 b,c
并添加 period
代替
SELECT name,period,
max(b), max(c), min(b), min(c)
FROM tablename
group by name,period
我想从数据库中获取一些统计数据(最小值、最大值、平均值),但我目前遇到了问题。我认为语法不正确,但我不确定。我得不到预期的结果。
这里是 table;
name period b c
-----------------------------
KONYA July - 2014 7 3
KONYA July - 2014 8 5
KONYA July - 2014 9 9
KARAMAN July - 2014 5 2
KARAMAN July - 2014 4 5
LYON July - 2014 5 1
LYON July - 2014 6 9
LYON July - 2014 7 7
这是我所期望的;
name period max(b) min(b) max(c) min(c) ...
--------------------------------------------------------------
KONYA July - 2014 9 7 9 3
KARAMAN July - 2014 5 4 5 2
LYON July - 2014 7 5 9 1
还有我的 sql 查询;
SELECT
name,
max(b), max(c), min(b), min(c)
FROM
tablename
GROUP BY
name, b, c;
你能帮帮我吗?
提前致谢。
从 group by
中删除 b,c
并添加 period
代替
SELECT name,period,
max(b), max(c), min(b), min(c)
FROM tablename
group by name,period