oracle 按年份提取重复值,而不按SQL分组

wtlkbnrh  于 2023-05-16  发布在  Oracle
关注(0)|答案(2)|浏览(95)

我尝试提取所有可能的NUMB的重复值,这是一个例子
| 麻木|推荐_BALAN| FNT|关闭日期|持续时间|
| --------------|--------------|--------------|--------------|--------------|
| 9429410万|公司简介|E04| 2021年10月31日|十五岁|
| 9429410万|公司简介|E04| 2019 - 08 - 21 2019 - 08 - 21|二十|
因此我在OracleSQLDeveloper上构建了这个查询

SELECT ID_PPAL_NUMB AS NUMB,
ID_PPAL_FNT AS FNT,
BLNC_CLOSED_DATE AS CLOSED_DATE,
BLNC_DUR AS DURATION
FROM BLNC_BALANC

    INNER JOIN ID_PPAL
    ON BLNC_ID_PPAL_NUMB=ID_PPAL_NUMB

    WHERE BLNC_SIT='1'
    AND ID_PPAL_TIP_REG='1'
    AND ID_PPAL_IND_ESTAB='N'

GROUP BY (ID_PPAL_NUMB,ID_PPAL_FNT, BLNC_CLOSED_DATE, BLNC_DUR)
HAVING COUNT(EXTRACT(YEAR FROM BLNC_CLOSED_DATE)) > 1
ORDER BY BLNC_CLOSED_DATE DESC

我有42列,但是当我在下一个查询中添加BLNC_BAANC中的列“BLNC_TIP”时

SELECT ID_PPAL_NUMB AS NUMB,
BLNC_TIP AS TIP_BALAN,
ID_PPAL_FNT AS FNT,
BLNC_CLOSED_DATE AS CLOSED_DATE,
BLNC_DUR AS DURATION
FROM BLNC_BALANC

    INNER JOIN ID_PPAL
    ON BLNC_ID_PPAL_NUMB=ID_PPAL_NUMB

    WHERE BLNC_SIT='1'
    AND ID_PPAL_TIP_REG='1'
    AND ID_PPAL_IND_ESTAB='N'

GROUP BY (ID_PPAL_NUMB,ID_PPAL_FNT, BLNC_CLOSED_DATE, BLNC_DUR, BLNC_TIP)
HAVING COUNT(EXTRACT(YEAR FROM BLNC_CLOSED_DATE)) > 1
ORDER BY BLNC_CLOSED_DATE DESC

查询只给予了我6列,所以我尝试了多种方法来避免“Group by”代码,但我只有15 k列的整个数据。有什么帮助吗?

w8ntj3qf

w8ntj3qf1#

如果你不想使用GROUP BY,你可以使用COUNT解析函数:

SELECT numb,
       fnt,
       closed_date,
       duration,
       blnc_tip
FROM   (
  SELECT ID_PPAL_NUMB AS NUMB,
         ID_PPAL_FNT AS FNT,
         BLNC_CLOSED_DATE AS CLOSED_DATE,
         BLNC_DUR AS DURATION,
         BLNC_TIP,
         COUNT(*) OVER (
           PARTITION BY ID_PPAL_NUMB, EXTRACT(YEAR FROM BLNC_CLOSED_DATE)
         ) AS cnt
  FROM   BLNC_BALANC
         INNER JOIN ID_PPAL
         ON BLNC_ID_PPAL_NUMB = ID_PPAL_NUMB
  WHERE  BLNC_SIT          = '1'
  AND    ID_PPAL_TIP_REG   = '1'
  AND    ID_PPAL_IND_ESTAB = 'N'
  ORDER BY BLNC_CLOSED_DATE DESC
)
WHERE  cnt > 1  -- Move the `HAVING` clause to a `WHERE` filter in an outer query.

注1:
COUNT(EXTRACT(YEAR FROM BLNC_CLOSED_DATE))正在计算BLNC_CLOSED_DATE的年份不是NULL值的行数。它与COUNT(BLNC_CLOSED_DATE)相同(因为如果您有NULL日期值,则只会获得NULL年份)。它不统计每年的行数;如果需要,则需要在查询中使用COUNT(*)并将EXTRACT(YEAR FROM BLNC_CLOSED_DATE)添加到GROUP BY子句中。

eh57zj3b

eh57zj3b2#

看起来blnc_tip有许多不同的值,这导致了许多(行,而不是 * 列 *)。
你没有解释它代表什么(我们没有你的表),但是-你可以做的是以某种方式聚合该列的值。例如,如果它是一个NUMBER数据类型,你想得到 total,你会sum(blnc_tip)

SELECT id_ppal_numb AS numb,
         SUM (blnc_tip) AS tip_balan,        --> aggregate BLNC_TIP
         id_ppal_fnt AS fnt,
         blnc_closed_date AS closed_date,
         blnc_dur AS duration
    FROM blnc_balanc INNER JOIN id_ppal ON blnc_id_ppal_numb = id_ppal_numb
   WHERE     blnc_sit = '1'
         AND id_ppal_tip_reg = '1'
         AND id_ppal_ind_estab = 'N'
GROUP BY id_ppal_numb,
         id_ppal_fnt,
         blnc_closed_date,
         blnc_dur
  HAVING COUNT (EXTRACT (YEAR FROM blnc_closed_date)) > 1
ORDER BY blnc_closed_date DESC;

或者,您可能会对它的max值感到满意:

`max(blnc_tip)`

或者甚至 list 所有blnc_tip,它们都在group by子句中包含的所有其他值的组中:

listagg(blnc_tip, ', ') within group (order by null)

(note listagg限制为最多4000个字符)。
基本上,这取决于你想用blnc_tip做什么。

相关问题