'Lucky number' logic in SQL

Hi all

Nice day...

Note: This thread is just for fun and for learning the recursive techniques

Please ignore this if you are busy

-------------------------

https://en.Wikipedia.org/wiki/Happy_number

For example, 19 is happy, because the associated sequence is:

12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1.

I just wanted to know if anyone can re - write this below from pl/sql in regular SQL (using recursive with the clause may be)... Say then that pl/sql isn't so bad either... w.r.t. performance.

SET SERVEROUTPUT ON
DECLARE
   l_res       NUMBER;
   l_var_new   NUMBER;
BEGIN
   FOR rec IN 0 .. 1000
   LOOP
      l_var_new := rec;
      WHILE (TRUE)
      LOOP
         EXECUTE IMMEDIATE
               'select '
            || RTRIM (REGEXP_REPLACE (l_var_new, '(.)', 'power(\1,2)+'), '+')
            || ' from dual'
            INTO l_res;
         IF ( (LENGTH (l_res) = 1 AND l_res <> 1) OR l_res = 1)
         THEN
            IF (l_res = 1)
            THEN
               --logmein.in ('NUMBER -> ' || rec);   ---- logging method
               DBMS_OUTPUT.put_line (rec);  ---just for our comfort.. (not for logging ;-))
            END IF;
            EXIT;
         END IF;
         l_var_new := l_res;
      END LOOP;
   END LOOP;
END;

See you soon,.

Manik.

Very nice indeed.

I will add only minor simplification of the passage of the summons to a scalar subquery.

WITH data
     AS (    SELECT LEVEL n
               FROM DUAL
         CONNECT BY LEVEL <= 1000),
     recurse (n, pwrsum)
     AS (SELECT n,
                (    SELECT SUM (POWER (SUBSTR (n, LEVEL, 1), 2))
                       FROM DUAL
                 CONNECT BY LEVEL <= LENGTH (n))
                   AS pwrsum
           FROM data
         UNION ALL
         SELECT n,
                (    SELECT SUM (POWER (SUBSTR (pwrsum, LEVEL, 1), 2))
                       FROM DUAL
                 CONNECT BY LEVEL <= LENGTH (pwrsum))
           FROM recurse
          WHERE pwrsum >= 10)
  SELECT n
    FROM recurse
   WHERE pwrsum = 1
ORDER BY 1;

Tags: Database

