Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.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 - Fatal编程技术网

SQL:查找最大行数

SQL:查找最大行数,sql,oracle,Sql,Oracle,我对编码完全是新手,所以我的问题可能是愚蠢的,首先对此表示抱歉 我有一个数据库,它有一个客户号,代表一个客户号 CUST_NUM NAME_S NAME_F ADDRESS Z_CODE CUST_REFERRED 1001 MORALES BONITA P.O. BOX 651 32328 1002 THOMPSON RYAN P.O. BOX 9835

我对编码完全是新手,所以我的问题可能是愚蠢的,首先对此表示抱歉

我有一个数据库,它有一个客户号,代表一个客户号

CUST_NUM NAME_S     NAME_F      ADDRESS            Z_CODE          CUST_REFERRED
1001    MORALES     BONITA  P.O. BOX 651            32328   
1002    THOMPSON    RYAN    P.O. BOX 9835           90404   
1003    SMITH       LEILA   P.O. BOX 66             32306   
1004    PIERSON     THOMAS  69821 SOUTH AVENUE      83707   
1005    GIRARD      CINDY   P.O. BOX 851            98115   
1006    CRUZ        MESHIA  82 DIRT ROAD            12211   
1007    GIANA       TAMMY   9153 MAIN STREET        78710            1003
1008    JONES       KENNETH P.O. BOX 137            82003   
1009    PEREZ       JORGE   P.O. BOX 8564           91510            1003
1010    LUCAS       JAKE    114 EAST SAVANNAH       30314   
1011    MCGOVERN    REESE   P.O. BOX 18             60606   
1012    MCKENZIE    WILLIAM P.O. BOX 971            02110   
1013    NGUYEN      NICHOLAS    357 WHITE EAGLE AVE 34711            1006
1014    LEE         JASMINE P.O. BOX 2947           82414   
1015    SCHELL      STEVE   P.O. BOX 677            33111   
1016    DAUM        MICHELL 9851231 LONG ROAD       91508            1010
1017    NELSON      BECCA   P.O. BOX 563            49006   
1018    MONTIASA    GREG    1008 GRAND AVENUE       31206   
1019    SMITH       JENNIFER    P.O. BOX 1151       07962            1003
1020    FALAH       KENNETH P.O. BOX 335            08607   
我的想法是找到推荐max book的客户。因此,您可以看到3次
1003
number引用的书谁的名字是
LEILA SMITH

我尝试了一个代码,它可以:

SELECT
  CUST_REFERRED,
  COUNT(*)
FROM
  CUSTOMER
GROUP BY
  CUST_REFERRED
  ORDER BY CUST_REFERRED ASC;
这段代码给了我:

 1003          3
 1006          1
 1010          1 
首先,我的问题是我不能用极限函数来求最大数 第二个问题是如何添加更多的客户信息?

试试这个:

Select CUST_REFERRED, z.cnt from
   (SELECT CUST_REFERRED, COUNT(*) cnt
    FROM CUSTOMER where CUST_REFERRED is Not null
    GROUP BY CUST_REFERRED) Z
where z.cnt = 
     (select Max(cnt) from
          (SELECT COUNT(*) cnt
           FROM CUSTOMER where CUST_REFERRED is Not null
           GROUP BY CUST_REFERRED) ZZ)
请尝试此查询--


编辑以添加引用的客户详细信息

with data (cust_num, name_s, name_f, addr, code, cust_referred) as
    (
    /* begin: test data */
    select 1001    ,'MORALES     ','BONITA  ','P.O. BOX 651            ',32328,  null from dual union all   
    select 1002    ,'THOMPSON    ','RYAN    ','P.O. BOX 9835           ',90404,  null from dual union all 
    select 1003    ,'SMITH       ','LEILA   ','P.O. BOX 66             ',32306,  null from dual union all 
    select 1004    ,'PIERSON     ','THOMAS  ','69821, SOUTH AVENUE     ',83707,  null from dual union all 
    select 1005    ,'GIRARD      ','CINDY   ','P.O. BOX 851            ',98115,  null from dual union all 
    select 1006    ,'CRUZ        ','MESHIA  ','82 DIRT ROAD            ',12211,  null from dual union all 
    select 1007    ,'GIANA       ','TAMMY   ','9153 MAIN STREET        ',78710,            1003 from dual union all
    select 1008    ,'JONES       ','KENNETH ','P.O. BOX 137            ',82003,  null from dual union all 
    select 1009    ,'PEREZ       ','JORGE   ','P.O. BOX 8564           ',91510,            1003 from dual union all
    select 1010    ,'LUCAS       ','JAKE    ','114 EAST SAVANNAH       ',30314,  null from dual union all 
    select 1011    ,'MCGOVERN    ','REESE   ','P.O. BOX 18             ',60606,   null from dual union all
    select 1012    ,'MCKENZIE    ','WILLIAM ','P.O. BOX 971            ',02110,   null from dual union all
    select 1013    ,'NGUYEN      ','NICHOLAS    ','357 WHITE EAGLE AVE ',34711,            1006 from dual union all
    select 1014    ,'LEE         ','JASMINE ','P.O. BOX 2947           ',82414,   null from dual union all
    select 1015    ,'SCHELL      ','STEVE   ','P.O. BOX 677            ',33111,   null from dual union all
    select 1016    ,'DAUM        ','MICHELL ',',9851231, LONG ROAD     ',91508,            1010 from dual union all
    select 1017    ,'NELSON      ','BECCA   ','P.O. BOX 563            ',49006,   null from dual union all
    select 1018    ,'MONTIASA    ','GREG    ','1008 GRAND AVENUE       ',31206,   null from dual union all
    select 1019    ,'SMITH       ','JENNIFER    ','P.O. BOX 1151       ',07962,            1003 from dual union all
    select 1020    ,'FALAH       ','KENNETH ','P.O. BOX 335            ',08607,  null from dual
    /* end: test data */
    -- replace the above block with your table
    -- eg. select * from customers_table
    )
    , 
    max_referred as 
    (
      -- just interested in the first row after sorting by
      -- the count of referred column values
      select rownum, cust_referred, cnt from 
      (
        select cust_referred, count(cust_referred) cnt from data group by cust_referred order by 2 desc
      ) 
      where rownum = 1
    )
    -- joining on cust_referred column in *data* and *max_referred* tables to get the customer details
    -- and joining again to the *data* table for fetching the referred customer name
    select 
      cust.cust_num, cust.name_s, cust.name_f, cust.addr, cust.code, cust.cust_referred, ms.name_f || ms.name_s as "Referred Customer"
    from 
      data cust
     join 
      max_referred mr on (cust.cust_referred = mr.cust_referred)
      join 
      data ms
      on (mr.cust_referred = ms.cust_num)
    ;

