00001 <?php
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 function PivotTableSQL(&$db,$tables,$rowfields,$colfield, $where=false,
00035 $aggfield = false,$sumlabel='Sum ',$aggfn ='SUM', $showcount = true)
00036 {
00037 if ($aggfield) $hidecnt = true;
00038 else $hidecnt = false;
00039
00040 $iif = strpos($db->databaseType,'access') !== false;
00041
00042
00043
00044
00045 if ($where) $where = "\nWHERE $where";
00046 if (!is_array($colfield)) $colarr = $db->GetCol("select distinct $colfield from $tables $where order by 1");
00047 if (!$aggfield) $hidecnt = false;
00048
00049 $sel = "$rowfields, ";
00050 if (is_array($colfield)) {
00051 foreach ($colfield as $k => $v) {
00052 $k = trim($k);
00053 if (!$hidecnt) {
00054 $sel .= $iif ?
00055 "\n\t$aggfn(IIF($v,1,0)) AS \"$k\", "
00056 :
00057 "\n\t$aggfn(CASE WHEN $v THEN 1 ELSE 0 END) AS \"$k\", ";
00058 }
00059 if ($aggfield) {
00060 $sel .= $iif ?
00061 "\n\t$aggfn(IIF($v,$aggfield,0)) AS \"$sumlabel$k\", "
00062 :
00063 "\n\t$aggfn(CASE WHEN $v THEN $aggfield ELSE 0 END) AS \"$sumlabel$k\", ";
00064 }
00065 }
00066 } else {
00067 foreach ($colarr as $v) {
00068 if (!is_numeric($v)) $vq = $db->qstr($v);
00069 else $vq = $v;
00070 $v = trim($v);
00071 if (strlen($v) == 0 ) $v = 'null';
00072 if (!$hidecnt) {
00073 $sel .= $iif ?
00074 "\n\t$aggfn(IIF($colfield=$vq,1,0)) AS \"$v\", "
00075 :
00076 "\n\t$aggfn(CASE WHEN $colfield=$vq THEN 1 ELSE 0 END) AS \"$v\", ";
00077 }
00078 if ($aggfield) {
00079 if ($hidecnt) $label = $v;
00080 else $label = "{$v}_$aggfield";
00081 $sel .= $iif ?
00082 "\n\t$aggfn(IIF($colfield=$vq,$aggfield,0)) AS \"$label\", "
00083 :
00084 "\n\t$aggfn(CASE WHEN $colfield=$vq THEN $aggfield ELSE 0 END) AS \"$label\", ";
00085 }
00086 }
00087 }
00088 if ($aggfield && $aggfield != '1'){
00089 $agg = "$aggfn($aggfield)";
00090 $sel .= "\n\t$agg as \"$sumlabel$aggfield\", ";
00091 }
00092
00093 if ($showcount)
00094 $sel .= "\n\tSUM(1) as Total";
00095 else
00096 $sel = substr($sel,0,strlen($sel)-2);
00097
00098 $sql = "SELECT $sel \nFROM $tables $where \nGROUP BY $rowfields";
00099 return $sql;
00100 }
00101
00102
00103 if (0) {
00104
00105 # example1
00106 #
00107 # Query the main "product" table
00108 # Set the rows to CompanyName and QuantityPerUnit
00109 # and the columns to the Categories
00110 # and define the joins to link to lookup tables
00111 # "categories" and "suppliers"
00112 #
00113
00114 $sql = PivotTableSQL(
00115 $gDB, # adodb connection
00116 'products p ,categories c ,suppliers s', # tables
00117 'CompanyName,QuantityPerUnit', # row fields
00118 'CategoryName', # column fields
00119 'p.CategoryID = c.CategoryID and s.SupplierID= p.SupplierID' # joins/where
00120 );
00121 print "<pre>$sql";
00122 $rs = $gDB->Execute($sql);
00123 rs2html($rs);
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 # example2
00144 #
00145 # Query the main "product" table
00146 # Set the rows to CompanyName and QuantityPerUnit
00147 # and the columns to the UnitsInStock for diiferent ranges
00148 # and define the joins to link to lookup tables
00149 # "categories" and "suppliers"
00150 #
00151 $sql = PivotTableSQL(
00152 $gDB, # adodb connection
00153 'products p ,categories c ,suppliers s', # tables
00154 'CompanyName,QuantityPerUnit', # row fields
00155 # column ranges
00156 array(
00157 ' 0 ' => 'UnitsInStock <= 0',
00158 "1 to 5" => '0 < UnitsInStock and UnitsInStock <= 5',
00159 "6 to 10" => '5 < UnitsInStock and UnitsInStock <= 10',
00160 "11 to 15" => '10 < UnitsInStock and UnitsInStock <= 15',
00161 "16+" =>'15 < UnitsInStock'
00162 ),
00163 ' p.CategoryID = c.CategoryID and s.SupplierID= p.SupplierID', # joins/where
00164 'UnitsInStock', # sum this field
00165 'Sum' # sum label prefix
00166 );
00167 print "<pre>$sql";
00168 $rs = $gDB->Execute($sql);
00169 rs2html($rs);
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 }
00185 ?>