ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 有哪位高手能幫我优化一下該sql

有哪位高手能幫我优化一下該sql

原创 Linux操作系统 作者:hotwish 时间:2007-06-18 16:31:16 0 删除 编辑
sql 1
SELECT
                0                                        primary_quantity,
                0                                        transaction_quantity,
        (SUM(NVL(DECODE(TRUNC(MONTHS_BETWEEN(trunc(_as_of_date),TRUNC(MMT1.TRANSACTION_DATE))),
                        0,MMT1.SP_QTY,
                               1,MMT1.SP_QTY,
                               2,MMT1.SP_QTY,0),0)))         F1T3_QTY,
        (SUM(NVL(DECODE(TRUNC(MONTHS_BETWEEN(trunc(_as_of_date),TRUNC(MMT1.TRANSACTION_DATE))),
                        3,MMT1.SP_QTY,
                               4,MMT1.SP_QTY,
                        5,MMT1.SP_QTY,0),0)))         F4T6_QTY,
        (SUM(NVL(DECODE(TRUNC(MONTHS_BETWEEN(trunc(_as_of_date),TRUNC(MMT1.TRANSACTION_DATE))),
                        6,MMT1.SP_QTY,
                               7,MMT1.SP_QTY,
                               8,MMT1.SP_QTY,0),0)))         F7T9_QTY,
        (SUM(NVL(DECODE(TRUNC(MONTHS_BETWEEN(trunc(_as_of_date),TRUNC(MMT1.TRANSACTION_DATE))),
                        9,MMT1.SP_QTY,
                        10,MMT1.SP_QTY,
                        11,MMT1.SP_QTY,0),0)))         F10T12_QTY,
        (SUM(NVL(DECODE(TRUNC(MONTHS_BETWEEN(trunc(_as_of_date),TRUNC(MMT1.TRANSACTION_DATE))),
                        0,0,
                        1,0,
                        2,0,
                        3,0,
                        4,0,
                        5,0,
                        6,0,
                        7,0,
                        8,0,
                        9,0,
                        10,0,
                        11,0,MMT1.SP_QTY),0)))         OVR12_QTY,
        organization_id                                organization_id,
        inventory_item_id                                inventory_item_id,
        subinventory_code                                subinventory_code
        FROM
        (
                SELECT        MMT.ORGANIZATION_ID                ORGANIZATION_ID,
                        MMT.INVENTORY_ITEM_ID                INVENTORY_ITEM_ID,
                                                mmt.subinventory_code                subinventory_code,
                        TRUNC(MMT.TRANSACTION_DATE)        TRANSACTION_DATE,
                        SUM(DECODE(MMT.TRANSACTION_TYPE_ID,
                                                                                                                      2,MMT.PRIMARY_QUANTITY,
                                                              3,MMT.PRIMARY_QUANTITY,       
                                                              4,MMT.PRIMARY_QUANTITY,
                                                                                                                      8,MMT.PRIMARY_QUANTITY,
                                                              12,MMT.PRIMARY_QUANTITY,       
                                                                                                                      15,MMT.PRIMARY_QUANTITY,
                                                                                                                      17,MMT.PRIMARY_QUANTITY,
                                                                                                                      18,MMT.PRIMARY_QUANTITY,
                                                                                                                      36,MMT.PRIMARY_QUANTITY,
                                                                                                                      37,MMT.PRIMARY_QUANTITY,
                                                              38,MMT.PRIMARY_QUANTITY,
                                                                                                                      40,MMT.PRIMARY_QUANTITY,
                                                              41,MMT.PRIMARY_QUANTITY,
                                                                                                                      42,MMT.PRIMARY_QUANTITY,
                                                                                                                      44,MMT.PRIMARY_QUANTITY,
                                                                                                                      51,MMT.PRIMARY_QUANTITY,
                                                              70,MMT.PRIMARY_QUANTITY,
                                                                                                                      71,MMT.PRIMARY_QUANTITY,0))   SP_QTY
                FROM
                        MTL_MATERIAL_TRANSACTIONS MMT,
                        MTL_SYSTEM_ITEMS MSI
                WHERE         MMT.ORGANIZATION_ID = MSI.ORGANIZATION_ID
                AND         MMT.INVENTORY_ITEM_ID = MSI.INVENTORY_ITEM_ID
                AND         MMT.ORGANIZATION_ID = _org_id
                AND         TRUNC(MMT.TRANSACTION_DATE) <= trunc(_as_of_date)
                and         (mmt.transaction_type_id in (12,15,17,18,36,37,38,40,41,42,44,70,71)
                        or
                        (mmt.transaction_type_id in (2,3,4,8,51) and mmt.primary_quantity > 0))
                 &lp_item
                 GROUP BY
                         MMT.ORGANIZATION_ID,
                         MMT.INVENTORY_ITEM_ID,
                                                mmt.subinventory_code,
                        MMT.TRANSACTION_DATE
               
        ) MMT1       
        GROUP BY
                MMT1.ORGANIZATION_ID,
                MMT1.INVENTORY_ITEM_ID,
                                mmt1.subinventory_code
UNION
        SELECT        sum(mmt.primary_quantity)        primary_quantity,
                0                        transaction_quantity,
                0                        f1t3_qty,
                0                        f4t6_qty,
                0                        f7t9_qty,
                0                        f10t12_qty,
                0                        ovr12_qty,
                mmt.organization_id                organization_id,
                mmt.inventory_item_id        inventory_item_id,
                                mmt.subinventory_code            subinventory_code
        FROM
                mtl_material_transactions        mmt,
                mtl_system_items                msi       
        WHERE       
                                mmt.subinventory_code is not null       
        AND        mmt.inventory_item_id = msi.inventory_item_id
        AND        mmt.organization_id = msi.organization_id       
        AND        trunc(mmt.transaction_date) > trunc(_as_of_date)
        AND        mmt.organization_id = _org_id
        and         mmt.transaction_type_id not in (24,90)
        &lp_item       
        GROUP BY       
                mmt.organization_id,
                mmt.inventory_item_id,
                                mmt.subinventory_code
       
UNION
        SELECT        0                                primary_quantity,
                SUM(MOQ.TRANSACTION_QUANTITY)         TRANSACTION_QUANTITY,
                0                                f1t3_qty,
                0                                f4t6_qty,
                0                                f7t9_qty,
                0                                f10t12_qty,
                0                                ovr12_qty,
                MOQ.ORGANIZATION_ID                organization_id,
                MOQ.INVENTORY_ITEM_ID                inventory_item_id,
                                moq.subinventory_code                subinventory_code

        FROM
                MTL_ONHAND_QUANTITIES MOQ,
                MTL_SYSTEM_ITEMS MSI
        WHERE        MOQ.ORGANIZATION_ID = MSI.ORGANIZATION_ID
        AND         MOQ.INVENTORY_ITEM_ID = MSI.INVENTORY_ITEM_ID
        AND        MSI.ORGANIZATION_ID = _org_id
        &lp_item
        GROUP BY
                MOQ.ORGANIZATION_ID,
                MOQ.INVENTORY_ITEM_ID,
                                moq.subinventory_code

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2830/viewspace-479851/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-10-28

  • 博文量
    125
  • 访问量
    268865