Pivot + cross join

Hello

I wrote the following query, which works very well:
select * from
(
     select
          to_char(SCB_OPENTIME, 'YYYY-MM') as curr_date,
          SCB_TASK
     from EM_DATA_MSR_SC
)
pivot
(
     count(SCB_TASK) for curr_date in
     (
          '2011-01',
          '2011-02',
          '2011-03',
          '2011-04',
          '2011-05',
          '2011-06',
          '2011-07',
          '2011-08',
          '2011-09',
          '2011-10',
          '2011-11',
          '2011-12'
     )
);
Now, I need to apply a cross thereon join:
select * from
(
     select
          to_char(SCB_OPENTIME, 'YYYY-MM') as curr_date,
          SCB_TASK
     from EM_DATA_MSR_SC
     where
          EMSCG_STATUS = status.name
)
pivot
(
     count(SCB_TASK) for curr_date in
     (
          '2011-01',
          '2011-02',
          '2011-03',
          '2011-04',
          '2011-05',
          '2011-06',
          '2011-07',
          '2011-08',
          '2011-09',
          '2011-10',
          '2011-11',
          '2011-12'
     )
)
cross join
(
    select 0, 'Closed' as name from DUAL union
    select 1, 'Denied' from DUAL union
    select 2, 'Open' from DUAL
) status;
When I try to run this query, I get this error message:
"STATUS"."NAME": invalid identifier
However, SQL is supposed to have brought on a deep level table references.
I'm doing something wrong?

Hello

If this is what you want, then you don't want a cross join. The inner join in the view online with no name is the only reference to beaches you need.

with ranges as
(
    select 0, 0 as mini, 20 as maxi from DUAL union
    select 1, 21, 50 from DUAL union
    select 2, 51, 300 from DUAL
)
select * from
(
     select  r.mini,
          r.maxi,
          to_char (t.DAT, 'YYYY-MM')     as curr_date,
          t.NUM
     from      TEST     t
     join      RANGES     r     on      t.NUM >= r.mini
                    and      t.NUM <= r.maxi
)
pivot
(
     count(NUM) for curr_date in
     (
          '2011-01',
          '2011-02',
          '2011-03',
          '2011-04',
          '2011-05',
          '2011-06',
          '2011-07',
          '2011-08',
          '2011-09',
          '2011-10',
          '2011-11',
          '2011-12'
     )
)
ORDER BY  mini
;

Depending on your data and your needs, you might want an outer join, not an inner join, like this:

...     from          RANGES     r
     LEFT OUTER JOIN     TEST     t     on      t.NUM >= r.mini
                         and      t.NUM <= r.maxi

Published by: Frank Kulash, November 7, 2011 12:20
Alternative ADED of outer join

Tags: Database

