[英]SQL that requires two conditions be met in WHERE clause
I have table of bird observations.我有鸟类观察表。 This is an example:这是一个例子:
Unique_ID List_ID Locality Common_name
A1 001 Park Downy Woodpecker
A2 001 Park Hairy Woodpecker
A3 001 Park Carolina Chickadee
B1 002 Campground Blue Jay
B2 002 Campground Hairy Woodpecker
C1 003 Backyard Downy Woodpecker
C2 003 Backyard American Goldfinch
D1 004 School American Goldfinch
D2 004 School Hairy Woodpecker
E1 005 Park Downy Woodpecker
E2 005 Park Carolina Chickadee
I am trying to write a query for PostgreSQL that will only return the Localities that have occurrences of both woodpeckers, Downy and Hairy, together.我正在尝试为 PostgreSQL 编写一个查询,该查询将只返回同时出现啄木鸟 Downy 和 Hairy 的 Localities。 In the little example table, that would just be the Park.在这个小示例表中,那只是公园。 The other localities only have one or the other of the species.其他地区只有其中一种。
I tried我试过
SELECT List_ID, LOCALITY, COMMON_NAME FROM table
WHERE COMMON_NAME = 'Downy Woodpecker' and COMMON_NAME = 'Hairy Woodpecker';
But returned no results.但没有返回任何结果。 My table has 1,000s of observations, it's based on eBird data and those two species are generally common throughout the country, so there has to be at least one list_ID where they occur at the same time.我的表格有 1,000 条观察结果,它基于eBird 数据,这两个物种在全国普遍存在,因此必须至少有一个 list_ID 同时出现。 In my example table, only Park (based on list_ID 001) meets the condition for what I'm looking for.在我的示例表中,只有 Park(基于 list_ID 001)符合我要查找的条件。
If I understand IN
, it will return a row that meets either.如果我理解IN
,它将返回满足任一条件的行。 Any of the example list_IDs would work for that query, but that's not what I want.任何示例 list_IDs 都适用于该查询,但这不是我想要的。 How do I write a query that forces the WHERE
to meet multiple conditions?如何编写强制WHERE
满足多个条件的查询?
You can join the table with itself to get the locations you want.您可以将表格与自身连接起来以获得所需的位置。 For example:例如:
select distinct a.locality
from my_table a
join my_table b on a.locality = b.locality
where a.common_name = 'Downy Woodpecker'
and b.common_name = 'Hairy Woodpecker'
You could first write a query that returns list_ids “having” both types of woodpeckers.您可以先编写一个查询,返回 list_ids “具有”两种类型的啄木鸟。 (Group by list_id and write a condition in the having clause that will return the ids). (按 list_id 分组并在 having 子句中写一个返回 id 的条件)。
Something like就像是
max(case when common_name like ‘hairy%’ then 1 else 0 end) = 1
and max(case when common_name like ‘downy%’ then 1 else 0 end) = 1
Then, you can use that query to filter your base table for only the desired list_ids.然后,您可以使用该查询仅针对所需的 list_ids 过滤基表。
First filter the table, group by Locality
and return only the localities containing both kinds:首先过滤表, group by Locality
只返回包含两种类型的地区:
select Locality
from tablename
where Common_name in ('Downy Woodpecker', 'Hairy Woodpecker')
group by Locality
having count(distinct Common_name) = 2
The condition count(distinct Common_name) = 2
in the HAVING
clause makes sure that both kinds exist for the same Locality
. HAVING
子句中的条件count(distinct Common_name) = 2
确保同一Locality
存在两种类型。
If there is no case of duplicate Common_name
s in the same Locality
you may also omit distinct
from count()
.如果在同一Locality
中没有重复的Common_name
,您也可以省略distinct
from count()
。
See the demo .请参阅演示。
Results:结果:
| locality |
| -------- |
| Park |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.