<?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>topic SQL query to get the average value of a field in FlexSim Forum</title>
    <link>https://forums.autodesk.com/t5/flexsim-forum/sql-query-to-get-the-average-value-of-a-field/m-p/13504424#M18842</link>
    <description>&lt;P&gt;&lt;I&gt;[ FlexSim 21.2.4 ]&lt;/I&gt;&lt;/P&gt;&lt;DIV class="fr-view clearfix"&gt;&lt;P&gt;I've been trying for hours to write a very simple SQL query but I can't seem to find the correct synthax.&lt;/P&gt;&lt;P&gt;I have a List called "FinishedGoods" and I want to get the average of the field "Leadtime" and return it as a double avglt.&lt;/P&gt;&lt;P&gt;I wrote this but it doesn't work. I get "syntax error, unexpected '[', expecting end of code" in the Compiler Console.&lt;/P&gt;&lt;PRE&gt;Table result = Table.query("SELECT Leadtime FROM FinishedGoods \
AVG[Leadtime]");

double avglt = result[1]["Leadtime"];&lt;/PRE&gt;&lt;P&gt;What's wrong? I can't see it.&lt;/P&gt;&lt;P&gt;Thanks a lot,&lt;/P&gt;&lt;/DIV&gt;</description>
    <pubDate>Thu, 04 Nov 2021 21:11:57 GMT</pubDate>
    <dc:creator>patrickABAWF</dc:creator>
    <dc:date>2021-11-04T21:11:57Z</dc:date>
    <item>
      <title>SQL query to get the average value of a field</title>
      <link>https://forums.autodesk.com/t5/flexsim-forum/sql-query-to-get-the-average-value-of-a-field/m-p/13504424#M18842</link>
      <description>&lt;P&gt;&lt;I&gt;[ FlexSim 21.2.4 ]&lt;/I&gt;&lt;/P&gt;&lt;DIV class="fr-view clearfix"&gt;&lt;P&gt;I've been trying for hours to write a very simple SQL query but I can't seem to find the correct synthax.&lt;/P&gt;&lt;P&gt;I have a List called "FinishedGoods" and I want to get the average of the field "Leadtime" and return it as a double avglt.&lt;/P&gt;&lt;P&gt;I wrote this but it doesn't work. I get "syntax error, unexpected '[', expecting end of code" in the Compiler Console.&lt;/P&gt;&lt;PRE&gt;Table result = Table.query("SELECT Leadtime FROM FinishedGoods \
AVG[Leadtime]");

double avglt = result[1]["Leadtime"];&lt;/PRE&gt;&lt;P&gt;What's wrong? I can't see it.&lt;/P&gt;&lt;P&gt;Thanks a lot,&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 04 Nov 2021 21:11:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/flexsim-forum/sql-query-to-get-the-average-value-of-a-field/m-p/13504424#M18842</guid>
      <dc:creator>patrickABAWF</dc:creator>
      <dc:date>2021-11-04T21:11:57Z</dc:date>
    </item>
    <item>
      <title>Re: SQL query to get the average value of a field</title>
      <link>https://forums.autodesk.com/t5/flexsim-forum/sql-query-to-get-the-average-value-of-a-field/m-p/13504425#M18843</link>
      <description>&lt;DIV class="fr-view clearfix"&gt;&lt;P&gt;You need to put the AVG() function in the SELECT statement and use parentheses, not brackets:&lt;/P&gt;&lt;PRE&gt;SELECT AVG(Leadtime) FROM FinishedGoods&lt;/PRE&gt;&lt;P&gt;You can then return the result with result[1][1]&lt;/P&gt;&lt;PRE&gt;Table result = Table.query("SELECT AVG(Leadtime) FROM FinishedGoods");
return result[1][1];&lt;/PRE&gt;&lt;P&gt;You can also rename that column with an AS statement&lt;/P&gt;&lt;PRE&gt;Table result = Table.query("SELECT AVG(Leadtime) AS Leadtime FROM FinishedGoods");
return result[1]["Leadtime"];&lt;/PRE&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 04 Nov 2021 22:07:24 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/flexsim-forum/sql-query-to-get-the-average-value-of-a-field/m-p/13504425#M18843</guid>
      <dc:creator>Matthew_Gillespie</dc:creator>
      <dc:date>2021-11-04T22:07:24Z</dc:date>
    </item>
    <item>
      <title>Re: SQL query to get the average value of a field</title>
      <link>https://forums.autodesk.com/t5/flexsim-forum/sql-query-to-get-the-average-value-of-a-field/m-p/13504426#M18844</link>
      <description>&lt;DIV class="fr-view clearfix"&gt;&lt;P&gt;Thanks this works. Also, if I want to get 3 things from the data, can I do them in the same query or do I have to do 3 queries as I did below which works fine?&lt;/P&gt;&lt;P&gt;Could I do it more elegantly by doing the 3 queries within the first one:&lt;/P&gt;&lt;PRE&gt;Table result = Table.query("SELECT AVG(Leadtime) FROM FinishedGoods");
AvgLeadTime = result[1][1];

Table result2 = Table.query("SELECT MIN(Leadtime) FROM FinishedGoods");
MinLeadTime = result2[1][1];

Table result3 = Table.query("SELECT MAX(Leadtime) FROM FinishedGoods");
MaxLeadTime = result3[1][1];&lt;/PRE&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 05 Nov 2021 12:32:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/flexsim-forum/sql-query-to-get-the-average-value-of-a-field/m-p/13504426#M18844</guid>
      <dc:creator>patrickABAWF</dc:creator>
      <dc:date>2021-11-05T12:32:02Z</dc:date>
    </item>
    <item>
      <title>Re: SQL query to get the average value of a field</title>
      <link>https://forums.autodesk.com/t5/flexsim-forum/sql-query-to-get-the-average-value-of-a-field/m-p/13504427#M18845</link>
      <description>&lt;DIV class="fr-view clearfix"&gt;&lt;P&gt;Forget my comment. I found the more elegant way:&lt;/P&gt;&lt;PRE&gt;Table result = Table.query("SELECT AVG(Leadtime), MIN(Leadtime), MAX(Leadtime) FROM FinishedGoods");
AvgLeadTime = result[1][1];
MinLeadTime = result[1][2];
MaxLeadTime = result[1][3];&lt;/PRE&gt;&lt;P&gt;I'm starting to get the hang of this SQL thing and loving it !!!&lt;/P&gt;&lt;P&gt;Thanks Matthew !&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 05 Nov 2021 12:38:03 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/flexsim-forum/sql-query-to-get-the-average-value-of-a-field/m-p/13504427#M18845</guid>
      <dc:creator>patrickABAWF</dc:creator>
      <dc:date>2021-11-05T12:38:03Z</dc:date>
    </item>
  </channel>
</rss>

