2010-08-09 15 views
6

Ho una tabella che emette simile a questo (sebbene in migliaia):(Oracle) come righe di gruppo per l'impaginazione

 EMPNO ENAME  TRANDATE  AMT 
---------- ---------- --------- ------- 
     100 Alison  21-MAR-96 45000 
     100 Alison  12-DEC-78 23000 
     100 Alison  24-OCT-82 11000 
     101 Linda  15-JAN-84 16000 
     101 Linda  30-JUL-87 17000 
     102 Celia  31-DEC-90 78000 
     102 Celia  17-SEP-96 21000 
     103 James  21-MAR-96 45000 
     103 James  12-DEC-78 23000 
     103 James  24-OCT-82 11000 
     104 Robert  15-JAN-84 16000 
     104 Robert  30-JUL-87 17000 

mio output desiderato sarebbe simile a questo:

 EMPNO ENAME  TRANDATE  AMT PAGE 
---------- ---------- --------- ------- ---- 
     100 Alison  21-MAR-96 45000 1 
     100 Alison  12-DEC-78 23000 1 
     100 Alison  24-OCT-82 11000 1 
     101 Linda  15-JAN-84 16000 2 
     101 Linda  30-JUL-87 17000 2 
     102 Celia  31-DEC-90 78000 2 
     102 Celia  17-SEP-96 21000 2 
     103 James  21-MAR-96 45000 3 
     104 Robert  12-DEC-78 23000 4 
     104 Robert  24-OCT-82 11000 4 
     104 Robert  15-JAN-84 16000 4 
     104 Robert  30-JUL-87 17000 4 

Fondamentalmente, dovrebbe inserire un nuovo campo per identificare la pagina a cui appartiene. L'interruzione di pagina si basa sulle righe. E, come se "tenuto insieme" in EMPNO, aggiunge 1 alla PAGINA quando le righe non possono aggiungere il successivo lotto EMPNO. È per il limite di Excel poiché Excel non consente più di 65000 righe (o così) in un singolo foglio. Nel caso del campione, sono solo 4 righe. Il numero limite è statico.

+0

Quindi, si può garantire che non EMPNO avrà> 65000 record? – APC

+0

Inoltre, quale versione di Excel stai usando? Excel 2007 consente una pazzesca 1.048.576 righe per foglio di lavoro. – APC

+0

Beh, speravo che potesse supportare anche le versioni di Excel precedenti al 2007. (http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP005199291.aspx) – keiko

risposta

2

ThinkJet ha ragione nel ritenere che alcune delle altre risposte non soddisfino il requisito di "tenere insieme". Tuttavia penso che questo possa essere fatto senza ricorrere a un aggregato definito dall'utente.

dati campione

create table test (empno number, ename varchar2(20), trandate date, amt number); 
insert into test values (100, 'Alison' , to_date('21-MAR-1996') , 45000); 
insert into test values (100, 'Alison' , to_date('12-DEC-1978') , 23000); 
insert into test values (100, 'Alison' , to_date('24-OCT-1982') , 11000); 
insert into test values (101, 'Linda' , to_date('15-JAN-1984') , 16000); 
insert into test values (101, 'Linda' , to_date('30-JUL-1987') , 17000); 
insert into test values (102, 'Celia' , to_date('31-DEC-1990') , 78000); 
insert into test values (102, 'Celia' , to_date('17-SEP-1996') , 21000); 
insert into test values (103, 'James' , to_date('21-MAR-1996') , 45000); 
insert into test values (103, 'James' , to_date('12-DEC-1978') , 23000); 
insert into test values (103, 'James' , to_date('24-OCT-1982') , 11000); 
insert into test values (104, 'Robert' , to_date('15-JAN-1984') , 16000); 
insert into test values (104, 'Robert' , to_date('30-JUL-1987') , 17000); 

