<?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: CQL &amp; building SELECT .WHERE dynamically in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520503#M4692956</link>
    <description>&lt;P&gt;That's correct. The constant whereQuery would contain a string. I don't know if the string of my example would be correct, but you should be able to build a correct one ;).&lt;/P&gt;&lt;P&gt; I've got a short follow-up question on your reply though. You wrote:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;.where `{$whereStatement}`&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Was that a mistake here or did you write the very same in your code? The dollar sign and the opening curly brace should be switched `${value}`, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;.where `${whereStatement}`&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I don't know if I receive a notification if you don't mention me. Just saw your reply by random as you didn't comment on my reply :D.&lt;BR /&gt;&lt;BR /&gt;Best regards,&lt;/P&gt;&lt;P&gt;Tobias&lt;/P&gt;</description>
    <pubDate>Thu, 04 Nov 2021 09:55:22 GMT</pubDate>
    <dc:creator>tobias_steckenborn</dc:creator>
    <dc:date>2021-11-04T09:55:22Z</dc:date>
    <item>
      <title>CQL &amp; building SELECT .WHERE dynamically</title>
      <link>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaq-p/12520500</link>
      <description>&lt;P&gt;Hi.&lt;/P&gt;
  &lt;P&gt;I have a SELECT statement for which I want to 'build' a WHERE statement depending on a number of parameters of my JS function.&lt;/P&gt;
  &lt;P&gt;I had a look at the documentation &lt;A href="https://cap.cloud.sap/docs/node.js/cds-ql#where" target="test_blank"&gt;https://cap.cloud.sap/docs/node.js/cds-ql#where&lt;/A&gt; but I cannot find a proper way of doing it.&lt;/P&gt;
  &lt;P&gt;Ideally the .WHERE should be 'increased' based on my decisions.&lt;/P&gt;
  &lt;P&gt;Now I have to resort to this - which is ugly as hell - and not maintainable in the long run.&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;// Define query
        let q1 = SELECT.one.from(CampaignsSteps).columns("count(up__ID) as count");

        // Build our where depending on the parameters (ugly I know)
        if ( statuses === null &amp;amp;&amp;amp; campaignID === null ) {
         
            q1 = q1.where `active = true`;
            
        }        
        else if ( statuses !== null &amp;amp;&amp;amp; campaignID === null ) {

            q1 = q1.where `( active = true ) and ( status in ${statuses} )`;
            
        } else if ( statuses !== null &amp;amp;&amp;amp; campaignID !== null ) {

            q1 = q1.where `( active = true ) and ( status in ${statuses} ) and ( up__ID = ${campaignID} )`;
            
        }&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;Tried several approaches including building a string and then doing&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;.where `{$whereStatement}`&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;Anybody having better ideas ?&lt;/P&gt;
  &lt;P&gt;Thanks,&lt;/P&gt;
  &lt;P&gt;Steven&lt;/P&gt;</description>
      <pubDate>Wed, 03 Nov 2021 16:48:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaq-p/12520500</guid>
      <dc:creator>StevenDeSaeger</dc:creator>
      <dc:date>2021-11-03T16:48:58Z</dc:date>
    </item>
    <item>
      <title>Re: CQL &amp; building SELECT .WHERE dynamically</title>
      <link>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520501#M4692954</link>
      <description>&lt;P&gt;Hey Steven,&lt;BR /&gt;&lt;BR /&gt;What did lead to you giving up the "building a string" approach?&lt;/P&gt;&lt;P&gt;Did you try something in the direction of:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;const whereParameters = [{parameter: statuses, query: `status in ${statuses}`}, {parameter: campaignID, query: `up__ID = ${campaignID}`}]

