根据列值识别常见行,并为SQL中的分组添加条件

时间:2018-08-27 17:04:07

标签: sql oracle grouping

我遇到了SQL问题。您能否在以下方面为我提供帮助: 我目前有一个SQL,可以根据各种表和串联值检索数据。 我必须通过检查部分串联值是否相同来限制选择的数据,并进行计算以确定是否应显示这组记录。

数据示例:

ID  Acct ID    Credit  Debit  Balance
1   10.1.2.3      -10      0       10
2   10.5.2.4        0     10       10
3   22.6.1.1        0    -15      -15
4   11.3.5.5       -5      0       -5
5   12.3.5.6       -2      0       -2
6   15.1.1.1       30      0       30
7   20.16.5.1       0      7        7

串联的要考虑的部分是第三个值。 (使用的分隔符:“。”)

  1. 对于ID 1和2,串联字段的第三个值是“ 2”,它满足第一个条件。

然后对于两行记录,贷方和借方金额之和为零,这满足第二个条件。因此,不应显示ID 1和2。

  1. 与ID 4、5和7相同,第3个串联值相同,即“ 5”,贷方和借方金额总计为零。不应在SQL查询中选择它们。

  2. ID 3和6具有相同的第三级串联值,但总和不为零,因此应显示这两行。

您能为我提供上述帮助吗?请注意,我已经知道如何仅检索串联字段的第3个值。

非常感谢您。

回复: 您好,非常感谢,它似乎可以为您提供的代码起作用,但是我仍然无法使其与我的SQL一起使用。

当前的SQL:

SELECT Hca.ACCOUNT_NAME "Customer Name"
    ,hca.ACCOUNT_NUMBER "Account Number"
    ,AR_TRX_H.TRX_NUMBER "Invoice Number/Credit Memo"
    ,TO_CHAR(AR_TRX_GL.GL_DATE, 'DD/MM/YYYY') "Accounting Date"
    ,GLCC.SEGMENT1 || '.' || GLCC.SEGMENT2 || '.' || GLCC.SEGMENT3 || '.' || GLCC.SEGMENT4 || '.' || GLCC.SEGMENT5 || '.' || GLCC.SEGMENT6 || '.' || GLCC.SEGMENT7 || '.' || GLCC.SEGMENT8 || '.' || GLCC.SEGMENT9 AS "Concatenated Segments"
    ,XLA_L.ACCOUNTED_DR
    ,XLA_L.ACCOUNTED_CR
    ,
    --XLA_L.ACCOUNTED_CR-XLA_L.ACCOUNTED_DR "Balances"
    decode(XLA_L.ACCOUNTED_CR, NULL, 0, XLA_L.ACCOUNTED_CR) - decode(XLA_L.ACCOUNTED_DR, NULL, 0, XLA_L.ACCOUNTED_DR) "Balances"
    ,
FROM hz_cust_accounts hca
    ,RA_CUSTOMER_TRX_ALL AR_TRX_H
    ,RA_CUST_TRX_LINE_GL_DIST_ALL AR_TRX_GL
    ,GL_CODE_COMBINATIONS GLCC
    ,XLA_AE_HEADERS XLA_H
    ,XLA_AE_LINES XLA_L
