我使用了以下查询
SELECT
a.cluster_cd,
(SELECT
Sum(BALANCE_AREA)
FROM
AGRI.plantation_TRAN p
WHERE
p.plot_cluster_cd = a.cluster_cd
AND trunc(a.agreement_date) = trunc(sysdate)
AND a.PLOT_NO = p.PLOT_NO
AND a.SEASON_CD = p.SEASON_CD
AND (a.SEASON_CD = 10)) as Today,
(SELECT
Sum(BALANCE_AREA)
FROM
AGRI.plantation_TRAN p
WHERE
p.plot_cluster_cd = a.cluster_cd
AND trunc(a.agreement_date) BETWEEN to_date('2014-10-24','YYYY-MM-DD') AND trunc(sysdate)
AND a.PLOT_NO = p.PLOT_NO
AND a.SEASON_CD = p.SEASON_CD
AND (a.SEASON_CD=10)) as Todate
FROM
AGRI.agreement_TRAN a
我得到了
cluster_cd Today Todate
1 0.8 3.5
1 2.6 8.2
2 8.3 9.5
3 0.2 2.6
2 6.1 9.1
3 9.8 11.0
但我需要的是
今天和todate字段应该总结为不同的cluster_cd,如
Cluster_CD Today Todate
1 123 456
2 125 789
3 45 458
4 456 789
应该在唯一的cluster_cd
上进行求和答案 0 :(得分:0)
你可以试试这个:
Select cluster_cd,sum(today),sum(Todate) from(
SELECT a.cluster_cd,
(select Sum(BALANCE_AREA)
FROM AGRI.plantation_TRAN p
where p.plot_cluster_cd=a.cluster_cd and trunc(a.agreement_date)=trunc(sysdate) and a.PLOT_NO = p.PLOT_NO AND a.SEASON_CD = p.SEASON_CD AND (a.SEASON_CD=10)) as Today,
(select Sum(BALANCE_AREA)
FROM AGRI.plantation_TRAN p
where p.plot_cluster_cd=a.cluster_cd and trunc(a.agreement_date) between to_date('2014-10-
24','YYYY-MM-DD') and trunc(sysdate) and a.PLOT_NO = p.PLOT_NO AND a.SEASON_CD = p.SEASON_CD AND (a.SEASON_CD=10)) as Todate
from AGRI.agreement_TRAN a)t
group by cluster_cd
答案 1 :(得分:0)
此查询可能有所帮助:
SELECT a.cluster_cd,
sum(case when a.agreement_date >= trunc(sysdate) then nvl(BALANCE_AREA, 0) else 0 end) as Today,
sum(case when a.agreement_date < trunc(sysdate) then nvl(BALANCE_AREA, 0) else 0 end) as Todate
FROM AGRI.agreement_TRAN a
LEFT JOIN AGRI.plantation_TRAN p
ON (p.plot_cluster_cd = a.cluster_cd
and a.PLOT_NO = p.PLOT_NO
and a.SEASON_CD = p.SEASON_CD
)
WHERE a.agreement_date between to_date('2014-10-24000000','YYYY-MM-DDHH24MISS')
and trunc(sysdate)+1-1/24/60/60
AND a.SEASON_CD=10
GROUP BY a.cluster_cd;