If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > How to count number of each unique values in a table?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-03-09, 12:15
bigpockets bigpockets is offline
Registered User
 
Join Date: Sep 2009
Posts: 1
How to count number of each unique values in a table?

Hi all,

I've searched on the net for a solution, but to no avail - I'm hoping for luck here

Imagine this is the table:

Prod_ID Quantity Sold
=================================
ABCDEF 2
123456 6
ZZZZZZ 1
ABCDEF 7
ABCDEF 8
ZZZZZZ 2

So we have multiple entries of the same product with different quantities sold.

I need to summarize the total quantity sold of each product i.e. the result would be:

Prod_ID Quantity Sold
=================================
ABCDEF 17
123456 6
ZZZZZZ 3

Is this possible from a MySQL query, or would I need to use php scripting too?

I've tried to think of how to do this using DISTINCT, GROUP BY, SUM etc but can't seem to be able to do it.

Thanks!
Reply With Quote
  #2 (permalink)  
Old 09-03-09, 12:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
this is fairly basic SQL
Code:
SELECT Prod_ID
     , SUM(Quantity_Sold) AS total_qty
  FROM daTable
GROUP
    BY Prod_ID
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On