Canman2005 Posted November 13, 2008 Share Posted November 13, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/ Share on other sites More sharing options...
.josh Posted November 13, 2008 Share Posted November 13, 2008 distinct Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/#findComment-689766 Share on other sites More sharing options...
Gighalen Posted November 13, 2008 Share Posted November 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/#findComment-689767 Share on other sites More sharing options...
Canman2005 Posted November 13, 2008 Author Share Posted November 13, 2008 I tried SELECT DISTINCT `uid`, SUM(cost) FROM but it didnt seem to work any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/#findComment-689799 Share on other sites More sharing options...
Gighalen Posted November 13, 2008 Share Posted November 13, 2008 As said earlier, it's been ages since I've messed with DISTINCT (and SUM for that matter)... I might try this: SELECT SUM(cost) AS DISCINCT(uid) FROM table Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/#findComment-689807 Share on other sites More sharing options...
xtopolis Posted November 14, 2008 Share Posted November 14, 2008 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.. Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/#findComment-689889 Share on other sites More sharing options...
Canman2005 Posted November 14, 2008 Author Share Posted November 14, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/#findComment-690001 Share on other sites More sharing options...
Canman2005 Posted November 14, 2008 Author Share Posted November 14, 2008 Has anyone got any ideas on how to do this count? or do I need to give some more info Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/#findComment-690029 Share on other sites More sharing options...
.josh Posted November 14, 2008 Share Posted November 14, 2008 I think what he's trying to say is that there is no reason you should have two rows with the exact same info. Get rid of the duplicate row and you'll get rid of this issue....or is UID and COST not the only columns in your table, and those two rows have different values somewhere else? Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/#findComment-690100 Share on other sites More sharing options...
Barand Posted November 14, 2008 Share Posted November 14, 2008 SELECT SUM(x.cost) as total FROM ( SELECT DISTINCT uid, cost FROM mytablename ) as X Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/#findComment-690378 Share on other sites More sharing options...
Canman2005 Posted November 14, 2008 Author Share Posted November 14, 2008 thanks, but that seems to come back with the following error #1054 - Unknown column 'x.cost' in 'field list' any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/#findComment-690420 Share on other sites More sharing options...
Barand Posted November 14, 2008 Share Posted November 14, 2008 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] Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/#findComment-690422 Share on other sites More sharing options...
Canman2005 Posted November 14, 2008 Author Share Posted November 14, 2008 oh thanks mate, that seemed to work, not sure what I was doing. would it be possible to use a field called `type_price` rather than `cost` with this part of the QUERY SELECT DISTINCT uid, cost FROM canman Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/#findComment-690429 Share on other sites More sharing options...
Barand Posted November 14, 2008 Share Posted November 14, 2008 So long as the name of the field in the SUM() matches Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/#findComment-690434 Share on other sites More sharing options...
Canman2005 Posted November 14, 2008 Author Share Posted November 14, 2008 something like SUM( x.cost, y.type_price ) Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/#findComment-690439 Share on other sites More sharing options...
xtopolis Posted November 14, 2008 Share Posted November 14, 2008 It will won't work if you have the same uid's with different costs... but that goes with what I said before, why would you store duplicate data like that? Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/#findComment-690441 Share on other sites More sharing options...
Barand Posted November 14, 2008 Share Posted November 14, 2008 No. I think it better if you restate the new problem and table structure Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/#findComment-690451 Share on other sites More sharing options...
fenway Posted November 17, 2008 Share Posted November 17, 2008 No. I think it better if you restate the new problem and table structure Indeed. Quote Link to comment https://forums.phpfreaks.com/topic/132639-doing-a-sum/#findComment-692158 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.