DISTINCT COUNT个相关子查询SQL

时间:2019-01-19 08:25:31

标签: sql sql-server

它正试图解决以下问题

  

编写查询以查找具有多个客户的推销员。

数据库:

customer_id  cust_name     city        grade       salesman_id
-----------  ------------  ----------  ----------  -----------
3002         Nick Rimando  New York    100         5001
3005         Graham Zusi   California  200         5002
3001         Brad Guzan    London      100         5005
3004         Fabian Johns  Paris       300         5006
3007         Brad Davis    New York    200         5001
3009         Geoff Camero  Berlin      100         5003
3008         Julian Green  London      300         5002
3003         Jozy Altidor  Moncow      200         5007

我的解决方案是

SELECT * 
FROM Customer o
WHERE 2 <= (SELECT COUNT(DISTINCT customer_id)
            Customer i
            WHERE o.salesman_id = i.salesman_id)

我不明白为什么会出错。我正在尝试here。但是当我尝试执行它时却没有结果

预期结果:

salesman_id     city    
5001            New York
5002            Paris   

6 个答案:

答案 0 :(得分:1)

您应该group by salesman_id并选中count(*)>= 2

SELECT salesman_id, count(*) as counter 
FROM Customer 
group by salesman_id
having count(*) >= 2;

,如果需要销售员详细信息:

select salesman.salesman_id, salesman.city from salesman
inner join (
    SELECT salesman_id, count(*) as counter 
    FROM customer 
    group by salesman_id
    having count(*) >= 2
) as t
on t.salesman_id = salesman.salesman_id

答案 1 :(得分:1)

我看到您的链接,您可能会尝试在子查询中将JOIN与聚合函数一起使用。

将计数加salesman_id大于一。

SELECT s.salesman_id,s.city
FROM 
(  select salesman_id
    FROM  Customer i
    GROUP BY i.salesman_id
    having count(*) > 1
)o JOIN Salesman s on s.salesman_id = o.salesman_id  

答案 2 :(得分:1)

只需将JOINHAVING count(*)>1一起使用

SELECT s.salesman_id, s.city
  FROM salesman s
  JOIN customer c on c.salesman_id = s.salesman_id
 GROUP BY s.salesman_id, s.city
 HAVING count(*)>1;

 salesman_id     city    
 5001            New York
 5002            Paris   

关于您的逻辑,您可以考虑以下内容(通过使用2<=关键字将exists放入内部查询中)并共享在数据库中使用的salesman表引用的链接:

SELECT o.salesman_id, o.city
  FROM salesman o
 WHERE EXISTS
       (
        SELECT i.salesman_id
          FROM Customer i
         WHERE o.salesman_id = i.salesman_id
         GROUP BY i.salesman_id
        HAVING 2 <= count(distinct i.customer_id) 
       );

 salesman_id     city    
 5001            New York
 5002            Paris   

答案 3 :(得分:1)

如果销售员/客户行是唯一的,则:

select salesman_id, count(*) as counter 
from Customer 
group by salesman_id
having count(*) >= 2;

如果有重复项,则:

select salesman_id, count(*) as counter 
from Customer 
group by salesman_id
having count(distinct customer_id) >= 2;

但是,如果您想要原始的详细信息,我会使用exists

select c.*
from customer c
where exists (select 1
              from customer c2
              where c2.salesman_id = c.salesman_id and
                    c2.customer_id <> c.customer_id
             );

您的查询错误,因为子查询缺少FROM子句。因此,它存在语法错误。

它还有另外两个问题,都不是致命的。子查询中对customer_id的引用应该是合格的(即i.customer_id)。查找由不合格的列引起的问题是相关的子查询,这确实很痛苦,因此只需对所有列进行限定即可。

第二个问题是,从性能的角度来看,COUNT()过于矫kill过正。基本上,所有匹配的行都需要处理以计算count()进行比较。为此,EXISTS是更好的选择。

答案 4 :(得分:1)

仅显示客户表,您的预期结果意义不大。您说要查找拥有多个客户的推销员。您要显示给我们的唯一城市是在客户表中,我们可以看到推销员的客户可以居住在不同的城市。那么,要向推销员展示哪个城市?

您的演示链接显示了一个附加的销售员表,在这里我们可以看到一个销售员记录也有一个城市。这必须是您要显示的那个。您应该已经在请求中显示了业务员表。

这是找到销售员的方法。无需进行distinct计数,因为客户当然应该只在客户表中出现一次。

select *
from salesman
where salesman_id in
(
  select salesman_id
  from customer 
  group by salesman_id
  having count(*) > 1
);

关于您自己的查询(您试图在其中显示具有多个客户的推销员的所有客户行-并非完全是要询问的内容),请参阅Gordon Linoff的答案。

答案 5 :(得分:0)

也许是由于范围界定?

您是否尝试过将COUNT(DISTINCT customer_id)更改为COUNT(DISTINCT i.customer_id)?因为那是您要计算的customer_id,而不是o.customer_id