Jump to content

[SOLVED] Stupid SUM() Problem


rwachowiak

Recommended Posts

<?php include 'libs/dbopen.php';
	$id = (isset($_GET['Userid']))? mysql_escape_string($_GET['Userid']) : "1";
	$query  = "SELECT SUM(week1-week1hours) FROM staff JOIN project ON project.staff_id=staff.id WHERE project.staff_id='$id'";
	$result = mysql_query($query) or die(mysql_error());
	while($row = mysql_fetch_array($result)){  

	if ($row['SUM(week1-week1hours)'] >= 0)	{	echo "{$row['SUM(week1-week1hours)']}";	}
	else			{	echo "<span class='red'>({$row['SUM(week1-week1hours)']})</span>";	}
	}

	include 'libs/dbclose.php';?>

 

Ok, so what i want this to do is SUM up all of week1 and minus week1hours... but what its doing is its multiplying week1hours by how many ever rows there are of week1... so if theres 5 rows it looks like: "week1-week1hours*5"

 

any ideas?!

 

thanks!!

Link to comment
Share on other sites

$query = "SELECT SUM(week1-week1hours) AS theSum FROM staff JOIN project ON project.staff_id=staff.id WHERE project.staff_id='$id'";
$result = mysql_query($query) or die(mysql_error());

$totalRows = mysql_num_rows( $result );
while($row = mysql_fetch_array($result)){ 

if ($row['theSum'] > -1){
	echo $row['theSum'] * $totalRows;
else{
	echo "<span class='red'>".$row['theSum'] . "</span>";
}

Link to comment
Share on other sites

$query = "SELECT SUM(week1-week1hours) AS theSum FROM staff JOIN project ON project.staff_id=staff.id WHERE project.staff_id='$id'";
$result = mysql_query($query) or die(mysql_error());

$totalRows = mysql_num_rows( $result );
while($row = mysql_fetch_array($result)){ 

if ($row['theSum'] > -1){
	echo $row['theSum'] * $totalRows;
else{
	echo "<span class='red'>".$row['theSum'] . "</span>";
}

 

hmm, i put that in place of what i had and it breaks the page, makes it all white... hmmm

Link to comment
Share on other sites

Can you post table structures, sample data and expected results. It's a lot easier if I can see what you've got.

 

TABLE: project

FIELDS: id, staff_id, clientname, projecttype, partner, manager, week1, week2 ~ week 59, week 60, wk1act, wk2act ~ wk59act, wk60act

 

id staff_id clientname projecttype partner manager week1 week2

1  1  Mader Construction  Audit  KGB  EDC  10  10

2  1  Taber Industries  1120S Review  CWC  CAJ  5  5

 

 

TABLE: staff

FIELDS: id, firstname, lastname, skilllevel, week1hours, week2hours ~ week59hours, week60hours

 

id firstname lastname skilllevel week1hours week2hours

1  Ryan  Wachowiak  Intern  40  40

 

 

 

So, what i want it to do is add up Week1 hours from PROJECT, and do the following formula: "week1hours - SUM of week1"

 

So, for Ryan Wachowiak, it would be -25

 

 

That help?

 

 

Link to comment
Share on other sites

wow... that's a lot of fields you got their. just my idea though, are you adding additional fields for some time? IMHO, it's not a good idea to have that many fields... maybe a little normalization would do. :)

 

anyway, that's only a suggestion. going back to your question, try this:

 

SELECT DISTINCT staff.firstname,

staff.lastname,

(staff.week1hours - SUM(project.week1)) as resultOfDeduction

FROM staff

INNER JOIN project ON project.staff_id = staff.id

GROUP BY staff.id

 

did it work? (you can add additional info if you want though)

 

Jay,

Link to comment
Share on other sites

wow... that's a lot of fields you got their. just my idea though, are you adding additional fields for some time? IMHO, it's not a good idea to have that many fields... maybe a little normalization would do. :)

 

anyway, that's only a suggestion. going back to your question, try this:

 

SELECT DISTINCT staff.firstname,

staff.lastname,

(staff.week1hours - SUM(project.week1)) as resultOfDeduction

FROM staff

INNER JOIN project ON project.staff_id = staff.id

GROUP BY staff.id

 

did it work? (you can add additional info if you want though)

 

Jay,

 

what would the whole thing look like? including the echo and stuff? sorry im still pretty new to mysql =(

Link to comment
Share on other sites

A GROUP BY gives distinct results so the DISTINCT is superfluous

 

<?php
$sql = "SELECT staff.firstname,
        staff.lastname,
        (staff.week1hours - SUM(project.week1)) as resultOfDeduction
        FROM staff
        INNER JOIN project ON project.staff_id = staff.id
        GROUP BY staff.id";
$res = mysql_query($sql);
while (list($fn, $ln, $tot) = mysql_fetch_row($res))
{
    echo "$fn, $ln, $tot<br/>";
}

?>

Link to comment
Share on other sites

A GROUP BY gives distinct results so the DISTINCT is superfluous

 

<?php
$sql = "SELECT staff.firstname,
        staff.lastname,
        (staff.week1hours - SUM(project.week1)) as resultOfDeduction
        FROM staff
        INNER JOIN project ON project.staff_id = staff.id
        GROUP BY staff.id";
$res = mysql_query($sql);
while (list($fn, $ln, $tot) = mysql_fetch_row($res))
{
    echo "$fn, $ln, $tot<br/>";
}

?>

 

sorry to be a huge pain, i might be missing something. nothing is showing up on the page for this query =(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.