Ora, determinano la fila fine di ogni segmento empno (usando RANK per trovare l'inizio e COUNT..PARTITION BY per trovare il numero del segmento).

Quindi utilizzare ceil/4 dalla soluzione APC per raggrupparli nelle loro "pagine". Ancora una volta, come sottolineato da ThinkJet, c'è un problema nelle specifiche in quanto non si adatta alla situazione quando ci sono più record nel segmento 'keep together' di empno che possono essere contenuti in una pagina.

select empno, ename, 
     ceil((rank() over (order by empno) + 
     count(1) over (partition by empno))/6) as chunk 
from test 
order by 1; 

Come sottolineato da ThinkJet, questa soluzione non è a prova di proiettile.

drop table test purge; 

create table test (empno number, ename varchar2(20), trandate date, amt number); 
declare 
    cursor csr_name is 
    select rownum emp_id, 
      decode(rownum,1,'Alan',2,'Brian',3,'Clare',4,'David',5,'Edgar', 
      6,'Fred',7,'Greg',8,'Harry',9,'Imran',10,'John', 
      11,'Kevin',12,'Lewis',13,'Morris',14,'Nigel',15,'Oliver', 
      16,'Peter',17,'Quentin',18,'Richard',19,'Simon',20,'Terry', 
      21,'Uther',22,'Victor',23,'Wally',24,'Xander', 
      25,'Yasmin',26,'Zac') emp_name 
    from dual connect by level <= 26; 
begin 
    for c_name in csr_name loop 
    for i in 1..11 loop 
     insert into test values 
      (c_name.emp_id, c_name.emp_name, (date '2010-01-01') + i, 
      to_char(sysdate,'SS') * 1000); 
    end loop; 
    end loop; 
end; 
/

select chunk, count(*) 
from 
    (select empno, ename, 
     ceil((rank() over (order by empno) + 
     count(1) over (partition by empno))/25) as chunk 
    from test) 
group by chunk 
order by chunk 
; 

Quindi, con dimensioni pezzo di 25 e dimensione del gruppo di 11, otteniamo i salti dove si adatta 33 persone nel pezzo, nonostante il limite di 25. Grandi dimensioni del blocco e piccoli gruppi dovrebbero rendere questo poco frequente, ma vorresti consentire un po 'di margine. Quindi, forse metti i pezzi a 65.000 invece di andare fino a 65.536.

+0

Grazie mille! Questo funziona per il mio caso! E ho davvero pensato che la query sarebbe stata più lunga. Capisco il problema dei record 65k + con lo stesso EMPNO, ma nel mio caso, molto probabilmente non si verificherà. – keiko

+1

Ci sono gli stessi errori di altre risposte: lacune casuali alla fine della pagina non gestite nel calcolo. Per esempio. supponiamo di avere 3 gruppi di righe (A, B, C) con 6 righe in ciascun gruppo. Prova a posizionarlo su pagine, 6 righe per pagina: Gruppo A - rank() = 1 - Ok, prima pagina; Gruppo B - rank() = 7 - Ok, seconda pagina riempita dall'alto e 4 righe lasciate vuote nella prima pagina; Il gruppo C - rank() = 13 - ERR !, sembra adattarsi alla 2a pagina, ma deve essere posizionato su 3, perché il valore di rank() REAL deve includere righe vuote dalla prima pagina: 6 + 4 + 6 + 1 = 17. Nessun modo di prevedere il numero di righe vuote prima della costruzione della pagina reale. – ThinkJet

+0

Ciò può accadere anche per le righe 65k +, anche alla fine della seconda pagina: Es. 5 righe libere sulla 1a pagina + 4 righe libere sulla 2a pagina + il prossimo blocco ha dimensioni da 5 a 9 righe ... – ThinkJet

1

La seguente istruzione SQL divide i venti record nella mia tabella EMP in cinque pagine di quattro righe ciascuna:

SQL> select empno 
    2   , ename 
    3   , deptno 
    4   , ceil((row_number() over (order by deptno, empno)/4)) as pageno 
    5 from emp 
    6/

    EMPNO ENAME   DEPTNO  PAGENO 
---------- ---------- ---------- ---------- 
     7782 BOEHMER   10   1 
     7839 SCHNEIDER   10   1 
     7934 KISHORE   10   1 
     7369 CLARKE    20   1 
     7566 ROBERTSON   20   2 
     7788 RIGBY    20   2 
     7876 KULASH    20   2 
     7902 GASPAROTTO   20   2 
     7499 VAN WIJK   30   3 
     7521 PADFIELD   30   3 
     7654 BILLINGTON   30   3 
     7698 SPENCER   30   3 
     7844 CAVE    30   4 
     7900 HALL    30   4 
     8083 KESTELYN   30   4 
     8084 LIRA    30   4 
     8060 VERREYNNE   50   5 
     8061 FEUERSTEIN   50   5 
     8085 TRICHLER   50   5 
     8100 PODER    50   5 

20 rows selected. 

SQL> 
+1

non funzionerà. Condizione importante - che empno non può essere in due gruppi. Nel tuo caso empno 101 sarà in due gruppi separati. –

+0

Grazie! Questo codice funziona per me! (Aggiornamento: Ok ... forse no. Buona scoperta, Michael.) – keiko

+0

Non funziona - risultati sbagliati. Per esempio. dove vengono contate ulteriori righe vuote alla fine della pagina per la terza pagina e successive? – ThinkJet

2

E 'troppo difficile o addirittura impossibile da fare cosa del genere in SQL pianura.

Ma con alcune limitazioni il problema può essere risolto con l'aiuto di user-defined aggregate functions.

Innanzitutto, creare l'oggetto con implementazione dell'interfaccia ODCIAggregate:

create or replace type page_num_agg_type as object 
(
    -- Purpose : Pagination with "leave together" option 

    -- Attributes    

    -- Current page number 
    cur_page_number number,         

    -- Cumulative number of rows per page incremented by blocks 
    cur_page_row_count number, 

    -- Row-by-row counter for detect page overflow while placing single block 
    page_row_counter number, 

    -- Member functions and procedures 

    static function ODCIAggregateInitialize(
    sctx in out page_num_agg_type 
) 
    return number, 

    member function ODCIAggregateIterate(
    self  in out page_num_agg_type, 
    value  in  number 
) 
    return number, 

    member function ODCIAggregateTerminate(
    self  in page_num_agg_type, 
    returnValue out number, 
    flags  in number 
) 
    return number, 

    member function ODCIAggregateMerge(
    self in out page_num_agg_type, 
    ctx2 in  page_num_agg_type 
) 
    return number 

); 

Crea tipo di corpo:

create or replace type body PAGE_NUM_AGG_TYPE is 

    -- Member procedures and functions 
    static function ODCIAggregateInitialize(
    sctx in out page_num_agg_type 
) 
    return number 
    is 
    begin 
     sctx := page_num_agg_type(1, 0, 0); 
     return ODCIConst.Success; 
    end; 

    member function ODCIAggregateIterate(
    self  in out page_num_agg_type, 
    value  in  number 
) 
    return number 
is 
    -- !!! WARNING: HARDCODED !!! 
    RowsPerPage number := 4; 
begin 

    self.page_row_counter := self.page_row_counter + 1; 

    -- Main operations: determine number of page 

    if(value > 0) then 
    -- First row of new block 

    if(self.cur_page_row_count + value > RowsPerPage) then 
     -- If we reach next page with new block of records - switch to next page. 
     self.cur_page_number := self.cur_page_number + 1; 
     self.cur_page_row_count := value; 
     self.page_row_counter := 1; 
    else 
     -- Just increment rows and continue to place on current page 
     self.cur_page_row_count := self.cur_page_row_count + value; 
    end if; 

    else      
    -- Row from previous block 

    if(self.page_row_counter > RowsPerPage) then 
     -- Single block of rows exceeds page size - wrap to next page. 
     self.cur_page_number := self.cur_page_number + 1; 
     self.cur_page_row_count := self.cur_page_row_count - RowsPerPage; 
     self.page_row_counter := 1; 
    end if; 

    end if; 

    return ODCIConst.Success; 
end; 

member function ODCIAggregateTerminate(
    self  in page_num_agg_type, 
    returnValue out number, 
    flags  in number 
) 
    return number 
is 
begin 
    -- Returns current page number as result 
    returnValue := self.cur_page_number; 
    return ODCIConst.Success; 
end; 

member function ODCIAggregateMerge(
    self in out page_num_agg_type, 
    ctx2 in  page_num_agg_type 

) 
    return number 
is 
begin 
    -- Can't act in parallel - error on merging attempts 
    raise_application_error(-20202,'PAGE_NUM_AGG_TYPE can''t act in parallel mode'); 
    return ODCIConst.Success; 
end; 

end; 

Crea funzione agrreation da utilizzare con il tipo:

create function page_num_agg (
    input number 
) return number aggregate using page_num_agg_type; 

Successivo preparare dati e utilizzare una nuova funzione per calcolare i numeri di pagina:

with data_list as (
    -- Your example data as source 
    select 100 as EmpNo, 'Alison' as EmpName, to_date('21-MAR-96','dd-mon-yy') as TranDate, 45000 as AMT from dual union all 
    select 100 as EmpNo, 'Alison' as EmpName, to_date('12-DEC-78','dd-mon-yy') as TranDate, 23000 as AMT from dual union all 
    select 100 as EmpNo, 'Alison' as EmpName, to_date('24-OCT-82','dd-mon-yy') as TranDate, 11000 as AMT from dual union all 
    select 101 as EmpNo, 'Linda' as EmpName, to_date('15-JAN-84','dd-mon-yy') as TranDate, 16000 as AMT from dual union all 
    select 101 as EmpNo, 'Linda' as EmpName, to_date('30-JUL-87','dd-mon-yy') as TranDate, 17000 as AMT from dual union all 
    select 102 as EmpNo, 'Celia' as EmpName, to_date('31-DEC-90','dd-mon-yy') as TranDate, 78000 as AMT from dual union all 
    select 102 as EmpNo, 'Celia' as EmpName, to_date('17-SEP-96','dd-mon-yy') as TranDate, 21000 as AMT from dual union all 
    select 103 as EmpNo, 'James' as EmpName, to_date('21-MAR-96','dd-mon-yy') as TranDate, 45000 as AMT from dual union all 
    select 103 as EmpNo, 'James' as EmpName, to_date('12-DEC-78','dd-mon-yy') as TranDate, 23000 as AMT from dual union all 
    select 103 as EmpNo, 'James' as EmpName, to_date('24-OCT-82','dd-mon-yy') as TranDate, 11000 as AMT from dual union all 
    select 104 as EmpNo, 'Robert' as EmpName, to_date('15-JAN-84','dd-mon-yy') as TranDate, 16000 as AMT from dual union all 
    select 104 as EmpNo, 'Robert' as EmpName, to_date('30-JUL-87','dd-mon-yy') as TranDate, 17000 as AMT from dual union all 
    select 105 as EmpNo, 'Monica' as EmpName, to_date('30-JUL-88','dd-mon-yy') as TranDate, 31000 as AMT from dual union all 
    select 105 as EmpNo, 'Monica' as EmpName, to_date('01-JUL-87','dd-mon-yy') as TranDate, 19000 as AMT from dual union all 
    select 105 as EmpNo, 'Monica' as EmpName, to_date('31-JAN-97','dd-mon-yy') as TranDate, 11000 as AMT from dual union all 
    select 105 as EmpNo, 'Monica' as EmpName, to_date('17-DEC-93','dd-mon-yy') as TranDate, 33000 as AMT from dual union all 
    select 105 as EmpNo, 'Monica' as EmpName, to_date('11-DEC-91','dd-mon-yy') as TranDate, 65000 as AMT from dual union all 
    select 105 as EmpNo, 'Monica' as EmpName, to_date('22-OCT-89','dd-mon-yy') as TranDate, 19000 as AMT from dual 
), 
ordered_data as (
    select    
    -- Source table fields 
    src_data.EmpNo, src_data.EmpName, src_data.TranDate, src_data.AMT, 
    -- Calculate row count per one employee 
    count(src_data.EmpNo) over(partition by src_data.EmpNo)as emp_row_count, 
    -- Calculate rank of row inside employee data sorted in output order 
    rank() over(partition by src_data.EmpNo order by src_data.EmpName, src_data.TranDate) as emp_rnk 
    from 
    data_list src_data 
) 
-- Final step: calculate page number for rows 
select 
    -- Source table data 
    ordered_data.EmpNo, ordered_data.EmpName, ordered_data.TranDate, ordered_data.AMT, 
    -- Aggregate all data with our new function 
    page_num_agg(
     -- pass count of rows to aggregate function only for first employee's row 
     decode(ordered_data.emp_rnk, 1, ordered_data.emp_row_count, 0) 
    ) 
     over (order by ordered_data.EmpName, ordered_data.TranDate) as page_number 
from  
    ordered_data  
order by 
    ordered_data.EmpName, ordered_data.TranDate 

E, infine ...

svantaggi di questa soluzione:

  1. Hardcoded numero di pagine fila.
  2. Richiede una preparazione dati specifica nella query per utilizzare correttamente la funzione di aggregazione.

Vantaggi di questa soluzione:

  1. funziona :)

Aggiornato: migliorati per gestire blocchi di grandi dimensioni, ad esempio modificato.

+0

Non ho mai fatto alcuna funzione di aggregazione definita dall'utente prima. Sono autorizzato ad avere procedure però. – keiko

0

Che ne dite di questo: (100 è il limite di righe per pagina)

select 
    a.*, floor(count(1) over (order by empno, empname)/100)+1 as page 
from source_table a 
order by page, empno, empname; 
Problemi correlati