您可以使用分析函数在单个表扫描(即不使用任何自联接)中执行此操作:

SELECT *
FROM   (
  SELECT t.*,
         MIN( CUST_REFERRED )
           KEEP ( DENSE_RANK FIRST ORDER BY num_referrals DESC )
           OVER ()
           AS best_referrer
  FROM   (
    SELECT c.*,
           COUNT( CUST_REFERRED )
             OVER ( PARTITION BY CUST_REFERRED )
             AS num_referrals
    FROM   CUSTOMER c
  ) t
)
WHERE  cust_num = best_referrer;

谢谢,这是给我一个最大数量的客户。
with data (cust_num, name_s, name_f, addr, code, cust_referred) as
    (
    /* begin: test data */
    select 1001    ,'MORALES     ','BONITA  ','P.O. BOX 651            ',32328,  null from dual union all   
    select 1002    ,'THOMPSON    ','RYAN    ','P.O. BOX 9835           ',90404,  null from dual union all 
    select 1003    ,'SMITH       ','LEILA   ','P.O. BOX 66             ',32306,  null from dual union all 
    select 1004    ,'PIERSON     ','THOMAS  ','69821, SOUTH AVENUE     ',83707,  null from dual union all 
    select 1005    ,'GIRARD      ','CINDY   ','P.O. BOX 851            ',98115,  null from dual union all 
    select 1006    ,'CRUZ        ','MESHIA  ','82 DIRT ROAD            ',12211,  null from dual union all 
    select 1007    ,'GIANA       ','TAMMY   ','9153 MAIN STREET        ',78710,            1003 from dual union all
    select 1008    ,'JONES       ','KENNETH ','P.O. BOX 137            ',82003,  null from dual union all 
    select 1009    ,'PEREZ       ','JORGE   ','P.O. BOX 8564           ',91510,            1003 from dual union all
    select 1010    ,'LUCAS       ','JAKE    ','114 EAST SAVANNAH       ',30314,  null from dual union all 
    select 1011    ,'MCGOVERN    ','REESE   ','P.O. BOX 18             ',60606,   null from dual union all
    select 1012    ,'MCKENZIE    ','WILLIAM ','P.O. BOX 971            ',02110,   null from dual union all
    select 1013    ,'NGUYEN      ','NICHOLAS    ','357 WHITE EAGLE AVE ',34711,            1006 from dual union all
    select 1014    ,'LEE         ','JASMINE ','P.O. BOX 2947           ',82414,   null from dual union all
    select 1015    ,'SCHELL      ','STEVE   ','P.O. BOX 677            ',33111,   null from dual union all
    select 1016    ,'DAUM        ','MICHELL ',',9851231, LONG ROAD     ',91508,            1010 from dual union all
    select 1017    ,'NELSON      ','BECCA   ','P.O. BOX 563            ',49006,   null from dual union all
    select 1018    ,'MONTIASA    ','GREG    ','1008 GRAND AVENUE       ',31206,   null from dual union all
    select 1019    ,'SMITH       ','JENNIFER    ','P.O. BOX 1151       ',07962,            1003 from dual union all
    select 1020    ,'FALAH       ','KENNETH ','P.O. BOX 335            ',08607,  null from dual
    /* end: test data */
    -- replace the above block with your table
    -- eg. select * from customers_table
    )
    , 
    max_referred as 
    (
      -- just interested in the first row after sorting by
      -- the count of referred column values
      select rownum, cust_referred, cnt from 
      (
        select cust_referred, count(cust_referred) cnt from data group by cust_referred order by 2 desc
      ) 
      where rownum = 1
    )
    -- joining on cust_referred column in *data* and *max_referred* tables to get the customer details
    -- and joining again to the *data* table for fetching the referred customer name
    select 
      cust.cust_num, cust.name_s, cust.name_f, cust.addr, cust.code, cust.cust_referred, ms.name_f || ms.name_s as "Referred Customer"
    from 
      data cust
     join 
      max_referred mr on (cust.cust_referred = mr.cust_referred)
      join 
      data ms
      on (mr.cust_referred = ms.cust_num)
    ;
SELECT *
FROM   (
  SELECT t.*,
         MIN( CUST_REFERRED )
           KEEP ( DENSE_RANK FIRST ORDER BY num_referrals DESC )
           OVER ()
           AS best_referrer
  FROM   (
    SELECT c.*,
           COUNT( CUST_REFERRED )
             OVER ( PARTITION BY CUST_REFERRED )
             AS num_referrals
    FROM   CUSTOMER c
  ) t
)
WHERE  cust_num = best_referrer;