[英]Select where Count of 1 field is greater than a value and count of another is less than a value
我正在嘗試返回客戶表中statustype ='dc'的所有實例,然后針對這些結果,FC上的計數> 1,Address1上的計數為1。
IE:
FC Address1
111 abc
111 cde
432 qqq
432 qqq
我需要返回111 FC結果,因為它們的address1不同。 但是我不需要返回432 FC結果,因為該FC有多個地址
SELECT *
FROM Customers
where FC IN( select FC from Customers where StatusType= 'dc'
group by FC having COUNT(FC) > 1 and COUNT(Address1) < 2
)
order by FC, Address1
我也試過= 1而不是<2
如果需要有關具有多個唯一地址的FC的詳細信息,則此查詢將為您提供:
select c.* from customers c
join (
select FC
from customers
where statustype = 'dc'
group by fc having count(distinct Address1) > 1
) a on c.FC = a.FC
您還需要按地址1分組。
SELECT *
FROM Customers
where FC IN( select FC from Customers where StatusType= 'dc'
group by FC, Address1 having COUNT(FC) > 1 and COUNT(Address1) < 2)
order by FC, Address1
嘗試使用Distinct COUNT
SELECT *
FROM Customers
WHERE FC IN(SELECT FC
FROM Customers
WHERE StatusType = 'dc'
GROUP BY FC
HAVING Count(DISTINCT Address1) > 1)
ORDER BY FC,
Address1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.