ORACLE SQL -基于匹配值的逻辑修改值

icomxhvb  于 2023-11-17  发布在  Oracle
关注(0)|答案(2)|浏览(155)

| 交易ID|仓库|公司简介|产品线类型|创建者|
| --|--|--|--|--|
| 57261018 |CA||收入||
| 57281538 |CA| 57281544 |资产| 57261018 |
| 57281544 |WY| 57281538 |INTERCOINCOME||
我得到错误“失败与无效的SQL语句.”我不知道为什么.我想在这里实现:
正如您所看到的,ACNOW_LINE_TYPEINCOME时,这将是本例中的主记录。但是,此记录的WAREHOUSE列的值不正确,我们希望显示记录中的值,其中ACNOW_LINE_TYPE = INTERCOINCOME,也就是**“WY”。因此,在本例中,我们希望将“CA”替换为“WY”。我假设,如果我们将这些列中的各种值相互匹配,就可以实现这一点。我认为我们可以这样做:当记录Accounts_LINE_TYPE = ASSET时,Accounts_LINE_TYPE = INCOME且其TRANSACTION_IDCREATED_FROM中的值匹配;当记录Accounts_LINE_TYPE = INTERCOINCOME时,则将INTERCOMPANY_ID中的值与TRANSACTION_ID匹配然后从WAREHOUSE中获取值“WY”,并粘贴到列WAREHOUSE**的记录中,在本例中,ACNOW_LINE_TYPE = INCOME。您能帮我解决这个问题吗?
我试过这个:

SELECT
    T.id TRANSACTION_ID,
    CASE
        WHEN TL.accountinglinetype = 'INCOME' THEN (
            SELECT
                LOC2.fullname
            FROM
                transaction T2
                LEFT JOIN transactionLine TL2 ON T2.id = TL2.transaction
                LEFT JOIN location LOC2 ON LOC2.id = TL2.location
            WHERE
                T2.id = (
                    SELECT
                        T3.intercoTransaction
                    FROM
                        transaction T3
                        LEFT JOIN transactionLine TL3 ON T3.id = TL3.transaction
                    WHERE
                        TL3.createdFrom = T.id
                        AND TL3.accountinglinetype = 'ASSET'
                )
                AND TL2.accountinglinetype = 'INTERCOINCOME'
        )
        ELSE LOC.fullname
    END AS WAREHOUSE,
    T.intercoTransaction INTERCOMPANY_ID,
    TL.accountinglinetype ACCOUNT_LINE_TYPE,
    TL.createdFrom CREATED_FROM
FROM
    transaction T
    LEFT JOIN transactionLine TL ON T.id = TL.transaction
    LEFT JOIN location LOC ON LOC.id = TL.location

字符串

7dl7o3gd

7dl7o3gd1#

您可以LEFT OUTER JOIN该表本身来查找资产的收入和公司间收入:

SELECT a.transaction_id,
       COALESCE(ii.warehouse, a.warehouse) AS warehouse,
       a.intercompany_id,
       a.account_line_type,
       a.created_from
FROM   your_data a
       LEFT OUTER JOIN your_data i
       ON (    a.account_line_type = 'ASSET'
           AND i.account_line_type = 'INCOME'
           AND i.transaction_id = a.created_from )
       LEFT OUTER JOIN your_data ii
       ON (    a.account_line_type = 'ASSET'
           AND ii.account_line_type = 'INTERCOINCOME'
           AND ii.transaction_id = a.intercompany_id
           AND i.transaction_id IS NOT NULL );

字符串
其中,对于样本数据:

CREATE TABLE your_data (TRANSACTION_ID, WAREHOUSE, INTERCOMPANY_ID, ACCOUNT_LINE_TYPE, CREATED_FROM) AS
SELECT 57261018, 'CA', NULL,     'INCOME',        NULL     FROM DUAL UNION ALL
SELECT 57281538, 'CA', 57281544, 'ASSET',         57261018 FROM DUAL UNION ALL
SELECT 57281544, 'WY', 57281538, 'INTERCOINCOME', NULL     FROM DUAL;


