简体   繁体   English

SQL查询 - 计数 - 最大

[英]SQL Query - count - max

I cant manage to come up with a query for a problem. 我无法设法查询问题。 I have three tables 我有三张桌子

CREATE TABLE institute (
    iid INT PRIMARY KEY,
    sign VARCHAR(127) UNIQUE, 
    city VARCHAR(127) NOT NULL,
    area INT CHECK (area>0));

CREATE TABLE desease (
    did INT PRIMARY KEY,
    name VARCHAR(127) UNIQUE,
    level INT CHECK (level>0));

CREATE TABLE studies (
    did INT,
    iid INT,
    FOREIGN KEY (did) REFERENCES desease (did),
    FOREIGN KEY (iid) REFERENCES institute (iid),
    PRIMARY KEY (iid,did));

My question is: What are the names of the deseases by the largest number of institutes from Lisbon (Lisbon beeng the city from institute ). 我的问题是:由单位从里斯本数量最多哪些deseases的名称(里斯本beeng的cityinstitute )。 This is what i came up with but it doesnt give me the right answer. 这是我想出来的,但它没有给我正确的答案。

SELECT DISTINCT D.name, MAX(I.iid)
  FROM desease D, studies S
  JOIN institute I ON (S.iid = I.iid)
 WHERE I.city = 'Lisboa' AND D.did = S.did
 GROUP BY D.nome
HAVING COUNT(I.iid) = MAX(I.city)

As an example : Imagine 5 institutes al with city = 'Lisbon' and with iid A,B,C,D,E respectevely (just for demonstration purposes, I know type is INT); 举个例子:想象一下5个研究所al = city ='Lisbon'和iid A,B,C,D,E(仅用于演示目的,我知道类型是INT); 5 Diseases with name = Z,X,N,V,M respectevely. 5名疾病的名称分别为Z,X,N,V,M。

Now lets say desease Z,X, and M are studied by institutes A,B,C (in any order), desease N is studied by D(1 inst.) and desease V is studied by E (only one). 现在假设由研究所A,B,C(以任何顺序)研究Z,X和M,通过D(1 inst。)研究desease N,并且通过E(仅一个)研究desease V. So the max number of deseases studied by any Lisbon institute is 3 (A,B and C all study 3 deseases) so the table would look like this 因此,任何里斯本研究所研究的最大数量是3(A,B和C所有研究3 deseases)所以表格看起来像这样

Z - 3
X - 3
M - 3

Edit : I managed to found a way to do it. 编辑:我设法找到了办法。 Here is the query that I came up with 这是我提出的查询

SELECT DISTINCT D.name, COUNT(*) AS C
FROM desease D, studies E, institute I
WHERE I.iid = E.iid AND D.did = E.did AND I.city = "Lisboa"
GROUP BY D.name
HAVING C >= ALL (
SELECT COUNT(*)
FROM desease D, studies E, institute I
WHERE I.iid = E.iid AND D.did = E.did AND I.cidade = "Lisboa"
GROUP BY D.name

); );

I don't understand structure/problme well enough but I did see that you were mixing joins and had a cross join which would inflate the number of recrds. 我不太了解结构/问题,但我确实看到你正在混合连接并且有一个交叉连接会增加recrds的数量。

SELECT DISTINCT D.name, MAX(I.iid)
FROM desease D
INNER JOIN  studies S ON D.iid=S.Did
INNER JOIN  institute I ON (S.iid = I.iid)
WHERE I.city = 'Lisboa' AND D.did = S.did
GROUP BY D.nome
HAVING COUNT(I.iid) = MAX(I.city)

Just a rough guess what you need: 只是粗略猜测你需要什么:

SELECT stu.iid, COUNT(*) AS nstudies
FROM studies stu, institute ins
WHERE stu.iid=ins.iid
AND ins.city='Lisboa'
GROUP BY stu.iid
ORDER BY nstudies DESC;

This should give you a list of institutes that are in Lisboa and the number of studies they did. 这应该会给你一份里斯本学院的名单和他们所做的研究数量。

SELECT stu.did, COUNT(*) AS ninst
FROM studies stu, institute ins, disease dis
WHERE stu.iid=ins.iid
AND stu.did=dis.did
AND ins.city='Lisboa'
GROUP BY stu.did
ORDER BY ninst DESC;

This gives you a list of deseases and the number of Lisboa instutitues that did it. 这给你一个desease列表和Lisboa instutitues的数量。

Unfortunately your question leaves a lot of room for speculation as to what you need -- maybe you should add some example data and the expected result. 不幸的是,你的问题留下了很多猜测你需要什么的空间 - 也许你应该添加一些示例数据和预期结果。

This would return a list of disease names that have an institute in Lisbon starting with the one with the greatest number of institutes in Lisbon and going down: 这将返回一份在里斯本拥有一所研究所的疾病名单,从里斯本研究所数量最多的研究所开始下降:

SELECT D.name, COUNT(*) as numberOfInstitutes
FROM desease D
INNER JOIN studies S ON D.did=S.did
INNER JOIN institute I ON (S.iid = I.iid)
WHERE I.city = 'Lisbon'
GROUP BY D.did
ORDER BY COUNT(*) desc

If you need only the one that has the most institutes and you need the rest of the columns from the desease table, you can do this (in Sql Server): 如果您只需要具有最多机构的那个并且您需要desease表中的其余列,则可以执行此操作(在Sql Server中):

SELECT TOP 1 D.* 
FROM desease D
INNER JOIN
(
    SELECT D.did, COUNT(*) as numberOfInstitutes
    FROM desease D
    INNER JOIN studies S ON D.did=S.did
    INNER JOIN institute I ON (S.iid = I.iid)
    WHERE I.city = 'Lisbon'
    GROUP BY D.did
) as tblCount on tblCount.did = D.did
ORDER BY numberOfInstitutes desc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM