SQL创建一个最大值为

时间:2017-10-02 17:00:52

标签: sql

您好我有一个表格,其中我有与ID相关联的各种值,并且想要创建一个表格,其中每个ID只有一行,并且该行具有最大值。例如,如果输入是:

ID   VALUE
-----------
A     10
A     20
B     30
B     40

然后输出是:

ID   VALUE
----------- 
A     20
B     40

2 个答案:

答案 0 :(得分:5)

您可以使用CTAs

-- this will require refreshing and won't work on every RDBMS
CREATE TABLE table_name2 AS
SELECT ID, MAX(VALUE) AS VALUE
FROM table_name
GROUP BY ID;

或创建视图:

CREATE VIEW view_name AS
SELECT ID, MAX(VALUE) AS VALUE
FROM table_name
GROUP BY ID;

答案 1 :(得分:0)

CREATE TABLE IF NOT EXISTS qtab1
   AS
   SELECT id, max(value)
      FROM tab1
    group by id;