我有一张按客户和服务划分的销售表。我需要获取拥有多项服务的客户的摘要,但其中至少一项服务满足给定标准。
到目前为止我有
SELECT Account, Count(DISTINCT Service), bill
FROM jobs_by_service
GROUP GY bill
ORDER BY Count(DISTINCT Service) DESC, Account
当我介绍特定服务时,我会失去不同的服务结果。我想我需要在选择中使用选择,但似乎在绕圈子。
谢谢
要获取使用特定服务的所有服务使用的客户的摘要:
SELECT Account, Count(DISTINCT Service), bill
FROM jobs_by_service
WHERE customer_id in (
SELECT customer_id
FROM jobs_by_service
WHERE Service = ?)
Group By Account, bill
Order by Count(DISTINCT Service) DESC, Account
可以通过子查询来完成。你还没有给我们表创建语句,所以我在这里猜测一下。
Select account, Count(DISTINCT Service) as d_count
FROM jobs_by_service Group By account
这将告诉您不同服务的帐户和数量。将其转换为您可以参考的子查询
Select a.account , b.d_count
from jobs_by_service a
inner join (Select account, Count(DISTINCT Service) as d_count
FROM jobs_by_service Group By account) b
on b.account = a.account
where criteria1 = 'hi' or criteria2 = 'bye' or criteria1 = 'ok'