Similar Questions

  • Auto cross join

    Hello.

    Why Cross join on a table without specifying alias does not work properly ?


    with
        t1 as (
          select 1 as q1, 2 as q2 from dual union 
          select 3, 4 from dual union 
          select 5, 6 from dual
      )
    select *
      from t1
        cross join t1
    order by 1
            Q1         Q2         Q1         Q2
    ---------- ---------- ---------- ----------
             1          2          1          2 
             1          2          1          2 
             1          2          1          2 
             3          4          3          4 
             3          4          3          4 
             3          4          3          4 
             5          6          5          6 
             5          6          5          6 
             5          6          5          6 
    
     9 rows selected 
    

    11.2.0.3.0 - 64 bit

    2837285 wrote:

    Ok.

    Why 10 gr 2 we have error ORA-00918 (same self-join) and 11 g works, but the wrong result?

    Why don't you ask Oracle via a support request, it's a bug.

    You can't say he is working on 11g "but which gives bad result."  Clearly if it is to give a wrong result then it doesn't work.

    What happens on 11g is the fact that it runs without the exception of ambiguous column.  There are a few known bugs in the way SQL ANSI has implemented compared to the regular SQL Oracle syntax.  Oracle worked to correct.

    I don't have 12 c to test, but it may have already been corrected in this version.

  • Inline query vs cross join

    Hi all

    Two tables which have no relationship with each other. For example, an Employees table and a systemparameter with a startworktime table.

    We need a query with the data in the two tables:

    Get all employees and the startworktime (which is the same for everyone)

    Which is cheaper: an inline query or a product Cartesian or crossjoin?

    Inine:

    Select name, function

    (by selecting startworktime in systemparameter)

    employees;

    Cartesian product:

    SELECT name, function, startwoime

    rktfrom used

    Cross join systemparameter;

    Your opinion about this.

    Both these do the same thing. I seriously doubt if we would have the benefits of performance on the other.

    Kind regards

  • Difference between a CROSS JOIN and a Cartesian product of the noted comma?

    Hello everyone,

    Oracle version: Oracle Database 11 g Enterprise Edition Release 11.2.0.1.0 - 64 bit
    OS: Linux Fedora Core 17 (x86_64)

    I was practicing on recursive subquery factoring based on oracle examples available in the documentation
    http://docs.Oracle.com/CD/E11882_01/server.112/e26088/statements_10002.htm#i2129904

    I was working on an example that displays the hierarchy of each manager with related employees. Here's how
    WITH tmptab(empId, mgrId, lvl) AS
    (
        SELECT  employee_id, manager_id, 0 lvl
        FROM employees
        WHERE manager_id IS NULL
        UNION ALL
        SELECT  employee_id, manager_id, lvl+1
        FROM employees, tmptab
        WHERE (manager_id = empId)
    )
    SEARCH DEPTH FIRST BY mgrId SET order1
    SELECT LPAD(' ', lvl * 3, ' ') || empId AS empId
    FROM tmptab;
    Which gives the desired result
    EMPID
    ---------------------
    100
       101
          108
          109
          110
          111
          112
          113
          200
          203
          204
          205
          206
       102
          103
          104
          105
          106
          107
       114
          115
          116
          117
          118
          119
       120
          125
          126
          127
          128
          180
          181
          182
          183
       121
          129
          130
          131
          132
          184
          185
          186
          187
       122
          133
          134
          135
          136
          188
          189
          190
          191
       123
          137
          138
          139
          140
          192
          193
          194
          195
       124
          141
          142
          143
          144
          196
          197
          198
          199
       145
          150
          151
          152
          153
          154
          155
       146
          156
          157
          158
          159
          160
          161
       147
          162
          163
          164
          165
          166
          167
       148
          168
          169
          170
          171
          172
          173
       149
          174
          175
          176
          177
          178
          179
       201
          202
    
    107 rows selected.
    
    SQL> 
    However, by chance, I noticed that if I put CROSS JOIN instead of put a comma between table names, the same query behaves differently.

    In other words, if instead of writing
    . . .
    UNION ALL
        SELECT  employee_id, manager_id, lvl+1
        FROM employees, tmptab
        WHERE (manager_id = empId)
    I am writing
    . . .
    UNION ALL
        SELECT  employee_id, manager_id, lvl+1
        FROM employees CROSS JOIN tmptab
        WHERE (manager_id = empId)
    I get the following error message
    ERROR at line 4:
    ORA-32044: cycle detected while executing recursive WITH query
    Any idea?
    Correct me if I'm wrong, but I remember, oracle supports as many JOIN CROSSROADS notation for Cartesian product (vector product =). For example
    SQL> WITH tmptab1 AS
      2  (
      3      SELECT 'a1' AS colval FROM DUAL UNION ALL
      4      SELECT 'a2' AS colval FROM DUAL UNION ALL
      5      SELECT 'a3' AS colval FROM DUAL
      6  ),
      7  tmptab2 AS
      8  (
      9      SELECT 'b1' AS colval FROM DUAL UNION ALL
     10      SELECT 'b2' AS colval FROM DUAL
     11  )
     12  SELECT t1.colval, t2.colval
     13  FROM tmptab1 t1 CROSS JOIN tmptab2 t2;
    
    CO CO
    -- --
    a1 b1
    a2 b1
    a3 b1
    a1 b2
    a2 b2
    a3 b2
    
    6 rows selected.
    
    SQL> LIST 13
     13* FROM tmptab1 t1 CROSS JOIN tmptab2 t2
    SQL>
    SQL>
    SQL> CHANGE /CROSS JOIN/,
     13* FROM tmptab1 t1 , tmptab2 t2
    SQL> 
    SQL>
    SQL> LIST
      1  WITH tmptab1 AS
      2  (
      3  SELECT 'a1' AS colval FROM DUAL UNION ALL
      4  SELECT 'a2' AS colval FROM DUAL UNION ALL
      5  SELECT 'a3' AS colval FROM DUAL
      6  ),
      7  tmptab2 AS
      8  (
      9  SELECT 'b1' AS colval FROM DUAL UNION ALL
     10  SELECT 'b2' AS colval FROM DUAL
     11  )
     12  SELECT t1.colval, t2.colval
     13* FROM tmptab1 t1 , tmptab2 t2
    SQL> 
    SQL> /
    
    CO CO
    -- --
    a1 b1
    a2 b1
    a3 b1
    a1 b2
    a2 b2
    a3 b2
    
    6 rows selected.
    
    SQL> 
    So if the two rated commas and CROSS JOIN have the same semantics, why do I get a cycle mentioned above cites recursive subquery factoring while the same query works pretty well with comma between table instead of CROSS JOIN names? Because if a cycle is detected (= current element ancestor) it means that the product with the CROSS JOIN notation produces duplicates which are absent in the result of the Cartesian product rated comma.

    I would appreciate it if you could kindly shed some light.

    Thanks in advance,

    Kind regards
    Dariyoosh

    Hello

    dariyoosh wrote:
    ... Oracle terminology could become really confusing. But once again, according to the online glossary, a Cartesian product is apparently regarded as a join
    http://docs.Oracle.com/CD/E11882_01/server.112/e25789/glossary.htm?type=popup#CNCPT44493
    >

    There is no doubt that a Cartesian product (also called cross join) is a join. If loops in a WITH recursive clause are detected after completing the joins, but before other conditions apply, the relevant question here is "what are the requirements to join?
    In the ANSI syntax, the distinction is always clear. Join conditions occur in the... Clause WE

    SELECT  employee_id, manager_id, lvl + 1
    FROM      employees
    JOIN        tmptab          ON  (manager_id = empId)     -- Join condition
    ;
    

    and other conditions occur in the WHERE (or HAVING or CONNECT BY) clause.

    SELECT  employee_id, manager_id, lvl + 1
    FROM            employees
    CROSS JOIN         tmptab
    WHERE  (manager_id = empId)     -- NOT a join condition
    ;
    

    In the joins of the former, it seems to be the case that any condition involving 2 or more tables (or the + indicator of outer join) is a condtion of join:

    SELECT  employee_id, manager_id, lvl + 1
    FROM      employees
    ,         tmptab
    WHERE  (manager_id = empId)     -- Join condition
    ;
    
  • CARTESIAN/CROSS JOIN

    Hello
    I use OBIEE version 11.1.1.6.0. I created made dummy table and column in the physical layer. and join all the dimension table with this fact. but I don't know how the business model to deal with it. can someone help me to create a cross join?
    Thank you

    Published by: 968086 on October 30, 2012 12:59 AM

    Follow this link
    http://www.rittmanmead.com/2009/08/Oracle-BI-EE-10-1-3-4-1-reporting-on-non-transactional-dimension-values-equivalence-of-outer-joins/

    Score pls help if

  • Extract multiple lines using the technique of the cross join

    Hello.

    Can someone suggest a method to return 3 lines of a query when otherwise would return only one line?

    I am trying to reach the analog SQL logic to this topic-
    SELECT x.foo, p.id, p.name FROM people p CROSS JOIN (SELECT 1 AS foo UNION ALL SELECT 2 UNION ALL SELECT 3) x;
    Reason: I want a result three rows (n) force and use GROUP BY with case statements to create 3 levels summary.

    Here is a simple SQL logical expression that works for my setup - OBI
    SELECT
         "- Nx_CSDG0_Repair_Orders (Depot Repair Views)".Repair_Number saw_0,
         "- Nx_CSDG0_Repair_Orders (Depot Repair Views)".SR_Operating_Unit_Name saw_1
    FROM
         "[Noetix-NoetixGlobalRepository] NoetixViews for Oracle Service"     
    WHERE 
         ("- Nx_CSDG0_Repair_Orders (Depot Repair Views)".Repair_Number = '338246')
    But I think that I can't do--
    SELECT
         "- Nx_CSDG0_Repair_Orders (Depot Repair Views)".Repair_Number saw_0,
         "- Nx_CSDG0_Repair_Orders (Depot Repair Views)".SR_Operating_Unit_Name saw_1
    FROM
         "[Noetix-NoetixGlobalRepository] NoetixViews for Oracle Service"
         CROSS JOIN (SELECT 1 AS foo UNION ALL SELECT 2 UNION ALL SELECT 3)
    WHERE 
         ("- Nx_CSDG0_Repair_Orders (Depot Repair Views)".Repair_Number = '338246')
    But what peut do?

    Thank you.

    -cs

    Hi CSeelig,

    The BI server uses the ANSI SQL standard. Then all the SQL that follows this specification in OBIEE will work.

    I did an example with intensification:
    http://gerardnico.com/wiki/dat/OBIEE/logical_sql/obiee_sql_densification

    You will see a cross join to make a densification.

    See you soon
    Nico

  • [8i] need help with full outer join combined with a cross join...

    I can't understand how to combine a full outer join with a different type of join... is it possible?

    Here are some create table and insert for examples of database:
    CREATE TABLE     my_tab1
    (     record_id     NUMBER     NOT NULL     
    ,     workstation     VARCHAR2(4)
    ,     my_value     NUMBER
         CONSTRAINT my_tab1_pk PRIMARY KEY (record_id)
    );
    
    INSERT INTO     my_tab1
    VALUES(1,'ABCD',10);
    INSERT INTO     my_tab1
    VALUES(2,'ABCD',15);
    INSERT INTO     my_tab1
    VALUES(3,'ABCD',5);
    INSERT INTO     my_tab1
    VALUES(4,'A123',5);
    INSERT INTO     my_tab1
    VALUES(5,'A123',10);
    INSERT INTO     my_tab1
    VALUES(6,'A123',20);
    INSERT INTO     my_tab1
    VALUES(7,'????',5);
    
    
    CREATE TABLE     my_tab2
    (     workstation     VARCHAR2(4)
    ,     wkstn_name     VARCHAR2(20)
         CONSTRAINT my_tab2_pk PRIMARY KEY (workstation)
    );
    
    INSERT INTO     my_tab2
    VALUES('ABCD','WKSTN 1');
    INSERT INTO     my_tab2
    VALUES('A123','WKSTN 2');
    INSERT INTO     my_tab2
    VALUES('B456','WKSTN 3');
    
    CREATE TABLE     my_tab3
    (     my_nbr1     NUMBER
    ,     my_nbr2     NUMBER
    );
    
    INSERT INTO     my_tab3
    VALUES(1,2);
    INSERT INTO     my_tab3
    VALUES(2,3);
    INSERT INTO     my_tab3
    VALUES(3,4);
    And, the results that I want to get:
    workstation     sum(my_value)     wkstn_name     my_nbr1     my_nbr2
    ---------------------------------------------------------------
    ABCD          30          WKSTN 1          1     2
    ABCD          30          WKSTN 1          2     3
    ABCD          30          WKSTN 1          3     4
    A123          35          WKSTN 2          1     2
    A123          35          WKSTN 2          2     3
    A123          35          WKSTN 2          3     4
    B456          0          WKSTN 3          1     2
    B456          0          WKSTN 3          2     3
    B456          0          WKSTN 3          3     4
    ????          5          NULL          1     2
    ????          5          NULL          2     3
    ????          5          NULL          3     4
    I tried a number of different things, google my problem and no luck yet...
    SELECT     t1.workstation
    ,     SUM(t1.my_value)
    ,     t2.wkstn_name
    ,     t3.my_nbr1
    ,     t3.my_nbr2
    FROM     my_tab1 t1
    ,     my_tab2 t2
    ,     my_tab3 t3
    ...
    So, what I want, it's a full outer join of t1 and t2 on workstation and a cross join of one with the t3. I wonder if I can't find examples of it online because it is not possible...

    Note: I'm stuck dealing with Oracle 8i

    Thank you!!

    Hello

    The query I posted yesterday is a little more complex that it should be.
    My_tab2.workstation is unique, there is no reason to make a separate subquery as mt1. We can join my_tab1 to my_tab2 and get the SUM in a subquery.

    SELECT       foj.workstation
    ,       foj.sum_my_value
    ,       foj.wkstn_name
    ,       mt3.my_nbr1
    ,       mt3.my_nbr2
    FROM       (     -- Begin in-line view foj for full outer join
              SELECT        mt1.workstation
              ,        SUM (mt1.my_value)     AS sum_my_value
              ,        mt2.wkstn_name
              FROM        my_tab1   mt1
              ,        my_tab2   mt2
              WHERE        mt1.workstation     = mt2.workstation (+)
              GROUP BY   mt1.workstation
              ,        mt2.wkstn_name
                            --
                    UNION ALL
                            --
              SELECT      workstation
              ,      0      AS sum_my_value
              ,      wkstn_name
              FROM      my_tab2
              WHERE      workstation     NOT IN (     -- Begin NOT IN sub-query
                                               SELECT      workstation
                                       FROM      my_tab1
                                       WHERE      workstation     IS NOT NULL
                                     )     -- End NOT IN sub-query
           ) foj     -- End in-line view foj for full outer join
    ,       my_tab3  mt3
    ORDER BY  foj.wkstn_name
    ,       foj.workstation
    ,       mt3.my_nbr1
    ,       mt3.my_nbr2
    ;
    

    Thanks for posting the CREATE TABLE and INSERT statements, and very clear expected results!

    user11033437 wrote:
    ... So, what I want, it's a full outer join of t1 and t2 on workstation and a cross join of one with the t3.

    She, exactly!
    The trickiest part is when and how get SUM (my_value). You could address the question of exactly what my_tab3 must be attached to a cross that's exactly what should look like the result set of the full outer join between my_tab1 and my_tab2 to. To do this, take your desired results, remove columns that do not come from the outer join complete and delete duplicate rows. You will get:

    workstation     sum(my_value)     wkstn_name
    -----------     -------------   ----------
    ABCD          30          WKSTN 1
    A123          35          WKSTN 2
    B456          0          WKSTN 3
    ????          5          NULL          
    

    So the heart of the problem is how to get these results of my_tab1 and my_tab2, which is done in the subquery FOJ above.

    I tried to use auto-documenté in my code names. I hope you can understand.
    I could spend hours explaining the different parts of this query more in detail, but I don't know that I would lose some of that time, explain things that you already understand. If you want an explanation of the specific element (s), let me know.

  • cross join?

    I was asked if the veiw (select what is wrttien to make the view) has a cross join and follwing some ANSI compliance. So my question is... as table 3 below is used by alias tp and same table3 is used by alias tp2... commandeer that be considered as a cross join?

    Sorry, I'm very new to SQL...
    select col1, col2, col3 from table1 t, table2 t1, table3 tp
    where
    ---
    ---
    ---
    AND tp.phs_id =
                          (SELECT tp2.phs_id
                             FROM table3 tp2
                            WHERE tp2.id = tsp.id
                             AND ROWNUM < 2)
    Published by: user11168115 on May 14, 2009 12:43

    Hello (and welcome)
    No, would not be a CROSS JOIN, since your alias tp2 JOINED tp (I guess you mean tp even if you have a TSP). A CROSS JOIN is where no JOIN condition at all is specified for a table (also known as the Cartesian product ), and if it's compatible ANSI SQL will actually be the words CROSS JOIN specified in the JOIN clause. For example:

    SELECT *
       FROM table1
     CROSS JOIN table2;
    

    Otherwise, non-compliant ANSI:

    SELECT *
      FROM table1, table2;
    
  • Cross join to Hi in oracle

    Hello

    I have below the text values.

    Platimum

    Silver

    VIP

    Bronze

    I want to get cross-from above like four

    PlatimumSilverVIPBronze
    PlatimumVIPSilverBronze
    PlatimumBronzeVIPSilver
    PlatimumSilverVIPBronze

    And so on. It should give me 16 combinations that I guess.

    How can I achieve this?

    Kind regards

    Mahesh

    Another way to generate permutations (if nocycle seems suspicious)

    with

    data in the form of

    (select "Platimum" val of union double all the)

    Select 'Cash' of all the double union

    Select 'VIP' of all the double union

    Select "Bronze" double

    ),

    permutator (swap) as

    (select val

    from the data

    Union of all the

    Select swap. «, » || Val

    data,.

    permutator

    where instr (permutation, val) = 0

    )

    Select the permutation

    of the permutator

    where regexp_count (permutation, ',') = 3

    order by swapping

    PERMUTATION
    Bronze, Platimum, silver, VIP
    Bronze, Platimum, VIP, Silver
    Bronze, Silver, Platimum, VIP
    Bronze, Silver, VIP, Platimum
    Bronze, VIP, Platimum, Silver
    Bronze, Silver, VIP, Platimum
    Platimum, Bronze, silver, VIP
    Platimum, Bronze, Silver, VIP
    Platimum, silver, Bronze, VIP
    Platimum, silver, VIP, Bronze
    Platimum, VIP, Bronze, silver
    Platimum, VIP, silver, Bronze
    Silver, Bronze, Platimum, VIP
    Silver, Bronze, VIP, Platimum
    Money, Platimum, Bronze, VIP
    Silver, Platimum, VIP, Bronze
    Silver, VIP, Bronze, Platimum
    Silver, VIP, Platimum, Bronze
    VIP, Bronze, Platimum, silver
    VIP Bronze, silver, Platimum
    VIP, Platimum, Bronze, silver
    VIP, Platimum, silver, Bronze
    VIP, silver, Bronze, Platimum
    VIP, money, Platimum, Bronze
  • Put the 'dynamic' on top headers a pivot

    I have a table that I organize by exercise, with months, from April to March each year.

    I have everything worked except for how to get the names of exercise as column headings. Even though I have to_char had values, I always get an ORA-1790 expressioon must have the same data type when I try to UNION ALL headings on the rest of the data.

    The two components work fine separately. Miss me probably just something simple, but what? (the original problem has a table with data of 18 years - I gave three only for brevity)

    Thank you

    Jon

    Desired is output:

    M 2011/2012 2012 2013 2013/2014 1 25194 24716 24015 2 25275 24794 23916 3 25005 24475 23592 4 24770 24577 23471 5 24806 24470 23543 6 24795 24265 23606 7 24636 24318 23504 8 24664 24262 23307 9 24406 24119 23293 10 24717 24039 23355 11 24718 23910 23317 12 24637 23894

    The table is:

    create table rb_raw (date of the m_date, total_cases number (6));

    insert into rb_raw values (to_date('01-JAN-11','dd-mon-yy'), 25119);

    insert into rb_raw values (to_date('01-FEB-11','dd-mon-yy'), 25026);

    insert into rb_raw values (to_date('01-MAR-11','dd-mon-yy'), 25305);

    insert into rb_raw values (to_date('01-APR-11','dd-mon-yy'), 25194);

    insert into rb_raw values (to_date('01-JUN-11','dd-mon-yy'), 25005);

    insert into rb_raw values (to_date('01-JUL-11','dd-mon-yy'), 24770);

    insert into rb_raw values (to_date('01-AUG-11','dd-mon-yy'), 24806);

    insert into rb_raw values (to_date('01-SEP-11','dd-mon-yy'), 24795);

    insert into rb_raw values (to_date('01-OCT-11','dd-mon-yy'), 24636);

    insert into rb_raw values (to_date('01-NOV-11','dd-mon-yy'), 24664);

    insert into rb_raw values (to_date('01-DEC-11','dd-mon-yy'), 24406);

    insert into rb_raw values (to_date('01-FEB-12','dd-mon-yy'), 24718);

    insert into rb_raw values (to_date('01-MAR-12','dd-mon-yy'), 24637);

    insert into rb_raw values (to_date('01-APR-12','dd-mon-yy'), 24716);

    insert into rb_raw values (to_date('01-JUN-12','dd-mon-yy'), 24475);

    insert into rb_raw values (to_date('01-JUL-12','dd-mon-yy'), 24577);

    insert into rb_raw values (to_date('01-AUG-12','dd-mon-yy'), 24470);

    insert into rb_raw values (to_date('01-SEP-12','dd-mon-yy'), 24265);

    insert into rb_raw values (to_date('01-OCT-12','dd-mon-yy'), 24318);

    insert into rb_raw values (to_date('01-DEC-12','dd-mon-yy'), 24119);

    insert into rb_raw values (to_date('01-JAN-13','dd-mon-yy'), 24039);

    insert into rb_raw values (to_date('01-FEB-13','dd-mon-yy'), 23910);

    insert into rb_raw values (to_date('01-MAR-13','dd-mon-yy'), 23894);

    insert into rb_raw values (to_date('01-APR-13','dd-mon-yy'), 24015);

    insert into rb_raw values (to_date('01-JUN-13','dd-mon-yy'), 23592);

    insert into rb_raw values (to_date('01-JUL-13','dd-mon-yy'), 23471);

    insert into rb_raw values (to_date('01-AUG-13','dd-mon-yy'), 23543);

    insert into rb_raw values (to_date('01-SEP-13','dd-mon-yy'), 23606);

    insert into rb_raw values (to_date('01-OCT-13','dd-mon-yy'), 23504);

    insert into rb_raw values (to_date('01-NOV-13','dd-mon-yy'), 23307);

    insert into rb_raw values (to_date('01-DEC-13','dd-mon-yy'), 23293);

    insert into rb_raw values (to_date('01-MAY-11','dd-mon-yy'), 25275);

    insert into rb_raw values (to_date('01-MAY-12','dd-mon-yy'), 24794);

    insert into rb_raw values (to_date('01-MAY-13','dd-mon-yy'), 23916);

    insert into rb_raw values (to_date('01-JAN-14','dd-mon-yy'), 23355);

    insert into rb_raw values (to_date('01-FEB-14','dd-mon-yy'), 23317);

    insert into rb_raw values (to_date('01-JAN-12','dd-mon-yy'), 24717);

    insert into rb_raw values (to_date('01-NOV-12','dd-mon-yy'), 24262);

    and my current query (with the part of the position currently set comment):

    with b as

    (Select to_char (basefisc - (18-y)) |) '/' || TO_CHAR (basefisc - (17-y)) fyear, m_date, y, m, to_char (total_cases) total_cases

    Of

    (select m_date, / * months_between (m_date, f.fystart) MB, * / floor ((months_between (m_date,f.fystart)-1) / 12) + 18 y,)

    CASES WHERE extract (m_date months) > 3 then extract(month from m_date)-3 9 other + extract (m_date months) end m.

    extract (year fystart) basefisc, total_cases

    of rb_raw

    cross join

    (select case when extracted (month of to_date (: current_month, 'YYYYMM')) < 3 then add_months (to_date (": current_month, ' YYYYMM"), - 9-extract (month of to_date (: current_month, 'YYYYMM'))))

    else add_months (to_date (": current_month, ' YYYYMM"),-(extrait (mois de to_date (: current_month, 'AAAAMM'))-4))

    end fystart

    the double) f

    where the floor (months_between (m_date, f.fystart) / 12) + 18 > 0

    order of m_date)

    )

    --

    Select *.

    Of

    (/ *(select *))

    Of

    (select 0 m, fyear)

    b

    )

    Pivot (max (fyear) for y in (16,17,18)))

    UNION ALL * /.

    (select *)

    Of

    (select y, m, total_cases

    b

    )

    Pivot (sum (total_cases) for y in (16,17,18)))

    )

    order by m

    ;

    Hi, Jon,

    This can give you some ideas on how to increase the effectiveness of your request (and, at least in my opion) more easy to understand:

    WITH years_wanted AS

    (

    SELECT LEVEL AS no_y

    EXTRACT (YEAR FROM ADD_MONTHS (TO_DATE (: current_month, "YYYYMM")))

    9 + (12 * (1 - LEVEL))

    )

    ) As the exercise

    OF the double

    CONNECT BY LEVEL<= 3 ="" --="" change="" to="" 18="" for="" final="">

    )

    got_fy AS

    (

    SELECT TO_CHAR (EXTRACT (MONTH OF ADD_MONTHS (m_date-3))

    '99'

    )                                AS month

    , TO_CHAR (total_cases, "999999") AS total_string

    EXTRACT (YEAR FROM TRUNC (ADD_MONTHS (m_date, 9)))

    , 'YEAR '.

    )

    )                                AS fy

    Rb_raw r

    )

    SELECT 'MONTH' by MONTH

    -, MAX (CASE WHEN no_y = 18 THEN (fy - 1) |) '/' || the fiscal year END) AS y18

    --        ...

    , MAX (CASE WHEN no_y = 3 THEN (fy - 1) |) '/' || the fiscal year END) AS y3

    , MAX (CASE WHEN no_y = 2 THEN (fy - 1) |) '/' || the fiscal year END) AS y2

    , MAX (CASE WHEN no_y = 1 THEN (fy - 1) |) '/' || the fiscal year END) AS y1

    Of years_wanted

    UNION ALL

    SELECT month

    -, MAX (CASE WHEN w.y_num = 18 THEN f.total_string END) AS y18

    --        ...

    MAX (CASE WHEN w.y_num = 3 THEN f.total_string END) AS y3

    MAX (CASE WHEN w.y_num = 2 THEN f.total_string END) AS y2,

    MAX (CASE WHEN w.y_num = 1 THEN f.total_string END) AS y1,

    OF got_fy f

    JOIN years_wanted w f.fy = w.fy

    GROUP BY f.month

    ORDER BY month

    ;

    This option displays the exercises 3 (or 18), ending with one that includes: current_month.

    Given your sample data and: current_month = '201403', the output is:

    MONTH 2011/2012 2012 2013 2013/2014

    1 25194 24716 24015

    2 25275 24794 23916

    3 25005 24475 23592

    4 24770 24577 23471

    5 24806 24470 23543

    6 24795 24265 23606

    7 24636 24318 23504

    8 24664 24262 23307

    9 24406 24119 23293

    10 24717 24039 23355

    11 24718 23910 23317

    12 24637 23894

  • Cross Tab with group by period

    Good afternoon

    I wonder if some can help me with this query, I am writing?

    The cross-tab is for columns 1 to 31, representing the days of the month, sum of the quantity of an article sold.

    In addition the company description.

    I would then finish with 3 entrances in the results for each company, but these are for the sum of the quantity of items in the following periods: -.

    Breakfast: 01:00 - 10:00
    Lunch: 11:00 - 14:00
    Dinner: 17:00 - 22:30

    Yes, the company is closed for the missing periods, for example 10:00-11:00 & 14:00 to 17:00.

    Test case:
    CREATE TABLE COMPANIES
      (
        "ID"            NUMBER(9,0),
        "CODE"          NUMBER(8,0),
        "DESCRIPTION"   VARCHAR2(40 CHAR),
        CONSTRAINT "PK_COMPANIES" PRIMARY KEY ("ID")
      );
    
    INSERT INTO COMPANIES VALUES (1,1,'COMPANY A');
    INSERT INTO COMPANIES VALUES (2,2,'COMPANY B');
    INSERT INTO COMPANIES VALUES (3,3,'COMPANY C');
    
    
    CREATE TABLE CUSTOMERS
      (
        "ID"                     NUMBER(9,0),
        "CODE"                   VARCHAR2(30 CHAR),
        "CARD_NUM"               VARCHAR2(30 CHAR),
        "DESCRIPTION"            VARCHAR2(40 CHAR),
        "COMPANY_ID"                   NUMBER(9,0),
        CONSTRAINT "PK_CUSTOMERS" PRIMARY KEY ("ID")
      );
    
    INSERT INTO CUSTOMERS VALUES (1,'001','001','A CUSTOMER', 1);
    INSERT INTO CUSTOMERS VALUES (2,'002','002','A CUSTOMER', 2);
    INSERT INTO CUSTOMERS VALUES (3,'003','003','A CUSTOMER', 3);
    INSERT INTO CUSTOMERS VALUES (4,'004','004','A CUSTOMER', 1);
    INSERT INTO CUSTOMERS VALUES (5,'005','005','A CUSTOMER', 2);
    INSERT INTO CUSTOMERS VALUES (6,'006','006','A CUSTOMER', 1);
    
    
    CREATE TABLE ARTICLES
      (
        "ID"                     NUMBER(9,0),
        "CODE"                   VARCHAR2(20 CHAR),
        "DESCRIPTION"            VARCHAR2(40 CHAR),
        CONSTRAINT "PK_ARTICLES" PRIMARY KEY ("ID")
      );
    
    INSERT INTO ARTICLES
      VALUES (1,'001', 'A Meal');
    
    
    
    CREATE TABLE TRANSACTIONS
      (
        "ID"          NUMBER(9,0),
        "TILL_ID"     NUMBER(9,0) NOT NULL ENABLE,
        "SHOP_ID"     NUMBER(9,0) NOT NULL ENABLE,
        "OPERATOR_ID" NUMBER(9,0),
        "TRANS_NUM"   NUMBER(5,0) NOT NULL ENABLE,
        "SPLIT_NUM"   NUMBER(5,0),
        "TRANS_DATE" DATE NOT NULL ENABLE,
        "TOTAL_AMOUNT"      NUMBER(12,2) NOT NULL ENABLE,
        "BOOKKEEPING_DATE" DATE NOT NULL ENABLE,
        "CARD_NUM"           VARCHAR2(30 CHAR),
        CONSTRAINT "PK_TRANSACTIONS" PRIMARY KEY ("ID")
      );
    
    INSERT INTO TRANSACTIONS VALUES (1,1,1,1,1,NULL,to_date('27.04.2011 10:30:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'001');
    INSERT INTO TRANSACTIONS VALUES (2,1,1,1,1,NULL,to_date('27.04.2011 10:31:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'002');
    INSERT INTO TRANSACTIONS VALUES (3,1,1,1,1,NULL,to_date('27.04.2011 10:32:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'003');
    INSERT INTO TRANSACTIONS VALUES (4,1,1,1,1,NULL,to_date('27.04.2011 10:33:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'004');
    INSERT INTO TRANSACTIONS VALUES (5,1,1,1,1,NULL,to_date('27.04.2011 10:34:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'005');
    INSERT INTO TRANSACTIONS VALUES (6,1,1,1,1,NULL,to_date('27.04.2011 10:35:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'006');
    INSERT INTO TRANSACTIONS VALUES (7,1,1,1,1,NULL,to_date('27.04.2011 14:30:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'001');
    INSERT INTO TRANSACTIONS VALUES (8,1,1,1,1,NULL,to_date('27.04.2011 14:31:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'002');
    INSERT INTO TRANSACTIONS VALUES (9,1,1,1,1,NULL,to_date('27.04.2011 14:32:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'003');
    INSERT INTO TRANSACTIONS VALUES (10,1,1,1,1,NULL,to_date('27.04.2011 14:33:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'004');
    INSERT INTO TRANSACTIONS VALUES (11,1,1,1,1,NULL,to_date('27.04.2011 14:34:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'005');
    INSERT INTO TRANSACTIONS VALUES (12,1,1,1,1,NULL,to_date('27.04.2011 14:35:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'006');
    INSERT INTO TRANSACTIONS VALUES (13,1,1,1,1,NULL,to_date('27.04.2011 22:00:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'001');
    INSERT INTO TRANSACTIONS VALUES (14,1,1,1,1,NULL,to_date('27.04.2011 22:01:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'002');
    INSERT INTO TRANSACTIONS VALUES (15,1,1,1,1,NULL,to_date('27.04.2011 22:02:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'003');
    INSERT INTO TRANSACTIONS VALUES (16,1,1,1,1,NULL,to_date('27.04.2011 22:03:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'004');
    INSERT INTO TRANSACTIONS VALUES (17,1,1,1,1,NULL,to_date('27.04.2011 22:04:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'005');
    INSERT INTO TRANSACTIONS VALUES (18,1,1,1,1,NULL,to_date('27.04.2011 22:05:05', 'DD.MM.YYYY HH24:MI:SS'),0.01,to_date('27.04.2011', 'DD.MM.YYYY'),'006');
    
    CREATE TABLE TRANS_ARTICLES
      (
        "TRANSACTION_ID"        NUMBER(9,0) NOT NULL ENABLE,
        "ARTICLE_ID"            NUMBER(9,0) NOT NULL ENABLE,
        "QTY_WEIGHT"            NUMBER(10,3) NOT NULL ENABLE,
        "PRICE"                 NUMBER(10,2) NOT NULL ENABLE,
        CONSTRAINT "PK_TRANS_ARTICLES" PRIMARY KEY ("TRANSACTION_ID")
      )
    
    INSERT INTO TRANS_ARTICLES VALUES (1,1,1,0.01);
    INSERT INTO TRANS_ARTICLES VALUES (2,1,1,0.01);
    INSERT INTO TRANS_ARTICLES VALUES (3,1,1,0.01);
    INSERT INTO TRANS_ARTICLES VALUES (4,1,1,0.01);
    INSERT INTO TRANS_ARTICLES VALUES (5,1,1,0.01);
    INSERT INTO TRANS_ARTICLES VALUES (6,1,1,0.01);
    INSERT INTO TRANS_ARTICLES VALUES (7,1,1,0.01);
    INSERT INTO TRANS_ARTICLES VALUES (8,1,1,0.01);
    INSERT INTO TRANS_ARTICLES VALUES (9,1,1,0.01);
    INSERT INTO TRANS_ARTICLES VALUES (10,1,1,0.01);
    INSERT INTO TRANS_ARTICLES VALUES (11,1,1,0.01);
    INSERT INTO TRANS_ARTICLES VALUES (12,1,1,0.01);
    INSERT INTO TRANS_ARTICLES VALUES (13,1,1,0.01);
    INSERT INTO TRANS_ARTICLES VALUES (14,1,1,0.01);
    INSERT INTO TRANS_ARTICLES VALUES (15,1,1,0.01);
    INSERT INTO TRANS_ARTICLES VALUES (16,1,1,0.01);
    INSERT INTO TRANS_ARTICLES VALUES (17,1,1,0.01);
    INSERT INTO TRANS_ARTICLES VALUES (18,1,1,0.01);
    With similar results to: -.
    1     Company A - 01:00 10:00     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     0     0     0     0
    1     Company A - 11:00 14:00     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     0     0     0     0
    1     Company A - 17:00 22:30     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     3     0     0     0     0
    2     Company B - 01:00 10:00     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     0     0     0     0
    2     Company B - 11:00 14:00     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     2     0     0     0     0
    2     Company B - 17:00 22:30     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     2     0     0     0     0
    This is the Group of periods which is I don't know how to do.

    Thank you & best regards,

    Andrew

    Hello

    Here's a way to do it:

    WITH     all_time_slots     AS
    (
         SELECT  '01:00 - 11:00' AS label,  1 / 24 AS start_time, 11 / 24 AS end_time     FROM dual
         UNION ALL
         SELECT  '12:00 - 15:00',       12 / 24,               15 / 24          FROM dual
         UNION ALL
         SELECT  '17:00 - 23:30',          17 / 24,               23.5 / 24          FROM dual
    )
    ,     all_days     AS
    (
         SELECT     LEVEL     AS day_of_month
         FROM     dual
         WHERE     LEVEL     IN (1, 27, 31)          --     *****  FOR TESTING ONLY  *****
         CONNECT BY     LEVEL     <= 31
    )
    ,     inner_joins     AS
    (
         SELECT       EXTRACT  (DAY  FROM  tr.bookkeeping_date)     AS day_of_month
         ,       tr.trans_date
         ,       ta.qty_weight
         ,       cu.company_id
         FROM       articles            ar
         JOIN       trans_articles       ta  ON   ta.article_id     = ar.id
         JOIN       transactions              tr  ON   tr.id          = ta.transaction_id
         JOIN       customers            cu  ON   cu.card_num          = tr.card_num
               WHERE       ar.description       = 'A Meal'
               AND       tr.bookkeeping_date  >= DATE '2011-04-01'
         AND       tr.bookkeeping_date  <  DATE '2011-05-01'
    )
    ,     unpivoted_data     AS
    (
         SELECT       co.description
         ,       ad.day_of_month
         ,       NVL (ij.qty_weight, 0)     AS weight_0
         ,       ts.label
         FROM            companies       co
         CROSS JOIN        all_time_slots  ts
         CROSS JOIN       all_days       ad
         LEFT OUTER JOIN       inner_joins       ij   ON   ij.company_id     = co.id
                                              AND  ij.day_of_month     = ad.day_of_month
                                              AND  ij.trans_date - TRUNC (ij.trans_date)
                                                                 BETWEEN  ts.start_time
                                                        AND          ts.end_time
    )
    SELECT       *
    FROM       unpivoted_data
    PIVOT       (     SUM (weight_0)
           FOR     day_of_month     IN
                (      1     AS day_1
              ,     27     AS day_27
              ,     31     AS day_31
              )
           )
    ORDER BY  description
    ,            label
    ;
    

    Ouput, given the example of data that you changed it about an hour ago:

    DESCRIPTION LABEL              DAY_1     DAY_27     DAY_31
    ----------- ------------- ---------- ---------- ----------
    COMPANY A   01:00 - 11:00          0          3          0
    COMPANY A   12:00 - 15:00          0          3          0
    COMPANY A   17:00 - 23:30          0          3          0
    
    COMPANY B   01:00 - 11:00          0          2          0
    COMPANY B   12:00 - 15:00          0          2          0
    COMPANY B   17:00 - 23:30          0          2          0
    
    COMPANY C   01:00 - 11:00          0          1          0
    COMPANY C   12:00 - 15:00          0          1          0
    COMPANY C   17:00 - 23:30          0          1          0
    

    Sorry, I'm short on time right now.  I'll try to post an explanation in 4 hours approximately.
    See my next post for an explanation.

    Published by: Frank Kulash, 28 April 2011 21:29

  • Expected to a join behaviours?

    Hello

    formalities:

    Select * from version of v$.

    Oracle Database 11 g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production

    PL/SQL Release 11.2.0.3.0 - Production

    CORE Production 11.2.0.3.0

    AMT for 64-bit Windows: Version 11.2.0.3.0 - Production

    NLSRTL Version 11.2.0.3.0 - Production

    the question:

    I was just wondering why this "join" between A and B do not work. A has no lines, B a 1 row

    > my first answer... it should give 1 rank...

    but alas... it doesn't!

    the original request:

    SELECT A.*,
      B.*,
    CASE WHEN A.METH = 'PAL' THEN '1'
      ELSE A.CFGNR_OFRESTPAL 
    END VWNR_OFRESTPAL
    FROM    CDCROBOTVERDICHTLOCSTATVW A,
      CDCVERDICHTSETTINGSVW B
    

    > No rows returned

    so I checked the counts:

    Select count (*) in the CDCROBOTVERDICHTLOCSTATVW A;

    0

    Select count (*) in the CDCVERDICHTSETTINGSVW B;

    1

    Hmmm... very strange

    so I ran a few tests more:

    SELECT  A.*, B.*, CASE WHEN A.METH = 'PAL' THEN '1' ELSE A.CFGNR_OFRESTPAL END VWNR_OFRESTPAL
    FROM    CDCVERDICHTSETTINGSVW B , CDCROBOTVERDICHTLOCSTATVW A 
    

    > no line

    SELECT A.*, B.*, CASE WHEN A.METH = 'PAL' THEN '1' ELSE A.CFGNR_OFRESTPAL END VWNR_OFRESTPAL
    FROM    CDCVERDICHTSETTINGSVW B cross join CDCROBOTVERDICHTLOCSTATVW A 
    

    > no line

    SELECT A.*, B.*, CASE WHEN A.METH = 'PAL' THEN '1' ELSE A.CFGNR_OFRESTPAL END VWNR_OFRESTPAL
    FROM    CDCVERDICHTSETTINGSVW B full outer join CDCROBOTVERDICHTLOCSTATVW A on 1 =1
    

    > This returns the line from B correctly

    ARTNR PROJECTNR VARIANT PROVIDER QMAX PRIO METH PALLETS SOMQTY FULLPALS RESTQTY RESTPALS CFGNR_OFRESTPAL FREELOCS DUMMYRECIP GEEN_VERDICHTING MAXSRCPALS MAXOPENPICKSETS NR_OF_RESTPAL ORDTYPE PRIO_ARTNR VERD_INRGRP VWNR_OFRESTPAL
    VERDICHT1143VERDICHTQSE00918CLL80CDCROB

    SELECT A.*, B.*, CASE WHEN A.METH = 'PAL' THEN '1' ELSE A.CFGNR_OFRESTPAL END VWNR_OFRESTPAL
    FROM  CDCVERDICHTSETTINGSVW B left join CDCROBOTVERDICHTLOCSTATVW A on 1 = 1
    

    > it is also returns the same rank B

    Does anyone have an idea on how to fix this... or where to look to fix this?

    This is a cross join. It is a Cartesian product. If there are no rows in a table, then you get no rows in the result.

    SQL> with a as (select 1 x from dual), b as (select 1 y from dual where 1=0)
      2  select a.*, b.*
      3  from a, b;                                                             
    
    no rows selected
    

    A left or right, or full outer join is apparently what you wanted.

  • Join Oracle

    Hello

    I have the following data

    create or replace view offdays as

    (

    Select 1 empid, to_date('23/01/2015','dd/mm/yyyy') dt, 1 offday for all double union

    Select 2 empid, to_date('23/01/2015','dd/mm/yyyy') dt, 1 offday for all double union

    Select 3 empid, to_date('21/01/2015','dd/mm/yyyy') dt, 1 double offday

    );

    create or replace view payrolldates as

    (

    Select to_date('21/01/2015','dd/mm/yyyy') as dt of union double all the

    Select to_date('22/01/2015','dd/mm/yyyy') as dt of union double all the

    Select to_date('23/01/2015','dd/mm/yyyy') as dt of union double all the

    Select to_date('24/01/2015','dd/mm/yyyy') as dt of union double all the

    Select to_date (' 25/01/2015 ',' dd/mm/yyyy') in the double dt

    );

    Select * from payrolldates and b offdays left outer join on b.dt order = b.empid a.dt, a.dt;

    With the above query, I get after release

    DATE

    EMPID

    OFF

    23/01/2015

    1

    1

    23/01/2015

    2

    1

    21/01/2015

    3

    1

    22/01/2015

    < NULL >

    < NULL >

    24/01/2015

    < NULL >

    < NULL >

    25/01/2015

    < NULL >

    < NULL >

    But I need the output as follows

    DATE

    EMPID

    OFF

    21/01/2015

    1

    0

    22/01/2015

    1

    0

    23/01/2015

    1

    1

    24/01/2015

    1

    0

    25/01/2015

    1

    0

    21/01/2015

    2

    0

    22/01/2015

    2

    0

    23/01/2015

    2

    1

    24/01/2015

    2

    0

    25/01/2015

    2

    0

    21/01/2015

    3

    1

    22/01/2015

    3

    0

    23/01/2015

    3

    0

    24/01/2015

    3

    0

    25/01/2015

    3

    0

    I use oracle 10 g.

    Help, please

    According to the power requested that each record in the view offdays should be repeated as many times as the number of records in the view of payrolldates-, this could be done via the cross join and a simple CASE statement to get an output of the column offday.

    Select a.dt

    empid

    case when a.dt = b.dt

    then offday

    0 otherwise

    end offday

    of payrolldates one

    offdays b

    order of b.empid, a.dt;

  • join with condition

    Hi all

    Is there a possible way to cross the join with condition?

    I want something like this:

    Table A: custid, accu_id, sum

    CustID, accu_id unique =

    CustomerID amount accu_id

    10 25 400

    10 35 447

    10 29 420

    20 30 510

    30 35 472

    .

    .

    .

    Table b: accu_id, description

    accu_id description

    shoes 25

    Book 29

    30 computer

    pen 35

    Note: in table A, it is a mismatch of the accu_id values that do not use. Please, take into account this.

    I want to see all the columns in the TABLE B for each custid in A. TABLE (something like cross join but with ONE article.) The following query does not work.

    Select * from a right join B on A.accu_id = B.accu_id;

    10-25

    10-29

    10-30

    10-35

    20-25

    20-29

    20-30

    20-35

    30 25

    30 29

    30 30

    30-35

    What should I do for this?

    Thanks in advance

    Use partition outer join:

    with a (too)

    Select double union all 10 custid, accu_id 25, amount 400

    Select 10,35,447 from all the double union

    Select 10,29,420 from all the double union

    Select 20,30,510 from all the double union

    Select double 30,35,472

    ),

    b like)

    Select accu_id 25, "shoe" description of all the double union

    Select 29, 'book' from dual union all

    Select 30, 'computer' from dual union all

    Select 35, 'pen' from dual

    )

    Select a.custid,

    a.accu_id,

    a.amount,

    b.

    a

    by (a.custid) partition

    right join

    b

    On a.accu_id = b.accu_id

    order of a.custid,

    a.accu_id

    /

    AMOUNT ACCU_ID CUSTID DESCRIPT
    ---------- ---------- ---------- --------
    10 25 400 shoe
    10 29 420 book
    10 35 447 pen
    10 computer
    20 30 510 computer
    shoe 20
    book 20
    20                       pen
    30 35 472 pen
    30 shoe
    30 book

    AMOUNT ACCU_ID CUSTID DESCRIPT
    ---------- ---------- ---------- --------
    30 computer

    12 selected lines.

    SQL >

    SY.

  • Join dba_segments to dba_indexes

    Oracle 11.2.0.2.0

    Oracle 5.6 Linux 64 bit

    The following query is based on one I've seen on AskTom.  The first is to generate the sql code to move the tables to one tablespace to another.  I am trying to add logic to

    (1) to generate code for a single table, as specified by the user

    (2) also generate code to rebuild all the indexes on the selected table.

    Here's what I have so far.  Note that all "tables" are seen in the standard dictionary.

    Select decode (segment_type, 'TABLE',

    nom_segment, table_name) order_col1,.

    Decode (segment_type, 'TABLE', 1, 2) order_col2.

    'Edit ' | segment_type. ' ' || nom_segment |

    Decode (segment_type, 'TABLE', 'move', 'rebuild') |

    Decode (segment_type, 'TABLE', "tablespace stageb",

    ' (rebuild tablspace stage_idx') |

    ';'

    from dba_segments,.

    (select table_name, index_name dba_indexes)

    where segment_type in ('TABLE', 'INDEX')

    and nom_tablespace in ('STAGE', 'STAGE_IDX')

    and (segment_type = 'TABLE' and nom_segment = upper ('& tblnme'))

    and nom_segment = index_name

    order by 1, 2

    I tried various permutations, or return the code for all indexes regardless of their association of table, or no index at all.  Having trouble getting my head wrapped around it.

    Like this? I've highlighted in RED user input values.

    SQL > with my_segment
    2 as
    (3)
    4. Select the owner 'STAGE', 'TABLE', 'My_table' double nom_segment segment_type
    5 Union all the
    6. Select the owner 'STAGE', 'TABLE', 'FUBAR' double nom_segment segment_type
    7 union of all the
    8. Select owner 'STAGE', 'INDEX', 'UK_ATTR_GROUP' nom_segment double segment_type
    9)
    10, my_index
    11 as
    (12)
    13. Select owner 'STAGE' index_name 'UK_ATTR_GROUP', 'STAGE' table_owner, table_name "My_table", nom_tablespace "STAGE".
    14 double
    all 15 union
    16. Select index_name 'PK_MY_TABLE', 'STAGE' table_owner, table_name 'My_table', owner 'STAGE', 'STAGE_IDX' nom_tablespace
    17 of the double
    18)
    19 select coalesce (table_script, index_script) script
    20 of)
    21 select case when rno = 1 then
    22 'alter table ' | owner | '.' || nom_segment | "move the tablespace STAGE_B;
    end 23 table_script
    24, "alter index". owner | '.' || index_name | 'rebuild TABLESPACE STAGE_IDX;' index_script
    25 years of)
    26 select s.owner
    27, s.segment_name
    28, i.index_name


    29, row_number() over NWR (partition by order of s.segment_name of i.index_name)
    30                           , ind
    s my_segment 31
    32                        left
    Join 33 my_index I
    34 on s.owner = i.table_owner
    35 and s.segment_name = i.table_name
    36 cross join (select level double ind connect by level<=>
    37 where s.segment_type = 'TABLE '.
    38 and s.segment_name = 'my_table '.
    39                   )
    where the 40 (rno = 1 and ind (1, 2))
    41 or (rno > 1 and ind in (1))
    42         );

    SCRIPT
    -------------------------------------------------------------
    ALTER table STEP. My_table move tablespace STAGE_B;
    change the index STEP. PK_MY_TABLE rebuild TABLESPACE STAGE_IDX;
    change the STAGE.UK_ATTR_GROUP TABLESPACE STAGE_IDX index rebuild;

    SQL >

Maybe you are looking for