简体   繁体   English

查询优化 - 重复 Oracle SQL 中的模式

[英]Query Optimization - To repeat a pattern in Oracle SQL

Introduction: I can do this in MS-Excel, it takes me 1 minute, but I m trying to get this in Oracle SQL简介:我可以在 MS-Excel 中做到这一点,这需要我 1 分钟,但我试图在 Oracle SQL 中得到这个

Here is my Code:这是我的代码:

SELECT A.*, (CASE WHEN A.r = 1 then 'X1' when A.r = 2 then 'X2' when A.r = 3 then 'X3' when A.r = 4 
then 'X4' when A.r = 5 then 'X2' when A.r = 6 then 'X6' end) X FROM
(
    Select Rownum r
    From dual
    Connect By Rownum <= 6 ) A

This is the Output:这是 Output:

在此处输入图像描述

Now, what if I have to do it for 25000 numbers, meaning when (rownum <= 25000) currently I have it only for 6, Is there a better method to do this with out case statement?现在,如果我必须为 25000 个数字执行此操作,这意味着当 (rownum <= 25000) 目前我只有 6 个时,是否有更好的方法来执行此操作而不使用 case 语句?

If you want to repeat this pattern of 6 rows for the remaining rows, then you can do:如果您想为剩余的行重复这种 6 行的模式,那么您可以执行以下操作:

select t.*, 
       (case when mod(rownum, 6) = 5 then 'X2'
             else 'X' || (mod(rownum - 1, 6) + 1)
        end)
from t;

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

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