从按子查询分组的多个列中插入
Insert into Multiple Columns from a Grouped by Subquery
INSERT INTO TABLE (COLUMN_A, COLUMN_B, COLUMN_C) -- Line 1
SELECT 'Label A', -- Line 2
(SELECT CATEGORY, COUNT(1) FROM SUB_TABLE GROUP BY CATEGORY); -- Line 3, need help
第 3 行的结果
我想达到的目标
- 'Label A' 被插入到 COLUMN_A
- 从第 3 行按 CATEGORY 分组以插入到 COLUMN_B
- 从第 3 行按 COUNT(1) 分组以插入到 COLUMN_C
预期结果
我遇到错误:“子查询必须 return 只有一列”。怎样才能达到预期的效果?
如果想使用 subsqery,请尝试如下所示
INSERT INTO TABLE (COLUMN_A, COLUMN_B, COLUMN_C) -
SELECT 'Label A', CATEGORY, cnt from
(SELECT CATEGORY, COUNT(1) as cnt FROM SUB_TABLE GROUP BY CATEGORY) a
其实不需要sub-query
INSERT INTO TABLE (COLUMN_A, COLUMN_B, COLUMN_C) -
SELECT 'Label A', CATEGORY, COUNT(1) as cnt from
FROM SUB_TABLE GROUP BY CATEGORY
INSERT INTO TABLE (COLUMN_A, COLUMN_B, COLUMN_C) -- Line 1
SELECT 'Label A', -- Line 2
(SELECT CATEGORY, COUNT(1) FROM SUB_TABLE GROUP BY CATEGORY); -- Line 3, need help
第 3 行的结果
我想达到的目标
- 'Label A' 被插入到 COLUMN_A
- 从第 3 行按 CATEGORY 分组以插入到 COLUMN_B
- 从第 3 行按 COUNT(1) 分组以插入到 COLUMN_C
预期结果
我遇到错误:“子查询必须 return 只有一列”。怎样才能达到预期的效果?
如果想使用 subsqery,请尝试如下所示
INSERT INTO TABLE (COLUMN_A, COLUMN_B, COLUMN_C) -
SELECT 'Label A', CATEGORY, cnt from
(SELECT CATEGORY, COUNT(1) as cnt FROM SUB_TABLE GROUP BY CATEGORY) a
其实不需要sub-query
INSERT INTO TABLE (COLUMN_A, COLUMN_B, COLUMN_C) -
SELECT 'Label A', CATEGORY, COUNT(1) as cnt from
FROM SUB_TABLE GROUP BY CATEGORY