Difference between revisions of "Sscalculation"

From SmartWiki
Jump to: navigation, search
(Using sscalculation to Change Date Formats of Data in Custom Fields)
m
 
(33 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
{{MySQL Syntax}}
 
Sscalculation is a [[Variables|variable]] syntax for:
 
Sscalculation is a [[Variables|variable]] syntax for:
 
* [[Web Page View Field|Web Page Views]]
 
* [[Web Page View Field|Web Page Views]]
Line 7: Line 8:
 
This works similar to [[sslogic]] but will do calculations based on the formula you provide.  It works as follows:
 
This works similar to [[sslogic]] but will do calculations based on the formula you provide.  It works as follows:
  
<pre>
+
<pre style="white-space: pre-wrap;
 +
white-space: -moz-pre-wrap;
 +
white-space: -pre-wrap;
 +
white-space: -o-pre-wrap;
 +
word-wrap: break-word;"><!--@sscalculation('@field@'*0.05)-->
 +
</pre>
  
<!--@sscalculation(@field@*0.05)-->
+
This will return the computed value of '@field@'*0.05 and replaces the sscalculation tag.
  
</pre>
+
This way you can do calculations right on the [[Web Page View Field|web page views]] without having to use JavaScript. This resolves issues with creating PDF files from [[Web Page View Field|web page views]] that contain JavaScript.
  
This will return the computed value of @field@*0.05 and replaces the sscalculation tag.
 
  
This way you can do calculations right on the [[Web Page View Field|web page views]] without having to use javascript which solves the problem on PDF and javascript.
+
<font size=3>'''The "sscalculation" method uses MySQL syntax. Any MySQL function such as concat(...), format(...), round(...), can be used.'''</font>
 
 
This method uses MySQL syntax, so you can do any MySQL functions like concat(...), format(...), round(...), etc.<br>
 
  
 
==Using sscalculation in a List==
 
==Using sscalculation in a List==
  
 
To obtain a Sum (for example) of the values in a [[Custom Field]] for all transactions associated with a Company/Contact using [[sscalculation]]:
 
To obtain a Sum (for example) of the values in a [[Custom Field]] for all transactions associated with a Company/Contact using [[sscalculation]]:
<pre>
+
<pre style="white-space: pre-wrap;
<!--@sscalculation( [#(?object=transaction;)$add this$+#]0)-->
+
white-space: -moz-pre-wrap;
 +
white-space: -pre-wrap;
 +
white-space: -o-pre-wrap;
 +
word-wrap: break-word;">
 +
<!--@sscalculation( [#(?object=transaction)$add this$+#]0)-->
 
</pre>
 
</pre>
  
Line 29: Line 36:
 
* '''Important:''' There <u>must</u> be a space prior to the opening square bracket.
 
* '''Important:''' There <u>must</u> be a space prior to the opening square bracket.
 
* Also: if you are finding the sum don't forget the zero at the end of the statement. For example, if there are transactions with values of 10, 15 and 20 the statement between the square brackets would be translated as follows:
 
* Also: if you are finding the sum don't forget the zero at the end of the statement. For example, if there are transactions with values of 10, 15 and 20 the statement between the square brackets would be translated as follows:
::''[#(?object=transaction;)$add this$+#]'' &nbsp;&nbsp;&nbsp; becomes    &nbsp;&nbsp;&nbsp;  ''10+15+20+''
+
::''[#(?object=transaction)$add this$+#]'' &nbsp;&nbsp;&nbsp; becomes    &nbsp;&nbsp;&nbsp;  ''10+15+20+''
so the 0 at the end ensures that the statement evaluated doesn't end with a plus sign (which will fail).
+
::so the 0 at the end ensures that the statement evaluated doesn't end with a plus sign (which will fail).
  
* If you need to pull information from the level 1 when you are at level 2 within the sscalculation tag, you can use the parent keyword
+
* If you need to pull information from the level 1 when you are at level 2 within the sscalculation tag, you can use the ''parent'' keyword
  
<pre>
+
<pre style="white-space: pre-wrap;
<!--@sscalculation(@year(parent.fullstartdate)@)-->
+
white-space: -moz-pre-wrap;
 +
white-space: -pre-wrap;
 +
white-space: -o-pre-wrap;
 +
word-wrap: break-word;">
 +
<!--@sscalculation(date_format("@parent.fullstartdate@","%Y"))-->
 
</pre>
 
</pre>
 +
'''Note:''' When using date_format with a standard date field you must refer to the field using either @datetime(parent.startdate)@ or @parent.fullstartdate@
 +
 +
* To sum a value on a [[Company Role]] field (Custom Field ID 123456) for all companies with the role of ''Funding Agency'' AND a value stored on the level 1 (Custom Field ID 545454) and format it with no decimal places:
 +
 +
<pre style="white-space: pre-wrap;
 +
white-space: -moz-pre-wrap;
 +
white-space: -pre-wrap;
 +
white-space: -o-pre-wrap;
 +
word-wrap: break-word;"><!--@sscalculation(format(( [#(?object=company::criteria=rolename='Funding Agency') $?opc_123456.value$ + #] @#545454.value#@),0))--></pre>
 +
  
 +
* To find the average of all values (Custom Field ID 1755555) on a particular Level 2 Type (typeid=54321), excluding a particular status ([[statusid]] is not 10001), rounded to the nearest whole number:
 +
<pre style="white-space: pre-wrap;
 +
white-space: -moz-pre-wrap;
 +
white-space: -pre-wrap;
 +
white-space: -o-pre-wrap;
 +
word-wrap: break-word;"><!--@sscalculation(format( ( [#(?object=activity::criteria=typeid='54321' and status.statusid not in ('10001')) $1755555$ + #] 0 )/@level2.count(*):typeid='54321' and status.statusid not in ('10001')@,0))--> </pre>
  
* To sum a value on a UTA Company field (Custom Field ID 123456) for all companies with role ''Funding Agency'' AND a value stored on the level 1 (Custom Field ID 545454) and format it with no decimal places:
 
  
  &lt;!--@sscalculation(format(( [#(?object=company;criteria=rolename='Funding Agency') $?opc_123456.value$ + #] @#545454.value#@),0))-->
+
* To find the different in days between two different dates:
 +
<pre style="white-space: pre-wrap;
 +
  white-space: -moz-pre-wrap;  
 +
white-space: -pre-wrap;
 +
white-space: -o-pre-wrap;
 +
word-wrap: break-word;"><!--@sscalculation(datediff('@datetime(currentdate)@','@Last Contact Date@'))--></pre>
  
  
* To find the average of all values (Custom Field ID 1755555) on a particular Level 2 Type (typeid=54321), excluding a particular status ([[statusid]] is not 10001), rounded to the nearest whole number:
+
* To add a number of months to a date equal to the number selected in another field:  
  &lt;!--@sscalculation(format( ( [#(?object=activity;criteria=typeid='54321' and status.statusid not in ('10001')) $1755555$ + #] 0 )/@level2.count(*):typeid='54321' and status.statusid not in ('10001')@,0))-->  
+
<pre style="white-space: pre-wrap;
 +
  white-space: -moz-pre-wrap;  
 +
white-space: -pre-wrap;
 +
white-space: -o-pre-wrap;
 +
word-wrap: break-word;"><!--@sscalculation(DATE_ADD('@Anticipated Project End Date@', INTERVAL @system.Grants-FinalDisbursementDelay@ MONTH))--></pre>
 +
 
 +
 
 +
* To subtract a number of months to a date you use a negative number for the INTERVAL (for example -6 months below):  
 +
<pre style="white-space: pre-wrap;
 +
white-space: -moz-pre-wrap;
 +
white-space: -pre-wrap;
 +
white-space: -o-pre-wrap;
 +
word-wrap: break-word;"><!--@sscalculation(DATE_ADD('@Anticipated Project End Date@', INTERVAL -6 MONTH))--></pre>
 +
 
  
 +
* To calculate the difference in months between two date values:
 +
<pre style="white-space: pre-wrap;
 +
white-space: -moz-pre-wrap;
 +
white-space: -pre-wrap;
 +
white-space: -o-pre-wrap;
 +
word-wrap: break-word;"><!--@sscalculation(PERIOD_DIFF(DATE_FORMAT('@Anticipated Project End Date@','%Y%m'),DATE_FORMAT('@Anticipated Project Start Date@','%Y%m')))--></pre>
 +
or
 +
<pre style="white-space: pre-wrap;
 +
white-space: -moz-pre-wrap;
 +
white-space: -pre-wrap;
 +
white-space: -o-pre-wrap;
 +
word-wrap: break-word;"><!--@sscalculation(TIMESTAMPDIFF( MONTH,'@Start Date.value@','@End Date.value@') )--></pre>
  
 
==Using sscalculation to Change Date Formats of Data in Custom Fields==
 
==Using sscalculation to Change Date Formats of Data in Custom Fields==
Line 52: Line 108:
 
* To parse out date/time information from a custom field and display it in a different format, you can use the following syntax on level 2, referencing the field ''fieldname'' on level 1:
 
* To parse out date/time information from a custom field and display it in a different format, you can use the following syntax on level 2, referencing the field ''fieldname'' on level 1:
  
<pre><!--@sscalculation(date_format("@parent.fieldname@","%M %d, %Y"))--></pre>
+
<pre style="white-space: pre-wrap;
 +
white-space: -moz-pre-wrap;
 +
white-space: -pre-wrap;
 +
white-space: -o-pre-wrap;
 +
word-wrap: break-word;"><!--@sscalculation(date_format("@parent.fieldname@","%M %d, %Y"))--></pre>
  
* Use the following arguments to format the date/time information
+
* Use the following arguments to format the date/time information:
  
:{| border="1" cellpadding="5"
+
{{MYSQLDATETIMEFORMATS}}
|-
 
|%M %d, %Y||will return date in format of ''September 30, 2012''
 
|-
 
|%a||will return the truncated day of the week, e.g. ''Sun.'' for Sunday
 
|-
 
|%b||will return the truncated name of the month, e.g. ''Sep'' for September
 
|-
 
|%c||will return the month number in short format, e.g. ''9'' for September
 
|-
 
|%h||will return hour, e.g. ''12''
 
|-
 
|%i||will return minutes, e.g. ''00''
 
|-
 
|%m||will return the month number, e.g. ''09'' for September
 
|-
 
|%p||will return AM/PM information, e.g. ''AM''
 
|-
 
|%r||will return the full time format, e.g. ''12:00:00 AM''
 
|-
 
  
|%y||will return the truncated year, e.g. ''12'' for 2012
+
'''Note''': Any MYSQL syntax can be used within sscalculation. See [http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format here] for additional date/time formatting options. (Note: this is an external website.)
|-
 
|%D||will return the month ordinal, e.g. ''30th''
 
|-
 
|%W||will return the day of the week, e.g. ''Sunday''
 
|}
 
  
<!-- NOT SURE ABOUT THE FOLLOWING TWO ENTRIES, %u and %v (Arthur Lathrop): |%u||will return the week number, e.g. ''39'' for September 30, 2012; |%v||will return the week number treating Sunday as the first day of the week, e.g. ''40'' for September 30, 2012 -->
+
* It is not necessary to use sscalculation if the custom field being referenced is on the same level as the field referencing it. For example, if you are referencing a level 2 custom field in a web page view field on level 2. In this case, you can use the syntax outlined on the [[Web_Page_View_Field_Variables#Custom_Fields_Date.2FTime|Web Page View Field Variables]] page.
  
 +
==Using sscalculation to replace text==
  
* It is not necessary to use sscalculation if the custom field being referenced is on the same level as the field referencing it. For example, if you are referencing a level 2 custom field in a web page view field on level 2. In this case, you can use the syntax outlined on the [[Web_Page_View_Field_Variables#Custom_Fields_Date.2FTime|Web Page View Field Variables]] page.
+
In the following example, the variable ''@Select Many Checkbox Fields@'' returns a semicolon-separated list of selected values from the field:
 +
 
 +
<pre style="white-space: pre-wrap;
 +
white-space: -moz-pre-wrap;
 +
white-space: -pre-wrap;
 +
white-space: -o-pre-wrap;
 +
word-wrap: break-word;">Option 1;Option 2;Option 3</pre>
 +
 
 +
In order to replace the semicolons with a comma, followed by a space, you can use the following syntax:
 +
 
 +
<pre style="white-space: pre-wrap;
 +
white-space: -moz-pre-wrap;
 +
white-space: -pre-wrap;
 +
white-space: -o-pre-wrap;
 +
word-wrap: break-word;">
 +
<!--@sscalculation(REPLACE("@Select Many Checkbox Field@",";",", "))--></pre>
 +
 
 +
...which will return:
 +
 
 +
<pre style="white-space: pre-wrap;
 +
white-space: -moz-pre-wrap;
 +
white-space: -pre-wrap;
 +
white-space: -o-pre-wrap;
 +
word-wrap: break-word;">Option 1, Option 2, Option 3</pre>
 +
 
 +
==See Also==
 +
* [http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format MYSQL Date Format] Note: this is an external website.
  
[[Category:Variables]]
+
[[Category:Variables]][[Category:Date Formats]]

Latest revision as of 12:09, 6 January 2016

This feature uses MariaDB syntax

Sscalculation is a variable syntax for:


This works similar to sslogic but will do calculations based on the formula you provide. It works as follows:

<!--@sscalculation('@field@'*0.05)-->

This will return the computed value of '@field@'*0.05 and replaces the sscalculation tag.

This way you can do calculations right on the web page views without having to use JavaScript. This resolves issues with creating PDF files from web page views that contain JavaScript.


The "sscalculation" method uses MySQL syntax. Any MySQL function such as concat(...), format(...), round(...), can be used.

Using sscalculation in a List

To obtain a Sum (for example) of the values in a Custom Field for all transactions associated with a Company/Contact using sscalculation:

<!--@sscalculation( [#(?object=transaction)$add this$+#]0)-->


  • Important: There must be a space prior to the opening square bracket.
  • Also: if you are finding the sum don't forget the zero at the end of the statement. For example, if there are transactions with values of 10, 15 and 20 the statement between the square brackets would be translated as follows:
[#(?object=transaction)$add this$+#]     becomes     10+15+20+
so the 0 at the end ensures that the statement evaluated doesn't end with a plus sign (which will fail).
  • If you need to pull information from the level 1 when you are at level 2 within the sscalculation tag, you can use the parent keyword
<!--@sscalculation(date_format("@parent.fullstartdate@","%Y"))-->

Note: When using date_format with a standard date field you must refer to the field using either @datetime(parent.startdate)@ or @parent.fullstartdate@

  • To sum a value on a Company Role field (Custom Field ID 123456) for all companies with the role of Funding Agency AND a value stored on the level 1 (Custom Field ID 545454) and format it with no decimal places:
<!--@sscalculation(format(( [#(?object=company::criteria=rolename='Funding Agency') $?opc_123456.value$ + #] @#545454.value#@),0))-->


  • To find the average of all values (Custom Field ID 1755555) on a particular Level 2 Type (typeid=54321), excluding a particular status (statusid is not 10001), rounded to the nearest whole number:
<!--@sscalculation(format( ( [#(?object=activity::criteria=typeid='54321' and status.statusid not in ('10001')) $1755555$ + #] 0 )/@level2.count(*):typeid='54321' and status.statusid not in ('10001')@,0))--> 


  • To find the different in days between two different dates:
<!--@sscalculation(datediff('@datetime(currentdate)@','@Last Contact Date@'))-->


  • To add a number of months to a date equal to the number selected in another field:
<!--@sscalculation(DATE_ADD('@Anticipated Project End Date@', INTERVAL @system.Grants-FinalDisbursementDelay@ MONTH))-->


  • To subtract a number of months to a date you use a negative number for the INTERVAL (for example -6 months below):
<!--@sscalculation(DATE_ADD('@Anticipated Project End Date@', INTERVAL -6 MONTH))-->


  • To calculate the difference in months between two date values:
<!--@sscalculation(PERIOD_DIFF(DATE_FORMAT('@Anticipated Project End Date@','%Y%m'),DATE_FORMAT('@Anticipated Project Start Date@','%Y%m')))-->

or

<!--@sscalculation(TIMESTAMPDIFF( MONTH,'@Start Date.value@','@End Date.value@') )-->

Using sscalculation to Change Date Formats of Data in Custom Fields

  • To parse out date/time information from a custom field and display it in a different format, you can use the following syntax on level 2, referencing the field fieldname on level 1:
<!--@sscalculation(date_format("@parent.fieldname@","%M %d, %Y"))-->
  • Use the following arguments to format the date/time information:
Format Description
%M %d, %Y will return date in format of September 30, 2012
%a will return the truncated day of the week, e.g. Sun. for Sunday
%b will return the truncated name of the month, e.g. Sep for September
%c will return the month number in short format, e.g. 9 for September
%d will return the numeric day of month (01-31)
%e will return the numeric day of month (1-31)
%h will return hour (01-12)
%i will return minutes (00-59)
%j will return day of year (001-366)
%k will return hour (0-23)
%l will return hour (1-12)
%m will return the month number, e.g. 09 for September
%p will return AM/PM information, e.g. AM
%r will return the full time in 12-hour format, e.g. 12:00:00 AM
%s will return seconds (00-59)
%u will return the week number (00-53) where Monday is the first day of the week
%w day of the week (0=Sunday...6=Saturday)
%y will return the truncated year, e.g. 12 for 2012
%D will return the month ordinal, e.g. 30th
%H will return hour (00-23)
%M will return the month name, e.g. September
%T will return the full time in 24-hour format, e.g. 23:00:00
%U will return the week number (00-53) where Sunday is the first day of the week
%W will return the day of the week, e.g. Sunday
%Y will return the four-digit year, e.g. 2013

Note: Any MYSQL syntax can be used within sscalculation. See here for additional date/time formatting options. (Note: this is an external website.)

  • It is not necessary to use sscalculation if the custom field being referenced is on the same level as the field referencing it. For example, if you are referencing a level 2 custom field in a web page view field on level 2. In this case, you can use the syntax outlined on the Web Page View Field Variables page.

Using sscalculation to replace text

In the following example, the variable @Select Many Checkbox Fields@ returns a semicolon-separated list of selected values from the field:

Option 1;Option 2;Option 3

In order to replace the semicolons with a comma, followed by a space, you can use the following syntax:

<!--@sscalculation(REPLACE("@Select Many Checkbox Field@",";",", "))-->

...which will return:

Option 1, Option 2, Option 3

See Also