[英]Oracle SQL: Obtain count of distinct column values based on another column
Here is a sample data to explain my case,这是一个示例数据来解释我的情况,
CompanyInfo:公司信息:
Name, Company, Location, Completed, Pending
Andy AA Home 4 2
Jim AA Office 3 3
Dahli AA Home 4 2
Monica AA Home 4 2
Chandler AA Home-Office 1 0
Ashley AA Home-Office 1 0
The last three columns have duplicated information and I am trying to obtain count of location, completed and pending which are bound to each other.最后三列有重复的信息,我正在尝试获取相互绑定的位置、已完成和待处理的计数。 So the output would look something like below,
所以 output 看起来像下面这样,
Company, Count(Locations), Count( Completed+Pending > 0),
AA 3 3
Why Count( Completed+Pending > 0) is 3?为什么 Count(Completed+Pending > 0) 是 3? there are just three unique combinations of Home, Office and home-office columns where sum of completed+pending is > 0.
只有三个独特的 Home、Office 和 home-office 列组合,其中已完成 + 待处理的总和 > 0。
I did try below, but it gives me (AA, 3, 6) since it is processing all the 6 rows to obtain the count.我确实在下面尝试过,但它给了我(AA, 3, 6)因为它正在处理所有 6 行以获得计数。
select Company,
count(distinct Location),
SUM (
CASE
WHEN (Completed + Pending) > 0 THEN 1
ELSE 0
END)
AS Total
From CompanyInfo
group by Company;
Any pointers?任何指针?
I think you want a conditional count(distinct)
:我认为你想要一个条件
count(distinct)
:
select Company,
count(distinct Location),
count(distinct case when Completed + Pending > 0 then location end)
from CompanyInfo
group by Company;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.