Avatar billede doncarnage Nybegynder
26. oktober 2010 - 03:23 Der er 6 kommentarer og
1 løsning

Group by flere felter - virker ikke efter hensigten

Godaften eksperter..

Jeg er i gang med at forsøge at udarbejde et budget/resultat-skema og det ser ud som følgende: http://www.valuable.dk/eksperten/budget/

Jeg har forsøgt at vise problemet ved at man kan vælge at gruppere resultaterne ud fra enten "gruppe" eller "kategori". Jeg vil gerne ha' lavet således at der er grupperet ud fra den overordnede kategori. Problemet er bare at når jeg forsøger at gøre det, så bliver "pengene" smidt sammen til ét felt :/ Jeg har forsøgt ved at lave "GROUP BY" ud fra flere felter, men desværre uden held..

Spørgsmålet er derfor: Hvordan får jeg adskilt "pengene" i kategori-visningen?

Mine tabeller i databasen ser ud som her med følgende relationer:
http://www.valuable.dk/eksperten/budget/dia.jpg


Til at udskrive som jeg gør nu bruger jeg følgende stykke kode:


<table border="1" cellspacing="0" cellpadding="0">
<tr bgcolor="#ECECEC">
  <td width="125" height="20" style="padding-left: 5px;"><font color="#155c76">Produktkategori</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">JAN</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">FEB</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">MAR</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">APR</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">MAJ</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">JUN</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">JUL</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">AUG</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">SEP</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">OKT</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">NOV</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">DEC</font></td>
  <td width="60" height="20" align="center"><font color="#155c76">Total</font></td>
</tr>

<?php

    if ($_GET['g'] == 'gruppe'){

    $query = mysql_query("SELECT *, tbl_productcategory.Name as produktkategori, tbl_productgroup.Name as produktgruppe, SUM(tbl_transactions.TotalPrice) AS pris
            FROM tbl_productgroup
            INNER JOIN tbl_productcategory ON tbl_productcategory.ProductCategoryID = tbl_productgroup.ProductCategoryID
            INNER JOIN tbl_product ON tbl_product.ProductGroupID = tbl_productgroup.ProductGroupID
            INNER JOIN tbl_transactions ON tbl_transactions.ProductID = tbl_product.ProductID
            WHERE Temp = 0 GROUP BY tbl_productgroup.ProductGroupID
            ") or die(mysql_error());
    }

    else if ($_GET['g'] == 'kategori'){

    $query = mysql_query("SELECT *, tbl_productcategory.Name as produktkategori, tbl_productgroup.Name as produktgruppe, SUM(tbl_transactions.TotalPrice) AS pris
            FROM tbl_productgroup
            INNER JOIN tbl_productcategory ON tbl_productcategory.ProductCategoryID = tbl_productgroup.ProductCategoryID
            INNER JOIN tbl_product ON tbl_product.ProductGroupID = tbl_productgroup.ProductGroupID
            INNER JOIN tbl_transactions ON tbl_transactions.ProductID = tbl_product.ProductID
            WHERE Temp = 0 GROUP BY tbl_productgroup.ProductCategoryID
            ") or die(mysql_error());
    }


          while ($row = mysql_fetch_assoc($query)) {

        echo '<tr>';
      echo '<td width="125" height="20" style="padding-left: 5px;">' . $row['produktkategori'] . '</td>';
         
          if($row['BudgetMonth'] == 1){
        echo '<td width="50" height="20" align="center">'.$row['pris'].'</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
      }
          if($row['BudgetMonth'] == 2){
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">'.$row['pris'].'</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
      }
          if($row['BudgetMonth'] == 3){
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">'.$row['pris'].'</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
      }
          if($row['BudgetMonth'] == 4){
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">'.$row['pris'].'</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
      }
          if($row['BudgetMonth'] == 5){
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">'.$row['pris'].'</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
      }
          if($row['BudgetMonth'] == 6){
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">'.$row['pris'].'</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
      }
          if($row['BudgetMonth'] == 7){
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">'.$row['pris'].'</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
      }
          if($row['BudgetMonth'] == 8){
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">'.$row['pris'].'</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
      }
          if($row['BudgetMonth'] == 9){
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">'.$row['pris'].'</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
      }
          if($row['BudgetMonth'] == 10){
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">'.$row['pris'].'</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
      }
          if($row['BudgetMonth'] == 11){
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">'.$row['pris'].'</td>';
        echo '<td width="50" height="20" align="center">0</td>';
      }
          if($row['BudgetMonth'] == 12){
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">0</td>';
        echo '<td width="50" height="20" align="center">'.$row['pris'].'</td>';
      } 

          echo '<td width="60" height="20" align="center">0</td>';
          echo '</tr>';

      }


?>


<tr bgcolor="#ECECEC">
  <td width="125" height="20" style="padding-left: 5px;"><font color="#155c76">I alt</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">0</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">0</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">0</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">0</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">0</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">0</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">0</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">0</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">0</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">0</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">0</font></td>
  <td width="50" height="20" align="center"><font color="#155c76">0</font></td>
  <td width="60" height="20" align="center"><font color="#155c76">0</font></td>
</tr>
</table>



Håber ikke ovenstående blev betragtet som spam.. Det var blot for at give så mange oplysninger med som der kunne være nødvendigt for at få hjælp - er nemlig helt på herrens mark med det her :/
26. oktober 2010 - 09:27 #1
Det staar mig ikke helt klart hvad problemet er.  Du siger at du kan vaelge mellem at gruppere resultater efter gruppe og kategori, altsaa $_GET['g'] == 'gruppe' eller $_GET['g'] == 'kategori', og afhaengig deraf koerer du den foerste eller den anden query.  I begge tilfaelde stiller du resultatet op ved hjaelp af koden i din while loekke.  Hvis jeg forstaar dig korrekt faar du det oenskede resultat naar du vaelger at gruppere efter gruppe men ikke naar du vaelger at gruppere efter kategori.  Kunne du vise et billed af et resultat grupperet efter gruppe og et resultat grupperet efter kategori?
Avatar billede majbom Novice
26. oktober 2010 - 09:37 #2
-> #1 - du kan selv se det på siden...

-> #0 - jeg tror ikke det er muligt på den måde, at dele det ud. du kan evt lave et array, som i kategori-tilfældet har en key pr kategori, og i hver key har du et array med de 12 måneder. når du så løber dit db-resultat igennem, smider du beløbet ind i den tilsvarende måned under den tilsvarende kategori (beløbet skal selvfølgelig lægges sammen med det beløb der allerede står der).

håber det gav mening :)
Avatar billede doncarnage Nybegynder
26. oktober 2010 - 13:04 #3
Christian_Belgien: Jeg vil gerne have det til at se ud som her: http://www.valuable.dk/eksperten/budget/her.jpg

splazz: Er desværre ikke helt med på hvordan jeg gør det som du nænvner :S
26. oktober 2010 - 17:36 #4
Ok, jeg tror jeg forstaar det.  Jeg er lige kommet fra arbejde - jeg skal kikke paa det i aften.
Avatar billede doncarnage Nybegynder
26. oktober 2010 - 17:41 #5
Jeg er netop kommet hjem fra arbejde, og det viste sig at de ikke skulle bruge budget-skemaet til noget alligevel..

Derfor dropper vi idéen og jeg lukker tråden igen..

Men i må gerne smide et svar, hvis i vil have lidt points fordi i forsøgte at hjælpe ;)
26. oktober 2010 - 19:45 #6
OK, fordi jeg var godt igang vil jeg byde paa en andel af points.
Avatar billede majbom Novice
27. oktober 2010 - 09:12 #7
jeg springer over :)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester