如何在视图表中包含group by

时间:2013-10-18 18:55:05

标签: sql oracle

我有以下脚本。

drop view proba;
create view proba as
select distinct oper as p_name
, sum(duration) as out_duration 
from TA_OUT 
where oper in ('CZE','FRA')
and timestamp like '201306%' 
group by oper;

drop view proba1;
create view proba1 as
select distinct oper as p_name
,sum(duration) as in_duration 
from TA_IN 
where oper in ('CZE','FRA')
and timestamp like '201306%'
group by oper;

drop view proba3; 
create view proba3 as
select distinct oper as p_name
,sum(duration) as out_duration 
from TA_OUT
where oper in ('CZE','FRA')
and timestamp like '201305%' 
group by oper;

drop view proba4;
create view proba4 as
select distinct oper as p_name
,sum(duration) as in_duration 
from TA_IN 
where oper in ('CZE','FRA')
and timestamp like '201305%'
group by oper;

drop view proba2;
create view proba2 as
select distinct oper as p_name 
from OPER_DESCRIPTION;

此代码的想法是接收不同月份的持续时间,然后在下一个代码中比较此持续时间。这是下一个代码。

drop view proba5;

CREATE VIEW proba5 AS
SELECT (BAL1 + BAL2) AS BAL
FROM 
(
    SELECT 
        CASE

            WHEN proba.out_duration <= proba1.in_duration
             AND proba.p_name IN ('CZE', 'FRA')
             AND proba1.p_name IN ('CZE', 'FRA')
            THEN (proba.out_duration / 60) * 0.14

            WHEN proba.out_duration > proba1.in_duration
             AND proba.p_name IN ('CZE', 'FRA')
             AND proba1.p_name IN ('CZE', 'FRA')
            THEN
                (proba1.in_duration / 60) * 0.14
                +
                ((proba.out_duration - proba1.in_duration) / 60) * 0.09

            ELSE 0

        END AS BAL1

        , CASE

            WHEN proba3.out_duration <= proba4.in_duration
             AND proba3.p_name IN ('CZE', 'FRA')
             AND proba4.p_name IN ('CZE', 'FRA')
            THEN (proba3.out_duration / 60) * 0.14

            WHEN proba3.out_duration > proba4.in_duration
             AND proba3.p_name IN ('CZE', 'FRA')
             AND proba4.p_name IN ('CZE', 'FRA')
            THEN
                (proba4.in_duration / 60) * 0.14
                + ((proba3.out_duration - proba4.in_duration) / 60) * 0.09

            ELSE 0

        END AS BAL2

    FROM 
    (
        (
            (
                (
                    proba2
                    LEFT JOIN proba
                        ON proba2.p_name = proba.p_name
                )
                LEFT JOIN proba1
                    ON proba1.p_name = proba1.p_name
                LEFT JOIN proba3
                    ON proba3.p_name = proba3.p_name
            )
            LEFT JOIN proba4
                ON proba4.p_name = proba4.p_name
        )
        WHERE proba.p_name IN ('CZE', 'FRA')
        AND proba1.p_name IN ('CZE', 'FRA')
        AND proba3.p_name IN ('CZE', 'FRA')
        AND proba4.p_name IN ('CZE', 'FRA')
    )
);

当我使用一个p_name时,例如“CZE”,我收到正确的总持续时间。我想为每个P_name接收不同的持续时间。例如:

CZE 500
FRA 1300

然而,问题是我无法仅通过p_name进行分组,因为我也应该按持续时间包括分组。

请告诉我如何解决这个问题。

2 个答案:

答案 0 :(得分:1)

检查一下:

CREATE VIEW proba5
AS
SELECT p_name, SUM(BAL1 + BAL2) 
FROM (
    SELECT
        proba.p_name, 
        CASE
            WHEN     proba.out_duration <= proba1.in_duration
                THEN     proba.out_duration / 60) * 0.14
            WHEN     proba.out_duration > proba1.in_duration
                THEN     (proba1.in_duration / 60) * 0.14
                       + ((proba.out_duration - proba1.in_duration)/60) * 0.09
            ELSE 0
        END AS BAL1,
        CASE
            WHEN     proba3.out_duration <= proba4.in_duration
                THEN  (proba3.out_duration / 60) * 0.14
            WHEN     proba3.out_duration > proba4.in_duration
                THEN    (proba4.in_duration / 60) * 0.14
                       + ((proba3.out_duration - proba4.in_duration)/60) * 0.09
            ELSE 0
        END AS BAL2
    FROM proba2
        LEFT JOIN proba  ON proba2.p_name = proba.p_name
        LEFT JOIN proba1 ON proba1.p_name = proba1.p_name
        LEFT JOIN proba3 ON proba3.p_name = proba3.p_name
        LEFT JOIN proba4 ON proba4.p_name = proba4.p_name
    WHERE     
        proba.p_name IN ('CZE', 'FRA')
    AND proba1.p_name IN ('CZE', 'FRA')
    AND proba3.p_name IN ('CZE', 'FRA')
    AND proba4.p_name IN ('CZE', 'FRA')
)
    GROUP BY proba.p_name;

答案 1 :(得分:1)

我认为下面的代码应该完成所有这些观点合并的尝试/希望它也更容易看到正在发生的事情:

create view probAll as
select coalesce(tin.oper,tout.oper) as p_name
, sum
    (
        0.14 * tout.duration / 60 
        + case 
            when tout.duration > tin.duration
            then 0.09 * (tout.duration - tin.duration) / 60
            else 0
        end
    )
from 

(
    select oper
    , EXTRACT(Year FROM DATE timestamp) as tYear
    , EXTRACT(Month FROM DATE timestamp) as tMonth
    , sum(duration) as Balance
    from TA_OUT
    group by oper
    , EXTRACT(Year FROM DATE timestamp)
    , EXTRACT(Month FROM DATE timestamp) 
) as tout

full outer join

(    
    select oper
    , EXTRACT(Year FROM DATE timestamp) as tYear
    , EXTRACT(Month FROM DATE timestamp) as tMonth
    , sum(duration) as Balance
    from TA_IN
    group by oper
    , EXTRACT(Year FROM DATE timestamp)
    , EXTRACT(Month FROM DATE timestamp) 
) as tin

on tin.oper = tout.oper
and tin.tyear = tout.tyear
and tin.tmonth = tout.tmonth

where coalesce(tin.oper,tout.oper) in ('CZE','FRA')
and coalesce(tin.tyear,tout.tyear) = 2013
and coalesce(tin.tmonth,tout.tmonth) in (5, 6)

group by coalesce(tin.oper,tout.oper)

(未测试的)