Jump to content

Getting total amount of sales for products


subcool

Recommended Posts

MySQL version 5.0.22

 

I have the following tables: "product" and "sales"

 

I put these in pastebin cause they're HUGE.

Product Table SQL: http://pastebin.com/f5ae734b3

Sale Table SQL: http://pastebin.com/f41677401

 

note: i didnt make these tables i just have to work with em :(

 

Now i need to get the count(sale.id) from all products of 1 developer (each dev has a unique ID and each sale a unique ID) grouped product (1 product can have multiple product_id's)

but here comes the hard part, products have unique ID's (logical) but they also have VERSIONS which have unique ID's too BUT they have 'parentid' set and name is set to NULL

 

example:

product_id = 1

name = Something

 

product_id = 2

name = NULL (empty)

parentid = 1

 

Now i have to count all the sales for ID 1 and 2 together so i get the total amount of sales for that product.

Here is the query that gets the top 25 products of a developer, but this DOESNT add the sales of the childproducts.

 

SELECT sale.product_id, product.name,
Count(sale.id) AS sales
FROM sale Inner Join product ON (sale.product_id = product.id)
WHERE product.developer_id =  '2137'
GROUP BY product.id
ORDER BY sales DESC
LIMIT 0,25

 

If you can/want to help, please do... any questions don't hesitate to ask... i'v been trying this for a few days now without success

Link to comment
Share on other sites

How can one product have multiple ids?

 

Because 1 product can have multiple versions, version of the same product have unique ProductID's too but the "name" field is NULL and the ParentID is filled in

 

so if you have

 

ProductID 1, 2 and 3

and ProductID 2 and 3 have "parentid = 1" then they are the same version as ID 1 but their productid is unique. So making it 1 product with multiple ProductID's

its a stupid setup, but i have to work with it :(

Link to comment
Share on other sites

something like this?

 

SELECT values FROM (SELECT * FROM table WHERE devid = '1') WHERE.......etc

 

Cause the main problem is that i am unable to associate the child products with the parents in the query, it counts only counts the parents OR counts the parents and childs seperate. I'v tried it with joining too, i can already get the top 25 Parent products just need to add the # of sales from their childs to the result.

 

This is the query to get the top 25 parent ID's (for a predefined period)

SELECT
countryProductStats.productid AS product_id
FROM
countryProductStats
Inner Join product ON (countryProductStats.productid = product.id)
Inner Join sale ON product.id = sale.product_id
WHERE
sale.sale_date BETWEEN '2007-11-01 0:00:00' AND '2007-11-31 00:00:00' AND
product.name <>  '' AND
product.developer_id = '2137'
GROUP BY
countryProductStats.productid,
ORDER BY
Count(sale.id)  DESC
LIMIT 0,25

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.