<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>OpenText Analytics Database 26.2.x – Top-k projections</title>
    <link>/en/data-analysis/data-aggregation/pre-aggregating-data-projections/top-k-projections/</link>
    <description>Recent content in Top-k projections on OpenText Analytics Database 26.2.x</description>
    <generator>Hugo -- gohugo.io</generator>
    
	  <atom:link href="/en/data-analysis/data-aggregation/pre-aggregating-data-projections/top-k-projections/index.xml" rel="self" type="application/rss+xml" />
    
    
      
        
      
    
    
    <item>
      <title>Data-Analysis: Creating top-k projections</title>
      <link>/en/data-analysis/data-aggregation/pre-aggregating-data-projections/top-k-projections/creating-top-k-projections/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/data-analysis/data-aggregation/pre-aggregating-data-projections/top-k-projections/creating-top-k-projections/</guid>
      <description>
        
        
        &lt;p&gt;You define a Top-K projection with the following syntax:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
CREATE PROJECTION &lt;span class=&#34;code-variable&#34;&gt;proj-name&lt;/span&gt; [(&lt;span class=&#34;code-variable&#34;&gt;proj-column-spec&lt;/span&gt;)]
    AS SELECT &lt;span class=&#34;code-variable&#34;&gt;select-expression&lt;/span&gt; FROM &lt;span class=&#34;code-variable&#34;&gt;table&lt;/span&gt;
    LIMIT &lt;span class=&#34;code-variable&#34;&gt;num-rows&lt;/span&gt; OVER (PARTITION BY &lt;span class=&#34;code-variable&#34;&gt;expression&lt;/span&gt; ORDER BY &lt;span class=&#34;code-variable&#34;&gt;column-expr&lt;/span&gt;);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For full syntax options, see &lt;a href=&#34;../../../../../en/sql-reference/statements/create-statements/create-projection/#&#34;&gt;CREATE PROJECTION&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
=&amp;gt; CREATE PROJECTION readings_topk (meter_id, recent_date, recent_value)
    AS SELECT meter_id, reading_date, reading_value FROM readings
    LIMIT 5 OVER (PARTITION BY meter_id ORDER BY reading_date DESC);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For an extended discussion, see &lt;a href=&#34;../../../../../en/data-analysis/data-aggregation/pre-aggregating-data-projections/top-k-projections/top-k-projection-examples/#&#34;&gt;Top-k projection examples&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;requirements&#34;&gt;Requirements&lt;/h2&gt;
&lt;p&gt;The following requirements apply to Top-K projections:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The projection cannot be unsegmented.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;a href=&#34;../../../../../en/sql-reference/language-elements/window-clauses/window-partition-clause/&#34;&gt;window partition clause&lt;/a&gt; must use &lt;code&gt;PARTITION BY&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Columns in &lt;code&gt;PARTITION BY&lt;/code&gt; and &lt;code&gt;ORDER BY&lt;/code&gt; clauses must be the first columns specified in the &lt;code&gt;SELECT&lt;/code&gt; list.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You must use the &lt;code&gt;LIMIT&lt;/code&gt; option to create a Top-K projection, instead of subqueries. For example, the following &lt;code&gt;SELECT&lt;/code&gt; statements are equivalent:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
=&amp;gt; SELECT symbol, trade_time last_trade, price last_price FROM (
    SELECT symbol, trade_time, price, ROW_NUMBER()
    OVER(PARTITION BY symbol ORDER BY trade_time DESC) rn FROM trades) trds WHERE rn &amp;lt;=1;

=&amp;gt; SELECT symbol, trade_time last_trade, price last_price FROM trades
    LIMIT 1 OVER(PARTITION BY symbol ORDER BY trade_time DESC);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Both return the same results:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
      symbol      |      last_trade       | last_price
------------------+-----------------------+------------
 AAPL             | 2011-11-10 10:10:20.5 |   108.4000
 HPQ              | 2012-10-10 10:10:10.4 |    42.0500
