假设我有一个表(在MySQL中),如:
CREATE TEMPORARY table IF NOT EXISTS tbl
(
REF_REQ_ID int AUTO_INCREMENT PRIMARY KEY,
FunctionalArea varchar(200),
Industry varchar(200)
);
INSERT INTO tbl
SELECT 1, 'IT Jobs', 'IT related Jobs' UNION ALL
SELECT 2, 'HR Jobs', NULL UNION ALL
SELECT 3, NULL, 'HR related Jobs' UNION ALL
SELECT 4, NULL, 'IT related Jobs' UNION ALL
SELECT 5, NULL, NULL UNION ALL
SELECT 6, 'IT Jobs', 'IT related Jobs' UNION ALL
SELECT 7, 'HR Jobs', NULL UNION ALL
SELECT 8, NULL, 'HR related Jobs' UNION ALL
SELECT 9, 'HR Jobs', NULL;
我想在单列(distinct
值)中加入并合并两列,并在NoOfJobs
上排序,我该如何实现?
预期结果
JobTitle NoOfJobs
HR Jobs 3
IT related Jobs 3
HR related Jobs 2
IT Jobs 2
如果您有任何疑问或问题内容不合适,请发表评论。
Datebase是MySQL,我知道如何在SQL Server中实现这一点。
只需运行此查询(对于MS SQL Server):
DECLARE @tbl table
(
REF_REQ_ID int IDENTITY(1,1),
FunctionalArea varchar(200),
Industry varchar(200)
)
INSERT INTO @tbl
SELECT 'IT Jobs', 'IT related Jobs' UNION ALL
SELECT 'HR Jobs', NULL UNION ALL
SELECT NULL, 'HR related Jobs' UNION ALL
SELECT NULL, 'IT related Jobs' UNION ALL
SELECT NULL, NULL UNION ALL
SELECT 'IT Jobs', 'IT related Jobs' UNION ALL
SELECT 'HR Jobs', NULL UNION ALL
SELECT NULL, 'HR related Jobs' UNION ALL
SELECT 'HR Jobs', NULL
SELECT
JobTitle As JobTitle,
COUNT(REF_REQ_ID) As NoOfJobs
FROM
(SELECT
FunctionalArea As JobTitle,
REF_REQ_ID
FROM @tbl
UNION
SELECT
Industry As JobTitle,
REF_REQ_ID
FROM @tbl)t
WHERE JobTitle IS NOT NULL
GROUP BY JobTitle
ORDER BY NoOfJobs desc
它会给我一些我期望的结果,但不幸的是MySQL不支持UNION
在同一个表上(临时)。
MySQL读者:我如何达到预期效果?
SQL Server读者:另类方法吗?
答案 0 :(得分:0)
select * from (
select FunctionalArea as JobTitle,count(1) as NoOfJobs
from tbl
where FunctionalArea IS NOT NULL
group by FunctionalArea
UNION ALL
select Industry as JobTitle,count(1)as NoOfJobs
from tbl
where Industry IS NOT NULL
group by Industry) t
order by t.NoOfJobs desc
结果
JobTitle NoOfJobs
HR Jobs 3
IT related Jobs 3
HR related Jobs 2
IT Jobs 2
注意:我在Demo sqlfiddle中使用了不同的表名。
答案 1 :(得分:0)
最简单的解决方案是创建一个真实的表而不是临时表。
只要您记得在完成后删除表,并且只要表名不与其他表名冲突,那么它就没有任何区别。
This question对此进行了很好的讨论,并且它还显示了另一种解决方法(尽管在我看来,其他解决方法比使用真实表格要麻烦得多)。
更新:另一种可能性是重新设计,以便联合在临时表之前发生:临时表可以填充联合操作的结果。这是否有意义取决于你的其余代码。
答案 2 :(得分:0)
您不能在同一查询中多次引用TEMPORARY表。例如,以下内容不起作用:
mysql> SELECT * FROM temp_table, temp_table AS t2;
ERROR 1137: Can't reopen table: 'temp_table'
@ dan1111解决方案看起来不错。
答案 3 :(得分:0)
我发布了答案,它给了我预期的结果,并且非常感谢所有人。
我已经添加了一个临时表(不是真实表)
CREATE TEMPORARY table IF NOT EXISTS tbl
(
REF_REQ_ID int AUTO_INCREMENT PRIMARY KEY,
FunctionalArea varchar(200),
Industry varchar(200)
);
TRUNCATE TABLE tbl;
INSERT INTO tbl
SELECT 1, 'IT Jobs', 'IT related Jobs' UNION ALL
SELECT 2, 'HR Jobs', NULL UNION ALL
SELECT 3, NULL, 'HR related Jobs' UNION ALL
SELECT 4, NULL, 'IT related Jobs' UNION ALL
SELECT 5, NULL, NULL UNION ALL
SELECT 6, 'IT Jobs', 'IT related Jobs' UNION ALL
SELECT 7, 'HR Jobs', NULL UNION ALL
SELECT 8, NULL, 'HR related Jobs' UNION ALL
SELECT 9, 'HR Jobs', NULL;
CREATE TEMPORARY table IF NOT EXISTS tbl_industry
(
REF_REQ_ID int,
Industry varchar(200)
);
TRUNCATE TABLE tbl_industry;
INSERT INTO tbl_industry
SELECT
REF_REQ_ID,
Industry
FROM tbl
WHERE Industry IS NOT NULL;
SELECT
JobTitle As JobTitle,
COUNT(REF_REQ_ID) As NoOfJobs
FROM
(SELECT
FunctionalArea As JobTitle,
REF_REQ_ID
FROM tbl
UNION
SELECT
Industry As JobTitle,
REF_REQ_ID
FROM tbl_industry)t
WHERE JobTitle IS NOT NULL
GROUP BY JobTitle
ORDER BY NoOfJobs desc