尝试从物化视图中的另一列计算SUM

时间:2018-10-18 07:48:38

标签: sql oracle materialized-views

我正在尝试计算Oracle MV中每月的工作日总数

这是我的要求:

CREATE MATERIALIZED VIEW DIM_DATE_MV
BUILD IMMEDIATE
REFRESH COMPLETE ON DEMAND
START WITH sysdate NEXT (TRUNC(sysdate)+1)  + 7 / 24
as SELECT
CAL.DATE_D as ID_DATE,
(CASE WHEN (
    (TRIM(TO_CHAR(CAL.DATE_D,'Day','nls_date_language=english')) IN ('Saturday','Sunday')) OR 
    (TRIM(TO_CHAR(CAL.DATE_D,'DD-MM')) IN ('01-01', '01-05', '08-05', '14-07', '15-08', '01-11', '11-11', '25-12')) OR 
    (TO_CHAR(CAL.DATE_D, 'DD-MM-YYYY') IN  (SELECT TO_CHAR(DOFF.DATE_OFF, 'DD-MM-YYYY') FROM ODSISIC.DAY_OFF DOFF where DOFF.IMPACT='ALL'))
           )  THEN 0 ELSE 1 END) as IS_WORKING_DAY,


(CASE WHEN TO_CHAR(CAL.DATE_D , 'YYYY-MM') = TO_CHAR(CAL.DATE_D , 'YYYY-MM') THEN (Select SUM(IS_WORKING_DAY) from DIM_DATE_MV group by CAL.YEAR_MONTH_NUM) ELSE 0 END)
 as NB_WORKING_DAY_MONTH

FROM ODSISIC.ORACLE_CALENDAR CAL
LEFT JOIN ODSISIC.DAY_OFF DOFF
ON DOFF.DATE_OFF = CAL.DATE_D

IS_WORKING_DAY = 0,如果它是表DATE_OFF中的“假期”,“周末”或“日期”,则该表包含所有年份的年份不同的假期。

我希望NB_WORKING_DAY_MONTH中IS_WORKING_DAY = 1的月份的总和。

如何直接在查询中计算此SUM,而不是为与DAY_OFF表的联接创建中间表?

谢谢:)

1 个答案:

答案 0 :(得分:0)

明智地思考后,我通过重做SQL查询来解决此问题:

{
    "payment_id": "1",
    "type": "post",
    "title": "Create Order",
    "request": {
        "headers": {
            "Content-type": "application/json",
            "Accept": "application/json",
            "Authorization": "string"
        },
        "method": "POST",
        "body": "{
            "order": {
                "items": [{
                    "reference": "8",
                    "name": "Test Product",
                    "quantity": 2,
                    "unit": "pcs",
                    "unitPrice": 12500,
                    "taxRate": 2500,
                    "taxAmount": 5000,
                    "grossTotalAmount": 25000,
                    "netTotalAmount": 20000
                }, {
                    "reference": "test",
                    "name": "Test",
                    "quantity": 1,
                    "unit": "pcs",
                    "unitPrice": 20000,
                    "taxRate": 2500,
                    "taxAmount": 5000,
                    "grossTotalAmount": 25000,
                    "netTotalAmount": 15000
                }, {
                    "reference": "1",
                    "name": "Flat rate",
                    "quantity": 1,
                    "unit": "pcs",
                    "unitPrice": 6125,
                    "taxRate": 2500,
                    "taxAmount": 1225,
                    "grossTotalAmount": 6125,
                    "netTotalAmount": 4900
                }],
                "amount": 56125,
                "currency": "SEK",
                "shipping": {
                    "costSpecified": true
                }
            },
            "checkout": {
                "url": "",
                "termsUrl": "",
                "shipping": {
                    "countries": [],
                    "merchantHandlesShippingCost": true
                },
                "consumerType": {
                    "supportedTypes": ["B2C"]
                }
            },
            "notifications": {
                "webHooks": [{
                    "eventName": "payment.reservation.created",
                    "url": "",
                    "authorization": ""
                }]
            }
        }"
    },
    "response": {
        "headers": {},
        "body": {
            "errors": {
                "notifications.WebHooks[0].Url": ["Field is not accepted as a valid URL"]
            }
        },
        "response": {
            "code": 400,
            "message": "Bad Request"
        },
        "cookies": [{
            "name": "visid_incap_1152497",
            "value": "2qY5gE/QT6W4CmOH1cLUuig yFsAAAAAQUIPAAAAAABsjnmr3oFW5wXzhutW3EWq",
            "expires": 1571353603,
            "path": "/",
            "domain": "api.dibspayment.eu"
        }, {
            "name": "incap_ses_273_1152497",
            "value": "hXtkOPG/TwEHQCnMhuTJAyg yFsAAAAALrqG9pupkT/7iEMdOohRTw==",
            "expires": null,
            "path": "/",
            "domain": "api.dibspayment.eu"
        }],
        "filename": null,
        "http_response": {
            "data": null,
            "headers": null,
            "status": null
        }
    }
}

我在创建视图之前计算了工作日(这意味着必须在ORACLE_CALENDAR之前输入我的表DATE_OFF)

我添加了一个联接以根据id_month填充表。

现在可以正常工作