<?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: CAP using subqueries for case separation in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/cap-using-subqueries-for-case-separation/qaa-p/13961590#M4895886</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.sap.com/t5/user/viewprofilepage/user-id/44224"&gt;@a_oezsoy59&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;How about defining a new entity with the &lt;FONT face="courier new,courier"&gt;as select from&lt;/FONT&gt; variant as described in the &lt;A href="https://cap.cloud.sap/docs/cds/cdl#as-select-from" target="_self"&gt;documentation&lt;/A&gt;?&lt;BR /&gt;Further reference for functions:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;A href="https://cap.cloud.sap/docs/guides/databases#functions-mappings-for-runtime-queries" target="_self"&gt;Functions Mappings for Runtime Queries&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;&lt;A href="https://cap.cloud.sap/docs/guides/databases#native-db-functions" target="_self"&gt;Using Native Features&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;The syntax is slightly different, but the query in your example could be translated into such select I believe.&lt;BR /&gt;If you already defined the entities - e.g. to table1 -, the "Subqueries for SQLite" query will look like this:&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;/* Option 1: Subqueries */
entity AggregateView as
  select from table1 as a
  join (
    select from table1 {
      RefA,
      RefB,
      max(Value1) as max1,
    }
    group by
      RefA,
      RefB
  ) as subquery
    on  a.RefA = subquery.RefA
    and a.RefB = subquery.RefB
  {
    a.RefA,
    a.RefB,
    max(
      a.Value1
    ) as max1,
    max(
      case
        when
          a.Value1 = subquery.max1
        then
          a.Value2
        else
          null
      end
    ) as max2
  }
  group by
    a.RefA,
    a.RefB;&lt;/LI-CODE&gt;&lt;P&gt;Best,&lt;BR /&gt;Peter&lt;/P&gt;</description>
    <pubDate>Thu, 12 Dec 2024 07:44:02 GMT</pubDate>
    <dc:creator>catano</dc:creator>
    <dc:date>2024-12-12T07:44:02Z</dc:date>
    <item>
      <title>CAP using subqueries for case separation</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-using-subqueries-for-case-separation/qaq-p/13959678</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;From the following table I am trying to get the the records grouped by RefA and RefB columns with the maximum value of Value1. In case the Value1 is same for multiple records, then the maximum value of Value2.&lt;/P&gt;&lt;P&gt;So only in this example the records 1 and 5.&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="20%"&gt;ID&lt;/TD&gt;&lt;TD width="20%"&gt;RefA&lt;/TD&gt;&lt;TD width="20%"&gt;RefB&lt;/TD&gt;&lt;TD width="20%"&gt;Value1&lt;/TD&gt;&lt;TD width="20%"&gt;Value2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="20%"&gt;1&lt;/TD&gt;&lt;TD width="20%"&gt;a1&lt;/TD&gt;&lt;TD width="20%"&gt;b1&lt;/TD&gt;&lt;TD width="20%"&gt;5&lt;/TD&gt;&lt;TD width="20%"&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="20%"&gt;2&lt;/TD&gt;&lt;TD width="20%"&gt;a1&lt;/TD&gt;&lt;TD width="20%"&gt;b1&lt;/TD&gt;&lt;TD width="20%"&gt;3&lt;/TD&gt;&lt;TD width="20%"&gt;15&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="20%"&gt;3&lt;/TD&gt;&lt;TD width="20%"&gt;a1&lt;/TD&gt;&lt;TD width="20%"&gt;b1&lt;/TD&gt;&lt;TD width="20%"&gt;3&lt;/TD&gt;&lt;TD width="20%"&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="20%"&gt;4&lt;/TD&gt;&lt;TD width="20%"&gt;a2&lt;/TD&gt;&lt;TD width="20%"&gt;b1&lt;/TD&gt;&lt;TD width="20%"&gt;4&lt;/TD&gt;&lt;TD width="20%"&gt;25&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="20%"&gt;5&lt;/TD&gt;&lt;TD width="20%"&gt;a2&lt;/TD&gt;&lt;TD width="20%"&gt;b1&lt;/TD&gt;&lt;TD width="20%"&gt;4&lt;/TD&gt;&lt;TD width="20%"&gt;35&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Ideally I would like to do this operation with only one query and no server-side post processing.&lt;/P&gt;&lt;P&gt;In SQL the following query gets me the correct result.&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;-- Option 1: Subqueries for SQLite
