需要有关此sql命令的帮助。
表:
category ( category, id_category, salary, year)
oldcategory ( year, category, salary )
我如何获取oldcategory上的数据并将其插入到类别中,同时为id_category
上的每个不同类别创建一个数字ID?
INSERT INTO category(category, id_category, salary, year)
SELECT DISTINCT category, () , salary, year
FROM oldcategory
WHERE category IS NOT NULL AND salary IS NOT NULL AND year IS NOT NULL
答案 0 :(得分:0)
按类别的字母顺序考虑序列号的相关计数聚合:
INSERT INTO category(category, id_category, salary, year)
SELECT DISTINCT
category,
(SELECT Count(*) FROM
(SELECT DISTINCT deepsub.category
FROM oldcategory deepsub) sub
WHERE sub.category <= oldcategory.category) AS id_category,
salary, year
FROM oldcategory
WHERE category IS NOT NULL
AND salary IS NOT NULL
AND year IS NOT NULL
答案 1 :(得分:0)
INSERT INTO CATEGORY(CATEGORY, ID_CATEGORY, SALARY, YEAR)
SELECT DISTINCT A.CATEGORY,DENSE_RANK() OVER(ORDER BY A.CATEGORY) AS ID_CATEGORY,
A.SALARY,A.YEAR FROM OLDCATEGORY AS A
WHERE A.CATEGORY IS NOT NULL AND A.SALARY IS NOT NULL AND A.YEAR IS NOT NULL
ORDER BY A.YEAR