产出:
| 交易ID|仓库|公司简介|产品线类型|创建者|
| --|--|--|--|--|
| 57281538 |WY| 57281544 |资产| 57261018 |
| 57281544 |WY| 57281538 |INTERCOINCOME| * 空 *|
| 57261018 |CA| * 空 *| 收入| * 空 *|
fiddle

q5lcpyga

q5lcpyga2#

没有得到它在哪里把'WY'而不是'CA'的所有匹配或只是类型=资产或收入-所以我做了所有三个。你的真实的仓库是在最后3列的结果数据集(INTERCO_WH_ALL,INTERCO_WH_ASSET,INTERCO_WH_INCOME),你会采取一个符合你的预期结果。
示例数据和代码:

WITH    --  S a m p l e   D  a t a :
    tbl (TRANSACTION_ID,    WAREHOUSE,  INTERCOMPANY_ID,    ACCOUNT_LINE_TYPE,  CREATED_FROM) AS
        (   Select '57261018',  'CA', Null, 'INCOME', Null From Dual Union All
            Select '57281538',  'CA', '57281544', 'ASSET', '57261018' From Dual Union All
            Select '57281544',  'WY', '57281538', 'INTERCOINCOME', Null From Dual Union All
            --
            Select '57261010',  'CC', Null, 'INCOME', Null From Dual Union All
            Select '57281530',  'CC', '57281540', 'ASSET', '57261010' From Dual Union All
            Select '57281540',  'WZ', '57281530', 'INTERCOINCOME', Null From Dual
        ),

字符串
使用Case表达式和分析函数(Max()Over())的组合按虚拟列COMMON_ID对行进行分组。
... cte用于分组

interco AS    -- cte to prepare rows for grouping
    ( Select  t.TRANSACTION_ID, t.WAREHOUSE, t.INTERCOMPANY_ID, t.ACCOUNT_LINE_TYPE, t.CREATED_FROM,
              Case When t.ACCOUNT_LINE_TYPE = 'INTERCOINCOME' 
                   Then (Select CREATED_FROM From tbl Where TRANSACTION_ID = t.INTERCOMPANY_ID) 
              Else Max(t.CREATED_FROM) OVER(Partition By Nvl(t.CREATED_FROM, t.TRANSACTION_ID))
              End "COMMON_ID",
              Case When t.ACCOUNT_LINE_TYPE = 'INTERCOINCOME' Then t.WAREHOUSE End "INTERCO_WH" 
      From    tbl t
      Order By  t.TRANSACTION_ID
    )


M a i n S Q L:

SELECT    i.TRANSACTION_ID, i.WAREHOUSE, i.INTERCOMPANY_ID, i.ACCOUNT_LINE_TYPE, i.CREATED_FROM,
          i.COMMON_ID,
          Max(i.INTERCO_WH) OVER(Partition By i.COMMON_ID) "INTERCO_WH_ALL",
          Case  When i.ACCOUNT_LINE_TYPE IN('INTERCOINCOME', 'ASSET') 
                Then Max(i.INTERCO_WH) OVER(Partition By i.COMMON_ID) 
          End "INTERCO_WH_ASSET",
          Case  When i.ACCOUNT_LINE_TYPE IN('INTERCOINCOME', 'INCOME') 
                Then Max(i.INTERCO_WH) OVER(Partition By i.COMMON_ID) 
          End "INTERCO_WH_INCOME"
FROM      interco i
ORDER BY  i.COMMON_ID, i.TRANSACTION_ID


.结果如下:

/*
TRANSACTION_ID WAREHOUSE INTERCOMPANY_ID ACCOUNT_LINE_ CREATED_FROM COMMON_ID INTERCO_WH_ALL INTERCO_WH_ASSET INTERCO_WH_INCOME
-------------- --------- --------------- ------------- ------------ --------- -------------- ---------------- -----------------
57261010       CC                        INCOME                     57261010 WZ                               WZ
57281530       CC               57281540 ASSET         57261010     57261010 WZ              WZ
57281540       WZ               57281530 INTERCOINCOME              57261010 WZ              WZ               WZ

57261018       CA                        INCOME                     57261018 WY                               WZ
57281538       CA               57281544 ASSET         57261018     57261018 WY              WY
57281544       WY               57281538 INTERCOINCOME              57261018 WY              WY               WZ                */

相关问题