SELECT
    a.RefA,
    a.RefB,
    MAX(a.Value1) AS max1,
    MAX(
        CASE
            WHEN a.Value1 = subquery.max1
	    THEN a.Value2
            ELSE NULL
        END
    ) AS max2
FROM table1 as a
JOIN (
    SELECT RefA, RefB, MAX(Value1) AS max1
    FROM table1
    GROUP BY RefA, RefB
) AS subquery
    ON a.RefA = subquery.RefA AND a.RefB = subquery.RefB
GROUP BY a.RefA, a.RefB;

-- Option 2: Window function in case separation (general)
SELECT
	RefA,
	RefB,
	MAX(Value1) AS max1,
	MAX(
		CASE
			WHEN Value1 = MAX(Value1) OVER(
				PARTITION BY RefA, RefB
			)
			THEN Value2
			ELSE null
		END
	) AS max2
FROM table1
GROUP BY RefA, RefB;&lt;/LI-CODE&gt;&lt;P&gt;However, I don't know what the correct approach for this is in CAP. Can you help me with this?&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;Atakan&lt;/P&gt;</description>
      <pubDate>Tue, 10 Dec 2024 08:13:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-using-subqueries-for-case-separation/qaq-p/13959678</guid>
      <dc:creator>a_oezsoy59</dc:creator>
      <dc:date>2024-12-10T08:13:01Z</dc:date>
    </item>
    <item>
      <title>Re: CAP using subqueries for case separation</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-using-subqueries-for-case-separation/qaa-p/13961590#M4895886</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.sap.com/t5/user/viewprofilepage/user-id/44224"&gt;@a_oezsoy59&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;How about defining a new entity with the &lt;FONT face="courier new,courier"&gt;as select from&lt;/FONT&gt; variant as described in the &lt;A href="https://cap.cloud.sap/docs/cds/cdl#as-select-from" target="_self"&gt;documentation&lt;/A&gt;?&lt;BR /&gt;Further reference for functions:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;A href="https://cap.cloud.sap/docs/guides/databases#functions-mappings-for-runtime-queries" target="_self"&gt;Functions Mappings for Runtime Queries&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;&lt;A href="https://cap.cloud.sap/docs/guides/databases#native-db-functions" target="_self"&gt;Using Native Features&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;The syntax is slightly different, but the query in your example could be translated into such select I believe.&lt;BR /&gt;If you already defined the entities - e.g. to table1 -, the "Subqueries for SQLite" query will look like this:&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;/* Option 1: Subqueries */
entity AggregateView as
  select from table1 as a
  join (
    select from table1 {
      RefA,
      RefB,
      max(Value1) as max1,
    }
    group by
      RefA,
      RefB
  ) as subquery
    on  a.RefA = subquery.RefA
    and a.RefB = subquery.RefB
  {
    a.RefA,
    a.RefB,
    max(
      a.Value1
    ) as max1,
    max(
      case
        when
          a.Value1 = subquery.max1
        then
          a.Value2
        else
          null
      end
    ) as max2
  }
  group by
    a.RefA,
    a.RefB;&lt;/LI-CODE&gt;&lt;P&gt;Best,&lt;BR /&gt;Peter&lt;/P&gt;</description>
      <pubDate>Thu, 12 Dec 2024 07:44:02 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-using-subqueries-for-case-separation/qaa-p/13961590#M4895886</guid>
      <dc:creator>catano</dc:creator>
      <dc:date>2024-12-12T07:44:02Z</dc:date>
    </item>
    <item>
      <title>Re: CAP using subqueries for case separation</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-using-subqueries-for-case-separation/qaa-p/13964296#M4896326</link>
      <description>&lt;P&gt;Hi Peter,&lt;/P&gt;&lt;P&gt;Thank you! That was exactly what I was looking for &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2024 07:48:16 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-using-subqueries-for-case-separation/qaa-p/13964296#M4896326</guid>
      <dc:creator>a_oezsoy59</dc:creator>
      <dc:date>2024-12-16T07:48:16Z</dc:date>
    </item>
    <item>
      <title>Re: CAP using subqueries for case separation</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-using-subqueries-for-case-separation/qaa-p/13964318#M4896332</link>
      <description>I'm glad it worked</description>
      <pubDate>Mon, 16 Dec 2024 08:16:41 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-using-subqueries-for-case-separation/qaa-p/13964318#M4896332</guid>
      <dc:creator>catano</dc:creator>
      <dc:date>2024-12-16T08:16:41Z</dc:date>
    </item>
  </channel>
</rss>

