聚合后如何保留额外的列?

时间:2017-10-13 20:48:33

标签: mysql group-by aggregate-functions greatest-n-per-group

我在mysql中有以下表格,它是真实问题的简化版本。

C:\WINDOWS\system32>gem install json
Building native extensions.  This could take a while...
ERROR:  Error installing json:
        ERROR: Failed to build gem native extension.

    C:/Ruby24-x64/bin/ruby.exe -r ./siteconf20171013-12212-1y42085.rb extconf.rb
creating Makefile

make "DESTDIR=" clean

make "DESTDIR="
generating generator-x64-mingw32.def
compiling generator.c
linking shared-object json/ext/generator.so
c:/ruby24-x64/programing/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgmp
collect2.exe: error: ld returned 1 exit status
make: *** [generator.so] Error 1

make failed, exit code 2

Gem files will remain installed in C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/json-2.1.0 for inspection.
Results logged to C:/Ruby24-x64/lib/ruby/gems/2.4.0/extensions/x64-mingw32/2.4.0/json-2.1.0/gem_make.out

我想选择标有*的行,以获取下表:

+-------+-------+-------+-------+
| a_col | b_col | c_col | extra |
+-------+-------+-------+-------+
|     1 |     1 |     1 |     7 |*
|     1 |     2 |     1 |    10 |
|     1 |     2 |     2 |    20 |*
|     1 |     3 |     2 |    20 |
|     1 |     3 |     3 |    15 |*
+-------+-------+-------+-------+

如果a_col和b_col中的两行具有相同的值,则保留c_col中具有最大值的那个

我的尝试是:

+-------+-------+-------+-------+
| a_col | b_col | c_col | extra |
+-------+-------+-------+-------+
|     1 |     1 |     1 |     7 |
|     1 |     2 |     2 |    20 |
|     1 |     3 |     3 |    15 |
+-------+-------+-------+-------+

但是我收到以下错误消息:

select a_col, b_col, max(c_col), extra
from mytable
group by a_col, b_col

没有提到“额外”专栏,我得到的东西接近我想要的东西:

ERROR 1055 (42000): Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'bd17_g12.mytable.extra' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

但我需要保留“额外”列的值

3 个答案:

答案 0 :(得分:0)

您可以通过应用汇总extra功能获取c_colgroup_concat列。要选择相关的最高c_col及其相关的额外值,您可以使用substring_index

select a_col, b_col, max(c_col), 
substring_index(group_concat(extra order by c_col desc),',',1) `extra`
from mytable
group by a_col, b_col

DEMO

或自我加入

select a.*
from mytable a
left join mytable b 
          on a.a_col = b.a_col 
          and a.b_col = b.b_col
          and a.c_col < b.c_col
where b.c_col is null

DEMO

答案 1 :(得分:0)

根据我的理解,您希望extra代表最大的c_col

SQL Fiddle

MySQL 5.6架构设置

查询1

select t2.*
from (
  select a_col, b_col, max(c_col) c_colm
  from t
  group by a_col, b_col
  ) t1
  INNER join t t2 
    on t1.a_col = t2.a_col
      and t1.b_col = t2.b_col
      and t1.c_colm = t2.c_col

<强> Results

| a_col | b_col | c_col | extra |
|-------|-------|-------|-------|
|     1 |     1 |     1 |     7 |
|     1 |     2 |     2 |    20 |
|     1 |     3 |     3 |    15 |

答案 2 :(得分:0)

对于低版本的mysql(例如版本5.5),似乎不会出现包含$data = <<<END <bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> </bookstore> END; $xml = simplexml_load_string($data); $categoryAttributes = $xml->xpath('/bookstore/book/@category'); echo $categoryAttributes[0]; 列时出现的错误。因此问题可能出现在更高版本的mysql上的问题,如下所述:Error Code: 1055 incompatible with sql_mode=only_full_group_by