WHERE 1 = 1
    AND HCA.CUST_ACCOUNT_ID = AR_TRX_H.BILL_TO_CUSTOMER_ID
    AND AR_TRX_H.CUSTOMER_TRX_ID = AR_TRX_GL.CUSTOMER_TRX_ID
    AND AR_TRX_GL.ACCOUNT_CLASS = 'REV'
    AND AR_TRX_GL.CODE_COMBINATION_ID = GLCC.CODE_COMBINATION_ID
    AND AR_TRX_GL.EVENT_ID = XLA_H.EVENT_ID
    AND AR_TRX_GL.SET_OF_BOOKS_ID = XLA_H.LEDGER_ID
    AND XLA_H.AE_HEADER_ID = XLA_L.AE_HEADER_ID
    AND XLA_L.CODE_COMBINATION_ID = AR_TRX_GL.CODE_COMBINATION_ID
    AND GLCC.SEGMENT2 LIKE '419%'
    AND GLCC.SEGMENT1 || '.' || GLCC.SEGMENT2 || '.' || GLCC.SEGMENT3 || '.' || GLCC.SEGMENT4 || '.' || GLCC.SEGMENT5 || '.' || GLCC.SEGMENT6 || '.' || GLCC.SEGMENT7 || '.' || GLCC.SEGMENT8 || '.' || GLCC.SEGMENT9 IN (
        SELECT GLCC.SEGMENT1 || '.' || GLCC.SEGMENT2 || '.' || GLCC.SEGMENT3 || '.' || GLCC.SEGMENT4 || '.' || GLCC.SEGMENT5 || '.' || GLCC.SEGMENT6 || '.' || GLCC.SEGMENT7 || '.' || GLCC.SEGMENT8 || '.' || GLCC.SEGMENT9 AS "Concatenated"
        FROM hz_cust_accounts hca1
            ,RA_CUSTOMER_TRX_ALL AR_TRX_H
            ,RA_CUST_TRX_LINE_GL_DIST_ALL AR_TRX_GL
            ,GL_CODE_COMBINATIONS GLCC
            ,XLA_AE_HEADERS XLA_H
            ,XLA_AE_LINES XLA_L
        WHERE 1 = 1
            AND HCA1.CUST_ACCOUNT_ID = AR_TRX_H.BILL_TO_CUSTOMER_ID
            AND AR_TRX_H.CUSTOMER_TRX_ID = AR_TRX_GL.CUSTOMER_TRX_ID
            AND AR_TRX_GL.ACCOUNT_CLASS = 'REV'
            AND AR_TRX_GL.CODE_COMBINATION_ID = GLCC.CODE_COMBINATION_ID
            AND AR_TRX_GL.EVENT_ID = XLA_H.EVENT_ID
            AND AR_TRX_GL.SET_OF_BOOKS_ID = XLA_H.LEDGER_ID
            AND XLA_H.AE_HEADER_ID = XLA_L.AE_HEADER_ID
            AND XLA_L.CODE_COMBINATION_ID = AR_TRX_GL.CODE_COMBINATION_ID
            AND GLCC.SEGMENT2 LIKE '419%'
        GROUP BY GLCC.SEGMENT1 || '.' || GLCC.SEGMENT2 || '.' || GLCC.SEGMENT3 || '.' || GLCC.SEGMENT4 || '.' || GLCC.SEGMENT5 || '.' || GLCC.SEGMENT6 || '.' || GLCC.SEGMENT7 || '.' || GLCC.SEGMENT8 || '.' || GLCC.SEGMENT9
        )

请注意,这是Segment6,这是从串联值中使用的值。

尝试添加您提供的逻辑后:

WITH base_data
AS (
    SELECT Hca.ACCOUNT_NAME "Customer Name"
        ,hca.ACCOUNT_NUMBER "Account Number"
        ,AR_TRX_H.TRX_NUMBER "Invoice Number/Credit Memo"
        ,TO_CHAR(AR_TRX_GL.GL_DATE, 'DD/MM/YYYY') "Accounting Date"
        ,GLCC.SEGMENT1 || '.' || GLCC.SEGMENT2 || '.' || GLCC.SEGMENT3 || '.' || GLCC.SEGMENT4 || '.' || GLCC.SEGMENT5 || '.' || GLCC.SEGMENT6 || '.' || GLCC.SEGMENT7 || '.' || GLCC.SEGMENT8 || '.' || GLCC.SEGMENT9 AS "Concatenated Segments"
        ,XLA_L.ACCOUNTED_DR AS CREDIT
        ,XLA_L.ACCOUNTED_CR AS DEBIT
        ,decode(XLA_L.ACCOUNTED_CR, NULL, 0, XLA_L.ACCOUNTED_CR) - decode(XLA_L.ACCOUNTED_DR, NULL, 0, XLA_L.ACCOUNTED_DR) "Balances"
        ,
        --Segment 6 is the value to group in order to check the amount
        GLCC.SEGMENT6 AS segment6
    FROM hz_cust_accounts hca
        ,RA_CUSTOMER_TRX_ALL AR_TRX_H
        ,RA_CUST_TRX_LINE_GL_DIST_ALL AR_TRX_GL
        ,GL_CODE_COMBINATIONS GLCC
        ,XLA_AE_HEADERS XLA_H
        ,XLA_AE_LINES XLA_L DUAL

    UNION ALL
    )
