How can I SELECT the Attr1 from subquery to the parent query? I have tried to use Average.Attr1 but doesn't work.
not a single-group group function
SELECT SUM(Report.Attr1),
SUM(Report.Attr2),
SUM(Report.Attr3),
0
FROM {Report},
(
SELECT AVG(Report.Attr1),
AVG(Report.Attr2),
AVG(Report.Attr3)
FROM Report
WHERE
StartDate <= Report.Date AND
Report.Date <= EndDate
GROUP BY Report.Attr1, Report.Attr2, Report.Attr3
) Average
WHERE
StartDate <= Report.Date AND
Report.Date <= EndDate
You can add aliases to those aggregate functions, so that you can use them in the outer query
SELECT SUM(avg_attr1),
SUM(avg_attr2),
SUM(avg_attr3),
0
FROM {Report},
(
SELECT AVG(Report.Attr1) as avg_attr1,
AVG(Report.Attr2) as avg_attr2,
AVG(Report.Attr3) as avg_attr3
...
Or if you just want to limit on them, then you could use the HAVING clause together with the GROUP BY
...
GROUP BY Report.Attr1, Report.Attr2, Report.Attr3
HAVING AVG(Report.Attr1) > {SomeNumber}
) Average
...
I am not sure if this is what you want, but I get it to work by:
WITH REPORT (STARTDATE,ENDDATE,DATE1,ATTR1,ATTR2,ATTR3)AS(
SELECT DATE '2018-01-01',DATE '2018-02-01',DATE '2018-01-11', 1,2,3 FROM DUAL UNION ALL
SELECT DATE '2018-01-01',DATE '2018-02-01',DATE '2018-01-11',1,2,3 FROM DUAL UNION ALL
select date '2018-01-01',DATE '2018-02-01',DATE '2018-01-11',1,2,3 from dual
)
SELECT SUM(Report.Attr1),
SUM(Report.Attr2),
SUM(REPORT.ATTR3),
0,
average.a,average.b,average.c
FROM REPORT,
(
SELECT AVG(REPORT.ATTR1) A,
AVG(REPORT.ATTR2) B ,
AVG(Report.Attr3) c
FROM Report
WHERE
STARTDATE <= REPORT.DATE1 AND
Report.Date1 <= EndDate
GROUP BY Report.Attr1, Report.Attr2, Report.Attr3
) Average
WHERE
STARTDATE <= REPORT.DATE1 AND
REPORT.DATE1 <= ENDDATE
GROUP BY 0,
average.a,average.b,average.c
You can refer to them by giving an Alias name. Now if you say, Average.attr
you get the value corresponding to Report.Attr1
SELECT SUM(Report.Attr1),
SUM(Report.Attr2),
SUM(Report.Attr3),
0,
Average.attr
FROM {Report},
(
SELECT AVG(Report.Attr1) as attr,
AVG(Report.Attr2),
AVG(Report.Attr3)
FROM Report
WHERE
StartDate <= Report.Date AND
Report.Date <= EndDate
GROUP BY Report.Attr1, Report.Attr2, Report.Attr3
) Average
WHERE
StartDate <= Report.Date AND
Report.Date <= EndDate
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.