简体   繁体   English

从选择子查询里面选择一列?

[英]Select a column from subquery inside select?

How can I SELECT the Attr1 from subquery to the parent query? 如何从子查询到父查询中选择Attr1? I have tried to use Average.Attr1 but doesn't work. 我试图使用Average.Attr1,但不起作用。

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 或者,如果您只想限制它们,则可以将HAVING子句与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 现在,如果您说“ Average.attr您将获得与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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM