Jump to content

Doing a SUM


Canman2005

Recommended Posts

Hi all

 

I have the following table

 

UID    COST

1      1.99

2      3.99

1      1.99

3      5.22

 

As you will notice, there are 2 rows which have the same UID number

 

How can I add up all of the COSTS using a

 

SELECT SUM(cost) FROM `log`

 

But if any rows have the same UID number, then it would only count one instance of that, so with the above table it would add up all of

 

UID    COST

2      3.99

3      5.22

 

and also just one instance of

 

UID    COST

1      1.99

 

Giving a total of

 

11.2

 

Any help would be great

 

Thanks

Link to comment
Share on other sites

I think you need to add a DISTINCT UID thingie to your SQL string. It's been a while since I've done anything, but DISTINCT UID should only select UID that are different, so the value of 1 would only appear in the SUM function once.

 

If that makes any sense >< I'm horrible at explaining things sometimes.

Link to comment
Share on other sites

That doesn't make sense.

 

You have uid 1, cost 1.99 twice in your table... but you want to get only one occurrence of the uid's cost value..  But what if you had uid 1 , cost 2.34, so that they were no longer matching..  How would you decide which uid 1 cost value gets selected?

 

If they are exact duplicates, why not just remove the duplicates..

 

Currently, with that data you provided, the only way I know how to get 11.2 as an answer is to

SELECT SUM(DISTINCT cost) FROM `yourTable`

But that is only because none of the other uids have a matching cost value; it will break as soon as two different uids have the same cost value

 

I say you should just remove the dupes..

Link to comment
Share on other sites

That doesn't make sense.

 

You have uid 1, cost 1.99 twice in your table... but you want to get only one occurrence of the uid's cost value..  But what if you had uid 1 , cost 2.34, so that they were no longer matching..  How would you decide which uid 1 cost value gets selected?

 

If they are exact duplicates, why not just remove the duplicates..

 

Currently, with that data you provided, the only way I know how to get 11.2 as an answer is to

SELECT SUM(DISTINCT cost) FROM `yourTable`

But that is only because none of the other uids have a matching cost value; it will break as soon as two different uids have the same cost value

 

I say you should just remove the dupes..

 

Two rows with the same UID number will never have different `cost` value, therefore I need to just add up how I explained, I tried the

 

SELECT SUM(cost) AS DISCINCT(uid) FROM table

 

but that didnt work

 

Any ideas?

Link to comment
Share on other sites

Is the X/x case sensitive

 

On winXP

[pre]

mysql> SELECT * FROM `canman`;

+----------+-----+------+

| idcanman | uid | cost |

+----------+-----+------+

|        1 |  1 | 1.99 |

|        2 |  2 | 3.99 |

|        3 |  1 | 1.99 |

|        4 |  3 | 5.22 |

+----------+-----+------+

 

 

mysql> SELECT SUM(x.cost) as total

    -> FROM (

    ->        SELECT DISTINCT uid, cost FROM canman

    ->        ) as X;

+-------+

| total |

+-------+

| 11.20 |

+-------+[/pre]

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.