| | |
Need to sum distinct rows, not distinct values
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
I have a somewhat complicated select query which returns, in part, the following resultset (as an example):
If I were to do the following select:
then it would return 4, 30. (5 + 10 + 15)
I am looking for a select where I want to find the SUM of views for distinct threadids, not distinct views. Something that would return 4, 40. (5 + 10 + 15 + 10) Is there something I can do?
text Syntax (Toggle Plain Text)
threadid views -------- ----- 1 5 1 5 2 10 3 15 4 10
If I were to do the following select:
MySQL Syntax (Toggle Plain Text)
COUNT (DISTINCT threadid), SUM (DISTINCT views)
I am looking for a select where I want to find the SUM of views for distinct threadids, not distinct views. Something that would return 4, 40. (5 + 10 + 15 + 10) Is there something I can do?
Last edited by cscgal; Oct 2nd, 2009 at 1:49 am.
•
•
•
•
I have a somewhat complicated select query which returns, in part, the following resultset (as an example):
text Syntax (Toggle Plain Text)
threadid views -------- ----- 1 5 1 5 2 10 3 15 4 10
If I were to do the following select:
then it would return 4, 30. (5 + 10 + 15)MySQL Syntax (Toggle Plain Text)
COUNT (DISTINCT threadid), SUM (DISTINCT views)
I am looking for a select where I want to find the SUM of views for distinct threadids, not distinct views. Something that would return 4, 40. (5 + 10 + 15 + 10) Is there something I can do?
I hope that helps!!
If I helped you I would appreciate it if you would give me some reputation.
read my actionscript to english blog
Currently developing what should be social network 2.0 offline.
read my actionscript to english blog
Currently developing what should be social network 2.0 offline.
That looks like a database-design flaw to me 
Haven't MySQL'd in a while, but I think something like this might do the trick:

•
•
•
•
I am looking for a select where I want to find the SUM of views for distinct threadids, not distinct views. Something that would return 4, 40. (5 + 10 + 15 + 10) Is there something I can do?
mysql Syntax (Toggle Plain Text)
SELECT SUM(views) FROM (SELECT threadid, views FROM table_name GROUP BY threadid, views)
Hey guys! Thanks for the assistance. Right now I have it working a very, very BAD way (Basically I have it broken up into one sql query that loops through, with a second sql query within the loop.) It gives the right data but it's obviously not ideal.
It's 3 am and I've lost every ounce of concentration in me, so I will check out your solutions tomorrow. I tried playing around with subqueries earlier, but I got stuck because the example I'm giving you is a huge oversimplification of a piece of the resultset ... there are a whole bunch of joins and where clauses and I wasn't sure exactly how much needed to be "duplicated" in the subquery.
Also ... about it being a db design flaw ... This is really just a single crazy behind-the-scenes page to pull stats history on traffic to the various sections of the site, so the database wasn't designed around it.
It's 3 am and I've lost every ounce of concentration in me, so I will check out your solutions tomorrow. I tried playing around with subqueries earlier, but I got stuck because the example I'm giving you is a huge oversimplification of a piece of the resultset ... there are a whole bunch of joins and where clauses and I wasn't sure exactly how much needed to be "duplicated" in the subquery.
Also ... about it being a db design flaw ... This is really just a single crazy behind-the-scenes page to pull stats history on traffic to the various sections of the site, so the database wasn't designed around it.
![]() |
Similar Threads
- Order By Group By Distinct Problem (MySQL)
- Count/Sum distinct problem (MS SQL)
- distinct Multi Union help (MS SQL)
- MS-Access SQL DISTINCT to find unique rows (MS Access and FileMaker Pro)
- SQL SUM problem (MS SQL)
- How to remove duplicate records (MS SQL)
- SUM two fileds from different rows from the same table (MS SQL)
Other Threads in the MySQL Forum
- Previous Thread: Can't insert record into mySQL database
- Next Thread: MySQL where statement - comparing attributes from two differen tables
| Thread Tools | Search this Thread |
array base box cfquery character check coldfusion conversion count counttheoccurenceofanintegerinthe10inputs dependent digit distinct exists frequency inputs insert integer interation javascript linked list media multipletables mysql mysqlquery occurence php query radix recursion resultset running select social sql statistics string view word








