Migrate adodb via composer to vendor directory.
[openemr.git] / vendor / adodb / adodb-php / pivottable.inc.php
blob6faee4e2bbf8a6f5deb41b8e131999537d359470
1 <?php
2 /**
3 * @version v5.20.2 27-Dec-2015
4 * @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
5 * @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
6 * Released under both BSD license and Lesser GPL library license.
7 * Whenever there is any discrepancy between the two licenses,
8 * the BSD license will take precedence.
10 * Set tabs to 4 for best viewing.
15 * Concept from daniel.lucazeau@ajornet.com.
17 * @param db Adodb database connection
18 * @param tables List of tables to join
19 * @rowfields List of fields to display on each row
20 * @colfield Pivot field to slice and display in columns, if we want to calculate
21 * ranges, we pass in an array (see example2)
22 * @where Where clause. Optional.
23 * @aggfield This is the field to sum. Optional.
24 * Since 2.3.1, if you can use your own aggregate function
25 * instead of SUM, eg. $aggfield = 'fieldname'; $aggfn = 'AVG';
26 * @sumlabel Prefix to display in sum columns. Optional.
27 * @aggfn Aggregate function to use (could be AVG, SUM, COUNT)
28 * @showcount Show count of records
30 * @returns Sql generated
33 function PivotTableSQL(&$db,$tables,$rowfields,$colfield, $where=false,
34 $aggfield = false,$sumlabel='Sum ',$aggfn ='SUM', $showcount = true)
36 if ($aggfield) $hidecnt = true;
37 else $hidecnt = false;
39 $iif = strpos($db->databaseType,'access') !== false;
40 // note - vfp 6 still doesn' work even with IIF enabled || $db->databaseType == 'vfp';
42 //$hidecnt = false;
44 if ($where) $where = "\nWHERE $where";
45 if (!is_array($colfield)) $colarr = $db->GetCol("select distinct $colfield from $tables $where order by 1");
46 if (!$aggfield) $hidecnt = false;
48 $sel = "$rowfields, ";
49 if (is_array($colfield)) {
50 foreach ($colfield as $k => $v) {
51 $k = trim($k);
52 if (!$hidecnt) {
53 $sel .= $iif ?
54 "\n\t$aggfn(IIF($v,1,0)) AS \"$k\", "
56 "\n\t$aggfn(CASE WHEN $v THEN 1 ELSE 0 END) AS \"$k\", ";
58 if ($aggfield) {
59 $sel .= $iif ?
60 "\n\t$aggfn(IIF($v,$aggfield,0)) AS \"$sumlabel$k\", "
62 "\n\t$aggfn(CASE WHEN $v THEN $aggfield ELSE 0 END) AS \"$sumlabel$k\", ";
65 } else {
66 foreach ($colarr as $v) {
67 if (!is_numeric($v)) $vq = $db->qstr($v);
68 else $vq = $v;
69 $v = trim($v);
70 if (strlen($v) == 0 ) $v = 'null';
71 if (!$hidecnt) {
72 $sel .= $iif ?
73 "\n\t$aggfn(IIF($colfield=$vq,1,0)) AS \"$v\", "
75 "\n\t$aggfn(CASE WHEN $colfield=$vq THEN 1 ELSE 0 END) AS \"$v\", ";
77 if ($aggfield) {
78 if ($hidecnt) $label = $v;
79 else $label = "{$v}_$aggfield";
80 $sel .= $iif ?
81 "\n\t$aggfn(IIF($colfield=$vq,$aggfield,0)) AS \"$label\", "
83 "\n\t$aggfn(CASE WHEN $colfield=$vq THEN $aggfield ELSE 0 END) AS \"$label\", ";
87 if ($aggfield && $aggfield != '1'){
88 $agg = "$aggfn($aggfield)";
89 $sel .= "\n\t$agg as \"$sumlabel$aggfield\", ";
92 if ($showcount)
93 $sel .= "\n\tSUM(1) as Total";
94 else
95 $sel = substr($sel,0,strlen($sel)-2);
98 // Strip aliases
99 $rowfields = preg_replace('/ AS (\w+)/i', '', $rowfields);
101 $sql = "SELECT $sel \nFROM $tables $where \nGROUP BY $rowfields";
103 return $sql;
106 /* EXAMPLES USING MS NORTHWIND DATABASE */
107 if (0) {
109 # example1
111 # Query the main "product" table
112 # Set the rows to CompanyName and QuantityPerUnit
113 # and the columns to the Categories
114 # and define the joins to link to lookup tables
115 # "categories" and "suppliers"
118 $sql = PivotTableSQL(
119 $gDB, # adodb connection
120 'products p ,categories c ,suppliers s', # tables
121 'CompanyName,QuantityPerUnit', # row fields
122 'CategoryName', # column fields
123 'p.CategoryID = c.CategoryID and s.SupplierID= p.SupplierID' # joins/where
125 print "<pre>$sql";
126 $rs = $gDB->Execute($sql);
127 rs2html($rs);
130 Generated SQL:
132 SELECT CompanyName,QuantityPerUnit,
133 SUM(CASE WHEN CategoryName='Beverages' THEN 1 ELSE 0 END) AS "Beverages",
134 SUM(CASE WHEN CategoryName='Condiments' THEN 1 ELSE 0 END) AS "Condiments",
135 SUM(CASE WHEN CategoryName='Confections' THEN 1 ELSE 0 END) AS "Confections",
136 SUM(CASE WHEN CategoryName='Dairy Products' THEN 1 ELSE 0 END) AS "Dairy Products",
137 SUM(CASE WHEN CategoryName='Grains/Cereals' THEN 1 ELSE 0 END) AS "Grains/Cereals",
138 SUM(CASE WHEN CategoryName='Meat/Poultry' THEN 1 ELSE 0 END) AS "Meat/Poultry",
139 SUM(CASE WHEN CategoryName='Produce' THEN 1 ELSE 0 END) AS "Produce",
140 SUM(CASE WHEN CategoryName='Seafood' THEN 1 ELSE 0 END) AS "Seafood",
141 SUM(1) as Total
142 FROM products p ,categories c ,suppliers s WHERE p.CategoryID = c.CategoryID and s.SupplierID= p.SupplierID
143 GROUP BY CompanyName,QuantityPerUnit
145 //=====================================================================
147 # example2
149 # Query the main "product" table
150 # Set the rows to CompanyName and QuantityPerUnit
151 # and the columns to the UnitsInStock for diiferent ranges
152 # and define the joins to link to lookup tables
153 # "categories" and "suppliers"
155 $sql = PivotTableSQL(
156 $gDB, # adodb connection
157 'products p ,categories c ,suppliers s', # tables
158 'CompanyName,QuantityPerUnit', # row fields
159 # column ranges
160 array(
161 ' 0 ' => 'UnitsInStock <= 0',
162 "1 to 5" => '0 < UnitsInStock and UnitsInStock <= 5',
163 "6 to 10" => '5 < UnitsInStock and UnitsInStock <= 10',
164 "11 to 15" => '10 < UnitsInStock and UnitsInStock <= 15',
165 "16+" =>'15 < UnitsInStock'
167 ' p.CategoryID = c.CategoryID and s.SupplierID= p.SupplierID', # joins/where
168 'UnitsInStock', # sum this field
169 'Sum' # sum label prefix
171 print "<pre>$sql";
172 $rs = $gDB->Execute($sql);
173 rs2html($rs);
175 Generated SQL:
177 SELECT CompanyName,QuantityPerUnit,
178 SUM(CASE WHEN UnitsInStock <= 0 THEN UnitsInStock ELSE 0 END) AS "Sum 0 ",
179 SUM(CASE WHEN 0 < UnitsInStock and UnitsInStock <= 5 THEN UnitsInStock ELSE 0 END) AS "Sum 1 to 5",
180 SUM(CASE WHEN 5 < UnitsInStock and UnitsInStock <= 10 THEN UnitsInStock ELSE 0 END) AS "Sum 6 to 10",
181 SUM(CASE WHEN 10 < UnitsInStock and UnitsInStock <= 15 THEN UnitsInStock ELSE 0 END) AS "Sum 11 to 15",
182 SUM(CASE WHEN 15 < UnitsInStock THEN UnitsInStock ELSE 0 END) AS "Sum 16+",
183 SUM(UnitsInStock) AS "Sum UnitsInStock",
184 SUM(1) as Total
185 FROM products p ,categories c ,suppliers s WHERE p.CategoryID = c.CategoryID and s.SupplierID= p.SupplierID
186 GROUP BY CompanyName,QuantityPerUnit