min函数返回多行

时间:2017-05-16 19:39:26

标签: sql oracle min

我使用以下查询,但仍会返回按句点分组的行。如果我删除任何group by字段,它将表示不是按功能分组。 我一直在挖掘分析函数,但它仍会返回多行,如果它可以工作的话。 谢谢!

select 
       AB1.ID,
       AB1.NAME,
       AB1.ACCOUNT_BALANCE,
       AB1.DELINQUENCY,
       AB1.DELINQUENCY_DESC,
       min(AB1.PERIOD) as oldest_per_owed,
       AB1.bal_sum

from
(select RA.ID,
       RA.NAME,
       RA.ACCOUNT_BALANCE,
       RA.DELINQUENCY,
       RA.DELINQUENCY_DESC,
       RAD.PERIOD,
        sum(RAD.BALANCE) as bal_sum
  from RAD RAD
       RA RA
 where ( RA.ID = RAD.ID )
   and ( RA.DELINQUENCY in('02','03','05'))
   and (RA.BALANCE >0
        or
        RAD.BALANCE <0)
group by
RECEIVABLE_ACCOUNT.ID,
       RA.NAME,
       RA.ACCOUNT_BALANCE,
       RA.DELINQUENCY,
       RA.DELINQUENCY_DESC,
       RAD.PERIOD
order by RA.NAME)  AB1

group by
       AB1.ID,
       AB1.NAME,
       AB1.ACCOUNT_BALANCE,
       AB1.DELINQUENCY,
       AB1.DELINQUENCY_DESC,
       --AB1.PERIOD,
       AB1.bal_sum      
Order by 2,6 asc

以下是我的结果:

|ID |NAME   |ACCOUNT_BALANCE |DELINQUENCY   |DELINQUENCY_DESC   |OLDEST_TERM    |BAL_SUM
---     ---     ---              ---            ---                     ---             ---
|111    |Tom    |500             |AA            |D1                 |201010     |500
|222    |Dick   |501             |AA            |D1                 |201110     |250
|222    |Dick   |501             |AA            |D1                 |201190     |251
|333    |Harry  |600             |BB            |D2                 |201690     |600
|444    |Jane   |601             |AA            |D1                 |200510     |101
|444    |Jane   |601             |AA            |D1                 |200690     |500
|555    |Sally  |700             |AA            |D1                 |200890     |340
|555    |Sally  |700             |AA            |D1                 |200910     |360

2 个答案:

答案 0 :(得分:1)

您正在按bal_sum分组,该子句在子查询中按句点分组。将Select语句更改为:

select 
       AB1.ID,
       AB1.NAME,
       AB1.ACCOUNT_BALANCE,
       AB1.DELINQUENCY,
       AB1.DELINQUENCY_DESC,
       min(AB1.PERIOD) as oldest_per_owed,
       sum(AB1.bal_sum) as bal_sum

从最后的Group By中删除bal_sum

答案 1 :(得分:0)

如果我正确理解了您的目标,您希望每个拖欠帐户id占一行,其中包含该帐户的最早期限,并且您希望包含该期间的bal_sum (只)。

如果是这种情况,也许KEEP可以满足您的需求。

我猜测RARAD中的数据是什么,但这是一个例子:

创建并加载测试表(一些RAD记录有多个RA记录,一个新用户Benedetto没有拖欠,预计不会出现在结果中):

CREATE TABLE RAD (
  ID      NUMBER,
  "PERIOD" NUMBER,
  BALANCE NUMBER
);

INSERT INTO RAD VALUES (111, 201010, 500);
INSERT INTO RAD VALUES (222, 201010, 120);
INSERT INTO RAD VALUES (222, 201010, 130);
INSERT INTO RAD VALUES (222, 201190, 101);
INSERT INTO RAD VALUES (222, 201190, 150);
INSERT INTO RAD VALUES (333, 201690, 600);
INSERT INTO RAD VALUES (444, 200510, 101);
INSERT INTO RAD VALUES (444, 200690, 500);
INSERT INTO RAD VALUES (555, 200890, 340);
INSERT INTO RAD VALUES (555, 200910, 360);
INSERT INTO RAD VALUES (666, 200911, 200);


CREATE TABLE RA (
  ID               NUMBER,
  NAME             VARCHAR2(256),
  ACCOUNT_BALANCE  NUMBER,
  DELINQUENCY      VARCHAR2(3),
  DELINQUENCY_DESC VARCHAR2(256),
  BALANCE          NUMBER
);

