1

我有 1 个表,我想查询特定列。Aa -> 是不同的,

Bb -> Sum B 按 Aa 进行的总分组,

Cc -> 特定 Aa 的最小值 C,

Bb1,Dd -> C 最小时的 B,D 值,

Ee -> 不同的

表格1

id | A | B | C | D | E |
------------------------
1  | 2 | 1 | 2 | 1 | A |

2  | 2 | 2 | 7 | 0 | A |

3  | 4 | 1 | 7 | 3 | B |

4  | 4 | 0 | 5 | 4 | B |

结果

Aa | Bb | Cc | Bb1 | Dd | Ee |
-----------------------------
2  | 3  | 2  | 1   | 1  | A  |

4  | 1  | 5  | 0   | 4  | B  |

我的查询给出错误的结果

  SELECT
    test.A as Aa,
    SUM(test.B) as Bb,
    min(test.C) as Cc,
    test.D as Dd,
    test.E as Ee
    FROM `test` 
    group by A,E
4

1 回答 1

0
SELECT
      a.A as Aa,
      SUM(a.B) as Bb,
      min(a.C) as Cc,
      b.b B1,
      b.D DD,
      a.E EE
FROM  `Table1` a
      INNER JOIN
      (
        SELECT  a.*
        FROM    table1 a
                INNER JOIN
                (
                  SELECT  A, MIN(C) min_C
                  FROM    table1
                  GROUP   BY A
                ) b ON  a.a = b.a AND a.c = b.min_C
      ) b ON a.a = b.a 
group by a.A, b.b
于 2013-02-27T09:23:13.113 回答