0

嗨,我正在尝试编写一个查询,该查询返回对该类别感兴趣的人数最多的类别(如果有意义的话)

所以无论如何这是我到目前为止的尝试..

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)
);
4

2 回答 2

1

看一下您通常如何做到这一点:

CREATE TABLE categories (
  category_id NUMBER,
  category_name VARCHAR2(20)
);

CREATE TABLE dep_table (
  category_id NUMBER
);

INSERT INTO categories VALUES (1, 'A');
INSERT INTO categories VALUES (2, 'B');
INSERT INTO categories VALUES (3, 'C');

INSERT INTO dep_table VALUES (1);
INSERT INTO dep_table VALUES (1);
INSERT INTO dep_table VALUES (1);
INSERT INTO dep_table VALUES (2);
INSERT INTO dep_table VALUES (3);
INSERT INTO dep_table VALUES (3);

COMMIT:

WITH
  main_query AS (
    SELECT ct.category_name, dt.category_id
      FROM categories ct
        JOIN dep_table dt ON (ct.category_id = dt.category_id)
  )
SELECT category_name, COUNT(1)
  FROM main_query
GROUP BY category_name
HAVING COUNT(1) = (SELECT MAX(COUNT(1)) FROM main_query GROUP BY category_name);

因此,您的查询将如下所示:

WITH
  main_query AS (
    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
  )
SELECT category, COUNT(1)
  FROM main_query
GROUP BY catid, category
HAVING COUNT(1) = (SELECT MAX(COUNT(1)) FROM main_query GROUP BY catid, category);
于 2013-10-31T21:47:55.423 回答
0

您可以执行子查询并选择第一行:

SELECT category, 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 order by catcount desc)
WHERE ROWNUM <= 1

另见这个问题

唯一的麻烦是,当您希望能够获得满足的所有行时,我猜count(id) = max(count(id))您将不得不使用。HAVING

于 2013-10-31T21:34:28.020 回答