SELECT *
FROM (
    SELECT A.*
        ,sum(A.DEBIT + A.CREDIT) OVER (PARTITION BY A.segment6) AS extr_ID_balance
    FROM base_data A
    )
WHERE extr_ID_balance <> 0
    AND HCA.CUST_ACCOUNT_ID = AR_TRX_H.BILL_TO_CUSTOMER_ID
    AND AR_TRX_H.CUSTOMER_TRX_ID = AR_TRX_GL.CUSTOMER_TRX_ID
    AND AR_TRX_GL.ACCOUNT_CLASS = 'REV'
    AND AR_TRX_GL.CODE_COMBINATION_ID = GLCC.CODE_COMBINATION_ID
    AND AR_TRX_GL.EVENT_ID = XLA_H.EVENT_ID
    AND AR_TRX_GL.SET_OF_BOOKS_ID = XLA_H.LEDGER_ID
    AND XLA_H.AE_HEADER_ID = XLA_L.AE_HEADER_ID
    AND XLA_L.CODE_COMBINATION_ID = AR_TRX_GL.CODE_COMBINATION_ID
    AND GLCC.SEGMENT2 LIKE '419%'
    AND GLCC.SEGMENT1 || '.' || GLCC.SEGMENT2 || '.' || GLCC.SEGMENT3 || '.' || GLCC.SEGMENT4 || '.' || GLCC.SEGMENT5 || '.' || GLCC.SEGMENT6 || '.' || GLCC.SEGMENT7 || '.' || GLCC.SEGMENT8 || '.' || GLCC.SEGMENT9 IN (
        SELECT GLCC.SEGMENT1 || '.' || GLCC.SEGMENT2 || '.' || GLCC.SEGMENT3 || '.' || GLCC.SEGMENT4 || '.' || GLCC.SEGMENT5 || '.' || GLCC.SEGMENT6 || '.' || GLCC.SEGMENT7 || '.' || GLCC.SEGMENT8 || '.' || GLCC.SEGMENT9 AS "Concatenated"
        FROM hz_cust_accounts hca1
            ,RA_CUSTOMER_TRX_ALL AR_TRX_H
            ,RA_CUST_TRX_LINE_GL_DIST_ALL AR_TRX_GL
            ,GL_CODE_COMBINATIONS GLCC
            ,XLA_AE_HEADERS XLA_H
            ,XLA_AE_LINES XLA_L
        WHERE 1 = 1
            AND HCA1.CUST_ACCOUNT_ID = AR_TRX_H.BILL_TO_CUSTOMER_ID
            AND AR_TRX_H.CUSTOMER_TRX_ID = AR_TRX_GL.CUSTOMER_TRX_ID
            AND AR_TRX_GL.ACCOUNT_CLASS = 'REV'
            AND AR_TRX_GL.CODE_COMBINATION_ID = GLCC.CODE_COMBINATION_ID
            AND AR_TRX_GL.EVENT_ID = XLA_H.EVENT_ID
            AND AR_TRX_GL.SET_OF_BOOKS_ID = XLA_H.LEDGER_ID
            AND XLA_H.AE_HEADER_ID = XLA_L.AE_HEADER_ID
            AND XLA_L.CODE_COMBINATION_ID = AR_TRX_GL.CODE_COMBINATION_ID
            AND GLCC.SEGMENT2 LIKE '419%'
        GROUP BY GLCC.SEGMENT1 || '.' || GLCC.SEGMENT2 || '.' || GLCC.SEGMENT3 || '.' || GLCC.SEGMENT4 || '.' || GLCC.SEGMENT5 || '.' || GLCC.SEGMENT6 || '.' || GLCC.SEGMENT7 || '.' || GLCC.SEGMENT8 || '.' || GLCC.SEGMENT9
        )