Similar Questions

  • Number of error SQL ORA-00904: invalid column name has occurred.

    Hello
    on P8.18 on a Win 2003 server when we launch SWPAUDIT, it failed with:
    Number of error SQL ORA-00904: invalid column name has occurred. Query process failed.
    I searched this error on metalink3. Nothing in connection with.

    Any idea?

    Thank you.

    Please give a clear picture of what you are doing... .and what paintings... There is the possibility of the audit refers to certain tables... with deleted. names of columns just look in the structure of what you audit?

    Please, find the name of column...

    ORA-00904: string: invalid identifier
    Cause: The column name entered is invalid or missing.
    Action: Enter a valid column name. A valid column name must start with a letter, must be less than or equal to 30 characters and include only alphanumeric characters and the special characters $, _, and #. If it contains other characters, then it must be enclosed in quotation marks. It cannot be a reserved word.

  • How to get sql % number of dynamic sql code lines

    Hello

    In this procedure I'm inserting and updating using dynamic sql. Now I want to go back two more PARAMETERS, the parameter should be back the number of lines inserted and how updated by stmtas of UPDATE as well as an INSERT. I'm not able to do can help you on that?

    CREATE OR REPLACE PROCEDURE Sp_Array_Test( PV_TGT_NAME   IN  VARCHAR2,
                                               PV_SRC_NAME   IN  VARCHAR2,
                                               PV_PK_COLS    IN  VARCHAR2,
                                               PN_ERR_CD     OUT NUMBER,
                                               PN_ERR_MSG    OUT VARCHAR2)
    AS
    
    
    TYPE ARR_TAB IS TABLE OF VARCHAR2(4000) INDEX BY BINARY_INTEGER;
    
    
    --UTL_FP        UTL_FILE.FILE_TYPE;
    LV_AN_
    
    
    
    BLOCK   VARCHAR2(32767);
    LN_CUR        BINARY_INTEGER := DBMS_SQL.OPEN_CURSOR;
    LN_DESC       DBMS_SQL.DESC_TAB;
    LN_COL_CNT    PLS_INTEGER := 0;
    LV_SEL_UPD_STMT   VARCHAR2(4000);
    LV_SEL_INS_STMT   VARCHAR2(4000);
    ARR_INDX      NUMBER := 1;
    LV_DATA_TYPE  VARCHAR2(8);
    LN_FIND_FLAG  NUMBER := 0;
    LN_TAB        ARR_TAB;
    LV_COLS_ARR   ARR_TAB;
    LV_ERR_MSG    VARCHAR2(500);
    
    
    
    
    --PROCEDURE FILE_WRITE ( FH_IN     IN UTL_FILE.FILE_TYPE,
    --                STRING_IN IN VARCHAR2 ) IS
    --BEGIN
    --   UTL_FILE.PUT_LINE(FH_IN,STRING_IN);
    --   LV_AN_BLOCK := LV_AN_BLOCK||STRING_IN;
    --EXCEPTION
    --   WHEN OTHERS THEN
    --      RAISE;
    --END FILE_WRITE;
    
    
    
    
    BEGIN
    
    
    --   UTL_FP := UTL_FILE.FOPEN('TEST_DIR', 'TEST.sql', 'W');
    
    
        LV_SEL_UPD_STMT := 'SELECT A.'||REPLACE(PV_PK_COLS,',','||A.')||' PK_COLS , A.* , B.ROWID FROM '||PV_SRC_NAME||' A, '||PV_TGT_NAME||' B WHERE ';
    
    
        LV_SEL_INS_STMT := 'SELECT A.* FROM '||PV_SRC_NAME||' A WHERE NOT EXISTS (SELECT ''1'' FROM '||PV_TGT_NAME||' B WHERE ';
    
    
        LN_TAB(ARR_INDX) := 'DECLARE ';
        ARR_INDX := ARR_INDX + 1;
    
    
        LN_TAB(ARR_INDX) := 'CURSOR CUR_VIEW_UPD IS '||LV_SEL_UPD_STMT ;
        ARR_INDX := ARR_INDX + 1;
    
    
    
    
        SELECT SUBSTR(COLS,DECODE(RN,1,1,INSTR(COLS,',',1,RN-1)+1),DECODE(RN,1,INSTR(COLS,',',1,RN)-1,INSTR(COLS,',',1,RN)-INSTR(COLS,',',1,RN-1)-1))
    BULK COLLECT INTO LV_COLS_ARR
          FROM ( SELECT RN, PV_PK_COLS||',' COLS
                   FROM (SELECT ROWNUM RN
                           FROM ALL_OBJECTS
                          WHERE ROWNUM <= LENGTH(PV_PK_COLS)- LENGTH(REPLACE(PV_PK_COLS,','))+1)) ;
        FOR K IN 1 .. LV_COLS_ARR.COUNT LOOP
            LV_SEL_UPD_STMT     := LV_SEL_UPD_STMT||' A.'||LV_COLS_ARR(K)||' = ';
            LN_TAB(ARR_INDX) := ' A.'||LV_COLS_ARR(K)||' = ';
            LV_SEL_UPD_STMT     := LV_SEL_UPD_STMT||' B.'||LV_COLS_ARR(K) ||CASE WHEN K = LV_COLS_ARR.COUNT THEN NULL ELSE ' AND ' END;
            LN_TAB(ARR_INDX) := LN_TAB(ARR_INDX)||' B.'||LV_COLS_ARR(K) ||CASE WHEN K = LV_COLS_ARR.COUNT THEN ' ;' ELSE ' AND ' END;
            ARR_INDX := ARR_INDX + 1;
        END LOOP;
    
    
        LN_TAB(ARR_INDX) := 'CURSOR CUR_VIEW_INS IS '||LV_SEL_INS_STMT ;
        ARR_INDX := ARR_INDX + 1;
    
    
        FOR K IN 1 .. LV_COLS_ARR.COUNT LOOP
            LV_SEL_INS_STMT     := LV_SEL_INS_STMT||' A.'||LV_COLS_ARR(K)||' = ';
            LN_TAB(ARR_INDX) := ' A.'||LV_COLS_ARR(K)||' = ';
            LV_SEL_INS_STMT     := LV_SEL_INS_STMT||' B.'||LV_COLS_ARR(K) ||CASE WHEN K = LV_COLS_ARR.COUNT THEN NULL ELSE ' AND ' END;
            LN_TAB(ARR_INDX) := LN_TAB(ARR_INDX)||' B.'||LV_COLS_ARR(K) ||CASE WHEN K = LV_COLS_ARR.COUNT THEN ' );' ELSE ' AND ' END;
            ARR_INDX := ARR_INDX + 1;
        END LOOP;
    
    
    
    
        LV_ERR_MSG := 'WHILE PARSING SELECT STATEMENT -- '||LV_SEL_UPD_STMT;
        DBMS_SQL.PARSE(LN_CUR, LV_SEL_UPD_STMT, DBMS_SQL.NATIVE);
    
    
        LV_ERR_MSG := 'WHILE DESCRIBING SELECT STATEMENT -- '||LV_SEL_UPD_STMT;
        DBMS_SQL.DESCRIBE_COLUMNS(LN_CUR, LN_COL_CNT, LN_DESC);
    
    
    
    
    
    
       FOR i IN LN_DESC.FIRST .. LN_DESC.LAST LOOP
          IF LN_DESC(i).col_type = 2 THEN
             LV_DATA_TYPE := 'NUMBER';
          ELSIF LN_DESC(i).col_type = 12 THEN
             LV_DATA_TYPE := 'DATE';
          ELSE
             LV_DATA_TYPE := 'VARCHAR2';
          END IF;
           LN_TAB(ARR_INDX) := '   T_'||LN_DESC(i).col_name||' DBMS_SQL.'||LV_DATA_TYPE||'_TABLE;';
           ARR_INDX := ARR_INDX + 1;
       END LOOP;
    
    
    
    
    
    
        LN_TAB(ARR_INDX) := 'BEGIN ';
        ARR_INDX := ARR_INDX + 1;
        LN_TAB(ARR_INDX) := '   EXECUTE IMMEDIATE ''ALTER SESSION SET NLS_DATE_FORMAT = ''''DD-MON-YYYY HH24:MI:SS'''''';';
        ARR_INDX := ARR_INDX + 1;
        LN_TAB(ARR_INDX) := '   OPEN CUR_VIEW_UPD;';
        ARR_INDX := ARR_INDX + 1;
        LN_TAB(ARR_INDX) := '   LOOP';
        ARR_INDX := ARR_INDX + 1;
        LN_TAB(ARR_INDX) := '      FETCH CUR_VIEW_UPD BULK COLLECT INTO T_'||LN_DESC(LN_DESC.FIRST).col_name||',';
        ARR_INDX := ARR_INDX + 1;
        FOR i IN LN_DESC.FIRST + 1 .. LN_DESC.LAST - 1 LOOP
          LN_TAB(ARR_INDX) := '                        T_'||LN_DESC(i).col_name||',';
          ARR_INDX := ARR_INDX + 1;
        END LOOP;
       LN_TAB(ARR_INDX) := '                        T_'||LN_DESC(LN_DESC.LAST).col_name||' LIMIT 500 ;';
       ARR_INDX := ARR_INDX + 1;
       LN_TAB(ARR_INDX) := '     FORALL I IN 1 .. '||'T_'||LN_DESC(LN_DESC.LAST).col_name||'.COUNT ';
       ARR_INDX := ARR_INDX + 1;
        LN_TAB(ARR_INDX) := '      UPDATE '||PV_TGT_NAME||' SET  ';
        ARR_INDX := ARR_INDX + 1;
        LN_FIND_FLAG := 0;
        FOR I IN LN_DESC.FIRST + 1 .. LN_DESC.LAST-1 LOOP
          FOR K IN 1 .. LV_COLS_ARR.COUNT LOOP
             LN_FIND_FLAG := 0;
             IF LN_DESC(I).COL_NAME = LV_COLS_ARR(K) THEN
                LN_FIND_FLAG := 1;
                EXIT;
             END IF;
          END LOOP;
          IF LN_FIND_FLAG = 0 THEN
            LN_TAB(ARR_INDX) := '                        '||LN_DESC(i).col_name||' = '||'T_'||LN_DESC(i).col_name||'(I)'||CASE WHEN I = LN_DESC.LAST-1 THEN ' WHERE ' ELSE ',' END;
            ARR_INDX := ARR_INDX + 1;
          END IF ;
        END LOOP;
    
    
       LN_TAB(ARR_INDX) := '                        ROWID = '||'T_'||LN_DESC(LN_DESC.LAST).col_name||'(I) ;';
       ARR_INDX := ARR_INDX + 1;
    
    
       LN_TAB(ARR_INDX) := '      COMMIT;';
       ARR_INDX := ARR_INDX + 1;
    
    
       LN_TAB(ARR_INDX) := '      EXIT WHEN CUR_VIEW_UPD%NOTFOUND;';
       ARR_INDX := ARR_INDX + 1;
    
    
       LN_TAB(ARR_INDX) := '   END LOOP;';
       ARR_INDX := ARR_INDX + 1;
    
    
       LN_TAB(ARR_INDX) := '   CLOSE CUR_VIEW_UPD;';
       ARR_INDX := ARR_INDX + 1;
    
    
       LN_TAB(ARR_INDX) := '      COMMIT;';
       ARR_INDX := ARR_INDX + 1;
    
    
    
    
        LN_TAB(ARR_INDX) := '   OPEN CUR_VIEW_INS;';
        ARR_INDX := ARR_INDX + 1;
        LN_TAB(ARR_INDX) := '   LOOP';
        ARR_INDX := ARR_INDX + 1;
        LN_TAB(ARR_INDX) := '      FETCH CUR_VIEW_INS BULK COLLECT INTO T_'||LN_DESC(LN_DESC.FIRST+1).col_name||',';
        ARR_INDX := ARR_INDX + 1;
        FOR i IN LN_DESC.FIRST + 2 .. LN_DESC.LAST - 2 LOOP
          LN_TAB(ARR_INDX) := '                        T_'||LN_DESC(i).col_name||',';
          ARR_INDX := ARR_INDX + 1;
        END LOOP;
       LN_TAB(ARR_INDX) := '                        T_'||LN_DESC(LN_DESC.LAST-1).col_name||' LIMIT 500 ;';
       ARR_INDX := ARR_INDX + 1;
    
    
       LN_TAB(ARR_INDX) := '     FORALL J IN 1 .. '||'T_'||LN_DESC(LN_DESC.FIRST + 1).col_name||'.COUNT ';
       ARR_INDX := ARR_INDX + 1;
       LN_TAB(ARR_INDX) := '     INSERT INTO '||PV_TGT_NAME||' (';
       ARR_INDX := ARR_INDX + 1;
        FOR i IN LN_DESC.FIRST + 1 .. LN_DESC.LAST - 1 LOOP
          LN_TAB(ARR_INDX) := '                        '||LN_DESC(i).col_name||CASE WHEN I = LN_DESC.LAST - 1  THEN ' )' ELSE ',' END ;
          ARR_INDX := ARR_INDX + 1;
        END LOOP;
        FOR i IN LN_DESC.FIRST + 1 .. LN_DESC.LAST - 1 LOOP
          LN_TAB(ARR_INDX) := CASE WHEN I = LN_DESC.FIRST + 1 THEN 'VALUES (' ELSE NULL END ||'                        T_'||LN_DESC(i).col_name||'(J)'||CASE WHEN I = LN_DESC.LAST - 1  THEN ' ) ;' ELSE ',' END ;
          ARR_INDX := ARR_INDX + 1;
        END LOOP;
    
    
       LN_TAB(ARR_INDX) := '      COMMIT;';
       ARR_INDX := ARR_INDX + 1;
    
    
       LN_TAB(ARR_INDX) := '      EXIT WHEN CUR_VIEW_INS%NOTFOUND;';
       ARR_INDX := ARR_INDX + 1;
    
    
       LN_TAB(ARR_INDX) := '   END LOOP;';
       ARR_INDX := ARR_INDX + 1;
    
    
       LN_TAB(ARR_INDX) := '   CLOSE CUR_VIEW_INS;';
       ARR_INDX := ARR_INDX + 1;
    
    
       LN_TAB(ARR_INDX) := '      COMMIT;';
       ARR_INDX := ARR_INDX + 1;
    
    
       LN_TAB(ARR_INDX) := 'END ;';
       ARR_INDX := ARR_INDX + 1;
    
    
    
    
       FOR J IN 1 .. LN_TAB.COUNT LOOP
    --     DBMS_OUTPUT.PUT_LINE( LN_TAB(J));
    --     FILE_WRITE(UTL_FP,LN_TAB(J));
         LV_AN_BLOCK := LV_AN_BLOCK||LN_TAB(J);
       END LOOP;
    
    
    --   UTL_FILE.FCLOSE(UTL_FP);
    
    
       EXECUTE IMMEDIATE LV_AN_BLOCK;
    
    
    PN_ERR_CD    := 0;
    PN_ERR_MSG   := 'Successful Completion';
    
    
    EXCEPTION
    WHEN OTHERS THEN
    PN_ERR_CD    := SQLCODE;
    PN_ERR_MSG   := LV_ERR_MSG||' -- '||SQLERRM ;
    END;
    /
    

    Thank you all for your answers. I agree with you all. I have manged this time by adding variable bind, then run it immediately ON aid. I don't know how it's going to be training.

    From now on my side no problem I don't thank you.

  • How can I get the logic of sql from the catalog or the obiee Manager

    I try to get the logic sql behind a report. Can someone help me how to get sql obiee presentation services or from Catalog Manager

    Hello

    Addition to the response of Srini, you know that there is a new view in g 11 responses to display SQL logic? Just choose in the analysis-> results-> new view-> other-> SQL-> Standard SQL logic views.

    I hope this helps.

    Diakité

  • Displacement of the unit number logic from one ESX host to another

    I am a newbies VI3. Are there opportunities to move a unit logic of an ESX host number to another?

    I can't find any guide. Help, please.

    Thank you.

    You don't need to remove the LUN from the host of the first.

    It is enough to present the second ESX host storage, don't know how you do it for the EMC storage.

    If you have virtual center then you just select the host, go to Setup and select storage adapters and then rescan the HBAs. This storage should appear.

    If you do not have virtual Center, then you should be able to do it from the host.

    The main thing to make sure, it is that you present the LUN with the same ID for all guests.

    If you really need to delete a logical unit number not used, check that no virtual machines are using, use the management of storage to anyone there and then rescan the HBAs as above.

  • Number sequences using SQL

    I want to output sequence as below

    SQL > SELECT ID FROM (SELECT level as the id FROM dual connect by level < 20) WHERE id BETWEEN 8 AND 19.

    ID
    --------------
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19

    12 selected lines.


    Y at - it all another way to achieve the same result.

    Thanks in advance.

    concerning

    Hello

    CONNECT is the best way. It is not more complicated than the others, always at least as fast, sometimes much more quickly.

    If the output does not 1, then it might be a bit more efficient to add a constant, rather than generate many kinds you want:

    SELECT  LEVEL + 7     AS id
    FROM    dual
    CONNECT BY   LEVEL <= (20 - 8)
    ;
    

    In Oracle 8 (and earlier versions), this kind of CONNECT BY query did not work. People had to SELECT ROWNUM of a real table or view, for example object, which usually included at least 500 lines:

    SELECT  ROWNUM + 7   AS id
    FROM    all_objects
    WHERE   ROWNUM  <= (20 - 8)
    ;
    

    If there is no doubt that all_object had the same number of rows you need numbers, your could square of this:

    SELECT  ROWNUM + 7   AS id
    FROM    all_objects
    ,       all_objects
    WHERE   ROWNUM  <= (20 - 8)
    ;
    

    From Oracle 10, you can use model. Features XML to do this became available at the same time.
    From Oracle 11.2, you can use a recursive subquery in the WITH clause.
    Published by: Frank Kulash, April 21, 2011 09:05

  • dynamic logic in SQL WHERE clause based on the value of the ELEMENT

    Hello

    I have a report based on the following SQL query.
       select dept_no,
           dept_name,
           dept_loc
      from dept
     where dept_loc = :P1_DEPT_LOC
       // If P1_DEPT_LOC is not null, I want the WHERE clause and IF  P1_DEPT_LOC is null, then I don't need the WHERE clause.
       // how can I build this logic in same SQL
    Thank you
    Deepak

    Published by: Deepak_J on March 11, 2010 16:37

    where: P1_DEPT_LOC IS NULL or dept_loc =: P1_DEPT_LOC

    If dept_loc is not null better would be

    where dept_loc = coalesce(:P1_DEPT_LOC,dept_loc)

  • Logic of SQL query for shipping to calculate prices

    Hi Experts,

    As I have created a thread already in this regard. You can refer to my previous thread in [previous Thread: http://forums.oracle.com/forums/thread.jspa?threadID=1027799 & tstart = 0]

    I'll post my question again in this thread

    Records from the table
         
    SHIPMENT_ID     SHIPMENT_PRICE     SHIPMENT_ADDT_PRICE     PRODUCT_ID     PRODUCT_QTY     VIRTUAL_COLUMN (Just for your Reference)     
                                  
    1000          6.95          2               11          4          1
    1000          6.95          2               11          4          2
    1000          3.95          1               13          2          3
    1000          3.95          1               12          2          4
    1001          12.95          2               11          4          5
    1001          12.95          1               13          2          6
    1001          12.95          2               11          4          7
    1001          12.95          1               12          2          8
    1002          20.95          1               12          2          9
    1002          20.95          2               11          4          10
    1002          20.95          2               11          4          11
    1002          20.95          1               13          2          12
    Repeated values in table with Maximum Value,
    SHIPMENT_ID     SHIP_PRICE     SHIPMENT_ADDT_PRICE     PRODUCT_QTY     PRODUCT_ID     VIRTUAL_COLUMN
    1000          6.95          2               4          11          1
    1000          6.95          2               4          11          2
    1001          12.95          2               4          11          5
    1001          12.95          2               4          11          7
    1002          20.95          2               4          11          10
    1002          20.95          2               4          11          11
    *
    In this case, there are two same lines with a maximum value for all shipments of three,
    but the need to take a line like maximum and to consider the other as not maximum line to process logic
    *

    I looked at the folder with the value VIRTUAL_COLUMN 1.5 and 10 as maximum value and other values not maximum
    Expected Result (For Each Row) - Just listed for one shipment_id = 1000 here
    SHIPMENT_ID     SHIP_PRICE     CALCULATION          STEPS                                        VIRTUAL_COLUMN     
    1000          41.7          (1*6.95)+((4-1)*2)     Maximum ShipPrice + (PRODUCT_QTY-1)*SHIPMENT_ADDT_PRICE          1
    1000          8          (4*2)               PRODUCT_QTY*SHIPMENT_ADDT_PRICE                         2
    1000          2          (2*1)               PRODUCT_QTY*SHIPMENT_ADDT_PRICE                         3
    1000          2          (2*1)               PRODUCT_QTY*SHIPMENT_ADDT_PRICE                         4
    * Note: The virtual column in the data provided is just for the reference and not exists in the table

    Thank you
    Dharan V

    Queries 1 and query 2 are wrong because two of them use the SHIPMENT_PRICE of the line and not the MAXIMUM SHIPMENT_PRICE for this product like you id requested in the original message.
    To get the MOST out of SHIPMENT_PRICE, you must use my last request:

    SQL> with shipment_table as (
      2  select 1000 SHIPMENT_ID, 6.95 SHIPMENT_PRICE, 2 SHIPMENT_ADDT_PRICE,
      3         11 PRODUCT_ID, 4 PRODUCT_QTY, 1 VIRTUAL_COLUMN from dual union
      4  select 1000,6.95,2,11,4,2 from dual union
      5  select 1000,6.95,2,11,4,3 from dual union
      6  select 1000,6.95,2,11,4,4 from dual union
      7  select 1001,12.95,2,11,4,5 from dual union
      8  select 1001,12.95,2,11,4,6 from dual union
      9  select 1001,12.95,2,11,4,7 from dual union
     10  select 1001,12.95,2,11,4,8 from dual union
     11  select 1002,20.95,1,12,2,9 from dual union
     12  select 1002,20.95,1,12,2,10 from dual union
     13  select 1002,20.95,1,12,2,11 from dual union
     14  select 1002,20.95,1,12,2,12 from dual)
     15  SELECT shipment_id,
     16         shipment_price,
     17         shipment_addt_price,
     18         product_id,
     19         product_qty,
     20         CASE
     21            WHEN ROW_NUMBER() OVER (PARTITION BY shipment_id
     22                 ORDER BY shipment_addt_price DESC) = 1
     23            THEN
     24               (max(shipment_price) over (PARTITION BY shipment_id))
     25               + (product_qty - 1) * shipment_addt_price
     26            ELSE
     27               product_qty * shipment_addt_price
     28         END price
     29    FROM shipment_table;
    
    SHIPMENT_ID SHIPMENT_PRICE SHIPMENT_ADDT_PRICE PRODUCT_ID PRODUCT_QTY          PRICE
    ----------- -------------- ------------------- ---------- ----------- --------------
           1000           6,95                   2         11           4        12.9500
           1000           6,95                   2         11           4         8.0000
           1000           6,95                   2         11           4         8.0000
           1000           6,95                   2         11           4         8.0000
           1001          12,95                   2         11           4        18.9500
           1001          12,95                   2         11           4         8.0000
           1001          12,95                   2         11           4         8.0000
           1001          12,95                   2         11           4         8.0000
           1002          20,95                   1         12           2        21.9500
           1002          20,95                   1         12           2         2.0000
           1002          20,95                   1         12           2         2.0000
           1002          20,95                   1         12           2         2.0000
    
    12 rows selected.
    

    Look at line 24 of the query and you will see the difference.

    On your data, there is no difference because the same product as always the same shipment_price...
    We will change a value in the sample data:

    Query 1

    SQL> with shipment_table as (
      2  select 1000 SHIPMENT_ID, 6.95 SHIPMENT_PRICE, 2 SHIPMENT_ADDT_PRICE,
      3         11 PRODUCT_ID, 4 PRODUCT_QTY, 1 VIRTUAL_COLUMN from dual union
      4  select 1000,6.95,2,11,4,2 from dual union
      5  select 1000,100.95,2,11,4,3 from dual union
      6  select 1000,6.95,2,11,4,4 from dual union
      7  select 1001,12.95,2,11,4,5 from dual union
      8  select 1001,12.95,2,11,4,6 from dual union
      9  select 1001,12.95,2,11,4,7 from dual union
     10  select 1001,12.95,2,11,4,8 from dual union
     11  select 1002,20.95,1,12,2,9 from dual union
     12  select 1002,20.95,1,12,2,10 from dual union
     13  select 1002,20.95,1,12,2,11 from dual union
     14  select 1002,20.95,1,12,2,12 from dual)
     15  SELECT
     16     SHIPMENT_ID,
     17     SHIPMENT_PRICE,
     18     SHIPMENT_ADDT_PRICE,
     19     PRODUCT_ID,
     20     PRODUCT_QTY,
     21     CASE
     22        WHEN ROW_NUMBER() OVER (PARTITION BY SHIPMENT_ID
     23             ORDER BY SHIPMENT_ADDT_PRICE DESC) = 1
     24        THEN
     25           SHIPMENT_PRICE + (PRODUCT_QTY - 1) * SHIPMENT_ADDT_PRICE
     26        ELSE
     27           PRODUCT_QTY * SHIPMENT_ADDT_PRICE
     28     END NEW_SHIP_PRICE
     29  FROM shipment_table;
    
    SHIPMENT_ID SHIPMENT_PRICE SHIPMENT_ADDT_PRICE PRODUCT_ID PRODUCT_QTY NEW_SHIP_PRICE
    ----------- -------------- ------------------- ---------- ----------- --------------
           1000           6,95                   2         11           4          12,95
           1000           6,95                   2         11           4              8
           1000           6,95                   2         11           4              8
           1000         100,95                   2         11           4              8
           1001          12,95                   2         11           4          18,95
           1001          12,95                   2         11           4              8
           1001          12,95                   2         11           4              8
           1001          12,95                   2         11           4              8
           1002          20,95                   1         12           2          21,95
           1002          20,95                   1         12           2              2
           1002          20,95                   1         12           2              2
           1002          20,95                   1         12           2              2
    
    12 rows selected.
    

    My query:

    SQL> with shipment_table as (
      2  select 1000 SHIPMENT_ID, 6.95 SHIPMENT_PRICE, 2 SHIPMENT_ADDT_PRICE,
      3         11 PRODUCT_ID, 4 PRODUCT_QTY, 1 VIRTUAL_COLUMN from dual union
      4  select 1000,6.95,2,11,4,2 from dual union
      5  select 1000,100.95,2,11,4,3 from dual union
      6  select 1000,6.95,2,11,4,4 from dual union
      7  select 1001,12.95,2,11,4,5 from dual union
      8  select 1001,12.95,2,11,4,6 from dual union
      9  select 1001,12.95,2,11,4,7 from dual union
     10  select 1001,12.95,2,11,4,8 from dual union
     11  select 1002,20.95,1,12,2,9 from dual union
     12  select 1002,20.95,1,12,2,10 from dual union
     13  select 1002,20.95,1,12,2,11 from dual union
     14  select 1002,20.95,1,12,2,12 from dual)
     15  SELECT shipment_id,
     16         shipment_price,
     17         shipment_addt_price,
     18         product_id,
     19         product_qty,
     20         CASE
     21            WHEN ROW_NUMBER() OVER (PARTITION BY shipment_id
     22                 ORDER BY shipment_addt_price DESC) = 1
     23            THEN
     24               (max(shipment_price) over (PARTITION BY shipment_id))
     25               + (product_qty - 1) * shipment_addt_price
     26            ELSE
     27               product_qty * shipment_addt_price
     28         END price
     29    FROM shipment_table;
    
    SHIPMENT_ID SHIPMENT_PRICE SHIPMENT_ADDT_PRICE PRODUCT_ID PRODUCT_QTY          PRICE
    ----------- -------------- ------------------- ---------- ----------- --------------
           1000           6,95                   2         11           4       106.9500
           1000           6,95                   2         11           4         8.0000
           1000           6,95                   2         11           4         8.0000
           1000         100,95                   2         11           4         8.0000
           1001          12,95                   2         11           4        18.9500
           1001          12,95                   2         11           4         8.0000
           1001          12,95                   2         11           4         8.0000
           1001          12,95                   2         11           4         8.0000
           1002          20,95                   1         12           2        21.9500
           1002          20,95                   1         12           2         2.0000
           1002          20,95                   1         12           2         2.0000
           1002          20,95                   1         12           2         2.0000
    
    12 rows selected.
    

    Max
    http://oracleitalia.WordPress.com

    Published by: Massimo Ruocchio, February 12, 2010 15:10
    Examples added

  • Logical year SQL help...

    Hi, can anyone suggest a better way to please...
    I'm looking to calculate the number of policies here and I want to group counties datewise/yearwise as below:

    (1) County policies added during the month of the date of entry into
    (2) policies County added since (1 month to 1 year)
    (3) policies County added in the second year (i.e. between 1 year and 2nd year)
    (4) County of policies are complemented by year 2 (i.e. > 2 years)

    Below is the example sql with that I came:

    (1) select count (plcy_id)
    from table_1 b
    where
    b.PLCY_EFDT < = b.ORIGINAL_PLCY_EFDT + 30


    (2) select count (plcy_id)
    from table_1 b
    where
    b.PLCY_EFDT (b.ORIGINAL_PLCY_EFDT + 30) AND (b.ORIGINAL_PLCY_EFDT + 360)

    (3) select count (plcy_id)
    from table_1 b
    where
    b.PLCY_EFDT (b.ORIGINAL_PLCY_EFDT + 360) AND (b.ORIGINAL_PLCY_EFDT + 720)


    Count (plcy_id) select 4)
    from table_1 b
    where
    b.PLCY_EFDT > = b.ORIGINAL_PLCY_EFDT + 720


    Please let me know if there is any way to rewrite the query... Thanks for the help.

    better to use the add_month function
    http://download.Oracle.com/docs/CD/B19306_01/server.102/b14200/functions004.htm#SQLRF00603

  • Create function for ODD or EVEN (NUMBER) in pls sql

    Create the function for ODD or EVEN (NUMBER) also
    If number is odd, multiply by 5
    If there is an even number, multiply it by 10;

    865253 wrote:
    Create the function for ODD or EVEN (NUMBER) also
    If number is odd, multiply by 5
    If there is an even number, multiply it by 10;

    create function fn_get_no (n_in in number) return number is
    o_num number;
    begin
    if mod(n_in,2) = 0 then
    o_num := n_in*10;
    else
    o_num := n_in*5;
    end if;
    return o_num;
    end;
    
    select fn_get_no(5) odd, fn_get_no(4) even from dual;
    ODD EVEN
    25 40 
    

    Vivek L

  • error ora-00036 maximum number of levels of sql recursive (50) exceeded

    I have a trigger of the declaration on the LEVELS table, I want to update COMPONENTS field when another field is changed in TIERS, in order to avoid the calculations, I used to ch line shutter COMPONENTS to complete a package, but I get this error ora-00036 maximum number of recursive sql exceeded (50)

    It's my trigger:

    create or replace trigger DBTRGA_UPD_TIERS

      after update on tiers 

     

      I INT ;

     

      FOR I IN (PKG_TIERS. TAB. FIRST)... (PKG_TIERS. TAB. Last) LOOP

    UPDATE to levels

    PUT REFERENCE_EXTERNE = "YYYY"

    WHERE LEVELS = PKG_TIERS. TAB (I). I_TIERS;

      END LOOP;

    end DBTRGA_UPD_TIERS;


    This is ma for each trigger line:

    create or replace trigger DBTRGA_UPD_TIERS_E

    after Update FOR CODE_TYPE_TIERS on LEVELS

      for each row

    V_CPT INTEGER ;

      IF : NEW. CODE_TYPE_TIERS IS NOT NULL THEN

    IF PKG_TIERS. TAB. LAST IS NULL THEN

    PKG_TIERS. TAB(1). I_TIERS: =: NEW. BLEACHERS;

    PKG_TIERS. TAB(1). CODE_TYPE_TIERS: =: NEW. CODE_TYPE_TIERS;

       ELSE

    V_CPT: = PKG_TIERS. TAB. LAST + 1;

       END IF;

      END IF;

    end DBTRGA_UPD_TIERS_E;

    Clearly, you have past of recursion.  After updating the table of LEVELS, then a statement trigger level (after update) as the fires that updates the table LEVELS... which will cause then itself to once again fire... which will cause himself to fire once again... which will cause itself to the light again... etc etc.

    If it is a business logic that should be implemented, so you don't want to include it as a trigger, you want to just have a code in a package that updates the table once.

    If it were me, I would scrap two triggers in this case.  Have a trigger is fired for each row updated (in SQL) which has then cross to the PL/SQL package for details of this update line, means you to act so the context switching between SQL and PL/SQL for each update line, which will affect the performance of the updates.

    Explain what you're actually trying to do (business logic), with a few examples and data tables and we might be able to propose a better approach, which is not abusing triggers in this way.

  • How to check for number of RPC in SQL Server 2000?

    Please let me know how check the number RPC in sql server 2000.

    Hello

    You should post this question in the TechNet forums.

    Forums TechNet - Windows 7 IT Pro

    http://social.technet.Microsoft.com/forums/en/category/w7itpro

  • Display SCSI logic unit number name

    Hello

    I'm working on a script that will rename the name of unit number logic SCSI (the name of the LUN storage in vCenter under/device).  I've seen scripts that will do the name change, however I need to rename based on the ID of LUNS (Lun number).

    I have scripted one pace ID LUN and paths of our (NetApp) storage system to a csv file and that you want to rename in the CSV-based VMware.

    Any help would be greatly appreciated.

    Kind regards

    Simon

    Try something like that.

    Note that the script does not handle duplicate LunIDs!

    # Store the LUN info in a hash table

    # Key is the Monday

    $lunTab = @ {}

    Import-Csv-path testLuns.csv - UseCulture | %{

    $lunTab.Add ($_.) Monday, $_. Path)

    }

    # Look all ESXi

    {foreach ($esx in Get-VMHost)

    $storSys is get-view-Id $esx. ExtensionData.ConfigManager.StorageSystem

    # Look all LUNS and change DisplayName if Monday is in the list

    ScsiLun get - LunType - VmHost disk $esx | %{

    $lunID = $_. RuntimeName.Split('L') [1]

    {if ($lunTab.ContainsKey ($lunID))}

    $newName = $lunTab [$lunID]

    $uuid = $_. ExtensionData.Uuid

    $storSys.UpdateScsiLunDisplayName ($uuid, $newName)

    }

    }

    }

  • ESXi 5.5 - Direct access to the local logical unit number

    I have a 5.5 stand-alone ESXi server.

    It has 2 local SAS controllers

    Controller 1 (HP Smart Array P410): data bank single RAID 10 with two VM's local logic unit number

    Controller 2 (HP Smart Array P812): only RAID 6 LUNS nothing on it yet...

    My question is that I need one of the virtual machine to access that LUN. Is it possible to give direct access to the VM to the controller and/or the unit number logic rather than create a vmdk and massive data store?

    If it is I know Flash would not work on these data, but that really doesn't matter in this case.

    Thank you

    Kenny

    Hi Kenny, and welcome to the communities,

    There is an article dealing with your situation KB: http://kb.vmware.com/kb/1017530

    Concerning

    Tim

  • When you add a logical unit number to a an ASM diskgroup, made any start to process internally

    Version: 10.2, 11.2 on Unix/Linux platforms
    When you add a number logic unit to an ASM diskgroup, done any what beginning of process internally?

    Hello;

    I think that oracle ASM can start a process to restore balance after the addition of a Lun.

    http://docs.Oracle.com/CD/E11882_01/server.112/e18951/asmdiskgrps.htm

    Best regards

    mseberg

Maybe you are looking for

  • Problem, I continue to see messages Foxfire is obsolete, BUT I have the 30.0 version

    I Facebook, he says that Foxfire (version 30.0) is obsolete. I use Hulu or cracked, and again it is said that Foxfire is obsolete. I go to mozilla Support on the top side of the page, to the left of the screen, it says: "your Foxfire is out of date a

  • Trash folder

    Hi everyone, I don't know if you have this problem, but after upgrading to OS X 10.11.4, I can't empty / remove any of the emails in the trash of the Email app. I click on delete (an or command-a + delete) but clicking on another folder (such as Inbo

  • Qosmio F10: D - verse SCART for watching movies?

    Hello I have a problem I used the d - to the SCART and connected socket on my TV of LSD, but it sends no signal to my LCD tv.This cable work?I tried with a video cable s and who does not work either.Can someone help me please I want to use d-cable SC

  • 920 cartridge looks different than the old

    I just bought some new ink cartridges for printer Officejet 6500 a 710n-z.  I bought the same cartridges 920 XL that I bought and installed successfully before.  However, the most recent wouldn't install! These new cartridges are different that the o

  • Laptop TrackPad Sensitivty

    Hello: I have a HP laptop dv6500z running 32-bit Windows Home Premium. My trackpad sensitivity got zero due to execution installerinstructions HP printer: it out cleaned files 'useless' and restore settings, etc. Pointer Ctrl Panel allows me to chang