从主查询中排除某些行

问题描述 投票:-2回答:1

我想每年从每个公司的查询中排除两个最大帐户,我使用pl-SQL编写查询,但是如何重写它以使其表现更好却很慢?

我的主表包括9195771行。

如果有人可以帮助我,我非常感谢

SELECT  
    g."COMPANY", g."ACCOUNTING_YEAR", SUM(g."AMOUNT")
FROM
    accounting_attribute_con a
INNER JOIN
    dana_gl_hold_vou_row_qry_dash g ON a.code_part_value = g."ACCOUNT" 
                                    AND g."COMPANY" = a.company
WHERE
    g."ACCOUNTING_YEAR" IN ('2019', '2018')
    AND g."COMPANY" IN ('DB','DD','DC','DG','DP','DO' )
    AND accounting_attribute_value_api.get_desc(a.company, attribute, ATTRIBUTE_VALUE) = 'others'
    AND g."ACCOUNTING_PERIOD" <> '13'
    AND g."ACCOUNTING_YEAR"||g."COMPANY"||g."ACCOUNT" NOT IN (SELECT FROM (SELECT SUM(g."AMOUNT"), g."ACCOUNTING_YEAR" acc_year, g."COMPANY" company, g."ACCOUNT_DESC", g."ACCOUNT" acc, RANK() OVER (partition by g."ACCOUNTING_YEAR", g."COMPANY"  ORDER BY SUM(g."AMOUNT") DESC) AS rnknumber
                                  FROM accounting_attribute_con a
                                  INNER JOIN dana_gl_hold_vou_row_qry_dash g ON a.code_part_value = g."ACCOUNT" AND g."COMPANY" = a.company
                                  WHERE g."ACCOUNTING_YEAR" IN ('2019', '2018')
                                    AND g."COMPANY" IN ('DB','DD','DE','DG','DP','DO' )
                                    AND accounting_attribute_value_api.get_desc(a.company, attribute, ATTRIBUTE_VALUE) = 'others'
                                    AND g."ACCOUNTING_PERIOD" <> '13'
                                  GROUP BY g."ACCOUNTING_YEAR" , g."COMPANY"  , g."ACCOUNT_DESC" , g."ACCOUNT") b
               WHERE 
                   rnknumber <= 2) 
GROUP BY
    g."COMPANY", g."ACCOUNTING_YEAR"
sql oracle performance group-by rank
1个回答
0
投票

如果可以在查询中定义最大帐户,则可以使用MINUS运算符从主查询中排除这些记录。

© www.soinside.com 2019 - 2024. All rights reserved.