Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4604442
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:17:47+00:00 2026-05-22T00:17:47+00:00

I’ve a resultset which sums the totals for N situations and N different codes

  • 0

I’ve a resultset which sums the totals for N situations and N different codes for a certain period.

Data comes from this SQL

SELECT *, count(fldA) as ca 
FROM table 
WHERE ('... month/year ...') 
GROUP BY Sit, Cod 
ORDER BY count(fldA) DESC;

Ok here’s the table I want:

+--------------------------------+
| d/l | Sit. A | Sit. B | Sit. N |
+-----+--------+--------+--------+
| c91 |   10   |   05   |   10   |
+-----+--------+--------+--------+
| c93 |   15   |        |        |
+-----+--------+--------+--------+
| cN. |   07   |   01   |        |
+-----+--------+--------+--------+
| Tot |   22   |   06   |   10   |
+--------------------------------+

So the corresponding array (resultset) for the table above should be something like:

SitA, C93, 15;
SitN, c91, 10;
SitA, c91, 10;
SitA, CN., 07;
SitB, c91, 05;
SitB, cN., 01;

Note that number of situations (Collumns) and codes (Rows) WILL VARY. And there are cases where in the resultset array there should be NO value for some col x row combination.

Obviously, the totals should be calculated afterwards.

The question is… which is the best approach for this type of resultset => table creation? Preferably using only one SQL request.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:17:48+00:00Added an answer on May 22, 2026 at 12:17 am

    So, since no help comes… i’ve done this

    <?php
    

    …

    $sql="";
    
    function timeFilter(){
        $q="";
        if ($_GET['mes']!='' || $_GET['ano']!=''){ $q.=" WHERE";}
        if ($_GET['mes']!=''){ $q.=" MONTH(ouventrada)=$_GET[mes]";}
        if ($_GET['mes']!='' && $_GET['ano']!=''){ $q.=" AND";}
        if ($_GET['ano']!=''){$q.=" YEAR(ouventrada)=$_GET[ano]";}
        return $q;
    }   
    
    // we gonna use two buffers for collumns and rows...
    
    function fillBuff (&$buff, $fld){
        $buff = array();
        $sql = "SELECT $fld FROM ouv" . timeFilter() . " GROUP BY $fld";
        $res = mysql_query($sql);
        while ($row=mysql_fetch_assoc($res)){
            $buff[]=$row[$fld];
        }
    }
    
    $quad .= "<div id='stats'>";
    
    // Fill buffers
    $buffatc  = array();
    $bufftipo = array();
    fillBuff ($buffatc,  'ouvatc');
    fillBuff ($bufftipo, 'ouvtipo');
    
    // Table headers
    $tbl = "<table class='resumo'>";
    $tbl.= "<thead><tr><th></th>";
    foreach ($bufftipo as $l){ 
        $tbl.="<th>$l</th>";
    }
    $tbl.="</tr></thead>";
    
    // Table foot (Totals)
    $tbl.="<tfoot><tr style='background-color: #f0f0f0'><td style='font-size: 50%;'>Total</td>";
    foreach($bufftipo as $l){
        $sql="SELECT  count(*) AS ca FROM ouv" . timeFilter();
        $sql.= (strpos($sql,"WHERE"))?" AND":" WHERE";
        $sql.=" ouvtipo='$l'";
        $rowt = mysql_fetch_assoc(mysql_query($sql));
        $tbl.="<td style='font-weight: bold'>$rowt[ca]</td>";
    }
    $tbl.="</tfoot>";
    
    // Table Body
    $tbl.="<tbody>";
    foreach ($buffatc as $r){ //data
        $tbl.="<tr>";
        $tbl.="<td style='font-size: 50%;'>$r</td>";
        foreach($bufftipo as $l){
            $sql="SELECT count(*) as ca FROM ouv " . timeFilter();
            $sql.= (strpos($sql,"WHERE"))?" AND":" WHERE";
            $sql.=" ouvatc='$r' AND ouvtipo='$l'";
            $rowt = mysql_fetch_assoc(mysql_query($sql));
            $tbl.="<td style='font-weight: bold'>$rowt[ca]</td>";
        }
        $tbl.="</tr>";
    }
    $tbl.="</tbody>";
    $tbl .="</table>";
    
    $quad.= $tbl;
    
    $quad.= "</div>";
    

    We interact throught the two ‘buffers’ for collumns and rows setting a sql to calculate each cell at a time…

    gl, thx

    Paulo Bueno.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to count how many characters a certain string has in PHP, but
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.