Formatting a MySQL Query result -
i've got table follows,
column type time datetime ticket int(20) agentid int(20) exitstatus varchar(50) queue varchar(50) i want write query break down week, providing column count each exitstatus. far have this,
select exitstatus,count(exitstatus) exitstatuscount, day(time) timeperiod `table` group timeperiod, exitstatus output:
exitstatus exitstatuscount timeperiod noagentid 1 4 success 3 4 noagentid 1 5 success 5 5 i want change returns results in format:
week | count(noagentid) | count(success) | ideally, i'd columns dynamic other exitstatus values may possible.
this information formatted , presented end user in table on page. can done in sql or should reformat in php?
there no "general" solution problem (called cross tabulation) can achieved single query. there 4 possible solutions:
- hardcode possible
exitstatus'es in query , keep updated see need more , more of them. example:
select day(time) timeperiod, sum(if(exitstatus = 'noagentid', 1, 0)) noagentid, sum(if(exitstatus = 'success', 1, 0)) success -- #todo: add others here when/if needed table ... group timeperiod
do first query possible
exitstatus'es , create final query high-level programming language based on results.use special module cross tabulation on high-level programming language. perl, have sqlcrosstab module couldn't find 1 php
add layer application using olap (multi-dimensional views of data) pentaho , querying layer instead of original data
you can read lot more these solutions , overall discussion of subject
Comments
Post a Comment