我有下表:
site||kw||date
M ||50||1-1-2013
A ||60||1-1-2013
...
我需要显示,在两个日期之间进行过滤
Site||avg(kw)||MAX_KW_PERIOD||MAX_KW_TOTAL
MAX_KW_PERIOD
两个日期之间的最大kw值和MAX_KW_TOTAL最大历史值kw。
我尝试使用联合查询:
SELECT Site,
avg(TM.kw) "AVG_KW",
MAX(TM.kwGen) "MAX KW PERIOD",
TO_NUMBER(NULL) "MAX_TOTAL"
FROM TABLE
WHERE DATE BETWEEN DATE1
AND DATE2
GROUP BY
Site
UNION
SELECT Site,
TO_NUMBER(NULL)"AVG_kW",
TO_NUMBER(NULL)"MAX KW PERIOD",
MAX(TM.kwGEN) "MAX TOTAL"
FROM TABLE
GROUP BY
Site
但我获得了:
SITE||AVG_KW||MAX_KW PERIOD||MAX TOTAL
A||100 ||110 ||(null)
A||(null)||(null) ||160
如何只为网站获取一行
SITE||AVG_KW||MAX_KW PERIOD||MAX TOTAL
A||100 ||110 ||160
非常感谢。
答案 0 :(得分:1)
试试这个:
with maxkw as
((SELECT site, MAX(kw) maxtotal FROM tbl t GROUP BY SITE)),
avgkw as
(SELECT t.Site,
avg(kw) "AVG_KW",
MAX(kw) "MAX_KW_PERIOD"--,
FROM tbl t
WHERE DATE BETWEEN '20130101' AND '20130104'
GROUP BY t.Site)
select t.site, t.avg_kw, t.max_kw_period, c.maxtotal
from avgkw t
inner join maxkw c on t.site = c.site