在toad for oracle中,group by子句使用特定列而不是all

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

我有下面的查询,我只想分组,只为3列(主题_代码,类别_代码,assrotment_代码),因为这些是多个条目。
我知道我需要提供所有的列有选择,但我需要这些列在选择和不需要在组,否则输出将是不正确的。如何做到这一点,有没有其他方法可以做到这一点?
我可以提供一个快照为什么我需要这个?突出显示的是重复的附件,我想避免,只需要一个单一的记录。
请建议一下。

select rts.MERCH_DIV_NUM, min(rts.DEPT_NUM) DEPT_NUM, bc.subject_code, bc.category_code,
         rts.ASSORTMENT_CODE, rts.CURR_PEND_QTY, 
         rts.CURR_PEND_AMT, a.assortment_desc
  from rts_subcat_dept_pending rts
  inner join bn_assortment a on rts.assortment_code = a.assortment_code
  inner join bn_category bc on substr(rts.assortment_code, 0, 4) = bc.category_code      
  group by bc.subject_code, bc.category_code, rts.ASSORTMENT_CODE
vu8f3i0k

vu8f3i0k1#

使用Window函数,您可以避免对不在GROUP BY中的每个列使用聚合函数

WITH CTE aS (  select rts.MERCH_DIV_NUM,
  rts.DEPT_NUM,
  ROW_NUMBER() OVER ( PARTITION BY bc.subject_code, bc.category_code, rts.ASSORTMENT_CODE ORDER BY  rts.DEPT_NUM ASC) rn
        , bc.subject_code, bc.category_code,
         rts.ASSORTMENT_CODE, rts.CURR_PEND_QTY, 
         rts.CURR_PEND_AMT, a.assortment_desc
  from rts_subcat_dept_pending rts
  inner join bn_assortment a on rts.assortment_code = a.assortment_code
  inner join bn_category bc on substr(rts.assortment_code, 0, 4) = bc.category_code      
) SELECT MERCH_DIV_NUM,DEPT_NUM,subject_code,category_code,ASSORTMENT_CODECURR_PEND_QTY
,CURR_PEND_AMT,assortment_desc FROM CTE WHERE rn = 1
zzlelutf

zzlelutf2#

您似乎希望使用分析函数而不是聚合:

select rts.MERCH_DIV_NUM,
       min(rts.DEPT_NUM)
         OVER (PARTITION BY bc.subject_code, bc.category_code, rts.ASSORTMENT_CODE)
         AS DEPT_NUM,
       bc.subject_code,
       bc.category_code,
       rts.ASSORTMENT_CODE,
       rts.CURR_PEND_QTY, 
       rts.CURR_PEND_AMT,
       a.assortment_desc
from   rts_subcat_dept_pending rts
       inner join bn_assortment a
       on rts.assortment_code = a.assortment_code
       inner join bn_category bc
       on substr(rts.assortment_code, 0, 4) = bc.category_code

相关问题