<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.appian.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Appian version of below SQL query</title><link>https://community.appian.com/discussions/f/rules/25101/appian-version-of-below-sql-query</link><description>How can I write below SQL query through query entity in Appian 
 Select * from A where A.colmn1 = max(A.colmn1)</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: Appian version of below SQL query</title><link>https://community.appian.com/thread/97372?ContentTypeID=1</link><pubDate>Wed, 06 Jul 2022 15:00:14 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:6faabbd3-02b1-4489-82f1-5c7ac28d9cfd</guid><dc:creator>Mike Schmitt</dc:creator><description>&lt;p&gt;For me its biggest usefulness has been selecting a single result to join when sorted on an arbitrary parameter, which previously required super laborious and unintuitive workarounds to pull off (and even then, didn&amp;#39;t always work very well).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Appian version of below SQL query</title><link>https://community.appian.com/thread/97370?ContentTypeID=1</link><pubDate>Wed, 06 Jul 2022 14:54:49 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:c0379eb1-a9e8-4494-97cd-9fb8b7ccf02a</guid><dc:creator>Chris</dc:creator><description>&lt;p&gt;Yep, ROW_NUMBER() is powerful!&amp;nbsp; With Appian our biggest use for it is typically assigning a unique ID to a data set that would not have one otherwise, such as aggregating data from multiple sources, statistics, etc etc.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="sql"&gt;SELECT TOP (100) PERCENT ROW_NUMBER() OVER (ORDER BY requestStartTime DESC) AS Row, 
