Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL如何按函数向分组添加多个属性_Sql_Oracle_Group By_Aggregate Functions - Fatal编程技术网

SQL如何按函数向分组添加多个属性

SQL如何按函数向分组添加多个属性,sql,oracle,group-by,aggregate-functions,Sql,Oracle,Group By,Aggregate Functions,我正在使用Oracle SQL ad,希望编写一个查询,显示引用最大客户数的客户详细信息。可以有多个客户(如果每个客户的最大推荐数量相等) 在包含CUS\u ID、CUS\u NAME、CUS\u referenced的CUS\u referenced和COUNT(CUST\u referenced)的 select CUST_REFERRED, count(CUST_REFERRED) from CUSTOMER group by CUST_REFERRED hav

我正在使用Oracle SQL ad,希望编写一个查询,显示引用最大客户数的客户详细信息。可以有多个客户(如果每个客户的最大推荐数量相等)

在包含
CUS\u ID、CUS\u NAME、CUS\u referenced
CUS\u referenced
COUNT(CUST\u referenced)

   select  CUST_REFERRED, count(CUST_REFERRED)
   from CUSTOMER 
   group by CUST_REFERRED
   having count(CUST_REFERRED) = (select max(MYCOUNT) 
   from (select cust_referred, 
           count(CUST_REFERRED) MYCOUNT 
           from CUSTOMER 
           group by CUST_REFERRED));
样本数据:

Cust_Num        Cust_FName         Cust_Referred
------------------------------------------------
1000            Patricia           (null)
1001            Jim                1000
1002            Zoe                1003
1003            John               (null)
1004            Marie              1003
1005            Anthony            1002
1006            Jodee              1003                

CUST_REFERRED   COUNT(C.CUST_REFERRED) 
-------------   ---------------------- 
1003            3
CUST_FNAME  CUST_REFERRED   COUNT(C.CUST_REFERRED) 
----------  -------------   ---------------------- 
John        1003            3
电流输出:

Cust_Num        Cust_FName         Cust_Referred
------------------------------------------------
1000            Patricia           (null)
1001            Jim                1000
1002            Zoe                1003
1003            John               (null)
1004            Marie              1003
1005            Anthony            1002
1006            Jodee              1003                

CUST_REFERRED   COUNT(C.CUST_REFERRED) 
-------------   ---------------------- 
1003            3
CUST_FNAME  CUST_REFERRED   COUNT(C.CUST_REFERRED) 
----------  -------------   ---------------------- 
John        1003            3
所需输出:

Cust_Num        Cust_FName         Cust_Referred
------------------------------------------------
1000            Patricia           (null)
1001            Jim                1000
1002            Zoe                1003
1003            John               (null)
1004            Marie              1003
1005            Anthony            1002
1006            Jodee              1003                

CUST_REFERRED   COUNT(C.CUST_REFERRED) 
-------------   ---------------------- 
1003            3
CUST_FNAME  CUST_REFERRED   COUNT(C.CUST_REFERRED) 
----------  -------------   ---------------------- 
John        1003            3
其中,John是客户1003,并获得了最多的推荐(3)。如果另一个人(比如杰克)也做了3次推荐,他也会被列入名单

但是,每当我尝试添加
CUS\u FNAME
,我都会得到“未返回行”(如果在主选择查询中)或一个有1个参考的客户列表(在嵌套选择查询中)

非常感谢任何帮助

使用窗口功能:

select cust_referred, cnt
from (select cust_referred, count(*) as cnt,
             rank() over (order by count(*)) as seqnum
      from customer
      group by cust_referred
     ) c
where seqnum = 1;
如果您需要有关客户的更多信息,则可以将其加入到
customer
表中。

从Oracle 12c开始:

select c.cust_num, c.cust_fname, cr.cnt
from customer c
join
(
  select cust_referred, count(*) as cnt
  from customer
  group by cust_referred
  order by count(*) desc
  fetch first row with ties
) cr on cr.cust_referred = c.cust_num;
在旧版本中:

select c.cust_num, c.cust_fname, cr.cnt
from customer c
join
(
  select
    cust_referred,
    count(*) as cnt,
    rank() over (order by count(*) desc) as rnk
  from customer
  group by cust_referred
) cr on cr.cust_referred = c.cust_num and cr.rnk = 1;

演示:

谢谢,您的查询将返回非max客户列表。我的查询返回了一个推荐次数最多的客户,但我无法为该客户提供更多的结果。我的返回值:
CUST\u referenced COUNT(C.CUST\u referenced)--------------------------------------------------------------------------------------1003
Customer 1003需要添加到查询中的详细信息未选择任何返回行(我使用的是版本18.4),然后您犯了一些错误。我已经添加了一个演示,显示两个查询都可以工作。谢谢-你的小提琴可以工作,当我在我的数据库中复制它时,它可以工作,所以我会将它标记为答案。但是,由于某些原因,它在我的DB Customer表上不起作用。