Subject Re: [firebird-support] Help needed in complex query
Author Milan Babuskov
kiran.gutta@... wrote:
> I have the following tables.
> PRINTLOG - contains : USERID, PRINTERIP, PAGES, PAPERSIZE,PAPERTYPE,
> PRINTERS - Contains : PRINTERID, PRINTERIP
> PRINTERGROUPS - Contains : GROUPID, PRINTERID
> USERGRROUPS - Contains : GROUPID, USERID
>
> Using the above information, I need to display a report of Total Pages
> printed by each user on each printer for the selected User Group and
> Printer Group as shown below.

In such cases, I usually build a 2-dimensional in-memory array in client
application and print it.

In, PHP it's as simple as:

select printerid, papersize, usergroup, userid, count(*)
from ...
where ...
group by printerid, papersize, usergroup, userid

while ($row = fetch())
{
$data[$row->USERGROUP.':'.$row->USERID][
$row->PRINTERID.':'.$row->PAPERSIZE] = $row->COUNT;
}

For printout, use something like:

foreach ($data as $rows)
{
// print header
foreach ($data[$rows] as $columns)
// output cell
// output totals
}

> Please help me about how to write query /view to get the data in
> required format.

Having variable number of columns is impossible in SQL query.

--
Milan Babuskov
http://swoes.blogspot.com/
http://www.flamerobin.org