Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's what I'm thinking, but I'm not sure it's optimal enough for a view.</p> <pre><code>select the_date, sum(decode(the_type,'S',the_count,0)) samples, sum(decode(the_type,'R',the_count,0)) receipts, sum(decode(the_type,'C',the_count,0)) completions, sum(decode(the_type,'A',the_count,0)) authorizations from( select trunc(sampled_on,'HH24') the_date, 'S' the_type, count(1) the_count FROM sample group by trunc(sampled_on,'HH24') union all select trunc(received_on,'HH24'), 'R', count(1) FROM sample group by trunc(received_on,'HH24') union all select trunc(completed_on,'HH24'), 'C', count(1) FROM sample group by trunc(completed_on,'HH24') union all select trunc(authorized_on,'HH24'), 'A', count(1) FROM sample group by trunc(authorized_on,'HH24') ) group by the_date </code></pre> <p>Then, to query, you could just query with normal date contructs: </p> <pre><code>select * from magic_view where the_date &gt; sysdate-1; </code></pre> <h2>EDIT</h2> <p>Okay, so I created a sample table and did some metrics:</p> <pre><code>create table sample ( sample_id number primary key, sampled_on date, received_on date, completed_on date, authorized_on date ); insert into sample ( select level, trunc(sysdate) + dbms_random.value(0,2), trunc(sysdate) + dbms_random.value(0,2), trunc(sysdate) + dbms_random.value(0,2), trunc(sysdate) + dbms_random.value(0,2), from dual connect by level &lt;= 1000 ); </code></pre> <p>The explain plan is:</p> <pre><code>--------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| --------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 4000 | 97K| 25 (20)| | 1 | HASH GROUP BY | | 4000 | 97K| 25 (20)| | 2 | VIEW | | 4000 | 97K| 24 (17)| | 3 | UNION-ALL | | | | | | 4 | HASH GROUP BY | | 1000 | 9000 | 6 (17)| | 5 | TABLE ACCESS FULL| SAMPLE | 1000 | 9000 | 5 (0)| | 6 | HASH GROUP BY | | 1000 | 9000 | 6 (17)| | 7 | TABLE ACCESS FULL| SAMPLE | 1000 | 9000 | 5 (0)| | 8 | HASH GROUP BY | | 1000 | 9000 | 6 (17)| | 9 | TABLE ACCESS FULL| SAMPLE | 1000 | 9000 | 5 (0)| | 10 | HASH GROUP BY | | 1000 | 9000 | 6 (17)| | 11 | TABLE ACCESS FULL| SAMPLE | 1000 | 9000 | 5 (0)| --------------------------------------------------------------------- </code></pre> <p>On my machine, the a query against this view for the past 24 hours completes in 23ms. Not bad, but it's only 1,000 rows. Before you discount the 4 separate queries, you'll need to do performance analysis of the individual solutions.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload