Jump to content

how to use distinct and sum together?


Alicia

Recommended Posts

Hi,

 

I tried to run this query but it doesn't seem to work. Can some guru assist me on this? thanks

 

$get_rating = mysql_query("select distinct sum(crating), sum(srating) FROM myscore where roundID ='12'") or die(mysql_error()); 

 

there are multiple records with roundID value 12 and some of the records will have the same crating and srating. What I want to accomplish is  do not sum those records with the same crating and srating values.

 

e.g :

srating - crating - roundID

12 - 500 - 12

31 - 500 - 12

12 - 566 - 12

45 - 500 - 12

 

Final output I want is

total srating will be 12 + 31 + 45 ( the record with 12 will be excluded.)

same for total crating where 500 will be calculated once only 500+566

 

Please advise

Link to comment
Share on other sites

thanks.

 

I tried to use mysql_fetch _array to print out the output like below but why nothing is printed? Is there something wrong with my script? When I run the query in phpmyadmin, it does show the right output but when I tried to echo in php file, it failed to show the output.

 

I used this to print the output : echo $get_rating2['sum(DISTINCT(crating)']

 

Please advise and thanks

Link to comment
Share on other sites

thanks. I did try that but still nothing printed out. Any idea what have I done wrong?

 

$get_rating = mysql_query("select sum(DISTINCT(crating)), sum(DISTINCT(srating)) from myscore where roundID ='12'") or die(mysql_error());

$get_rating2 = mysql_fetch_array($get_rating);

echo $get_rating2['crating']."<br>";

Link to comment
Share on other sites

Yeah :)

 

$get_rating = mysql_query("select sum(DISTINCT(crating)) as crating, sum(DISTINCT(srating)) as srating from myscore where roundID ='12'") or die(mysql_error());

  $get_rating2 = mysql_fetch_array($get_rating);

  echo $get_rating2['crating']."<br>";

Link to comment
Share on other sites

is there anyway I can sum number of records I want instead of all records in the table? I tried the query below but it will  total up all records I have in the table

 

select sum(DISTINCT(crating)) as crating from myscore ORDER BY `mysff`.`diff` ASC LIMIT 0 ,  2

 

I want to total the 2 records with lowest value in mysff column only.. please advise.

 

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.