INSERT INTO RA VALUES (111, 'Tom',500,'02','D2',1);
INSERT INTO RA VALUES (222, 'Dick',501,'03','D3',1);
INSERT INTO RA VALUES (333, 'Harry',600,'05','D5',1);
INSERT INTO RA VALUES (444, 'Jane',600,'02','D2',1);
INSERT INTO RA VALUES (555, 'Sally',600,'03','D3',1);
INSERT INTO RA VALUES (666, 'Benedetto',23,'03','D3',-20);

运行原始查询(将RECEIVABLE_ACCOUNT.ID更改为RA.ID等)会显示类似于示例中的八行的内容:

SELECT
  AB1.ID,
  AB1.NAME,
  AB1.ACCOUNT_BALANCE,
  AB1.DELINQUENCY,
  AB1.DELINQUENCY_DESC,
  min(AB1.PERIOD) AS OLDEST_PER_OWED,
  AB1.BAL_SUM
FROM
  (SELECT
     RA.ID,
     RA.NAME,
     RA.ACCOUNT_BALANCE,
     RA.DELINQUENCY,
     RA.DELINQUENCY_DESC,
     RAD.PERIOD,
     sum(RAD.BALANCE) AS BAL_SUM
   FROM RAD RAD,
     RA RA
   WHERE (RA.ID = RAD.ID)
         AND (RA.DELINQUENCY IN ('02', '03', '05'))
         AND (RA.BALANCE > 0
              OR
              RAD.BALANCE < 0)
   GROUP BY
     RA.ID,
     RA.NAME,
     RA.ACCOUNT_BALANCE,
     RA.DELINQUENCY,
     RA.DELINQUENCY_DESC,
     RAD.PERIOD
   ORDER BY RA.NAME) AB1
GROUP BY
  AB1.ID,
  AB1.NAME,
  AB1.ACCOUNT_BALANCE,
  AB1.DELINQUENCY,
  AB1.DELINQUENCY_DESC,
  AB1.BAL_SUM
ORDER BY 1, 6 ASC;

结果:

ID   NAME   ACCOUNT_BALANCE  DELINQUENCY  DELINQUENCY_DESC  OLDEST_PER_OWED  BAL_SUM  
111  Tom    500              02           D2                201010           500      
222  Dick   501              03           D3                201010           250      
222  Dick   501              03           D3                201190           251      
333  Harry  600              05           D5                201690           600      
444  Jane   600              02           D2                200510           101      
444  Jane   600              02           D2                200690           500      
555  Sally  600              03           D3                200890           340      
555  Sally  600              03           D3                200910           360      

要仅获取最早的periodbal_sum,我们可以应用KEEP。这将获取最早的period并为其提取相应的bal_sum,如果存在平局(最长bal_sum多个period,它将解析领先于MIN。从这些数据来看,我不希望任何聚合解决方案是必要的。)

如果我们将查询更改为KEEP,我们现在每ID只能获得一行。此行包含最早的period和该期间的bal_sum。例如,Jane的最早期间是200510,bal_sum为101。

SELECT
  AB1.ID,
  AB1.NAME,
  AB1.ACCOUNT_BALANCE,
  AB1.DELINQUENCY,
  AB1.DELINQUENCY_DESC,
  min(AB1.PERIOD) AS OLDEST_PER_OWED,
  MIN(AB1.BAL_SUM) KEEP (DENSE_RANK FIRST ORDER BY AB1.PERIOD ASC) AS OLDEST_PER_BAL_SUM
FROM
  (SELECT
     RA.ID,
     RA.NAME,
     RA.ACCOUNT_BALANCE,
     RA.DELINQUENCY,
     RA.DELINQUENCY_DESC,
     RAD.PERIOD,
     sum(RAD.BALANCE) AS BAL_SUM
   FROM RAD RAD,
     RA RA
   WHERE (RA.ID = RAD.ID)
         AND (RA.DELINQUENCY IN ('02', '03', '05'))
         AND (RA.BALANCE > 0
              OR
              RAD.BALANCE < 0)
   GROUP BY
     RA.ID,
     RA.NAME,
     RA.ACCOUNT_BALANCE,
     RA.DELINQUENCY,
     RA.DELINQUENCY_DESC,
     RAD.PERIOD
   ORDER BY RA.NAME) AB1
GROUP BY
  AB1.ID,
  AB1.NAME,
  AB1.ACCOUNT_BALANCE,
  AB1.DELINQUENCY,
  AB1.DELINQUENCY_DESC
ORDER BY 1, 6 ASC;

结果:

ID   NAME   ACCOUNT_BALANCE  DELINQUENCY  DELINQUENCY_DESC  OLDEST_PER_OWED  OLDEST_PER_BAL_SUM  
111  Tom    500              02           D2                201010           500                 
222  Dick   501              03           D3                201010           250                 
333  Harry  600              05           D5                201690           600                 
444  Jane   600              02           D2                200510           101                 
555  Sally  600              03           D3                200890           340