Skyline Query Challenge

Suppose you are shopping for a new car, and are specifically looking for a big
car with decent gas mileage. Unfortunately, we are trying to satisfy the two
conflicting goals. If we are querying the Cars relation in the database, then we
certainly can ignore all models that are worse than others by both criteria. The
remaining set of cars is called the Skyline.
More formally the Skyline is defined as those points which are not dominated by
any other point. A point dominates the other point if it is as good or better in
all the dimensions. For example Roadster with mileage=20 and seating=2 dominates
Ferrari F1 with mileage=10 and seating=1. This condition can certainly be
expressed in SQL. In our example, the Skyline query is
select * from Cars c
where not exists (
select * from Cars cc
where cc.seats >= c.seats and cc.mileage > c.mileage
or cc.seats > c.seats and cc.mileage >= c.mileage
Despite apparent simplicity of this query, you I'm not pleased with this query
performance. Even if we index the seats and mileage columns, this wouldn’t help
much as only half of the records on average meet each individual inequality
condition. Certainly, there aren’t many records that satisfy the combined
predicate, but we can’t leverage index to match it. Bitmapped indexes, which
excel with Boolean expressions similar to what we have, demand a constant on one
side of the inequality predicate.
There is an efficient way to answer Skyline query. Order all the data by either
seats, mileage, or mileage, seats. Here a composite index might be handy.
Compare each record with its predecessor, and discard the one that is dominated
by the other. Let’s assume that our example has been extended to 4 records:
MANUFACTURER SEATS MILEAGE
Hummer 4 5
Ferrari 1 10
BMW 2 15
Roadster 2 20
Iterating record by record down, Ferrari would be excluded first, because it’s
dominated by the neighbor BMW. Then, BMW itself would be excluded, because it’s
dominated by Roadster.
The challenge is to express this algorithm (efficiently) in SQL. With or without Analytics.

Mike:
Performance aside, see if you can reproduce:
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.6.0 - Production
With the Partitioning, Oracle Label Security, OLAP and Oracle Data Mining options
JServer Release 9.2.0.6.0 - Production
flip@FLOP> CREATE TABLE CARS
  2  (
  3  MANUFACTURER VARCHAR2(10 BYTE),
  4  SEATS NUMBER(2),
  5  MILEAGE NUMBER
  6  );
Table created.
Elapsed: 00:00:00.00
flip@FLOP>
flip@FLOP> insert into cars
  2  select to_char(object_id) , mod(object_id,7)+1, mod(object_id,25)+25
  3  from   all_objects where rownum < 101;
100 rows created.
Elapsed: 00:00:00.00
flip@FLOP>
flip@FLOP> insert into cars
  2  select 'x'||manufacturer, seats, mileage
  3  from    cars;
100 rows created.
Elapsed: 00:00:00.00
flip@FLOP>
flip@FLOP> commit;
Commit complete.
Elapsed: 00:00:00.00
flip@FLOP>
flip@FLOP> CREATE OR REPLACE FORCE VIEW CARS_VIEW
  2  (MANUFACTURER,SEATS, MILEAGE, LVL, RNM, SKLN_FLG)
  3  AS
  4  select
  5  MANUFACTURER,
  6  seats,
  7  mileage,
  8  level lvl,
  9  rownum rnm,
10  (
11  case when level=rownum then 1
12  else 0
13  end
14  ) skln_flg
15  from
16  (
17  select
18  MANUFACTURER,
19  seats,
20  mileage
21  from cars
22  order by mileage desc
23  )
24  connect by prior seats < seats ;
View created.
Elapsed: 00:00:00.00
flip@FLOP>
flip@FLOP> select
  2  c.* from cars c,
  3  (
  4  select mileage,seats from cars_view where rownum <
  5  (
  6  select rnm from cars_view
  7  where skln_flg=0
  8  and rownum < 2
  9  )
10  )sbq
11  where c.mileage = sbq.mileage
12  and c.seats = sbq.seats
13  ;
MANUFACTUR      SEATS    MILEAGE
9849                1         49
x9849               1         49
9249                3         49
x9249               3         49
x22724              3         49
22724               3         49
14424               5         49
x14424              5         49
15174               6         49
x15174              6         49
7349                7         49
x7349               7         49
12 rows selected.
Elapsed: 00:00:00.00
flip@FLOP>
flip@FLOP> select * from Cars c
  2  where not exists (
  3  select * from Cars cc
  4  where cc.seats >= c.seats and cc.mileage > c.mileage
  5  or cc.seats > c.seats and cc.mileage >= c.mileage
  6  );
MANUFACTUR      SEATS    MILEAGE
7349                7         49
x7349               7         49
Elapsed: 00:00:00.00
flip@FLOP>

Similar Messages

  • Query challenge (IN ) not allow more than 1000 numbers

    I have this query in the "IN" condition i am passing more than 1000 numbers and it is showing me error "I can insert more than 1000 numbers in "in" condition" like
    number like 1,2,3,4,..... until 1000. I am getting error.
    How can i write this query as differently
    select a.barcode,a.othernumber,a.owner,b.booknum,b.context,c.dept,c.userid,
    c.firstname,c.lastname from objects a,bookitem b,users c
    where a.othernumber=b.booknum
    AND c.userid=a.owner(+)
    AND booknum in (#valuelist(search.booknum)#)
    order by barcode

    you could use a UNION
    e.g.
    select oid, dob from mytable
    where oid in ( mylist of first 1000 )
    UNION
    select oid, dob from mytable
    where oid in ( mylist of the next 1000 or less )
    I have this query in the "IN" condition i am passing more than 1000 numbers and it is showing me error "I can insert more than 1000 numbers in "in" condition" like
    number like 1,2,3,4,..... until 1000. I am getting error.
    How can i write this query as differently
    select a.barcode,a.othernumber,a.owner,b.booknum,b.context,c.dept,c.userid,
    c.firstname,c.lastname from objects a,bookitem b,users c
    where a.othernumber=b.booknum
    AND c.userid=a.owner(+)
    AND booknum in (#valuelist(search.booknum)#)
    order by barcode

  • SQL Query (challenge)

    Hello,
    I have 2 tables of events E1 and E2
    E1: (Time, Event), E2: (Time, Event)
    Where the columns Time in both tables are ordered.
    Ex.
       E1: ((1, a) (2, b) (4, d) (6, c))
       E2: ((2, x) (3, y) (6, z))
    To find the events of both tables at the same time it is obvious to do & join between E1 and E2
    Q1 -> select e1.Time, e1.Event, e2.Event from E1 e1, E2 e2 where e1.Time=e2.Time;
    The result of the query is:
    ((2, b, x) (6, c, z))
    Given that there is no indexes for this tables, an efficient execution plan can be a hash join (under conditions mentioned in Oracle Database Performance Tuning Guide Ch 14).
    Now, the hash join suffers from locality problem if the hash table is large and does not fit in memory; it may happen that one block of data is read in memory and swaped out frequently.
    Given that the Time columns are sorted is ascending order, I find the following algorithm, known idea in the literature, apropriate to this problem; The algorithm is in pseudocode close to pl/sql, for simplicity (I home the still is clear):
    -- start algorithm
    open cursors for e1 and e2
    loop
      if e1.Time = e2.Time then
         pipe row (e1.Time, e1.Event, e2.Event);
         fetch next e1 record
         exit when notfound
         fetch next e2 record
          exit when notfound
      else
         if e1.Time < e2.Time then
            fetch next e1 record
            exit when notfound
         else
            fetch next e2 record
            exit when notfound
         end if;
      end if;
    end loop
    -- end algorithm
    As you can see the algorithm does not suffer from locality issue since it iterates sequentially over the arrays.
    Now the problem: The algorithm shown below hints the use of pipelined function to implement it in pl/sql, but it is slow compared to hash join in the implicit cursor of the query shown above (Q1).
    Is there an implicit SQL query to implement this algorithm? The objective is to beat the hash join of the query (Q1), so queries that use sorting are not accepted.
    A difficulty I foound is that the explicit cursor are much slower that implict ones (SQL queries)
    Example: for a large table (2.5 million records)
    create table mytable (x number);
    declare
    begin
    open c for 'select 1 from mytable';
    fetch c bulk collect into l_data;
    close c;
    dbms_output.put_line('couont = '||l_data.count);
    end;
    is 5 times slower then
    select count(*) from mytable;
    I do not understand why it should be the case, I read that this may be explained because pl/sql is interpreted, but I think this does not explain the whole issue. May be because the fetch copies data from one space to your space and this takes a long time.

    Hi
    A correction in the algorithm:
    -- start algorithm
    open cursors for e1 and e2
    fetch next e1 record
    fetch next e2 record
    loop
      exit when e1%notfound
      exit when e2%notfound
      if e1.Time = e2.Time then
         pipe row (e1.Time, e1.Event, e2.Event);
         fetch next e1 record
         fetch next e2 record
      else
         if e1.Time < e2.Time then
            fetch next e1 record
         else
            fetch next e2 record
         end if;
      end if;
    end loop
    -- end algorithm
    Best regards
    Taoufik

  • ABAP Query Challenge

    I asked this question last time as well but with no positive/helpful answer. I need to develop a query ( SQ01) in which the user just wants to see when was the most recent posting done in the given G/L account. If i pull posting date it will give me all the dates in  chronical order or so which i dont really want.
    Lets get your gray/sap matter working
    THanks

    hi,
    here's a sample for table bsis:
    REPORT zzzz126.
    TABLES bsis.
    SELECT-OPTIONS: s_bukrs FOR bsis-bukrs,
                    s_saknr FOR bsis-hkont,
                    s_blart FOR bsis-blart.
    DATA: BEGIN OF itab ,
                  bukrs TYPE bsak-bukrs,
                  gjahr TYPE bsak-gjahr,
                  belnr TYPE bsak-belnr,
                  bldat TYPE bsak-bldat,
                END OF itab.
    SELECT bukrs gjahr belnr MAX( bldat ) FROM bsis INTO itab
              WHERE bukrs IN s_bukrs
                  AND hkont IN s_saknr
                  AND blart IN s_blart
                  GROUP BY BUKRS gjahr BELNR.
    ENDSELECT.
    WRITE: / itab-bukrs,  itab-belnr, itab-gjahr, itab-bldat.
    A.

  • Hierarchical and summary 2

    Hi
    In the last week I post a thread about SUM hierarchical ( sum in levels in the tree )
    Re: hierarchical  and summary and division : Query Challenge  2
    With help , I got to do , but the cost is very high, I explain again what I must to do
    I have a table (GRUPO_RELACAO) that save information about parent and child, but in this relation
    each group can to be different sectors, when I must to sum a Sector I can not to sum groups of other
    sector. look example below with respective values
                   1[3]
                /     \  nm_ciclo_fim is not null then 4 do not have parent                     
                 2[1]      4[1]
             5[3]  6[4] 8[2] 7[2]  
             |               
             |            
             11[6]
          9[5]   12[0]
             13[8]   16[4] 1,2,4,5,6,7,8,9 are in SECTOR 16
    11,12,13,16 are in SECTOR 18
    In structure of table (GRUPO_RELACAO) there is a column NM_CICLO_FIM that must to be NULL
    if is not null, then Child is not more relation with parent , in Example 4 was child of 1, but The
    column is filled
    In example below , to show only SECTOR 16
         GROUP     SUM     LEVEL    SECTOR
         1     1     1     16   TEAM OF GROUP 1 ONLY 1 (GROUP 2) because group 4 is not child
         1     12     2     16   NET      (GROUPS 5 AND 6 AND 9 , but no 11 because 11 is sector 18)
         1     16     3     16   NETWORK   (GROUPS 1,2,5,6,9) = 3+1+3+4+5 = 16
         2     7     1     16   TEAM OF GROUP 2
         2     5     2     16   NET OF GROUP 2
         2     8     3     16   NETWORK OF GROUP 2
         3     0     1     16   IS NOT IN TREE BUT IS IN T_ESTRUTURA_COMERCIAL
         3     0     2     16   IDEM ABOVE
         3     0     3     16   IDEM ABOVE
         4     3     1     16   TEAM OF GROUP 4
         4     0     2     16   NET OF GROUP 4
         4     4     3     16   NETWORK OF GROUP 4
         5     0     1     16   TEAM OF GROUP 5 (11 IS SECTOR 18) NO SUM
         5     5     2     16   NET OF GROUP 5  (9 IS SECTOR 16 ) SUM
         5     3     3     16   NETWORK  OF GROUP 5  (5 + 3 ) SELF VALUE
         6     0     1     16
         6     0     2     16
         6     4     3     16
         7     0     1     16
         7     0     2     16
         7     1     3     16
         8     0     1     16
         8     0     2     16
         8     2     3     16
         9     0     1     16
         9     0     2     16
         9     5     3     16
         165     0     1     16 Others groups that are not in tree must to show too
         165     0     2     16 in example only group 16
         165     0     3     16
         193     0     1     16
         193     0     2     16
         193     0     3     16
         194     0     1     16
         194     0     2     16
         194     0     3     16
         705     0     1     16
         705     0     2     16
         705     0     3     16
         706     0     1     16
         706     0     2     16
         706     0     3     16
         714     0     1     16
         714     0     2     16
         714     0     3     16
         715     0     1     16
         715     0     2     16
         715     0     3     16
         803     0     1     16
         803     0     2     16
         803     0     3     16
         804     0     1     16
         804     0     2     16
         804     0     3     16
         805     0     1     16
         805     0     2     16
         805     0     3     16
    Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.2.0
    Connected as PROCSCNOB
    SQL>
    SQL> SELECT T1.CD_EC_MADRINHA,
      2         T1.CD_EC_AFILHADA,
      3         ID_TIPO_RELACAO,
      4         NM_CICLO_FIM
      5   FROM SISSCNOB.GRUPO_RELACAO T1
      6  ORDER BY 1;
    CD_EC_MADRINHA CD_EC_AFILHADA ID_TIPO_RELACAO NM_CICLO_FIM
                 1              2               1
                 1              4               1       200802
                 2              5               1
                 2              6               1
                 4              7               1
                 4              8               1
                 5             11               1
                11              9               1
                11             12               1
                12             13               1
                12             16               1
    11 rows selected
    SQL> SELECT T2.CD_ESTRUTURA_COMERCIAL, T2.CD_SETOR, T2.ID_ESTRUTURA_ATIVA
      2     FROM T_ESTRUTURA_COMERCIAL T2
      3    WHERE T2.CD_TIPO_ESTRUTURA_COMERCIAL = 5
      4      AND T2.CD_ESTRUTURA_COMERCIAL IN (1,2,3,4,5,6,7,8,9,11,12,13,16);
    CD_ESTRUTURA_COMERCIAL CD_SETOR ID_ESTRUTURA_ATIVA
                         1       16                  1
                         2       16                  1
                         3       16                  0
                         4       16                  1
                         5       16                  1
                         6       16                  1
                         7       16                  1
                         8       16                  1
                         9       16                  1
                        11       18                  1
                        12       18                  1
                        13       18                  1
                        16       18                  1
    13 rows selected
    SQL> SELECT T3.CD_ESTRUTURA_COMERCIAL,
      2         T3.NM_CICLO_OPERACIONAL,
      3         T3.CD_INDICADOR,
      4         T3.VL_INDICADOR
      5     FROM T_EC_SETOR_PERFIL_CICLO T3
      6    WHERE  T3.CD_TIPO_ESTRUTURA_COMERCIAL = 5
      7     AND T3.CD_ESTRUTURA_COMERCIAL IN (1,2,3,4,5,6,7,8,9,11,12,13,16)
      8     AND T3.NM_CICLO_OPERACIONAL = 200712
      9     AND T3.CD_PERFIL = 1
    10     AND T3.CD_INDICADOR = 63;
    CD_ESTRUTURA_COMERCIAL NM_CICLO_OPERACIONAL CD_INDICADOR      VL_INDICADOR
                         1               200712           63              3.00
                         2               200712           63              1.00
                         4               200712           63              1.00
                         5               200712           63              3.00
                         6               200712           63              4.00
                         7               200712           63              1.00
                         8               200712           63              2.00
                         9               200712           63              5.00
                        11               200712           63              6.00
                        13               200712           63              8.00
                        16               200712           63              4.00
    11 rows selected
    THE TABLES
    -- In this table ID_TIPO_RELACAO must to be 1 and NM_CICLO_FIM IS NULL
    --- When NM_CICLO_FIM not is null the relation between cd_ec_madrinha and cd_ec_afilhada is break
    WITH GRUPO_RELACAO AS(
    select   1  CD_EC_MADRINHA,
             2 CD_EC_AFILHADA,
             1 ID_TIPO_RELACAO ,
             TO_NUMBER(NULL,0) NM_CICLO_FIM  FROM DUAL UNION
    SELECT   1,4,1,200802 FROM DUAL UNION
    SELECT   2,5,1,TO_NUMBER(NULL,0) FROM DUAL UNION
    SELECT   2,6,1,TO_NUMBER(NULL,0) FROM DUAL UNION
    SELECT   4,7,1,TO_NUMBER(NULL,0) FROM DUAL UNION
    SELECT   4,8,1,TO_NUMBER(NULL,0) FROM DUAL UNION
    SELECT   5,11,1,TO_NUMBER(NULL,0) FROM DUAL UNION
    SELECT   11,9,1,TO_NUMBER(NULL,0) FROM DUAL UNION
    SELECT   11,12,1,TO_NUMBER(NULL,0) FROM DUAL UNION
    SELECT   12,13,1,TO_NUMBER(NULL,0) FROM DUAL UNION
    SELECT   12,16,1,TO_NUMBER(NULL,0) FROM DUAL )
    ---  Here  table master all CD_EC_MADRINHA and CD_EC_AFILHADA from GRUPO_RELACAO
    ---  is in this table , There is data here that are not in table GRUPO_RELACAO
    ----  in JOIN  with GRUPO_RELACAO, the column   ID_ESTRUTURA_ATIVA must to be 1
    ----- When to sum a SECTOR  it must to sum only values of the SECTOR
    --- I can to have Group of a sector that is parent of the other group
    ---  In example I showed  Group 5(group 16) is parent of the group 11 (sector 18)
    -- that is parent of the group 9 (group 16)
    WITH T_ESTRUTURA_COMERCIAL AS(
    select    1 CD_ESTRUTURA_COMERCIAL,      16 CD_SECTOR,1 ID_ESTRUTURA_ATIVA FROM DUAL union
    select    2, 16,1 FROM DUAL UNION
    select    3,16,0 FROM DUAL UNION
    select    4,16,1 FROM DUAL UNION
    select    5,16,1 FROM DUAL UNION
    select    6,16,1 FROM DUAL UNION
    select    7,16,1 FROM DUAL UNION
    select    8,16,1 FROM DUAL UNION
    select    9,16,1 FROM DUAL UNION
    select    11,18,1 FROM DUAL UNION
    select    12,18,1 FROM DUAL UNION
    select    13,18,1 FROM DUAL UNION
    select    16,18,1 FROM DUAL )
    SELECT * FROM T_ESTRUTURA_COMERCIAL
    -- The 3rd and last table
    -----this Table have the values that I must to sum in LEVELS
    WITH T_EC_SETOR_PERFIL_CICLO AS (
    SELECT      1 CD_ESTRUTURA_COMERCIAL, 200712 NM_CICLO_OPERACIONAL,63 CD_INDICADOR ,3.00 VL_INDICADOR FROM DUAL UNION
    SELECT   2,200712,63,1.00 FROM DUAL UNION
    SELECT   4,200712,63,1.00 FROM DUAL UNION
    SELECT   5,200712,63,3.00 FROM DUAL UNION
    SELECT   6,200712,63,4.00 FROM DUAL UNION
    SELECT   7,200712,63,1.00 FROM DUAL UNION
    SELECT   8,200712,63,2.00 FROM DUAL UNION
    SELECT   9,200712,63,5.00 FROM DUAL UNION
    SELECT   11,200712,63,6.00 FROM DUAL UNION
    SELECT   13,200712,63,8.00 FROM DUAL UNION
    SELECT   16, 200712,63,4.00 FROM DUAL )
    SELECT * FROM T_EC_SETOR_PERFIL_CICLOSomebody can help me, because my solution is with COST high
    TIA

    That plan is better, don't know why you did not post
    this follow up in your old thread, where I asked
    whether
    [url=http://forums.oracle.com/forums/message.jspa?m
    essageID=2330090#2330090]42 billion rows
    was roughly the size of the results you expected.
    If you widen your sqlplus window and set linesize
    140 it would look like the output below.
    Unfortunately then we can see things like this
    |  50 |     VIEW                               |
    |   120T|  5474T|
    |   148G (96)|999:59:59 |
    Where I believe the T in 120T stands for
    tera[
    /b] or 120 trillion rows, that is 120,000,000,000,000
    or a right
    shedfull as it is sometimes known. The next step is
    to sort these rows, which could take some time,
    999:59:59
    is the estimate which looks suspiciously like just
    the biggest number the optimizer can produce.
    In your query you have several connect by statements
    but no start withs. This will generate every possible
    permutation
    of the hierarchy, I think in one place the results
    are used as the input of another query that then
    regenerates every
    possible permutation of that hierarchy.
    In these cases I am not really sure what you think
    you are trying to do apart from generate the biggest
    result set
    ever known to mankind. This is what happens to the 14
    row emp table when subjected to some of these
    queries. What
    are the original row counts of the tables you are
    doing this to in the query?
    SQL> select count(*)
    2  from emp
    3  start with mgr is null
    4  connect by mgr = prior empno;
    COUNT(*)
    14
    elect count(*)
    2  from emp
    3  connect by mgr = prior empno;
    COUNT(*)
    39
    di
    Wrote file afiedt.buf
    1  select count(*) from
    2    (
    3    select mgr, empno
    4    from emp
    5    connect by mgr = prior empno
    6    )
    7* connect by mgr = prior empno
    L> /
    COUNT(*)
    261
    The formatted plan
    [pre]
    Execution Plan
    Plan hash value: 3341212558
    | Id  | Operation                              | Name
    | Rows  | Bytes |TempSpc| Cost
    (%CPU)| Time     |
    |   0 | SELECT STATEMENT                       |
    |    48G|  2910G|       |
    150G (95)|999:59:59 |
    1 |  TEMP TABLE TRANSFORMATION             |
    |       |       |       |
    |          |
    OAD AS SELECT                       |
    |       |       |       |
    |          |
    HASH GROUP BY                       |
    |    13 |  2366 |       |
    14  (29)| 00:00:01 |
    4 |     VIEW                               |
    |    13 |  2366 |
    |    13  (24)| 00:00:01 |
    |      NESTED LOOPS OUTER                |
    |    13 |   949 |       |
    13  (24)| 00:00:01 |
    6 |       VIEW                             |
    |    13 |   845 |
    |     8  (38)| 00:00:01 |
    |        SORT UNIQUE                     |
    |    13 |  1150 |       |
    8  (63)| 00:00:01 |
    8 |         UNION-ALL                      |
    |       |       |       |
    |          |
    VIEW                          |
    |    12 |  1092 |       |
    3   (0)| 00:00:01 |
    0 |           NESTED LOOPS                 |
    |    12 |   516 |       |
    3   (0)| 00:00:01 |
    1 |            VIEW                        |
    |    11 |   319 |       |
    3   (0)| 00:00:01 |
    2 |             TABLE ACCESS FULL          |
    GRUPO_RELACAO               |    11 |    88 |       |
    3   (0)| 00:00:01 |
    3 |            TABLE ACCESS BY INDEX ROWID |
    M_ESTRUTURA_COMERCIAL_CAD   |     1 |    14 |       |
    0   (0)| 00:00:01 |
    4 |             INDEX UNIQUE SCAN          |
    I0_ESTRUTURA_COMERCIAL      |     1 |       |       |
    0   (0)| 00:00:01 |
    5 |          HASH JOIN ANTI                |
    |     1 |    58 |       |
    3  (34)| 00:00:01 |
    6 |           VIEW                         |
    |    12 |   660 |       |
    1   (0)| 00:00:01 |
    7 |            NESTED LOOPS                |
    |    12 |   204 |       |
    1   (0)| 00:00:01 |
    8 |             VIEW                       |
    |    11 |    33 |       |
    1   (0)| 00:00:01 |
    9 |              INDEX FULL SCAN           |
    GRP_RELAC_PK                |    11 |    33 |       |
    1   (0)| 00:00:01 |
    0 |             TABLE ACCESS BY INDEX ROWID|
    M_ESTRUTURA_COMERCIAL_CAD   |     1 |    14 |       |
    0   (0)| 00:00:01 |
    1 |              INDEX UNIQUE SCAN         |
    I0_ESTRUTURA_COMERCIAL      |     1 |       |       |
    0   (0)| 00:00:01 |
    2 |           INDEX FULL SCAN              |
    GRP_RELAC_PK                |    11 |    33 |       |
    1   (0)| 00:00:01 |
    3 |       VIEW                             |
    |     1 |     8 |       |
    0   (0)| 00:00:01 |
    4 |        INLIST ITERATOR                 |
    |       |       |       |
    |          |
    TABLE ACCESS BY INDEX ROWID    |
    T_EC_SETOR_PERFIL_CICLO     |     1 |    23 |       |
    0   (0)| 00:00:01 |
    6 |          INDEX RANGE SCAN              |
    I0_SETOR_PERFIL_CICLO       |     1 |       |       |
    0   (0)| 00:00:01 |
    7 |   LOAD AS SELECT                       |
    |       |       |       |
    |          |
    SORT UNIQUE                         |
    |     2 |   129 |       |
    4818K (57)| 16:03:40 |
    29 |     UNION-ALL                          |
    |       |       |       |
    |          |
    VIEW                              |
    |     1 |    59 |       |
    2409K (13)| 08:01:50 |
    31 |       SORT UNIQUE                      |
    |     1 |    81 |       |
    2409K (13)| 08:01:50 |
    32 |        WINDOW SORT                     |
    |     1 |    81 |       |
    2409K (13)| 08:01:50 |
    * 33 |         VIEW                           |
    |     1 |    81 |       |
    2409K (13)| 08:01:50 |
    34 |          SORT UNIQUE                   |
    |     1 |    91 |       |
    2409K (13)| 08:01:50 |
    * 35 |           CONNECT BY WITHOUT FILTERING |
    |       |       |       |
    |          |
    COUNT                       |
    |       |       |       |
    |          |
    VIEW                       |
    |   316M|    26G|       |
    2193K  (4)| 07:18:39 |
    38 |              TABLE ACCESS FULL         |
    SYS_TEMP_0FD9D6A2F_44B054AC |   316M|    26G|       |
    2193K  (4)| 07:18:39 |
    39 |      VIEW                              |
    |     1 |    70 |       |
    2409K (13)| 08:01:50 |
    40 |       SORT UNIQUE                      |
    |     1 |    65 |       |
    2409K (13)| 08:01:50 |
    41 |        WINDOW SORT                     |
    |     1 |    65 |       |
    2409K (13)| 08:01:50 |
    * 42 |         VIEW                           |
    |     1 |    65 |       |
    2409K (13)| 08:01:50 |
    43 |          SORT UNIQUE                   |
    |     1 |    91 |       |
    2409K (13)| 08:01:50 |
    * 44 |           CONNECT BY WITHOUT FILTERING |
    |       |       |       |
    |          |
    COUNT                       |
    |       |       |       |
    |          |
    VIEW                       |
    |   316M|    26G|       |
    2193K  (4)| 07:18:39 |
    47 |              TABLE ACCESS FULL         |
    SYS_TEMP_0FD9D6A2F_44B054AC |   316M|    26G|       |
    2193K  (4)| 07:18:39 |
    48 |   WINDOW BUFFER                        |
    |    48G|  2910G|       |
    150G (95)|999:59:59 |
    49 |    MERGE JOIN SEMI                     |
    |    48G|  2910G|
    |   148G (96)|999:59:59 |
    |     VIEW                               |
    |   120T|  5474T|       |
    148G (96)|999:59:59 |
    51 |      SORT ORDER BY                     |
    |   120T|     9P|
    23P|   148G (96)|999:59:59 |
    52 |       MERGE JOIN OUTER                 |
    |   120T|     9P|       |
    6506M(100)|999:59:59 |
    53 |        SORT JOIN                       |
    |  1900M|    42G|   128G|
    38M  (9)|126:57:43 |
    54 |         VIEW                           |
    |  1900M|    42G|       |
    13M (13)| 46:09:55 |
    55 |          SORT UNIQUE                   |
    |  1900M|    23G|    71G|
    13M (71)| 46:09:55 |
    56 |           UNION-ALL                    |
    |       |       |       |
    |          |
    VIEW                        |
    |   633M|  7853M|       |
    91649 (100)| 00:18:20 |
    |  58 |             TABLE ACCESS FULL          |
    SYS_TEMP_0FD9D6A30_44B054AC |   633M|    50G|       |
    91649 (100)| 00:18:20 |
    |  59 |            VIEW                        |
    |   633M|  7853M|       |
    91649 (100)| 00:18:20 |
    |  60 |             TABLE ACCESS FULL          |
    SYS_TEMP_0FD9D6A30_44B054AC |   633M|    50G|       |
    91649 (100)| 00:18:20 |
    |  61 |            VIEW                        |
    |   633M|  7853M|       |
    91649 (100)| 00:18:20 |
    |  62 |             TABLE ACCESS FULL          |
    SYS_TEMP_0FD9D6A30_44B054AC |   633M|    50G|       |
    91649 (100)| 00:18:20 |
    |* 63 |        SORT JOIN                       |
    |   633M|    40G|    99G|
    18M  (4)| 60:03:28 |
    64 |         VIEW                           |
    |   633M|    40G|       |
    91649 (100)| 00:18:20 |
    |  65 |          TABLE ACCESS FULL             |
    SYS_TEMP_0FD9D6A30_44B054AC |   633M|    50G|       |
    91649 (100)| 00:18:20 |
    |* 66 |     SORT UNIQUE                        |
    |     1 |    14 |       |
    1 (100)| 00:00:01 |
    7 |      TABLE ACCESS BY INDEX ROWID       |
    M_ESTRUTURA_COMERCIAL_CAD   |     1 |    14 |       |
    0   (0)| 00:00:01 |
    8 |       INDEX RANGE SCAN                 |
    I0_ESTRUTURA_COMERCIAL      |   811 |       |       |
    0   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
    14 - access("W1"."CD_TIPO_ESTRUTURA_COMERCIAL"=5
    AND
    "W2"."CD_EC_AFILHADA"="W1"."CD_ESTRUTURA_COMERCIAL")
    15 - access("CD_EC_AFILHADA"="W2"."CD_EC_MADRINHA")
    21 - access("W1"."CD_TIPO_ESTRUTURA_COMERCIAL"=5 AND
    "W2"."CD_EC_MADRINHA"="W1"."CD_ESTRUTURA_COMERCIAL")
    26 - access("W"."CD_TIPO_ESTRUTURA_COMERCIAL"=5 AND
    "T"."CD_EC_AFILHADA"="W"."CD_ESTRUTURA_COMERCIAL"
    AND
    "W"."NM_CICLO_OPERACIONAL"=200712 AND
    "W"."CD_PERFIL"=1 AND ("W"."CD_INDICADOR"=0 OR
    "W"."CD_INDICADOR"=63))
    33 - filter("CD_EC_AFILHADA"<>"PARENT" AND
    "CD_SETOR"=16 AND "ID_ESTRUTURA_ATIVA"=1)
    35 - access("CD_EC_MADRINHA"=PRIOR
    "CD_EC_AFILHADA")
    filter("ID_ESTRUTURA_ATIVA"=1)
    filter("CD_SETOR"=16 AND "ID_ESTRUTURA_ATIVA"=1)
    44 - access("CD_EC_MADRINHA"=PRIOR
    "CD_EC_AFILHADA")
    filter("ID_ESTRUTURA_ATIVA"=1)
    access("T1"."LVL"="T2"."LVL"(+))
    filter("T1"."PARENT"="T2"."PARENT"(+) AND
    "T1"."LVL"="T2"."LVL"(+))
    66 - access("T3"."CD_ESTRUTURA_COMERCIAL"="PARENT")
    filter("T3"."CD_ESTRUTURA_COMERCIAL"="PARENT")
    filter("T3"."CD_SETOR"=16 AND
    "T3"."ID_ESTRUTURA_ATIVA"=1)
    68 - access("T3"."CD_TIPO_ESTRUTURA_COMERCIAL"=5)
    /pre]Hi thank
    In your query you have several connect by statements
    but no start withs. This will generate every possible
    permutationPlease, [b] in my example how can  put a start with, that walk all tree ?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • URGENT : Challenge questions query: Oracle Access Manager 10g

    Hi all,
    This is a query regarding password challenge questions in Oracle Access Manager 10g. We have created password policies for a specific container in OID (say cn=xxx,cn=users,dc=oracle,dc=com) and it is working fine.
    In order to exclude certain set of users (say user ABC ) for password policies, we have set the obpasswordchangeflag to false for those users which are in same container for which password policy is created.
    When we try to login to the application with the user say ABC, I am not seeing any reset password page - I am happy till this point. However it is showing Configure Challenge questions page. Is there any way to bypass this page? Or is this the expected behavior?
    This is very urgent and prompt reply is very much appreciated.
    -Mahendra

    Hi Mahendra,
    This is expected behaviour. In order to exclude the password policy management for some certains user for particular domain/container. please add the below configuration parameter to your OAM10g password policy.
    Password Policy Filter Field     (!(|(cn=xxx)(cn=abc)))
    ----Ajay

  • Unique Query Performance Challenge

    Experts,
    Please I need help in this area.  I have a query written from a Multi-Provider. The query is using 98% of its data from 1 base cube. Currently it takes about 4 minutes to run and I want to bring it down to 1 minute.
    This query is run off a web template and it is not static. The users can drilldown in any direction as required. The performance is more of a problem from the drilldown.
    This query is a cost report with a lot of calculated and restricted key figures, and also a lot of excludes and includes all within the key figures.
    The query has 13 restricted key figures and 5 calculated using the restricted, so 18 in all. Each restricted key figure resembles this example:
    •     Cost Element (hierarchy restriction or singles values or ranges restriction)
    •     Sender/Receiver
    •     Version
    •     Value Type
    •     Amount
    I believe the complex restrictions are slowing this report down. At the moment I am trying to speed up this report and it has proved a big challenge.
    Has anybody experienced a similar challenge before?
    Please do not point me to OSS notes or the standard performance documents. I have tried all that. Is there something else beyond those that can help here? Maybe a trick someone has tried?
    Help!!

    Thank you all for replying:
    This Problem is still NOT solved but I have more Information.
    This query Contains a heirarchy (Main CH in row) and a second object contains hierachy also but selected via Authorisation in the User Profile.
    Acutually both hierarchies are selected in Authorisation from the User profile but once the User is in the report, the User can drilldown in the displayed hierarchy depending on thier authorisation.
    But most users are at the highest level in both hierarchies so they drilldown on both hierarchies and this is done via a separate selection Section in the Web template.
    I am trying to build the exact picture of my scenario.....pls any help.
    With this new information, Can I still do the following:
    Buffer Hier or Cache by Hier level???

  • 9i Forms Challenge! Query records from a data block based on this view

    According to Oracle Support, it is not unusual that complicated views like the one shown below fail to work in Forms. They recommend that I "try using a When-validate trigger" to solve the problem. I would be grateful if someone could show me how. It would also be helpful if someone could explain why Forms would have a problem querying such a view in the first place.
    Thanks very much!
    CREATE TABLE t (
    id NUMBER,
    property VARCHAR2(2),
    val NUMBER
    INSERT INTO t VALUES ( 1, 'A', 10 );
    INSERT INTO t VALUES ( 1, 'B', 11 );
    INSERT INTO t VALUES ( 2, 'A', 12 );
    INSERT INTO t VALUES ( 2, 'B', 13 );
    INSERT INTO t VALUES ( 3, 'A', 14 );
    || This view produces an ORA-01403 no data found error
    || in Oracle Forms 9i.
    CREATE OR REPLACE VIEW t_v
    AS
    SELECT id,
    MAX( DECODE( property, 'A', val, NULL ) ) AS a_val,
    MAX( DECODE( property, 'B', val, NULL ) ) AS b_val
    FROM t
    GROUP BY id;

    Steve,
    Your suggestion to put the view definition into the tablename field of the forms properties sheet was fruitful.
    Instead of the ORA-01403 error (No data found), I get ORA-24347 (Warning of a NULL column in an aggregate function). This error led me to discover bug 2457121, filed 11-Jul-2002, through Metalink. This bug is reported to only be a problem in Forms 9.0.2.7. It is reported to be fixed in Forms version 9.0.2.12.
    Thanks for your interest and help.
    - Doug

  • A challenging dynamic SQL query problem

    hi All,
    I have a very interesting problem at work:
    We have this particular table defined as follows :
    CREATE TABLE sales_data (
    sales_id NUMBER,
    sales_m01 NUMBER,
    sales_m02 NUMBER,
    sales_m03 NUMBER,
    sales_m04 NUMBER,
    sales_m05 NUMBER,
    sales_m06 NUMBER,
    sales_m07 NUMBER,
    sales_m08 NUMBER,
    sales_m09 NUMBER,
    sales_m10 NUMBER,
    sales_m11 NUMBER,
    sales_m12 NUMBER,
    sales_prior_yr NUMBER );
    The columns 'sales_m01 ..... sales_m12' represents aggregated monthly sales, in which 'sales_m01' translates to 'sales for the month of january, january being the first month, 'sales_m02' sales for the month of february, and so on.
    The problem I face is that we have a project which requires that a parameter be passed to a stored procedure which stands for the month number which is then used to build a SQL query with the following required field aggregations, which depends on the parameter passed :
    Sample 1 : parameter input: 4
    Dynamically-built SQL query should be :
    SELECT
    SUM(sales_m04) as CURRENT_SALES,
    SUM(sales_m01+sales_m02+sales_m03+sales_m04) SALES_YTD
    FROM
    sales_data
    WHERE
    sales_id = '0599768';
    Sample 2 : parameter input: 8
    Dynamically-built SQL query should be :
    SELECT
    SUM(sales_m08) as CURRENT_SALES,
    SUM(sales_m01+sales_m02+sales_m03+sales_m04+
    sales_m05+sales_m06+sales_m07+sales_m08) SALES_YTD
    FROM
    sales_data
    WHERE
    sales_id = '0599768';
    So in a sense, the contents of SUM(sales_m01 ....n) would vary depending on the parameter passed, which should be a number between 1 .. 12 which corresponds to a month, which in turn corresponds to an actual field range on the table itself. The resulting dynamic query should only aggregate those columns/fields in the table which falls within the range given by the input parameter and disregards all the remaining columns/fields.
    Any solution is greatly appreciated.
    Thanks.

    Hi another simpler approach is using decode
    try like this
    SQL> CREATE TABLE sales_data (
      2  sales_id NUMBER,
      3  sales_m01 NUMBER,
      4  sales_m02 NUMBER,
      5  sales_m03 NUMBER,
      6  sales_m04 NUMBER,
      7  sales_m05 NUMBER,
      8  sales_m06 NUMBER,
      9  sales_m07 NUMBER,
    10  sales_m08 NUMBER,
    11  sales_m09 NUMBER,
    12  sales_m10 NUMBER,
    13  sales_m11 NUMBER,
    14  sales_m12 NUMBER,
    15  sales_prior_yr NUMBER );
    Table created.
    SQL> select * from sales_data;
      SALES_ID  SALES_M01  SALES_M02  SALES_M03  SALES_M04  SALES_M05  SALES_M06  SALES_M07  SALES_M08  SALES_M09  SALES_M10  SALES_M11  SALES_M12 SALES_PRIOR_YR
             1        124        123        145        146        124        126        178        189        456        235        234        789          19878
             2        124        123        145        146        124        126        178        189        456        235        234        789          19878
             1        100        200        300        400        500        150        250        350        450        550        600        700          10000
             1        101        201        301        401        501        151        251        351        451        551        601        701          10000----now for your requirement. see below query if there is some problem then tell.
    SQL> SELECT sum(sales_m&input_data), DECODE (&input_data,
      2                 1, SUM (sales_m01),
      3                 2, SUM (sales_m01 + sales_m02),
      4                 3, SUM (sales_m01 + sales_m02 + sales_m03),
      5                 4, SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04),
      6                 5, SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04 + sales_m05),
      7                 6, SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04+sales_m05+sales_m06),
      8                 7, SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04+sales_m05+sales_m06+sales_m07),
      9                 8, SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04+sales_m05+sales_m06+sales_m07+sales_m08),
    10                 9, SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04+sales_m05+sales_m06+sales_m07+sales_m08+sales_m09),
    11                 10,SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04+sales_m05+sales_m06+sales_m07+sales_m08+sales_m09+sales_m10),
    12                 11,SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04+sales_m05+sales_m06+sales_m07+sales_m08+sales_m09+sales_m10+sales_m11),
    13                 12,SUM (sales_m01 + sales_m02 + sales_m03 + sales_m04+sales_m05+sales_m06+sales_m07+sales_m08+sales_m09+sales_m10+sales_m11+sales_m12)
    14                ) total
    15    FROM sales_data
    16   WHERE sales_id = 1;
    Enter value for input_data: 08
    Enter value for input_data: 08
    old   1: SELECT sum(sales_m&input_data), DECODE (&input_data,
    new   1: SELECT sum(sales_m08), DECODE (08,
    SUM(SALES_M08)      TOTAL
               890       5663

  • ABAP Query (SQ01) Challenge

    I asked this question last time as well but with no positive/helpful answer. I need to develop a query ( SQ01) in which the user just wants to see when was the most recent posting done in the given G/L account. If i pull posting date it will give me all the dates in  chronical order or so which i dont really want.
    Lets get your gray/sap matter working
    THanks

    sameer
    if you know how to create query.
    code is
    Select requred data from BSIS into internal table (I_BSIS).
    Sort I_BSIS by key field date desending.
    when you sort this you will get the latest record in first by date.
    take that date in one temp field.
    LOOP AT I_BSIS.
       AT END OF key field
      SUM amount .
    move both date and sum amount to final internal table.
    ENDLOOP.
    At last take final internal table and use  in below FM
    WS_EXCEL.
    you will get output.
    Pls. reward if helpful

  • Challenging Query working very slow

    Hi Guys
    my query is working very slow .. i think i am using old functions to fetch data .. is anybody can recommand changes into it to make it faster
    SELECT  a.Student_Updated,
            a.Course_Updated,
            b.Notes_Updated,
            a.School_ID,
            a.School_Code,
            a.School_Name,
            a.Course_Code,
            a.Course_Name,
            ass.ass_type  Assessment_Type,
            ass.ass_status Assessment_Status,
            ass.ass_work_due_date  Assessment_Date,
            ass.ass_result_achieved  Assessment_result,
            a.Last_Name,
            a.First_Name,
            a.Initials,
            a.Stud_ID,
            a.User_ID,
            a.Email,
            b.Code1,
            b.Code2,
            b.Code3,
            b.Code4,
            b.Code5,
            b.Code6,
            b.Code7,
            b.Code8,
            b.Code9,
            b.Code10,
            b.Code11,
            b.Code12,
            b.Code13,
            b.Code14,
            b.Code15,
            b.Code16,
            b.Code17,
            b.Code18,
            b.Code19,
            b.Code20,
            b.Code21,
            b.Code22,
            b.Code23,
            b.Code24,
            a.Cert_Status,
            a.Class_Status,
            b.Notes1,
            b.Notes2,
            b.Notes3,
            b.Notes4
    FROM  
        (SELECT s.s_ref                            Stud_ID,
         s.s_date_changed                    Student_Updated,
         max(sba.sba_date_changed)                Course_Updated,
         cc.cc_user_value_1                    School_ID,
         lv.lv_high_value                    School_Code,
         lv.lv_meaning                        School_Name,
         sba.sba_sb_ref                        Course_Code,
         sb.sb_name                          Course_Name,
         s.s_surname                        Last_Name,
         s.s_forename_1                        First_Name,
         substr(s.s_forename_2, 1, 1)                 Initials,
         s.s_vehicle_reg_no                    User_ID,
         decode(s.s_e_mail_address,
                 null, decode(s.s_vehicle_reg_no,
                            null, null, s.s_vehicle_reg_no||'@abdn.ac.uk'),
                s.s_e_mail_address||'@aberdeen.ac.uk')    Email,
         sba.sba_significance                    Cert_Status,
         sba.sba_status                        Class_Status
        FROM students s,
             study_block_associations sba,
             study_blocks sb,
             cost_centres cc,
             calendar_periods cp,
             local_values lv
        WHERE s.s_ref = sba.sba_s_ref
         AND sba.sba_sb_ref = sb.sb_ref
         AND sb.sb_cc_ref = cc.cc_ref
         AND cc.cc_user_value_1 = lv.lv_value(+)
         AND lv.lv_domain = 'CC USER VALUE 1'
         --    AND    nvl(s.s_disabilities, '99') not in ('0', '0T', '00', '97', '98', '99')
         AND length(sba.sba_sb_ref) = '6'
         AND instr('L|W', sba.sba_status) > 0
         AND sba.sba_type = 'E'
         AND sba.sba_calp_period_code = cp.calp_period_code
         AND calp_cal_ref = 'RYEAR'
         AND trunc(sysdate) BETWEEN calp_start_date AND calp_end_date
        GROUP BY
            s.s_ref,
            s.s_date_changed,
            sba.sba_sb_ref,
            sb.sb_name,
            cc.cc_user_value_1,
            lv.lv_high_value,
            lv.lv_meaning,
            s.s_surname,
            s.s_forename_1,
            substr(s.s_forename_2, 1, 1),
            s.s_disabilities,
            s.s_vehicle_reg_no,
            decode(    s.s_e_mail_address,
                null,    decode(    s.s_vehicle_reg_no,
                        null,    null,
                            s.s_vehicle_reg_no||'@abdn.ac.uk'),
                    s.s_e_mail_address||'@aberdeen.ac.uk'),
            sba.sba_significance,
            sba.sba_status
        ) a,
        (SELECT    sle.sle_s_ref                        Stud_ID,
         max(sle.sle_date_created)                Notes_Updated,
         sum(case when sle.sle_le_ref = '01' then 1 else 0 end)    Code1,
         sum(case when sle.sle_le_ref = '02' then 1 else 0 end)    Code2,
         sum(case when sle.sle_le_ref = '03' then 1 else 0 end)    Code3,
         sum(case when sle.sle_le_ref = '04' then 1 else 0 end)    Code4,
         sum(case when sle.sle_le_ref = '05' then 1 else 0 end)    Code5,
         sum(case when sle.sle_le_ref = '06' then 1 else 0 end)    Code6,
         sum(case when sle.sle_le_ref = '07' then 1 else 0 end)    Code7,
         sum(case when sle.sle_le_ref = '08' then 1 else 0 end)    Code8,
         sum(case when sle.sle_le_ref = '09' then 1 else 0 end)    Code9,
         sum(case when sle.sle_le_ref = '10' then 1 else 0 end)    Code10,
         sum(case when sle.sle_le_ref = '11' then 1 else 0 end)    Code11,
         sum(case when sle.sle_le_ref = '12' then 1 else 0 end)    Code12,
         sum(case when sle.sle_le_ref = '13' then 1 else 0 end)    Code13,
         sum(case when sle.sle_le_ref = '14' then 1 else 0 end)    Code14,
         sum(case when sle.sle_le_ref = '15' then 1 else 0 end)    Code15,
         sum(case when sle.sle_le_ref = '16' then 1 else 0 end)    Code16,
         sum(case when sle.sle_le_ref = '17' then 1 else 0 end)    Code17,
         sum(case when sle.sle_le_ref = '18' then 1 else 0 end)    Code18,
         sum(case when sle.sle_le_ref = '19' then 1 else 0 end)    Code19,
         sum(case when sle.sle_le_ref = '20' then 1 else 0 end)    Code20,
         sum(case when sle.sle_le_ref = '21' then 1 else 0 end)    Code21,
         sum(case when sle.sle_le_ref = '22' then 1 else 0 end)    Code22,
         sum(case when sle.sle_le_ref = '23' then 1 else 0 end)    Code23,
         sum(case when sle.sle_le_ref = '24' then 1 else 0 end)    Code24,
         sle4.Notes1,
         sle4.Notes2,
         sle4.Notes3,
         sle4.Notes4
        FROM abdn_student_le_provisions sle,
            (SELECT    sle3.sle_s_ref,
                    max(case when sle3.ord = 1 then sle3.sle_description end) Notes1,
                    max(case when sle3.ord = 2 then sle3.sle_description end) Notes2,
                    max(case when sle3.ord = 3 then sle3.sle_description end) Notes3,
                    max(case when sle3.ord = 4 then sle3.sle_description end) Notes4
              FROM (SELECT    row_number() over (partition by sle2.sle_s_ref order by sle2.sle_s_ref) as ord,
                            sle2.sle_s_ref,
                            sle2.sle_description
                    FROM abdn_student_le_provisions sle2
                    WHERE sle2.sle_description is not null
                    ) sle3
             GROUP BY
                sle3.sle_s_ref
            ) sle4
        WHERE    sle.sle_s_ref = sle4.sle_s_ref(+)
        GROUP BY
            sle.sle_s_ref,
            sle4.Notes1,
            sle4.Notes2,
            sle4.Notes3,
            sle4.Notes4
        ) b,
        assessments ass
    WHERE a.Stud_ID = b.Stud_ID
    AND a.Stud_ID = ass.ass_s_ref(+)
    AND a.Course_Code = ass.ass_sb_ref(+);

    Thread: HOW TO: Post a SQL statement tuning request - template posting
    HOW TO: Post a SQL statement tuning request - template posting

  • Challenged: Big query question

    Here is a sample of data I have for a table with four columns (id,status,start_date,end_date)
    What I need to do is to get difference of the start dates of the maximum available dates, if data is valid. The pseducode is as follows:
    IF end_date of New status is null
    THEN return null
    ELSE
    IF start_date of old >= start_date of new
    THEN return (start_date of old - start_date of new)
    ELSE return null
    I used the following query but always return the bold null
    select id,
    (case when max(end_date) keep (dense_rank last order by decode(status,'new',1,0),start_date) is null then
    null
    else
              (case when max(decode(status,'old',start_date,null)) >=
              max(decode(status,'new',start_date,null))
              then max(decode(status,'old',start_date,null)) - max(decode(status,'new',start_date,null))
    else
    null
    end)
    end) result
    from tbl where id =1
    Based on the below sample, I expected to get the following result; 14-Mar-07 - 16-Feb-07 which is the difference of the maximum start_dates of the two statuses. However the query is not working.. Please help me.. Thank you..
    Id Status start_date end_date
    1 new 03-Feb-07 07-Feb-07
    1 new 16-Feb-07 21-Feb-07
    1 old '10-Mar-07 12-Mar-07
    1 old '14-Mar-07 16-Mar-07

    Is it this you were looking for
    select id, status, start_date, end_date, lag_start_date
    from (
    select id,
    status,
    start_date,
    decode(status, 'new', null, end_date) end_date,
    lag(start_date) over (order by start_date) lag_start_date
    from test)
    where lag_start_date <= start_date ;
    ID STA START_DAT END_DATE LAG_START
    1 new 16-Feb-07 03-Feb-07
    1 old 10-Mar-07 12-Mar-07 16-Feb-07
    1 old 14-Mar-07 16-Mar-07 10-Mar-07

  • Challenge for SQL Query Experts

    Hello Everybody.
    I'm sorry but my English language is not good enough.
    I Have 2 tables.
    First 'tb_Transaction_In' Contains all sales  invoices (items Output from the stock)
    Second 'tb_Transaction_Out' Contains all purchases invoices  (items input  to the stock)
    I want to add  to every Invoice (which has many items) a column which calculate the cost value.
    example:
    stock 1 has an input invoice:
    Quantity of the invoice items 'x' is 10
    UTPrice is 100 for the one 'x'
    so my stock now has 10 pieces of x and the cost of the x is 100$
    =====================
    then the first Output:
    Quantity of the invoice items 'x' is 3
    Current Cost is 100$ for the one 'x'
    My stock has 7 pieces of x 
    =====================
    then the second input:
    Quantity of the invoice items 'x' is 5
    UTPrice is 75$ for the one 'x'
    so my stock now has 7+5 pieces of x and the cost of the x is (100*7 + 75*5) / 7+5
    =====================
    then the seconde Output:
    Quantity of the invoice items 'x' is 10
    Current Cost is 89.5 $ for the one 'x'
    My stock has 2 pieces of x 
    =====================
    ID Stock
    Product Price
    QtyIn QtyOut
    CurrentQty Cost
    1 R1
    x 100$
    10 0 10
    100$
    2 R1
    x 150$
    0 3 7
    100$
    3 R3
    x 75$ 5
    0 12
    89.5
    4 R2
    x 105$
    0 10 2
    89.5
    This is the DDL:
    USE [6]
    GO
    /****** Object:  Table [dbo].[tb_Transaction_In]    Script Date: 25/03/2014 1:19:25 AM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[tb_Transaction_In](
    [Date] [datetime] NOT NULL,
    [Quantity] [decimal](18, 5) NOT NULL,
    [ProductCode] [nvarchar](50) NOT NULL,
    [StockCode] [nvarchar](23) NOT NULL,
    [Type] [nvarchar](5) NOT NULL,
    [UTPrice] [decimal](18, 5) NULL,
    [NotUsedQty] [decimal](18, 5) NULL,
    [ID] [uniqueidentifier] NOT NULL,
     CONSTRAINT [PK_dbo.tb_Transaction_in] PRIMARY KEY CLUSTERED 
    [ID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    /****** Object:  Table [dbo].[tb_Transaction_Out]    Script Date: 25/03/2014 1:19:25 AM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[tb_Transaction_Out](
    [Date] [datetime] NOT NULL,
    [Quantity] [decimal](18, 5) NOT NULL,
    [ProductCode] [nvarchar](50) NOT NULL,
    [StockCode] [nvarchar](50) NOT NULL,
    [Type] [nvarchar](5) NOT NULL,
    [PendingAmount] [decimal](18, 5) NULL,
    [UTPrice] [decimal](18, 5) NULL,
    [ID] [uniqueidentifier] NOT NULL,
     CONSTRAINT [PK_dbo.tb_Transaction_out] PRIMARY KEY CLUSTERED 
    [ID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(48.00000 AS Decimal(18, 5)), N'TX121/50/W', N'RI', N'AI', CAST(11.09000 AS Decimal(18,
    5)), NULL, N'3b626123-799f-4e92-8fec-003cbeb2f6fc')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'RVS/SP-5-669-59', N'RI', N'AI', CAST(0.00045 AS Decimal(18,
    5)), NULL, N'b65e6927-969f-4bfb-aa0e-0273066bbdea')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501341DBC AS DateTime), CAST(20.00000 AS Decimal(18, 5)), N'YYIS-ICF2S13H1LDK*255', N'RP255', N'ST', CAST(61.08273
    AS Decimal(18, 5)), NULL, N'82de4604-ad11-40ae-a8d2-0926444123cd')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A5014657A5 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'BJ3361L', N'RO', N'RSL', CAST(7905.00000 AS Decimal(18,
    5)), NULL, N'8f3350d5-5cb7-49da-8b69-0e0c61d6c546')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'YYDG-CDM-R 35W', N'RI', N'AI', CAST(152.58000 AS Decimal(18,
    5)), NULL, N'f10eca1d-7215-4015-89de-10f629bc4091')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A50121C4DC AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'AF700F0/1T1.PT.01', N'RT', N'ST', CAST(82.79003 AS Decimal(18,
    5)), NULL, N'617576aa-5c6a-406c-8d76-168f8ba9d62b')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A5012F0B4C AS DateTime), CAST(2.00000 AS Decimal(18, 5)), N'YYHE-SIS180 220V*255', N'RP255', N'ST', CAST(49.64832 AS
    Decimal(18, 5)), NULL, N'a524aea7-2c75-4818-b6ac-196e80aef011')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(12.00000 AS Decimal(18, 5)), N'FO191116', N'RI', N'AI', CAST(132.52000 AS Decimal(18,
    5)), NULL, N'e1804c98-a753-44d2-9fe0-1b41a04099d2')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501341DBC AS DateTime), CAST(21.00000 AS Decimal(18, 5)), N'YYIS-IZT-2S26-M5-LD*255', N'RP255', N'ST', CAST(243.82820
    AS Decimal(18, 5)), NULL, N'93217ef1-a1c5-47af-8c49-1c8cacaf7f35')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'CIDV-ITACA/48*GOLD', N'RT', N'AI', CAST(441.94000 AS Decimal(18,
    5)), NULL, N'a9de52c4-a814-4db8-8db8-1e7a391f6450')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'FO25131', N'RI', N'AI', CAST(45.70000 AS Decimal(18, 5)),
    NULL, N'57480184-b8a7-43f3-a522-effc3733bcb7')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'NNSRC-181', N'RP303', N'AI', CAST(272.04000 AS Decimal(18,
    5)), NULL, N'292d363f-9be4-471c-b868-f41fc1a74138')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A5012F0B4C AS DateTime), CAST(42.00000 AS Decimal(18, 5)), N'YYHE-TC126-42*255', N'RP255', N'ST', CAST(47.80049 AS
    Decimal(18, 5)), NULL, N'732adced-785d-4c0a-b675-f436e9fcd430')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'SKDVW0808EA', N'RI', N'AI', CAST(3862.27000 AS Decimal(18,
    5)), NULL, N'f00853e4-f585-4cf7-994c-f4edf8cb421b')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(3.00000 AS Decimal(18, 5)), N'LLIL-89147', N'RI', N'AI', CAST(62.50219 AS Decimal(18,
    5)), NULL, N'3e7a26cd-ece2-44b0-8416-f500e5bb0bfb')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A5012F0B4C AS DateTime), CAST(100.00000 AS Decimal(18, 5)), N'YYHE-SI218-40*255', N'RP255', N'ST', CAST(52.80909 AS
    Decimal(18, 5)), NULL, N'90ad3dcd-6478-4b86-bdec-f7818ad7156f')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(135.00000 AS Decimal(18, 5)), N'LA4903020*294', N'RI', N'AI', CAST(379.04000 AS Decimal(18,
    5)), NULL, N'0b62da54-5db6-465f-8229-f79a93c4dbf1')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'DN921/FL 2X28*303', N'RI', N'AI', CAST(69.74000 AS Decimal(18,
    5)), NULL, N'270cb795-83c2-43b1-ae43-f8bef36b3470')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A501207B90 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'FO173011/220V*407', N'RI', N'AI', CAST(118.00000 AS Decimal(18,
    5)), NULL, N'cca4ac36-7538-45d0-90f8-f9a941515b74')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A5012F0B4C AS DateTime), CAST(12.00000 AS Decimal(18, 5)), N'YYHE-SI217-32-UNI*255', N'RP255', N'ST', CAST(51.15577
    AS Decimal(18, 5)), NULL, N'9da49db4-8bfb-488d-a153-fd36ef21df47')
    INSERT [dbo].[tb_Transaction_In] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [UTPrice], [NotUsedQty], [ID]) VALUES (CAST(0x0000A2A600DE48DC AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'LLIL-84700*253', N'RI', N'ST', CAST(143.01118 AS Decimal(18,
    5)), NULL, N'01831955-485b-4d6c-9665-fda401b8a5d1')
    INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A500C6CA8F AS DateTime), CAST(25.00000 AS Decimal(18, 5)), N'KHBG-4345*318', N'RI', N'ST', CAST(0.00000 AS Decimal(18,
    5)), CAST(1431.68531 AS Decimal(18, 5)), N'77d8fe2b-b7f7-4afb-baf2-00371e49ef50')
    INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A600DE48DC AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'FO81177', N'RP318', N'ST', CAST(0.00000 AS Decimal(18,
    5)), CAST(125.19275 AS Decimal(18, 5)), N'64bb5a74-4f36-40bb-8a15-0178d9ce3532')
    INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A500E4483C AS DateTime), CAST(2202.00000 AS Decimal(18, 5)), N'TNSP-0594*020', N'RI', N'TO', CAST(0.00000 AS Decimal(18,
    5)), CAST(4.88155 AS Decimal(18, 5)), N'a6d2c5e8-315e-427f-9745-01c5f6857f62')
    INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A50120C690 AS DateTime), CAST(10.00000 AS Decimal(18, 5)), N'ES651900S*253', N'RI', N'AO', CAST(0.00000 AS Decimal(18,
    5)), CAST(94.01000 AS Decimal(18, 5)), N'ae8a9ba7-74db-4f7d-b830-01f099ac24de')
    INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A501139E98 AS DateTime), CAST(29.00000 AS Decimal(18, 5)), N'KHZM-42176950*255', N'RI', N'ST', CAST(0.00000 AS
    Decimal(18, 5)), CAST(361.52127 AS Decimal(18, 5)), N'2611d667-8db3-4e71-a543-0245258a5788')
    INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A501203C48 AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'TN802/21', N'RP255', N'ST', CAST(0.00000 AS Decimal(18,
    5)), CAST(222.08500 AS Decimal(18, 5)), N'01027e8a-e576-4605-a420-03524fab994e')
    INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A500BE530C AS DateTime), CAST(13.00000 AS Decimal(18, 5)), N'ES651900S*253', N'RP253', N'SL', CAST(0.00000 AS Decimal(18,
    5)), CAST(120.00000 AS Decimal(18, 5)), N'dce64410-15b5-4ecc-88b4-04e2b7eac306')
    INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A5011CE020 AS DateTime), CAST(5.00000 AS Decimal(18, 5)), N'LA9801023*313', N'RI', N'TO', CAST(0.00000 AS Decimal(18,
    5)), CAST(1288.96600 AS Decimal(18, 5)), N'3ce3c4b9-b0b4-45c6-b14e-058cb08608d2')
    INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A500D8910A AS DateTime), CAST(4.00000 AS Decimal(18, 5)), N'LA4906203*294', N'RI', N'ST', CAST(0.00000 AS Decimal(18,
    5)), CAST(62.34000 AS Decimal(18, 5)), N'25332af7-8f8c-489f-9e96-06427e4b64db')
    INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A50103903E AS DateTime), CAST(24.00000 AS Decimal(18, 5)), N'YYLT-TRWW 155 HFP*294', N'RP313', N'SL', CAST(0.00000
    AS Decimal(18, 5)), CAST(325.00000 AS Decimal(18, 5)), N'4550841e-8ec9-4528-99d9-fcbef5215030')
    INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A5012F0B4C AS DateTime), CAST(2.00000 AS Decimal(18, 5)), N'YYHE-SIS180 220V*255', N'RI', N'ST', CAST(0.00000 AS
    Decimal(18, 5)), CAST(49.64832 AS Decimal(18, 5)), N'10018f9c-a6ba-47c4-aff3-fd515f18c883')
    INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A5012F0B4C AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'YYHE-SH150IZ*255', N'RI', N'ST', CAST(0.00000 AS Decimal(18,
    5)), CAST(74.20504 AS Decimal(18, 5)), N'2a43869a-4300-486d-b534-fe336cc37aeb')
    INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A500BE530C AS DateTime), CAST(4.00000 AS Decimal(18, 5)), N'YYVL-00358', N'RP253', N'SL', CAST(0.00000 AS Decimal(18,
    5)), CAST(80.00000 AS Decimal(18, 5)), N'c22c794d-cefe-40a2-b813-fea8aa2cc5fb')
    INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A500DBEC64 AS DateTime), CAST(50.00000 AS Decimal(18, 5)), N'TX203/50/BR', N'RI', N'ST', CAST(0.00000 AS Decimal(18,
    5)), CAST(33.85686 AS Decimal(18, 5)), N'3367f8a0-75ff-476e-b3ca-ff095ed25ba6')
    INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A500BE530C AS DateTime), CAST(1.00000 AS Decimal(18, 5)), N'TN0453/3/21', N'RP253', N'SL', CAST(0.00000 AS Decimal(18,
    5)), CAST(250.00000 AS Decimal(18, 5)), N'6c87e1ad-98f8-485d-87e6-ff238eb2317e')
    INSERT [dbo].[tb_Transaction_Out] ([Date], [Quantity], [ProductCode], [StockCode], [Type], [PendingAmount], [UTPrice], [ID]) VALUES (CAST(0x0000A2A501341DBC AS DateTime), CAST(244.00000 AS Decimal(18, 5)), N'YYIS-ICF2S26H1LDK*255', N'RI', N'ST', CAST(0.00000
    AS Decimal(18, 5)), CAST(61.08273 AS Decimal(18, 5)), N'2d04ada8-ee92-4f28-9cbe-ffb855ebd93b')
    ALTER TABLE [dbo].[tb_Transaction_In] ADD  CONSTRAINT [DF_tb_Transaction_In_Date]  DEFAULT (getdate()) FOR [Date]
    GO
    ALTER TABLE [dbo].[tb_Transaction_In] ADD  CONSTRAINT [DF_tb_Transaction_In_Quantity]  DEFAULT ((0)) FOR [Quantity]
    GO
    ALTER TABLE [dbo].[tb_Transaction_In] ADD  CONSTRAINT [DF_tb_Transaction_in_ID]  DEFAULT (newid()) FOR [ID]
    GO
    ALTER TABLE [dbo].[tb_Transaction_Out] ADD  CONSTRAINT [DF_tb_Transaction_Out_Date]  DEFAULT (getdate()) FOR [Date]
    GO
    ALTER TABLE [dbo].[tb_Transaction_Out] ADD  CONSTRAINT [DF_tb_Transaction_Out_Quantity]  DEFAULT ((0)) FOR [Quantity]
    GO
    ALTER TABLE [dbo].[tb_Transaction_Out] ADD  CONSTRAINT [DF_tb_Transaction_Out_Type]  DEFAULT ((0)) FOR [Type]
    GO
    ALTER TABLE [dbo].[tb_Transaction_Out] ADD  CONSTRAINT [DF_tb_Transaction_oUT_ID]  DEFAULT (newid()) FOR [ID]
    GO

    You can do it by means of computed column based on a UDF. 
    so in your table just add a column like below
    CREATE TABLE
    cost as dbo.GetItemCode(ItemCode)
    and function would be as below
    CREATE FUNCTION dbo.GetItemCode
    @ItemCode varchar(20)
    RETURNS Numeric(10,2)
    AS
    BEGIN
    DECLARE @WAP Numeric(10,2)
    SELECT @WAP = SUM((COALESCE(i.Qty,0) - COALESCE(o.Qty,0)) * i.UTPrice)* 1.0/NULLIF(SUM(COALESCE(i.Qty,0) - COALESCE(o.Qty,0)),0)
    FROM (SELECT ProductCode,StockCode,UTPrice,SUM(Quantity) AS Qty
    FROM transaction_in
    GROUP BY ProductCode,StockCode,UTPrice)i
    FULL OUTER JOIN SELECT ProductCode,StockCode,UTPrice,SUM(Quantity) AS Qty
    FROM transaction_out
    GROUP BY ProductCode,StockCode,UTPrice) o
    ON o.ProductCode = i.ProductCode
    AND o.StockCode = i.StockCode
    RETURN (@WAP)
    END
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • Update query performance - Challenging one

    Hi All,
    Below is the procedure I have created to update the five columns of one table. I have to update almost 9673533 records.
    With the below procedure for updating 500 records it took around 1.5 hrs.
    What would be the best strategy to enhance the performance of the procedure.. Please throw your ideas to make it in less time.
    create or replace procedure test is
      cursor cur_web_prd is
        select rp_id
          from dw_retl_prds t;
      lc_short_brief       varchar2(300);
      lc_long_brief        varchar2(2000);
      ld_date_first_online date;
      ld_date_sent         date;
      lc_rp_id             varchar2(20);
      lc_copos_ssl2        varchar2(40);
      lc_qoslistid         varchar2(40);
      ln_web_prd_cnt       number;
      ln_copos_cnt         number;
      ln_qolist_cnt        number;     
    begin
      for i in cur_web_prd loop
          select count(*)
          into ln_web_prd_cnt
          from dw_retl_prds t, web_prds@nrstp s
         where t.rp_id = s.rp_rp_id
           and t.rp_id = i.rp_id;
      if ln_web_prd_cnt = 1 then
        select t.rp_id,
               s.short_brief,
               s.long_brief,
               s.date_first_online,
               s.date_sent
          into lc_rp_id,
               lc_short_brief,
               lc_long_brief,
               ld_date_first_online,
               ld_date_sent
          from dw_retl_prds t, web_prds@nrstp s
         where t.rp_id = s.rp_rp_id
           and t.rp_id = i.rp_id;
          select count(*)
          into ln_copos_cnt
          from dw_retl_prd_cat_val_us@dwenq t, dw_retl_prd_cat_vals@dwenq s
         where t.rp_rp_id = i.rp_id
           and trunc(sysdate) between t.dw_eftv_from and
               nvl(t.dw_eftv_to, trunc(sysdate))
           and s.pc_pc_id = 'COPOS_SSL2'
           and trunc(sysdate) between s.dw_eftv_from and
               nvl(s.dw_eftv_to, trunc(sysdate))
           and t.pc_pc_id = s.pc_pc_id
           and t.pcv_pcv_id = s.pcv_id;
           if ln_copos_cnt = 1 then
        select s.descr
          into lc_copos_ssl2
          from dw_retl_prd_cat_val_us@dwenq t, dw_retl_prd_cat_vals@dwenq s
         where t.rp_rp_id = i.rp_id
           and trunc(sysdate) between t.dw_eftv_from and
               nvl(t.dw_eftv_to, trunc(sysdate))
           and s.pc_pc_id = 'COPOS_SSL2'
           and trunc(sysdate) between s.dw_eftv_from and
               nvl(s.dw_eftv_to, trunc(sysdate))
           and t.pc_pc_id = s.pc_pc_id
           and t.pcv_pcv_id = s.pcv_id;
           else
               lc_copos_ssl2 := null;
           end if;
            select count(*)
          into ln_qolist_cnt
          from dw_retl_prd_cat_val_us@dwenq t, dw_retl_prd_cat_vals@dwenq s
         where t.rp_rp_id = i.rp_id
           and trunc(sysdate) between t.dw_eftv_from and
               nvl(t.dw_eftv_to, trunc(sysdate))
           and s.pc_pc_id = 'QOSLISTID'
           and trunc(sysdate) between s.dw_eftv_from and
               nvl(s.dw_eftv_to, trunc(sysdate))
           and t.pc_pc_id = s.pc_pc_id
           and t.pcv_pcv_id = s.pcv_id;
           if ln_qolist_cnt = 1 then
        select s.descr
          into lc_qoslistid
          from dw_retl_prd_cat_val_us@dwenq t, dw_retl_prd_cat_vals@dwenq s
         where t.rp_rp_id = i.rp_id
           and trunc(sysdate) between t.dw_eftv_from and
               nvl(t.dw_eftv_to, trunc(sysdate))
           and s.pc_pc_id = 'QOSLISTID'
           and trunc(sysdate) between s.dw_eftv_from and
               nvl(s.dw_eftv_to, trunc(sysdate))
           and t.pc_pc_id = s.pc_pc_id
           and t.pcv_pcv_id = s.pcv_id;
           else
               lc_qoslistid := null;
           end if;
        update dw_retl_prds t
           set t.web_short_brief   = lc_short_brief,
               t.web_long_brief    = lc_long_brief,
               t.date_first_online = ld_date_first_online,
               t.date_sent         = ld_date_sent,
               t.copos_ssl#2       = lc_copos_ssl2,
               t.qoslistid         = lc_qoslistid
         where t.rp_id = i.rp_id;
       end if;
       ln_web_prd_cnt            := 0;   
       lc_short_brief            := null;          
       lc_long_brief             := null;
       ld_date_first_online      := null;
       ld_date_sent              := null;
       lc_copos_ssl2             := null;
       lc_qoslistid              := null;
      end loop;
      commit;
      end;I have taken count(*) to check and avoid the too_many_rows and no_data_found exceptions. As there is no Unique key on the table Web_prds@nrstp.
    For dw_retl_prds the unique key is rp_id.

    Hello
    First things first, you need to trace it to find out where it is spending it's time. Find out which statements are taking the longest and target them.
    That said however, you are using the slowest possible method for processing this data by using a cursor for loop. You are then doing separate lookups over a database link - and you are doing the queries twice! You could reduce the time of the lookups by 50% just by combining the queries...for example
    CREATE OR REPLACE PROCEDURE test
    IS
       CURSOR cur_web_prd
       IS
          SELECT rp_id
            FROM dw_retl_prds t;
       lc_short_brief         VARCHAR2 (300);
       lc_long_brief          VARCHAR2 (2000);
       ld_date_first_online   DATE;
       ld_date_sent           DATE;
       lc_rp_id               VARCHAR2 (20);
       lc_copos_ssl2          VARCHAR2 (40);
       lc_qoslistid           VARCHAR2 (40);
       ln_web_prd_cnt         NUMBER;
       ln_copos_cnt           NUMBER;
       ln_qolist_cnt          NUMBER;
    BEGIN
       FOR i IN cur_web_prd
       LOOP
          BEGIN
                   SELECT t.rp_id,
                    s.short_brief,
                    s.long_brief,
                    s.date_first_online,
                    s.date_sent
               INTO lc_rp_id,
                    lc_short_brief,
                    lc_long_brief,
                    ld_date_first_online,
                    ld_date_sent
               FROM dw_retl_prds t, web_prds@nrstp s
              WHERE t.rp_id = s.rp_rp_id AND t.rp_id = i.rp_id;
              BEGIN
                  SELECT s.descr
                      INTO lc_copos_ssl2
                      FROM dw_retl_prd_cat_val_us@dwenq t,
                           dw_retl_prd_cat_vals@dwenq s
                     WHERE t.rp_rp_id = i.rp_id
                           AND TRUNC (SYSDATE) BETWEEN t.dw_eftv_from
                                                   AND NVL (t.dw_eftv_to,
                                                            TRUNC (SYSDATE))
                           AND s.pc_pc_id = 'COPOS_SSL2'
                           AND TRUNC (SYSDATE) BETWEEN s.dw_eftv_from
                                                   AND NVL (s.dw_eftv_to,
                                                            TRUNC (SYSDATE))
                           AND t.pc_pc_id = s.pc_pc_id
                           AND t.pcv_pcv_id = s.pcv_id;  
              EXCEPTION
                    WHEN NO_DATA_FOUND THEN
                         lc_copos_ssl2 := NULL;
              END;
              BEGIN
                  SELECT s.descr
                      INTO lc_qoslistid
                      FROM dw_retl_prd_cat_val_us@dwenq t,
                           dw_retl_prd_cat_vals@dwenq s
                     WHERE t.rp_rp_id = i.rp_id
                           AND TRUNC (SYSDATE) BETWEEN t.dw_eftv_from
                                                   AND NVL (t.dw_eftv_to,
                                                            TRUNC (SYSDATE))
                           AND s.pc_pc_id = 'QOSLISTID'
                           AND TRUNC (SYSDATE) BETWEEN s.dw_eftv_from
                                                   AND NVL (s.dw_eftv_to,
                                                            TRUNC (SYSDATE))
                           AND t.pc_pc_id = s.pc_pc_id
                           AND t.pcv_pcv_id = s.pcv_id;
                EXCEPTION
                    WHEN NO_DATA_FOUND THEN
                        lc_qoslistid := NULL;
                END;
                UPDATE dw_retl_prds t
                SET t.web_short_brief = lc_short_brief,
                    t.web_long_brief = lc_long_brief,
                    t.date_first_online = ld_date_first_online,
                    t.date_sent = ld_date_sent,
                    t.copos_ssl#2 = lc_copos_ssl2,
                    t.qoslistid = lc_qoslistid
              WHERE t.rp_id = i.rp_id;
          EXCEPTION
                WHEN
                    NO_DATA_FOUND THEN
                        NULL;
          END;
          ln_web_prd_cnt := 0;
          lc_short_brief := NULL;
          lc_long_brief := NULL;
          ld_date_first_online := NULL;
          ld_date_sent := NULL;
          lc_copos_ssl2 := NULL;
          lc_qoslistid := NULL;
       END LOOP;
       COMMIT;
    END;But this is still not an efficient implementation - you should try to put all of this into 1 or more Update statements without the loop. Something like...
                UPDATE dw_retl_prds t
                SET (   t.web_short_brief,
                        t.web_long_brief,
                        t.date_first_online,
                        t.date_sent
                    ) =
                    (   SELECT
                            s.short_brief,
                            s.long_brief,
                            s.date_first_online,
                            s.date_sent
                        FROM
                            web_prds@nrstp s
                        WHERE
                            t.rp_id = s.rp_rp_id
                WHERE
                    EXISTS( SELECT
                                NULL
                            FROM
                                web_prds@nrstp s
                            WHERE
                                t.rp_id = s.rp_rp_id
                            );That way you would have 1, 2 or 3 update statements each of which will run in a fraction of the time it takes for your process to run. You may need to do more work after this, such as materializing the results locally from the remote database rather than joining over the database link, but that's a bridge you can cross when you come to it.
    HTH
    David

  • Challenging query....urgent

    Hi Gurus,
    My technology is WDP for ABAP.
    I have an adobe form having an input field called purchase order. once the user enters the PO, if its not found in the database, I have made a pop up window. Till here its fine. After I close the popup window. I entered the PO# again. But the variable which captures the PO is still having the  old PO#. I mean the variable is not getting refreshed with the new value given in ADOBE form.
    I'm capturing the PO# from the form like this.
      elem_podetails->get_attribute(
        EXPORTING
          name =  `PO`
        IMPORTING
          value = item_PO ).
    when its executing at 2nd time also, still I see the old value in ITEM_PO.
    This is happening only in ADOBE interactive forms. But in the normal WDP4A, its working fine. Please give me the suggestions at the  earliest. Please note that I want in WDP for ABAP interactive forms.(dont give any JAVA code for this).
    Thanks,
    Kesav.

    Hi,
    in your method to rebind the context when you select a new po,
    could you try node->invalidate( ). on the node before you bind the new values,
    the subnodes' supply functions are called again and maybe that po number is
    fetched on supply function and is in that case not bound to the new value.
    grtz,
    Koen

Maybe you are looking for