{"id":1167,"date":"2014-07-02T18:32:39","date_gmt":"2014-07-02T22:32:39","guid":{"rendered":"http:\/\/cfdbplugin.com\/?page_id=1167"},"modified":"2016-09-15T16:06:03","modified_gmt":"2016-09-15T20:06:03","slug":"transform-to-compute-statistics","status":"publish","type":"page","link":"https:\/\/cfdbplugin.com\/?page_id=1167","title":{"rendered":"Transform to Compute Statistics"},"content":{"rendered":"<p>There are some built-in transform classes to compute simple statistics.<\/p>\n<p><a name=\"MaxField\"><\/a>Example: you want to find the maximum score across all player&#8217;s entries.<\/p>\n<p><code>[cfdb-table form=\"games\"\u00a0<span style=\"color: #0000ff;\">trans=\"MaxField(scores)\"<span style=\"color: #000000;\">]<\/span><\/span><\/code><\/p>\n<p>This returns one row in your result like this:<\/p>\n<table>\n<thead>\n<tr>\n<th>score<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>(computed max)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Example: you want to find each person&#8217;s maximum score across a number of entries for each person&#8217;s name. This is like &#8220;max score grouped by name&#8221;<\/p>\n<p><code>[cfdb-table form=\"games\"\u00a0<span style=\"color: #0000ff;\">trans=\"MaxField(scores,name)\"<span style=\"color: #000000;\">]<\/span><\/span><\/code><\/p>\n<p>This returns one row per unique name in your result with<\/p>\n<table>\n<thead>\n<tr>\n<th>name<\/th>\n<th>score<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>John<\/td>\n<td>15<\/td>\n<\/tr>\n<tr>\n<td>Richard<\/td>\n<td>16<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><a name=\"CountField\"><\/a><a name=\"SumField\"><\/a><a name=\"MinField\"><\/a><a name=\"MaxField\"><\/a><a name=\"AverageField\"><\/a>You can so the same with these classes or build your own:<\/p>\n<ul>\n<li>CountField<\/li>\n<li>SumField<\/li>\n<li>MinField<\/li>\n<li>MaxField<\/li>\n<li>AverageField<\/li>\n<\/ul>\n<p>What if you want to &#8220;group by&#8221; more than one field?<\/p>\n<p>Then create a new field that includes the information from\u00a0all your fields, then &#8220;group by&#8221; it.<\/p>\n<p><code>[cfdb-table form=\"games\"\u00a0<span style=\"color: #0000ff;\">trans=\"full_name=concat(first_name,' ',last_name)&amp;&amp;MaxField(scores,full_name)\"<span style=\"color: #000000;\">]<\/span><\/span><\/code><\/p>\n<table>\n<thead>\n<tr>\n<th>name<\/th>\n<th>score<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>John Doe<\/td>\n<td>15<\/td>\n<\/tr>\n<tr>\n<td>Richard Roe<\/td>\n<td>16<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color: #0000ff;\"><span style=\"color: #000000;\">Notice the single-quoted space (&#8216; &#8216;) in &#8220;concat&#8221;. Single quote whitespace or it will be ignored.\u00a0<\/span><\/span><\/p>\n<p>Note: The &#8220;concat&#8221; function is provided by CFDB since there is no function for concatenation in PHP (it use the &#8220;.&#8221; operator).<\/p>\n<p><a name=\"CountInField\"><\/a><\/p>\n<h2><a name=\"CountInField\"><\/a>Counting Checkbox Values<\/h2>\n<table style=\"border: none;\">\n<tbody>\n<tr>\n<td style=\"border: none;\" width=\"40%\">In the case where you have a checkbox field with multiple selections, you may have data like this:<\/p>\n<table>\n<thead>\n<tr>\n<th>Vote<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>AA<\/td>\n<\/tr>\n<tr>\n<td>AA<\/td>\n<\/tr>\n<tr>\n<td>FF<\/td>\n<\/tr>\n<tr>\n<td>CC,DD,EE<\/td>\n<\/tr>\n<tr>\n<td>CC<\/td>\n<\/tr>\n<tr>\n<td>GG,HH<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/td>\n<td style=\"border: none;\" width=\"60%\">Count the votes:<br \/>\n<code>[cfdb-table form=\"voteform\" <span style=\"color: #0000ff;\">trans=\"CountInField(Vote,Count)\"<\/span>]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table>\n<thead>\n<tr>\n<th>Vote<\/th>\n<th>Count<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>AA<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>FF<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>CC<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>DD<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>EE<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>GG<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>HH<\/td>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div>\n<p><a name=\"NaturalSortByField\"><\/a>To sort by the largest number of votes, append the NaturalSortByField transform<\/p>\n<\/div>\n<div>\n<p><code>[cfdb-table form=\"voteform\" <span style=\"color: #0000ff;\">trans=\"CountInField(Vote,Count)&amp;&amp;NaturalSortByField(Count,DESC)\"<\/span>]<\/code><\/p>\n<\/div>\n<div>\n<p>If the values were separated with something other than a comma, such as &#8220;|&#8221; (e.g. &#8220;CC|DD|EE&#8221;) then indicate the delimiter:<br \/>\n<code>[cfdb-table form=\"voteform\" <span style=\"color: #0000ff;\">trans=\"CountInField(Vote,Count,|)\"<\/span>]<\/code><\/p>\n<\/div>\n<div>\n<h2><a name=\"AddRowNumberField\"><\/a>Add Row Number<\/h2>\n<\/div>\n<div>\n<p>Add a column with a row number in it (as of version 2.10.10)<\/p>\n<\/div>\n<div>\n<p>AddRowNumberField(fieldname,start) where &#8220;fieldname&#8221; is the column header and &#8220;start&#8221; (optional) is the number to start the count at (by default 1)<\/p>\n<\/div>\n<div>\n<p><code>[cfdb-table form=\"games\"\u00a0<span style=\"color: #0000ff;\">trans=\"AddRowNumberField(#)\"<span style=\"color: #000000;\">]<\/span><\/span><\/code><\/p>\n<\/div>\n<div>\n<table>\n<thead>\n<tr>\n<th>name<\/th>\n<th>score<\/th>\n<th>#<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>John<\/td>\n<td>5<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>John<\/td>\n<td>6<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>John<\/td>\n<td>7<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>Richard<\/td>\n<td>2<\/td>\n<td>4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>\n<p>But that adds it at the end. You can move it to the beginning using &#8220;show&#8221;. Here we use start to indicate the &#8220;#&#8221; column followed by everything else:<\/p>\n<\/div>\n<div>\n<p><code>[cfdb-table form=\"games\"\u00a0<span style=\"color: #0000ff;\">show=\"#,\/.*\/\" trans=\"AddRowNumberField(#)\"<span style=\"color: #000000;\">]<\/span><\/span><\/code><\/p>\n<\/div>\n<div>\n<table>\n<thead>\n<tr>\n<th>#<\/th>\n<th>name<\/th>\n<th>score<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>John<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>John<\/td>\n<td>6<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>John<\/td>\n<td>7<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>Richard<\/td>\n<td>2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>\n<h2><a name=\"SummationRow\"><\/a>Add a Summation Row at the Bottom of a Table<\/h2>\n<\/div>\n<div>\n<p>The SummationRow transform does this (as of version 2.9.12).<\/p>\n<\/div>\n<div>\n<p><code>[cfdb-table form=\"games\"\u00a0<span style=\"color: #0000ff;\">trans=\"SummationRow(score)\"<span style=\"color: #000000;\">]<\/span><\/span><\/code><\/p>\n<\/div>\n<div>\n<table>\n<thead>\n<tr>\n<th>name<\/th>\n<th>score<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>John<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<td>John<\/td>\n<td>6<\/td>\n<\/tr>\n<tr>\n<td>John<\/td>\n<td>7<\/td>\n<\/tr>\n<tr>\n<td>Richard<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>20<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>\n<p><strong>Sum Multiple Rows<\/strong><br \/>\n<code>[cfdb-table form=\"games\"\u00a0<span style=\"color: #0000ff;\">trans=\"SummationRow(score1,score2)\"<span style=\"color: #000000;\">]<\/span><\/span><\/code><\/p>\n<\/div>\n<div>\n<table>\n<thead>\n<tr>\n<th>name<\/th>\n<th>score1<\/th>\n<th>score2<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>John<\/td>\n<td>5<\/td>\n<td>4<\/td>\n<\/tr>\n<tr>\n<td>John<\/td>\n<td>6<\/td>\n<td>7<\/td>\n<\/tr>\n<tr>\n<td>John<\/td>\n<td>7<\/td>\n<td>9<\/td>\n<\/tr>\n<tr>\n<td>Richard<\/td>\n<td>2<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>20<\/td>\n<td>21<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>\n<p><strong>Add label &#8220;Total&#8221; to in the Name column<\/strong><br \/>\n<code>[cfdb-table form=\"games\"\u00a0<span style=\"color: #0000ff;\">trans=\"SummationRow(score1,score2,name:Total)\"<span style=\"color: #000000;\">]<\/span><\/span><\/code><\/p>\n<\/div>\n<div>\n<table>\n<thead>\n<tr>\n<th>name<\/th>\n<th>score1<\/th>\n<th>score2<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>John<\/td>\n<td>5<\/td>\n<td>4<\/td>\n<\/tr>\n<tr>\n<td>John<\/td>\n<td>6<\/td>\n<td>7<\/td>\n<\/tr>\n<tr>\n<td>John<\/td>\n<td>7<\/td>\n<td>9<\/td>\n<\/tr>\n<tr>\n<td>Richard<\/td>\n<td>2<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>Total<\/td>\n<td>20<\/td>\n<td>21<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are some built-in transform classes to compute simple statistics. Example: you want to find the maximum score across all player&#8217;s entries. This returns one row in your result like this: score (computed max) Example: you want to find each person&#8217;s maximum score across a number of entries for each person&#8217;s name. This is like [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1118,"menu_order":8,"comment_status":"closed","ping_status":"closed","template":"page-without-sidebar.php","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-1167","page","type-page","status-publish","hentry"],"jetpack_shortlink":"https:\/\/wp.me\/P1mptf-iP","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1167"}],"version-history":[{"count":14,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1167\/revisions"}],"predecessor-version":[{"id":1541,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1167\/revisions\/1541"}],"up":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1118"}],"wp:attachment":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}