(2 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;A Top-K projection that pre-aggregates data for use by both queries must include the &lt;code&gt;LIMIT&lt;/code&gt; option:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
=&amp;gt; CREATE PROJECTION trades_topk AS
     SELECT symbol, trade_time last_trade, price last_price FROM trades
     LIMIT 1 OVER(PARTITION BY symbol ORDER BY trade_time DESC);
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;restrictions&#34;&gt;Restrictions&lt;/h2&gt;
&lt;p&gt;The following restrictions apply to Top-K projections:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Top-K projections can reference only one table.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The database does not regard Top-K projections as superprojections, even those that include all table columns.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You cannot &lt;a href=&#34;../../../../../en/admin/working-with-native-tables/managing-table-columns/&#34;&gt;modify the anchor table metadata&lt;/a&gt; of columns that are included in Top-K projections—for example, a column&#39;s data type or default value. You also cannot &lt;a href=&#34;../../../../../en/admin/working-with-native-tables/altering-table-definitions/dropping-table-columns/&#34;&gt;drop&lt;/a&gt; these columns. To make these changes, first &lt;a href=&#34;../../../../../en/sql-reference/statements/drop-statements/drop-projection/&#34;&gt;drop&lt;/a&gt; all live aggregate and Top-K projections that are associated with the table.&lt;/p&gt;

&lt;div class=&#34;alert admonition note&#34; role=&#34;alert&#34;&gt;
&lt;h4 class=&#34;admonition-head&#34;&gt;Note&lt;/h4&gt;

One exception applies: You can set and drop NOT NULL on columns that are included in a live aggregate projection.

&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Data-Analysis: Top-k projection examples</title>
      <link>/en/data-analysis/data-aggregation/pre-aggregating-data-projections/top-k-projections/top-k-projection-examples/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/data-analysis/data-aggregation/pre-aggregating-data-projections/top-k-projections/top-k-projection-examples/</guid>
      <description>
        
        
        &lt;p&gt;The following examples show how to query a table with two Top-K projections for the most-recent trade and last trade of the day for each stock symbol.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create a table that contains information about individual stock trades:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Stock symbol&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Timestamp&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Price per share&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Number of shares&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE trades(
    symbol CHAR(16) NOT NULL,
    trade_time TIMESTAMP NOT NULL,
    price NUMERIC(12,4),
    volume INT )
    PARTITION BY (EXTRACT(year from trade_time) * 100 +
    EXTRACT(month from trade_time));
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Load data into the table:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
INSERT INTO trades VALUES(&amp;#39;AAPL&amp;#39;,&amp;#39;2010-10-10 10:10:10&amp;#39;::TIMESTAMP,100.00,100);
INSERT INTO trades VALUES(&amp;#39;AAPL&amp;#39;,&amp;#39;2010-10-10 10:10:10.3&amp;#39;::TIMESTAMP,101.00,100);
INSERT INTO trades VALUES (&amp;#39;AAPL&amp;#39;,&amp;#39;2011-10-10 10:10:10.5&amp;#39;::TIMESTAMP,106.1,1000);
INSERT INTO trades VALUES (&amp;#39;AAPL&amp;#39;,&amp;#39;2011-10-10 10:10:10.2&amp;#39;::TIMESTAMP,105.2,500);
INSERT INTO trades VALUES (&amp;#39;HPQ&amp;#39;,&amp;#39;2012-10-10 10:10:10.2&amp;#39;::TIMESTAMP,42.01,400);
INSERT INTO trades VALUES (&amp;#39;HPQ&amp;#39;,&amp;#39;2012-10-10 10:10:10.3&amp;#39;::TIMESTAMP,42.02,1000);
INSERT INTO trades VALUES (&amp;#39;HPQ&amp;#39;,&amp;#39;2012-10-10 10:10:10.4&amp;#39;::TIMESTAMP,42.05,100);
COMMIT;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create two Top-K projections that obtain the following information from the &lt;code&gt;trades&lt;/code&gt; table:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;#MostRecentTradesSymbol&#34;&gt;Return the most recent trades for each stock symbol&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;#MostRecentTradesSymbolTime&#34;&gt;Return the last trade on each trading day&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;MostRecentTradesSymbol&#34;&gt;&lt;/a&gt;&lt;strong&gt;For each stock symbol, return the most recent trade.&lt;/strong&gt;&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
=&amp;gt; CREATE PROJECTION trades_topk_a AS SELECT symbol, trade_time last_trade, price last_price
       FROM trades LIMIT 1 OVER(PARTITION BY symbol ORDER BY trade_time DESC);

=&amp;gt; SELECT symbol, trade_time last_trade, price last_price FROM trades
   LIMIT 1 OVER(PARTITION BY symbol ORDER BY trade_time DESC);

      symbol      |      last_trade       | last_price
------------------+-----------------------+------------
 HPQ              | 2012-10-10 10:10:10.4 |    42.0500
 AAPL             | 2011-10-10 10:10:10.5 |   106.1000
(2 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;br /&gt;&lt;a name=&#34;MostRecentTradesSymbolTime&#34;&gt;&lt;/a&gt;&lt;strong&gt;For each stock symbol, return the last trade on each trading day.&lt;/strong&gt;&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
=&amp;gt; CREATE PROJECTION trades_topk_b
    AS SELECT symbol, trade_time::DATE trade_date, trade_time, price close_price, volume
    FROM trades LIMIT 1 OVER(PARTITION BY symbol, trade_time::DATE ORDER BY trade_time DESC);

=&amp;gt; SELECT symbol, trade_time::DATE trade_date, trade_time, price close_price, volume
    FROM trades LIMIT 1 OVER(PARTITION BY symbol, trade_time::DATE ORDER BY trade_time DESC);

      symbol      | trade_date |      trade_time       | close_price | volume
------------------+------------+-----------------------+-------------+--------
 HPQ              | 2012-10-10 | 2012-10-10 10:10:10.4 |     42.0500 |    100
 AAPL             | 2011-10-10 | 2011-10-10 10:10:10.5 |    106.1000 |   1000
 AAPL             | 2010-10-10 | 2010-10-10 10:10:10.3 |    101.0000 |    100
(3 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In each scenario, the database redirects queries on the &lt;code&gt;trades&lt;/code&gt; table to the appropriate Top-K projection and returns the aggregated data from them. As additional data is loaded into this table, the database pre-aggregates the new data and updates the Top-K projections, so queries always return with the latest data.&lt;/p&gt;

      </description>
    </item>
    
  </channel>
</rss>
