Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to find Profit between 3 different tables
    primarykey
    data
    text
    <p>My project is about a jewelery store and i try to find the produxt with the max Profit.</p> <p>I have 3 tables which gives me the info:</p> <p>SALES table:</p> <pre><code>salesid productid Quantity Price 11001 13001 4 5 11002 13002 6 10 11003 13003 5 16 . . 11012 13012 7 15 </code></pre> <p>RETURN table:</p> <pre><code>salesid productid Quantity Price 11003 13003 1 16 11007 13007 3 12 11008 13008 3 8 </code></pre> <p>PROCUREMENT table:</p> <pre><code>procurementid productid Quantity Price 100001 13001 10 2 100002 13002 10 2 . . 100012 13012 10 2 </code></pre> <p>The profit is given from this formula:</p> <blockquote> <p>Profit = Quantity * Price(Sell) - Quantity * Price(Return) - Quantity * Price(Procurement)</p> </blockquote> <p>And now here is the problem. I came up to this so far </p> <pre><code>select a.productid,(a.quantity*a.price-b.quantity*b.price-c.quantity*c.price) as Profit from sales as a ,return as b ,procurement as c where a.productid = c.productid GROUP BY productid </code></pre> <p>In this situation I don't get the correct answer.</p> <p>This is because in the return table I have only 3 registers but at the other tables I have 12, so when it calculates the profit it uses the whole return table for each row of the other tables.</p> <p>I tried to use <code>max(Profit)</code> but it didn't do anything.</p> <p>I actually don't know how I can connect the 3 registers of the return table so that they are used only when they have to. When i tried the joins a lot of rows were null. I think that something has to be done with <code>OUTER JOIN</code> or something but I don't know what to do.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload