2

假设我有一个表(在 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

如果您有任何疑问或问题内容不合适,请发表评论。

数据库是 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 读者:做同样的替代方法?

4

4 回答 4

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 中使用了不同的表名。

于 2013-02-28T10:12:46.293 回答
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
于 2013-02-28T10:43:04.553 回答
0

您不能在同一查询中多次引用 TEMPORARY 表。例如,以下内容不起作用:

mysql> SELECT * FROM temp_table, temp_table AS t2;
ERROR 1137: Can't reopen table: 'temp_table'

参考

@dan1111 解决方案看起来不错。

于 2013-02-28T10:25:14.017 回答
0

最简单的解决方案是创建一个真实表而不是临时表。

只要你记得在完成后删除表,并且只要表名不与其他表名冲突,那么它不会有任何区别。

这个问题对此进行了很好的讨论,并且还显示了另一种解决方法(尽管在我看来,另一种解决方法比仅使用真实表格要混乱得多)。

更新:另一种可能性是重新设计,以便联合发生在临时表之前:临时表可以填充联合操作的结果。这是否有意义取决于您的其余代码。

于 2013-02-28T10:14:01.103 回答