嵌入式选择引用其父表

时间:2017-08-02 19:55:04

标签: sql oracle

好的,所以这个问题可能看起来有点奇怪,但如果有人知道它是否可能会对我正在编写的查询有很大的帮助。

这是我当前查询正在做的一些伪代码:

SELECT TERMS.Field1
   , TERMS.Field2
   , DISC.Field3
   FROM 
   (
      SELECT Field1
            ,Field2
      FROM
      --various tables
      WHERE
      --various expressions
   ) TERMS,
   (
      SELECT Field3
      FROM 
      --various tables
      WHERE
      --various expressions
   ) DISC
   WHERE
   --various expressions

我想要的是能够将嵌入式选择添加到最顶层的select语句中,该语句能够引用从TERMS返回的内容,如下所示:

SELECT TERMS.Field1
   , TERMS.Field2
   , DISC.Field3
   , (
      SELECT Count(*) 
      FROM TERMS
      WHERE
      --various conditions
     )
   FROM 
   (
      SELECT Field1
            ,Field2
      FROM
      --various tables
      WHERE
      --various expressions
   ) TERMS,
   (
      SELECT Field3
      FROM 
      --various tables
      WHERE
      --various expressions
   ) DISC
   WHERE
   --various expressions

您可能会问我为什么不直接在{TERMMS}中添加count(*),这是因为我希望在这些选项中将TERMS的输出应用到我希望能够的几个不同条件计数。如果有某种方法可以做到这一点,或者建议如何以更好的方式做到这一点,那将是有帮助的。

谢谢,如果没有解释清楚,请告诉我。

2 个答案:

答案 0 :(得分:0)

目前尚不清楚为什么要问 - 尽管它可能不是最有效的解决方案,但您在伪代码中显示的内容应该可以正常工作。你试过了吗?如果你没有,为什么不先试试,然后问这里?如果你确实尝试过,发生了什么?

在任何情况下,您都可以将 <div style="height:200px; border:1px solid #000;"> Container 1 </div> <div style="height:200px; border:1px solid #000;"> Container 2 </div> <div style="height:200px; border:1px solid #000;"> Container 3 </div> <div id="container4" class="container" style=" height:400px; border:1px solid #000; background:#fff;" > Container 4 </div> <div style="height:400px; border:1px solid #000;"> Container 5 </div> <div style="height:400px; border:1px solid #000;"> Container 6 </div> <script> anychart.onDocumentReady(function() { // set default icons for legend items // for lines, splines and markers anychart.theme({ chart: { defaultSeriesSettings: { line: {legendItem: {iconType: "line"}}, spline: {legendItem: {iconType: "spline"}}, marker: {legendItem: {iconType: "marker"}} }}}); // create a data set var data = anychart.data.set([ ["2012", 13, 21], ["2013", 18, 26], ["2014", 24, 28], ["2015", 22, 30], ["2016", 24, 32] ]); // map the data var seriesData_1 = data.mapAs({x: [0], value: [1]}); var seriesData_2 = data.mapAs({x: [0], value: [2]}); // create a chart chart = anychart.line(); // set the interactivity mode chart.interactivity().hoverMode("single"); // create the first series, set the data and name var series1 = chart.line(seriesData_1); series1.name("Residential Active"); // configure the visual settings of the first series series1.stroke("#2b89e1", 1); series1.hoverStroke("#2b89e1", 2); series1.selectStroke("#2b89e1", 4); // create the second series, set the data and name var series2 = chart.line(seriesData_2); series2.name("Non-Residential Active"); // configure the visual settings of the second series series2.stroke("#a91212", 1); series2.hoverStroke("#a91212", 2); series2.selectStroke("#a91212", 4); // set the titles of the axes var yAxis = chart.yAxis(); yAxis.title("Number of CES"); // make labels visible on chart series1.labels(true); series2.labels(true); // make marker shapes visible on chart series1.markers(true); series2.markers(true); // make legend at top of chart visible chart.legend(true); // set the container id chart.container("container4"); chart.animation(true, 800) // initiate drawing the chart chart.draw(); }); </script> 作为分析函数添加到内部查询中。让COUNT(*)成为

TERMS

然后选择( select Field1, Field2, Count(*) over () as cnt -- If you don't need an analytic clause from [various tables] ... ) TERMS 以及外部查询中的其他列。

您还可以在分析cnt函数中使用partition by...order by...以及窗口子句。 Google&#34; Oracle分析功能&#34;如果您不熟悉它们,或者在需要特定帮助时发布您的实际查询。

答案 1 :(得分:0)

可能WITH Clause : Subquery Factoring子句对您的案件有帮助:

WITH TERMS AS (
      SELECT Field1
            ,Field2
      FROM
      --various tables
      WHERE
      --various expressions
)

SELECT TERMS.Field1
   , TERMS.Field2
   , DISC.Field3
   , (
      SELECT Count(*) 
      FROM TERMS
      WHERE
      --various conditions
      )
FROM TERMS,
(
      SELECT Field3
      FROM 
      --various tables
      WHERE
      --various expressions
 ) DISC
 WHERE
   --various expressions