如果能帮助您完成上述工作,将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:1)

这是一个您可以使用分析功能的地方-请参阅下面的“上方(分隔)”。因为您说过您知道如何从字符串中提取ID,所以未显示。

WITH base_data as
(
SELECT 1 AS ID,  '10.1.2.3' AS acct_ID, 2 as extr_ID, -10 AS CREDIT, 0 AS DEBIT, 10 AS balance FROM DUAL UNION ALL
SELECT 2,        '10.5.2.4',            2,              0,          10,          10 FROM DUAL UNION ALL
SELECT 3,        '22.6.1.1',            1,              0,         -15,         -15 FROM DUAL UNION ALL
SELECT 4,        '11.3.5.5',            5,             -5,           0,          -5 FROM DUAL UNION ALL
SELECT 5,        '12.3.5.6',            5,             -2,           0,          -2 FROM DUAL UNION ALL
SELECT 6,        '15.1.1.1',            1,             30,           0,          30 FROM DUAL UNION ALL
SELECT 7,        '20.16.5.1',           5,              0,           7,           7 FROM DUAL)


select * 
from 
  (
  select A.*,
     sum(A.DEBIT + A.CREDIT) over (partition by extr_id) as extr_ID_balance
  from base_data A)

where extr_ID_balance <> 0

order by 1

结果:

ID  ACCT_ID EXTR_ID CREDIT  DEBIT   BALANCE EXTR_ID_BALANCE
3   22.6.1.1    1   0       -15     -15     15
6   15.1.1.1    1   30       0       30     15

更新以解决以后提交的查询。

1-您的“当前查询”不正确-选择列表中有悬挂的逗号。猜错了。

2-我没有您的数据库-示例查询中的“ with base_data”只是根据提供的示例数据构建表。

无论如何,您可能想要这样的东西:

