0

Here is the database schema:

[redacted]

I'll describe what I'm doing with the query below:

Innermost query: Select all the saleIds satisfying the WHERE conditions

Middle query: Select all the productIds that were a part of the saleId

Outermost query: SUM the products.cost and select the vendors.name.

And here is the SQL query I came up with:

SELECT vendors.name AS Company
, SUM(products.cost) AS Revenue
FROM
    products
    INNER JOIN sold_products
        ON (products.productId = sold_products.productId)
    INNER JOIN vendors
    ON (products.vendorId = vendors.vendorId)
            WHERE sold_products.productId IN (
                    SELECT sold_products.productId
                    FROM
                        sold_products
                    WHERE sold_products.saleId IN (
                            SELECT sales.saleId     
                            FROM
                                markets
                                INNER JOIN vendors
                                ON (markets.vendorId = vendors.vendorId)
                                INNER JOIN sales_campaign
                                ON (sales_campaign.marketId = markets.marketId)
                                INNER JOIN packet_headers
                                ON (sales_campaign.packetHeaderId = packet_headers.packetHeaderId)
                                INNER JOIN packet_details
                                ON (packet_details.packetHeaderId = packet_headers.packetHeaderId)
                                INNER JOIN sales
                                ON (sales.packetDetailsId = packet_details.packetDetailsId)
                            WHERE vendors.customerId=60
                            )
                    )
GROUP BY Company
ORDER BY Revenue DESC;

Any help in optimizing this?

2
  • What does EXPLAIN tell you? What have you tried? Commented Nov 11, 2012 at 13:06
  • @Bohemian: EXPLAIN shows me a detailed table, but I'm afraid I don't understand what it means.. Commented Nov 11, 2012 at 13:11

1 Answer 1

1

Since you are just using inner joins you normally simplify the query to smth like this:

SELECT  ve.name     AS Company
,       SUM(pr.cost) AS Revenue
FROM    products        pr
,       sold_products   sp
,       vendors         ve
,       markets         ma
,       sales_campaign  sc
,       packet_headers  ph
,       packet_details  pd
,       sales           sa
Where pr.productId = sp.productId
And   pr.vendorId  = ve.vendorId
And   ve.vendorId  = ma.vendorId
And   sc.marketId = ma.marketId
And   sc.packetHeaderId = ph.packetHeaderId
And   pd.packetHeaderId = ph.packetHeaderId)
And   sa.packetDetailsId = pd.packetDetailsId
And   ve.customerId = 60
GROUP BY ve.Company
ORDER BY pr.Revenue DESC;

Please try if this works and if it is faster and let me know.

Sign up to request clarification or add additional context in comments.

6 Comments

Heiz Matchinger: This is exactly what I was doing before, but the result was a completely wrong. This query would return SUM(products.cost) of ALL the products, whether they are sold or not..
have you checked if it is really ALL products or if its just SUMs a product multiple times?! with the join to sold_products it can not SUM all products! Look at your query without the SUM => what is the result?
Yes it really is a SUM of ALL the products by that Vendor; not the SUM of just the ones that were sold..
Try to simplify for finding the bug: SELECT ve.name AS Company , SUM(pr.cost) AS Revenue FROM products pr , sold_products sp , vendors ve Where pr.productId = sp.productId And pr.vendorId = ve.vendorId And ve.customerId = 60 GROUP BY ve.Company ORDER BY pr.Revenue DESC;
Wow that works perfectly! And instantaneously too!! Thanks :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.