SELECT DISTINCT values and INSERT INTO table

后端 未结 4 2049
独厮守ぢ
独厮守ぢ 2021-01-18 15:50

I want to take a column with values that repeat multiple times and get that value only once and store it for later use, but at the same time I would like to get another valu

相关标签:
4条回答
  • 2021-01-18 16:17

    If I understand what you're trying to accomplish correctly, you should be able to do this:

    INSERT INTO Table
    SELECT A, B, MAX(C) + 1
    FROM Table
    GROUP A, B
    

    This should insert a row for every distinct combination of A and B as well as increment C.

    0 讨论(0)
  • 2021-01-18 16:18

    You can use INSERT INTO... SELECT statement on this,

    INSERT INTO tableName (A, B, C)
    SELECT A, B, MAX(C) + 1
    FROM tableName
    GROUP BY A, B
    
    • SQLFiddle Demo
    0 讨论(0)
  • 2021-01-18 16:20

    Try this:

    SELECT * INTO new_table FROM
       (SELECT DISTINICT * FROM table_name) x
    
    0 讨论(0)
  • 2021-01-18 16:36

    This can be done using windowing functions.

    INSERT INTO TABLENAME (A, B, C)
    SELECT A, B, C + 1
    FROM(
    select A, B, C, MAX(C) OVER(PARTITION BY A, B) AS MAXC
    FROM TABLENAME 
    ) AS NEW_ROWS
    WHERE C = MAXC
    
    0 讨论(0)
提交回复
热议问题