嗨,我正在尝试编写一个查询,该查询返回对该类别感兴趣的人数最多的类别(如果有意义的话)
所以无论如何这是我到目前为止的尝试..
SELECT category, MAX(catcount) FROM (
select category, catid, COUNT(id) AS catcount FROM (
SELECT DISTINCT empinterest.id, INTERESTCATEGORY.CATID AS catid, INTERESTCATEGORY.category FROM EMPINTEREST
INNER JOIN interest ON EMPINTEREST.INTID = INTEREST.ID
INNER JOIN interestcategory ON INTEREST.CATID = INTERESTCATEGORY.CATID
order by empinterest.id)
group by catid, category) GROUP BY category
我知道第一个子选择会给我类别和人们对该类别的兴趣数量,这就是为什么我试图在计数上运行最大值但我认为 group by 子句是阻止最大值的原因被退回。我曾尝试使用 HAVING 子句,但似乎无法正确使用语法,因此希望有人能够帮助我。提前致谢!
这是表格,如果有帮助,如果有人需要进一步说明,我会提供!
CREATE TABLE department
(
dptID INT NOT NULL PRIMARY KEY,
name VARCHAR(30) NOT NULL
);
CREATE TABLE interestcategory
(
catID INT NOT NULL PRIMARY KEY,
category VARCHAR(30) NOT NULL
);
CREATE TABLE employee (
id INT NOT NULL PRIMARY KEY,
title VARCHAR(10) NOT NULL,
fName VARCHAR(50) NOT NULL,
sName VARCHAR(50) NOT NULL,
jTitle VARCHAR(50) NOT NULL,
startDate DATE NOT NULL,
teamLead VARCHAR(50) NOT NULL,
employeeType VARCHAR(30) NOT NULL,
dptID INT NOT NULL REFERENCES department(dptID)
);
CREATE TABLE interest
(
id INT NOT NULL PRIMARY KEY,
interest VARCHAR(50) NOT NULL,
catID INT NOT NULL REFERENCES interestcategory(catID)
);
CREATE TABLE empinterest
(
id INT NOT NULL REFERENCES employee(id),
intID INT NOT NULL REFERENCES interest(id),
PRIMARY KEY (id, intID)
);