【问题标题】:Inserting RANK as value of a column插入 RANK 作为列的值
【发布时间】:2021-08-12 13:46:32
【问题描述】:

我是 sql 的超级新手,不知道是否有可能,但是 我想从以下 SELECT DISTINCT 输出中选择 RANK 的值并将其插入到名为 Country 的表的第一列中。

然后选择 address_country 列并将其插入“Country”表的第二列,但我不知道该怎么做。我真的很感激任何帮助。谢谢!

INSERT INTO Country

SELECT country_code from
            (SELECT DISTINCT address_country,
            RANK() over (order by address_country asc) AS country_code      
            FROM CUSTOMER_INFO Where CUSTOMER_INFO.address_country is not NULL),

SELECT address_country from 
            (SELECT DISTINCT address_country,
            RANK() over (order by adr_country asc) AS country_code          
            FROM CUSTOMER_INFO
            Where CUSTOMER_INFO.address_country is not NULL);

【问题讨论】:

    标签: mysql sql sql-insert rank distinct-values


    【解决方案1】:

    如果您想使用INSERT INTO ... SELECT,请使用:

    INSERT INTO Country (address_country, country_code)
    SELECT address_country, RANK() OVER (ORDER BY adr_country)
    FROM CUSTOMER_INFO
    WHERE CUSTOMER_INFO.address_country IS NOT NULL;
    

    请注意,计算RANK 将产生派生数据,或者可能随着原始CUSTOMER_INFO 表的变化而变化的数据。因此,您可能需要重新考虑最初导致您想要执行此插入的方法。

    【讨论】:

    • 您好 Tim,上面的 sql 对我有用!在我的情况下,可以使用 rank 的值作为另一个表的数据输入,因为这是我的要求。太感谢了! :D
    猜你喜欢
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 2016-10-15
    • 2016-02-12
    相关资源
    最近更新 更多