简体   繁体   English

非常基本的SQL查询涉及计数

[英]Pretty basic SQL query involving count

Let's say I have the following schema 假设我有以下架构

Company:
-> company_id
-> company_name

Building_to_company:
-> building_id
-> company_id

So each building has its own id as well as a company id which relates it to a single company. 因此,每个建筑物都有自己的ID以及与单个公司相关的公司ID。

the following query gives two columns -- one for the company name, and then its associated buildings. 以下查询提供两列 - 一列用于公司名称,然后是其关联的建筑物。

SELECT company.company_name, building_to_company.building_id 
FROM company, building_to_company 
WHERE company.company_id = building_to_company.company_id;

The returned table would look something like this: 返回的表看起来像这样:

Company Name | Building Id
Smith Banking  2001
Smith Banking  0034
Smith Banking  0101
Smith Banking  4055
Reynolds       8191
TradeCo        7119
TradeCo        8510

So that's all simple enough. 所以这一切都很简单。

But I need to do something a bit different. 但我需要做一些不同的事情。 I need 2 columns. 我需要2列。 One for the company name and then on the right the number of buildings it owns. 一个用于公司名称,然后在右侧是它拥有的建筑物数量。 And then for a little extra challenge I only want to list companies with 3 or less buildings. 然后,为了一点额外的挑战,我只想列出3栋或更少建筑物的公司。

At this point the only real progress I've made is coming up with the query above. 在这一点上,我所做的唯一真正进展是提出上面的查询。 I know I some how have to use count on the building_id column and count the number of buildings associated with each company. 我知道如何在building_id列上使用count并计算与每个公司相关的建筑物数量。 And then at that point I can limit things by using something like WHERE x < 4 然后在那时我可以通过使用像WHERE x < 4这样的东西限制事物

You've basically got it in words already. 你基本上已经用语言表达了它。 Assuming company_name is unique, all you have to add to your explanation to get it to work is a GROUP BY clause: 假设company_name是唯一的,那么您必须添加到解释中才能使其工作的是GROUP BY子句:

SELECT company.company_name, COUNT(building_to_company.building_id)
FROM company
INNER JOIN building_to_company 
    ON company.company_id = building_to_company.company_id
GROUP BY company.company_name

( SQL Fiddle demo of this query in action ) 此查询的SQL Fiddle演示实际上


To limit it to companies with 3 or less buildings, the key is you have to use a HAVING clause and not WHERE . 要将它限制在3栋或更少建筑物的公司,关键是您必须使用HAVING子句而不是WHERE This is because you want to filter based on the results of an aggregate ( COUNT ); 这是因为你想根据聚合的结果( COUNT )进行过滤; simply put, WHERE filters come before aggregation and HAVING come after: 简单地说, WHERE过滤器在聚合和HAVING之前出现:

SELECT company.company_name, COUNT(building_to_company.building_id)
FROM company
INNER JOIN building_to_company 
    ON company.company_id = building_to_company.company_id
GROUP BY company.company_name
HAVING COUNT(building_to_company.building_id) < 4

( SQL Fiddle demo of this query in action ) 此查询的SQL Fiddle演示实际上

I think you want something like this 我想你想要这样的东西

SELECT c.company_name, count(b.building_id)
FROM 
  company as c, 
  building_to_company as b 
WHERE c.company_id = b.company_id
GROUP BY c.company_name;

Use the Below SQL-statement to find the company name with its own building count in descending order. 使用Below SQL语句以降序查找具有自己的构建计数的公司名称。

SELECT company.company_name, count(building_to_company.building_id)
FROM company (nolock), building_to_company(nolock) 
WHERE company.company_id = building_to_company.company_id
group by  company.company_name
having count(building_to_company.building_id)<=3
order by count(building_to_company.building_id) desc

Kindly let me know if you have any issue. 如果您有任何问题,请告诉我。 Thanks 谢谢

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

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