【问题标题】:Can column storing number be expanded in OracleOracle中可以扩展列存储数吗
【发布时间】:2014-10-02 20:54:08
【问题描述】:
Can we expand a column storing number somehow in the output.
I am trying to expand one column with other column having same value.

我正在尝试的列是数字。如果值为 3,则选择查询的结果应该是 1,2,3,而其他列具有相同的数据。 这可能吗?

For ex, 

输入表是

    Can we expand a column storing number somehow in the output.
    For ex, <br>
    <h4>INPUT TABLE IS :</h4>
     
    <table style="width:50%" border=1>
      <tr>
        <td>Thank</td>
        <td>You </td> 
        <td>2 </td>
      </tr>
      <tr>
    </table>
    <br>
    <h4>Output TABLE should be like this :</h4>
     
    <table style="width:50%" border=1>
      <tr>
        <td>Thank</td>
        <td>You </td> 
        <td>1 </td>
      </tr>
    <tr><br>
        <td>Thank</td>
        <td>You </td> 
        <td>2 </td>
      </tr>
      <tr>
    </table>
    <br><br>
    value of last column is number and has expanded.<br>
    <b>Column1 and column2 should contain same value.</b><br>
    
    The problem is that i can change the input table and this is how i need the output.<br>
    I am trying using dual table join with table1 and use CONNECT BY but not getting the result . 

<br> Any help on this..

输出表是

最后一列的值为数字并已扩展。 Column1 和 column2 应该包含相同的值。

The problem is that i can change the input table and this is how i need the output.
I am trying using dual join with table1 and use CONNECT BY but not getting the result . 

【问题讨论】:

  • 好的,不确定我是否理解正确。这就是我听起来的样子。 “我想输入一个数字。然后我希望输出表的行数与我输入的数字一样多,但第三列表示从 '1' 到 'n' 的行数,其中 n 是我的数字输入。正确吗?
  • 我的问题措辞不好。所以,我已经有一个包含数据的表。需要执行一个选择查询,该查询实际上将 column3(最后一列)扩展为具有该值相同数据的其他列。例如,column3 is = 4 然后选择查询应该给出 4 行的结果,这应该是 1,2,3,4 作为 column3 和 column1 ,column2 应该完全相同..
  • 在您的问题中添加 HTML 标记有什么意义?

标签: sql oracle expansion connect-by


【解决方案1】:

好的,这是获取“计数”列的逻辑...

SELECT LEVEL AS Column3
FROM DUAL
CONNECT BY LEVEL <= @EnteredNumber;

这将创建一个从 1 到您输入的数字的连续数字表。您所要做的就是将这个新表格与您预先制作的结果一起加入。

它的工作原理是它创建了一个自引用循环,该循环产生“继承”,其中每个后续行都被视为前一行的子行。这很奇怪,是的......但它应该可以工作

编辑以调整我的答案以使用您在评论中要求的细节:

SELECT t.Column1, t.Column2, LEVEL As Column3 
FROM (
    SELECT Column1, Column2, Column3Value 
    FROM Table_1  
    WHERE t.column1='value' 
    AND t.column2 = 'value2'
) t 
CONNECT BY LEVEL <= t.Column3Value

确保您在子查询中进行 WHERE 工作,否则它将在过滤之前针对整个表运行连接,这将影响您的性能。

【讨论】:

  • 好的,但@EnteredNumber 来自另一个表,即 TABLE_1。我尝试将此表与该表的列连接,但出现错误。
  • SELECT LEVEL AS Column3 FROM DUAL ,TABLE_1 t WHERE t.column1='value' AND t.column2 = 'value2' CONNECT BY LEVEL
  • 添加了第二个块,显示如何将此逻辑应用于您的案例的细节。
猜你喜欢
  • 2015-11-09
  • 1970-01-01
  • 1970-01-01
  • 2020-09-10
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 2016-08-16
相关资源
最近更新 更多