<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: SAP CAP error using ROW_NUMBER() in services in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/sap-cap-error-using-row-number-in-services/qaa-p/12780276#M4798249</link>
    <description>&lt;P&gt;Thanks, didn't realize that 'cds run' displays the error!&lt;/P&gt;</description>
    <pubDate>Mon, 09 Oct 2023 11:23:55 GMT</pubDate>
    <dc:creator>Epena</dc:creator>
    <dc:date>2023-10-09T11:23:55Z</dc:date>
    <item>
      <title>SAP CAP error using ROW_NUMBER() in services</title>
      <link>https://community.sap.com/t5/technology-q-a/sap-cap-error-using-row-number-in-services/qaq-p/12780274</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
  &lt;P&gt;As far as I understand, &lt;A href="https://cap.cloud.sap/docs/releases/archive/2021/dec21#sql-window-functions"&gt;as of Dec 2021 window functions are supported&lt;/A&gt;. I can use them just fine in model definition, however, I'm having issues trying to use them in services, as "cds watch" won't start.&lt;/P&gt;
  &lt;P&gt;Here's my data model:&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;namespace my.shop;

entity Products {
  key id     : UUID;
      name   : String;
      prices : Association to many PriceHistory
                 on prices.product = $self;
}

entity PriceHistory {
  key product   : Association to one Products;
  key priceDate : Timestamp @cds.on.insert: $now;
      price     : Double;
}


define view Test as
  SELECT from PriceHistory{
    product.id as productId,
    priceDate,
    price,
    ROW_NUMBER() OVER (PARTITION BY product.id ORDER BY priceDate DESC) AS rn
  };&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;If i deploy this to SQLITE using "cds deploy", it works just fine: tables are created and csv files are loaded, I can even connect to the local db.sqlite, run a query against table "Test" and get back the results:&lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/2214593-image.png" /&gt;&lt;/P&gt;
  &lt;P&gt;However, when I expose entity Test in a service, cds watch won't start.&lt;/P&gt;
  &lt;P&gt;Here's my service:&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;using my.shop as my from '../db/data-model';

service CatalogService {
    entity Products     as projection on my.Products;
    @cds.redirection.target: true
    entity PriceHistory as projection on my.PriceHistory;
    entity Test         as projection on my.Test;
}&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;Here's what happens when I run cds watch:&lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/2214594-image.png" /&gt;&lt;/P&gt;
  &lt;P&gt;As you can see, it doesn't get to the part where it listens on a port.&lt;/P&gt;
  &lt;P&gt;If I comment out the column containing the row function, cds watch works again:&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;namespace my.shop;

entity Products {
  key id     : UUID;
      name   : String;
      prices : Association to many PriceHistory
                 on prices.product = $self;
}

entity PriceHistory {
  key product   : Association to one Products;
  key priceDate : Timestamp @cds.on.insert: $now;
      price     : Double;
}


define view Test as
  SELECT from PriceHistory{
    product.id as productId,
    priceDate,
    price,
    // ROW_NUMBER() OVER (PARTITION BY product.id ORDER BY priceDate DESC) AS rn
  };&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/2214595-image.png" /&gt;&lt;/P&gt;
  &lt;P&gt;Here's a small repo with my model and service in case anyone wants to try it out:&lt;/P&gt;
  &lt;P&gt;&lt;A href="https://github.com/edg1506/CAP_TEST_PRODUCTS"&gt;https://github.com/edg1506/CAP_TEST_PRODUCTS&lt;/A&gt;&lt;/P&gt;
  &lt;P&gt;And here's my "cds -v":&lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/2214596-image.png" /&gt;&lt;/P&gt;
  &lt;P&gt;Regards.&lt;/P&gt;</description>
      <pubDate>Sun, 08 Oct 2023 15:14:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sap-cap-error-using-row-number-in-services/qaq-p/12780274</guid>
      <dc:creator>Epena</dc:creator>
      <dc:date>2023-10-08T15:14:40Z</dc:date>
    </item>
    <item>
      <title>Re: SAP CAP error using ROW_NUMBER() in services</title>
      <link>https://community.sap.com/t5/technology-q-a/sap-cap-error-using-row-number-in-services/qaa-p/12780275#M4798248</link>
      <description>&lt;P&gt;I noticed as well that `cds watch` doesn't return full error descriptions but just stops execution. To know the error you can use `cds run` which will display it correctly. As you can see, it complains about a missing key and a missing data type, so a working version of your view would be:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;define view Test as
  select from PriceHistory {
    key product.id as productId,
        priceDate,
        price,
        ROW_NUMBER() over(
          partition by product.id order by priceDate desc
        ) as rn : Integer
  };
&lt;/CODE&gt;&lt;/PRE&gt;Thanks for providing the git, that makes it easy to have a look and resolve.</description>
      <pubDate>Mon, 09 Oct 2023 05:07:09 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sap-cap-error-using-row-number-in-services/qaa-p/12780275#M4798248</guid>
      <dc:creator>Willem_Pardaens</dc:creator>
      <dc:date>2023-10-09T05:07:09Z</dc:date>
    </item>
    <item>
      <title>Re: SAP CAP error using ROW_NUMBER() in services</title>
      <link>https://community.sap.com/t5/technology-q-a/sap-cap-error-using-row-number-in-services/qaa-p/12780276#M4798249</link>
      <description>&lt;P&gt;Thanks, didn't realize that 'cds run' displays the error!&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 11:23:55 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sap-cap-error-using-row-number-in-services/qaa-p/12780276#M4798249</guid>
      <dc:creator>Epena</dc:creator>
      <dc:date>2023-10-09T11:23:55Z</dc:date>
    </item>
  </channel>
</rss>

