我想从数据库中获得一些统计数据(最小值,最大值,平均值),但我目前遇到了问题。我认为语法不正确,但我不确定。我无法得到预期的结果。
这是表格;
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;
你能帮我吗?
提前致谢。
答案 0 :(得分:6)
从b,c
移除group by
,然后添加period
SELECT name,period,
max(b), max(c), min(b), min(c)
FROM tablename
group by name,period