step,requestByName FROM tblCOE_SAMPLE_TABLE&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Appian version of below SQL query</title><link>https://community.appian.com/thread/97366?ContentTypeID=1</link><pubDate>Wed, 06 Jul 2022 14:45:02 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:0eaeeef8-0d1d-40da-badc-5fb65914f060</guid><dc:creator>Mike Schmitt</dc:creator><description>[quote userid="2401" url="~/discussions/f/rules/25101/appian-version-of-below-sql-query/97297#97297"]create a view in the database which contains the query that matches your main CDT, but points to the view instead.&amp;nbsp; [/quote]
&lt;p&gt;BTW, if we&amp;#39;re bringing bespoke Views into this, I hope you&amp;#39;re aware (as I only learned several weeks ago now) that with the current DB version(s), we can now use Window Functions, including row_number() which is &lt;em&gt;&lt;strong&gt;powerful&lt;/strong&gt;&lt;/em&gt;.&amp;nbsp; Basically you can arbitrarily define partitions within a given query result and assign row numbers to the resulting rows, and then sort (and even select by) those row numbers.&amp;nbsp; This means that in your new view, you could create a row_number that applies against all rows in the table, sorting inverted by the target date column, then just query the result for &amp;quot;row 1&amp;quot;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Appian version of below SQL query</title><link>https://community.appian.com/thread/97364?ContentTypeID=1</link><pubDate>Wed, 06 Jul 2022 14:40:59 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:99045aab-168c-4b1f-b564-6cf1d684cf49</guid><dc:creator>Mike Schmitt</dc:creator><description>[quote userid="87898" url="~/discussions/f/rules/25101/appian-version-of-below-sql-query/97363#97363"]That&amp;#39;s what I am doing right now[/quote]
&lt;p&gt;I think this is the best supported way of doing such a thing in Appian, at least when we assume the value you&amp;#39;re sorting by will usually be unique (and/or you&amp;#39;re only expecting a single row to be returned).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Appian version of below SQL query</title><link>https://community.appian.com/thread/97363?ContentTypeID=1</link><pubDate>Wed, 06 Jul 2022 14:39:15 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:058b1a9f-dfa8-4588-a0d7-f7e442f902a8</guid><dc:creator>thevarunbawa</dc:creator><description>&lt;p&gt;&lt;a href="/members/mikes0011"&gt;Mike Schmitt&lt;/a&gt; That&amp;#39;s what I am doing right now.&amp;nbsp; Haha &lt;span class="emoticon" data-url="https://community.appian.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Appian version of below SQL query</title><link>https://community.appian.com/thread/97362?ContentTypeID=1</link><pubDate>Wed, 06 Jul 2022 14:38:52 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:fe4f109d-60ad-4698-aa3d-942417c4899f</guid><dc:creator>Chris</dc:creator><description>&lt;p&gt;Sorting is a great option (including for dates).&amp;nbsp; I may have been rusty from the long weekend &lt;span class="emoticon" data-url="https://community.appian.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Appian version of below SQL query</title><link>https://community.appian.com/thread/97357?ContentTypeID=1</link><pubDate>Wed, 06 Jul 2022 14:28:08 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:4482506a-f0ac-4d9e-b390-39e0fdaa0281</guid><dc:creator>Mike Schmitt</dc:creator><description>&lt;p&gt;TBH, this seems like a lot of running around / bending-over-backwards to do essentially this:&lt;br /&gt;&lt;pre class="ui-code" data-mode="java"&gt;a!localVariables(
  local!maxRow: a!queryEntity(
    entity: cons!COE_DS_SAMPLE,
    query: a!query(
      paginginfo: a!pagingInfo(
        startIndex: 1,
        batchSize: 1,
        sort: a!sortInfo(   /* this should get the max row by &amp;quot;colmn1&amp;quot; in almost all cases */
          field: &amp;quot;colmn1&amp;quot;,
          ascending: false()
        )
      )
    )
  ).data,

  local!maxRow
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Appian version of below SQL query</title><link>https://community.appian.com/thread/97356?ContentTypeID=1</link><pubDate>Wed, 06 Jul 2022 14:23:40 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:82e4f895-42f4-429b-9f3f-42c5aa48857d</guid><dc:creator>thevarunbawa</dc:creator><description>&lt;p&gt;Thanks&amp;nbsp;&lt;a href="/members/csteward"&gt;Chris&lt;/a&gt;&amp;nbsp;for the elaborate response. For now we&amp;nbsp;had to go with second approach. In fact, first approach is reasonable, I have&amp;nbsp;recommended and asked&amp;nbsp;them for that equivalent view to SQL query.&lt;/p&gt;
&lt;p&gt;But, max() doesn&amp;#39;t work with dates. Any direct function to return max date, rather than sorting in descending order and then picking first item which I am doing right now?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Appian version of below SQL query</title><link>https://community.appian.com/thread/97297?ContentTypeID=1</link><pubDate>Tue, 05 Jul 2022 22:34:25 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:70917872-1cf2-41ad-b1ac-0a603f1a8072</guid><dc:creator>Chris</dc:creator><description>&lt;p&gt;There are&amp;nbsp;2 primary options here:&lt;/p&gt;
&lt;p&gt;Option 1 (most scalable), create a view in the database which contains the query that matches your main CDT, but points to the view instead.&amp;nbsp; Then utilize a simple a!queryEntity() over the view CDT to always return the max row.&amp;nbsp; Best for overhead and scalability.&amp;nbsp; I also always recommend for views, changing&amp;nbsp;the &amp;quot;SELECT *&amp;quot; into a list of all your columns specifically such as &amp;quot;SELECT id, userName, amount, date..&amp;quot; etc, as changes to the table structure will not automatically be reflected in your view and this can cause confusion for debugging without being able to see which columns are listed (the view would have to be re-published as-is, no changes, utilizing &amp;quot;*&amp;quot; if you change the underlying table structure later).&lt;/p&gt;
&lt;p&gt;Option 2 requires querying all values into Appian to utilize the max() function, then a second query to the data source to filter for the max value you are looking for.&amp;nbsp; If you are concerned with scalability (as we normally are) and your data set growing, this method will limit at 1 MB of data during the initial query, and slow down as it approaches that number.&amp;nbsp; For cases where, say, users enter 1 to 100 rows per request, and you want to find the max row of a specific&amp;nbsp;request, this should be fine.&amp;nbsp; Essentially if the submissions are unlimited, you will want to steer to option 1 (I like Option 1 for all of these cases anyway).&lt;/p&gt;
&lt;p&gt;Option 2 Sample:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="csharp"&gt;a!localVariables(
  local!yourDataPoint: &amp;quot;id&amp;quot;, /* change to your data point */
  local!max: tointeger(
    max(  
      property(
        a!queryEntity(
          entity: cons!COE_DS_SAMPLE, /* change to your data store */
          fetchTotalCount: false,
          query: a!query(
            paginginfo: a!pagingInfo(1,-1),
            selection: a!querySelection(
              columns: {
                a!queryColumn(
                  field: local!yourDataPoint
                )
              }
            )
          )
        ).data,
        local!yourDataPoint,
        0
      )
    )
  ),
  local!maxRow: a!queryEntity(
    entity: cons!COE_DS_SAMPLE, /* change to your data store */
    fetchTotalCount: false,
    query: a!query(
      paginginfo: a!pagingInfo(1,1),
      filter: a!queryFilter(
        field: local!yourDataPoint,
        operator: &amp;quot;=&amp;quot;,
        value: local!max
      )
    )
  ).data,
  
  local!maxRow
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>