我有一个包含客户名称和状态列的表。status列有两个值
Test
Live客户出现不止一次,可以分为测试、活动或以下两种类型:
**Customer | Status**
Logistics | Test
Logistics | Live
Ample | Live我想要的是一个查询,向我提供一个计数,显示属于这两种状态的不同客户的数量。因此,使用上面的表,我将计算客户物流(因为它既有测试又有活动),但不足够(因为它只是实时的)。
有什么想法吗?
发布于 2018-10-24 10:42:03
试着做这样的事情:
select customer
from
(select customer, max(IsTest) as IsTest , max(IsLive) as IsLive
from
(select customer,
case when status='test' then 1 else 0 end as IsTest,
case when status='live' then 1 else 0 end as IsLive
from table) a
group by customer) b
where IsTest = 1 and IsLive = 1发布于 2018-10-24 10:38:37
您可以使用group by子句:
select Customer, count(*)
from table t
group by Customer
having min(status) <> max(status); 如果您希望它具有特定的状态,那么包括where子句:
select Customer, count(*)
from table t
where status in ('Test', 'Live')
group by Customer
having count(distinct status) = 2;编辑:如果您也需要其他列,那么我更喜欢:
select t.*
from table t
where exists (select 1 from table t1 where t1.Customer = t.Customer and t1.status <> t.status);发布于 2018-10-24 12:39:06
您可以使用group子句来获得您的愿望输出。
select Customer, count(*)
from table t
group by Customer
having min(status) <> max(status); https://stackoverflow.com/questions/52966807
复制相似问题