I have the following script.
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;
The idea of this code is to receive duration for different months and after that in the next code to compare this duration. This is the next code.
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')
)
);
When I use one p_name, for instance "CZE" I receive the correct total duration. I want to receive distinct duration for every P_name. For example:
CZE 500
FRA 1300
However, the problem is that I am not able to group by p_name only, because I should include group by duration too.
Could you please tell me how to fix this problem.