Select 其中 1 个字段的计数大于一个值而另一个字段的计数小于一个值
Select where Count of 1 field is greater than a value and count of another is less than a value
我正在尝试 return Customers table 中的所有实例,其中 statustype= 'dc' 然后对于这些结果,FC 上的计数 > 1 和 Address1 上的计数是 1.
IE:
FC Address1
111 abc
111 cde
432 qqq
432 qqq
我需要返回 111 FC 结果,因为它们的 address1 不同。但我不需要返回 432 FC 结果,因为该 FC
的地址不止 1 个
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
您还需要按 Address1 分组。
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
如果您想要有关具有多个唯一地址的 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
我正在尝试 return Customers table 中的所有实例,其中 statustype= 'dc' 然后对于这些结果,FC 上的计数 > 1 和 Address1 上的计数是 1.
IE:
FC Address1
111 abc
111 cde
432 qqq
432 qqq
我需要返回 111 FC 结果,因为它们的 address1 不同。但我不需要返回 432 FC 结果,因为该 FC
的地址不止 1 个 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
您还需要按 Address1 分组。
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
如果您想要有关具有多个唯一地址的 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