WITH base_qry
AS (
    SELECT Hca.ACCOUNT_NAME "Customer Name"
        ,hca.ACCOUNT_NUMBER "Account Number"
        ,AR_TRX_H.TRX_NUMBER "Invoice Number/Credit Memo"
        ,TO_CHAR(AR_TRX_GL.GL_DATE, 'DD/MM/YYYY') "Accounting Date"
        ,GLCC.SEGMENT1 || '.' || GLCC.SEGMENT2 || '.' || GLCC.SEGMENT3 || '.' || GLCC.SEGMENT4 || '.' || GLCC.SEGMENT5 || '.' || GLCC.SEGMENT6 || '.' || GLCC.SEGMENT7 || '.' || GLCC.SEGMENT8 || '.' || GLCC.SEGMENT9 AS "Concatenated Segments"
        ,XLA_L.ACCOUNTED_DR
        ,XLA_L.ACCOUNTED_CR
        ,
        --XLA_L.ACCOUNTED_CR-XLA_L.ACCOUNTED_DR "Balances"
        decode(XLA_L.ACCOUNTED_CR, NULL, 0, XLA_L.ACCOUNTED_CR) - decode(XLA_L.ACCOUNTED_DR, NULL, 0, XLA_L.ACCOUNTED_DR) "Balances"
        --sum debits and credits over segment_6 here
        ,sum(XLA_L.ACCOUNTED_DR + XLA_L.ACCOUNTED_CR) OVER (PARTITION BY GLCC.SEGMENT6) AS extr_ID_balance
    FROM hz_cust_accounts hca
        ,RA_CUSTOMER_TRX_ALL AR_TRX_H
        ,RA_CUST_TRX_LINE_GL_DIST_ALL AR_TRX_GL
        ,GL_CODE_COMBINATIONS GLCC
        ,XLA_AE_HEADERS XLA_H
        ,XLA_AE_LINES XLA_L
    WHERE 1 = 1
        AND HCA.CUST_ACCOUNT_ID = AR_TRX_H.BILL_TO_CUSTOMER_ID
        AND AR_TRX_H.CUSTOMER_TRX_ID = AR_TRX_GL.CUSTOMER_TRX_ID
        AND AR_TRX_GL.ACCOUNT_CLASS = 'REV'
        AND AR_TRX_GL.CODE_COMBINATION_ID = GLCC.CODE_COMBINATION_ID
        AND AR_TRX_GL.EVENT_ID = XLA_H.EVENT_ID
        AND AR_TRX_GL.SET_OF_BOOKS_ID = XLA_H.LEDGER_ID
        AND XLA_H.AE_HEADER_ID = XLA_L.AE_HEADER_ID
        AND XLA_L.CODE_COMBINATION_ID = AR_TRX_GL.CODE_COMBINATION_ID
        AND GLCC.SEGMENT2 LIKE '419%'
        AND GLCC.SEGMENT1 || '.' || GLCC.SEGMENT2 || '.' || GLCC.SEGMENT3 || '.' || GLCC.SEGMENT4 || '.' || GLCC.SEGMENT5 || '.' || GLCC.SEGMENT6 || '.' || GLCC.SEGMENT7 || '.' || GLCC.SEGMENT8 || '.' || GLCC.SEGMENT9 IN (
            SELECT GLCC.SEGMENT1 || '.' || GLCC.SEGMENT2 || '.' || GLCC.SEGMENT3 || '.' || GLCC.SEGMENT4 || '.' || GLCC.SEGMENT5 || '.' || GLCC.SEGMENT6 || '.' || GLCC.SEGMENT7 || '.' || GLCC.SEGMENT8 || '.' || GLCC.SEGMENT9 AS "Concatenated"
            FROM hz_cust_accounts hca1
                ,RA_CUSTOMER_TRX_ALL AR_TRX_H
                ,RA_CUST_TRX_LINE_GL_DIST_ALL AR_TRX_GL
                ,GL_CODE_COMBINATIONS GLCC
                ,XLA_AE_HEADERS XLA_H
                ,XLA_AE_LINES XLA_L
            WHERE 1 = 1
                AND HCA1.CUST_ACCOUNT_ID = AR_TRX_H.BILL_TO_CUSTOMER_ID
                AND AR_TRX_H.CUSTOMER_TRX_ID = AR_TRX_GL.CUSTOMER_TRX_ID
                AND AR_TRX_GL.ACCOUNT_CLASS = 'REV'
                AND AR_TRX_GL.CODE_COMBINATION_ID = GLCC.CODE_COMBINATION_ID
                AND AR_TRX_GL.EVENT_ID = XLA_H.EVENT_ID
                AND AR_TRX_GL.SET_OF_BOOKS_ID = XLA_H.LEDGER_ID
                AND XLA_H.AE_HEADER_ID = XLA_L.AE_HEADER_ID
                AND XLA_L.CODE_COMBINATION_ID = AR_TRX_GL.CODE_COMBINATION_ID
                AND GLCC.SEGMENT2 LIKE '419%'
            GROUP BY GLCC.SEGMENT1 || '.' || GLCC.SEGMENT2 || '.' || GLCC.SEGMENT3 || '.' || GLCC.SEGMENT4 || '.' || GLCC.SEGMENT5 || '.' || GLCC.SEGMENT6 || '.' || GLCC.SEGMENT7 || '.' || GLCC.SEGMENT8 || '.' || GLCC.SEGMENT9
            )
    )
SELECT *
FROM base_qry
--filter to segment 6 where debit+credit sum <> 0
WHERE extr_ID_balance <> 0

因为我没有您的数据库,所以没有运行它-您可能需要调试,处理null等。