const whereQuery = whereParameters.reduce((previousValue, currentValue) =&amp;gt; {
  if (currentValue.parameter) {
    return `${previousValue} and ( ${currentValue.query} )`
  }
  return previousValue
}, "active = true")&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;EM&gt;&amp;lt;- This example might not be correct. Written on my iPad without access to a proper IDE.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;BR /&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;Depending on your inputs you should be able to construct a valid string that you can use in .where `${whereQuery}`.&lt;/P&gt;&lt;P&gt;Examples using the code above:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;// let statuses = undefined&lt;BR /&gt;// let campaignID = undefined&lt;BR /&gt;// =&amp;gt; active = true&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;// let statuses = ['test1']&lt;BR /&gt;// let campaignID = undefined&lt;BR /&gt;// =&amp;gt; active = true and ( status in test1 ) &lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;// let statuses = ['test1']&lt;BR /&gt;// let campaignID = 123123&lt;BR /&gt;// active = true and ( status in test1 ) and ( up__ID = 123123 )&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;// let statuses = undefined&lt;BR /&gt;// let campaignID = 123123&lt;BR /&gt;// active = true and ( up__ID = 123123 ) &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 Nov 2021 07:02:33 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520501#M4692954</guid>
      <dc:creator>tobias_steckenborn</dc:creator>
      <dc:date>2021-11-04T07:02:33Z</dc:date>
    </item>
    <item>
      <title>Re: CQL &amp; building SELECT .WHERE dynamically</title>
      <link>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520502#M4692955</link>
      <description>&lt;P&gt;Hi Tobias,&lt;/P&gt;&lt;P&gt;Thanks for your response.&lt;/P&gt;&lt;P&gt;Your nifty ES6 JS code in the ends leads to a string containing a 'resolved' where statement correct ?&lt;/P&gt;&lt;P&gt;I triedd the same but with some basic IF statements like&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;If ( condition ) {
   whereStatement = whereStatement + `additional ${...}`;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;but in the end you then try to apply them &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;.where `{$whereStatement}`&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;but this does not work.  The CQL does not seem to recognise this and just renders no where clause at all.&lt;/P&gt;&lt;P&gt;So the SELECT is executed without where.&lt;/P&gt;&lt;P&gt;Also tried something like&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;q1 = q1 + .where ``&lt;BR /&gt;if ( condition )&lt;BR /&gt;q1 = q1.where + `` &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;None of that really seems to work.  The where is only rendered when providing it a direct `` string.&lt;/P&gt;&lt;P&gt;Hence my question.&lt;/P&gt;&lt;P&gt;Kind Regards,&lt;/P&gt;&lt;P&gt;Steven&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 09:27:30 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520502#M4692955</guid>
      <dc:creator>StevenDeSaeger</dc:creator>
      <dc:date>2021-11-04T09:27:30Z</dc:date>
    </item>
    <item>
      <title>Re: CQL &amp; building SELECT .WHERE dynamically</title>
      <link>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520503#M4692956</link>
      <description>&lt;P&gt;That's correct. The constant whereQuery would contain a string. I don't know if the string of my example would be correct, but you should be able to build a correct one ;).&lt;/P&gt;&lt;P&gt; I've got a short follow-up question on your reply though. You wrote:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;.where `{$whereStatement}`&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Was that a mistake here or did you write the very same in your code? The dollar sign and the opening curly brace should be switched `${value}`, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;.where `${whereStatement}`&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I don't know if I receive a notification if you don't mention me. Just saw your reply by random as you didn't comment on my reply :D.&lt;BR /&gt;&lt;BR /&gt;Best regards,&lt;/P&gt;&lt;P&gt;Tobias&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 09:55:22 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520503#M4692956</guid>
      <dc:creator>tobias_steckenborn</dc:creator>
      <dc:date>2021-11-04T09:55:22Z</dc:date>
    </item>
    <item>
      <title>Re: CQL &amp; building SELECT .WHERE dynamically</title>
      <link>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520504#M4692957</link>
      <description>&lt;P&gt; &lt;SPAN class="mention-scrubbed"&gt;steven.desaeger&lt;/SPAN&gt; I just tested using a string in the cap-sflight codebase. Seems to work. There is probably a problem with the string you constructed or the way you referenced it.&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/1992448-test.png" /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 10:23:59 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520504#M4692957</guid>
      <dc:creator>tobias_steckenborn</dc:creator>
      <dc:date>2021-11-04T10:23:59Z</dc:date>
    </item>
    <item>
      <title>Re: CQL &amp; building SELECT .WHERE dynamically</title>
      <link>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520505#M4692958</link>
      <description>&lt;P&gt;Hi  &lt;SPAN class="mention-scrubbed"&gt;tobias_steckenborn&lt;/SPAN&gt; ,&lt;/P&gt;&lt;P&gt;I was indeed using the correct statement - just typed it wrongly here - sorry for the confusion.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;.where `${whereStatement}`&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The variable holds a correct where statement value - just like the one you generated.&lt;/P&gt;&lt;P&gt;Still this is not working and there is in fact no WHERE registered at all for the SELECT statement.&lt;/P&gt;&lt;P&gt;Kind Regards,&lt;/P&gt;&lt;P&gt;Steven&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 11:15:26 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520505#M4692958</guid>
      <dc:creator>StevenDeSaeger</dc:creator>
      <dc:date>2021-11-04T11:15:26Z</dc:date>
    </item>
    <item>
      <title>Re: CQL &amp; building SELECT .WHERE dynamically</title>
      <link>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520506#M4692959</link>
      <description>&lt;P&gt;If it's already a correct string did you already try using .where(whereStatement)?&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 12:48:55 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520506#M4692959</guid>
      <dc:creator>tobias_steckenborn</dc:creator>
      <dc:date>2021-11-04T12:48:55Z</dc:date>
    </item>
    <item>
      <title>Re: CQL &amp; building SELECT .WHERE dynamically</title>
      <link>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520507#M4692960</link>
      <description>&lt;P&gt;Hey Tobias,&lt;/P&gt;&lt;P&gt;Yeah just tried adding the statement using &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;.where(whereStatement)&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;=&amp;gt; Error with 'unknown From' ...  &lt;/P&gt;&lt;P&gt;This is vary puzzling as exact the same statement works when executing with the &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;.where(`${whereStatement}`)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;That gave me the idea on a potential version issue as I noticed I was still using cds 5.5.1 ... so upgraded to latest version 5.6.1 and now I am even more in a world of pain because now 'cds run' just dumps in some standard code ... it really never ends with this stuff.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 15:59:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520507#M4692960</guid>
      <dc:creator>StevenDeSaeger</dc:creator>
      <dc:date>2021-11-04T15:59:01Z</dc:date>
    </item>
    <item>
      <title>Re: CQL &amp; building SELECT .WHERE dynamically</title>
      <link>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520508#M4692961</link>
      <description>&lt;P&gt;  &lt;SPAN class="mention-scrubbed"&gt;tobias_steckenborn&lt;/SPAN&gt; &lt;/P&gt;&lt;P&gt;Interesting that works for you.&lt;/P&gt;&lt;P&gt;Which CDS version are you using ? Might be related to me using a slightly older one ... &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;@sap/cds: 5.5.4&lt;BR /&gt;@sap/cds-compiler: 2.7.0&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Nov 2021 14:14:32 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520508#M4692961</guid>
      <dc:creator>StevenDeSaeger</dc:creator>
      <dc:date>2021-11-05T14:14:32Z</dc:date>
    </item>
    <item>
      <title>Re: CQL &amp; building SELECT .WHERE dynamically</title>
      <link>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520509#M4692962</link>
      <description>&lt;P&gt;Hi. Have you solutioned it ?&lt;/P&gt;&lt;P&gt;I have the same problem.&lt;/P&gt;&lt;P&gt;it works only if I use it:&lt;/P&gt; .where `cpudt between ${dtFilterInitial} and ${dtFilterFinal} and status = 'PROCESSED'`&lt;BR /&gt;but not if I use: .where `cpudt between ${dtFilterInitial} and ${dtFilterFinal} and ( status = '09' or  status = '12' )`&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sun, 07 Jan 2024 11:48:00 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cql-building-select-where-dynamically/qaa-p/12520509#M4692962</guid>
      <dc:creator>glauco</dc:creator>
      <dc:date>2024-01-07T11:48:00Z</dc:date>
    </item>
  </channel>
</rss>

