Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL multiple table relational query
    primarykey
    data
    text
    <p>I am a MySQL newbie and need help with something I am trying to do. I have 3 tables <code>eod-stock</code>, <code>company</code> and <code>share_percentage</code>. <code>company</code> table has all data for the companies listed in stock exchange with the codes assigned by the stock exchange. <code>eod_stock</code> has data of every minute transaction of a day and it also has the same company_code field. <code>share_percentage</code> has data of number of shares etc. What I want to is summed up below in steps:</p> <p><strong>step 1</strong>: match the company_code field in the <code>eod_stock</code> table with code field in <code>company</code> table. the values for the fields are same (assigned codename for a company). </p> <p><strong>step 2</strong>: sort according to field <code>sector_id</code>. this field is in the <code>company</code> table. The sectors all have unique different ids and they have companies under them (kind of like category-subcategory. sector id is the category and companies that belong to a specific sector will go under that one )</p> <p><strong>step 3</strong>: sort the companies in step 2 according to <code>last_trade_price</code> (in <code>eod_stock</code> table) field and <code>datetime</code> (last)</p> <p><strong>step 4</strong>: match the companies with no_of_shares from <code>share_percentage</code> table by company_code field</p> <p><strong>step 5</strong>: multiply <code>step3</code> with <code>step4</code></p> <p><strong>step 6</strong>: sum of step 5 (the sum of companies by sector. for example, sum of all banks)</p> <p><strong>step 7</strong>: divide <code>step 5</code> by <code>step 6</code></p> <p><strong>step 8</strong>:sum of step 7 by sector(eg : all banks) = result</p> <p>I hope my explanation is detailed enough.I can't get this to work. any help is much appreciated. Thanks in advance! </p> <p>table samples and what I want:</p> <pre><code>TABLE company: code: "google", "HSBC", "yahoo", "SCB" sector_id: "1" ,"2", "1", "2" TABLE eod_stock: company_code : "google", "HSBC", "yahoo", "SCB" ltp: "110", "115.2", "122.4", 105" datetime: "1/1/2014 11:00", "1/1/2014 11:00", "1/1/2014 11:00", "1/1/2014 11:00" company_code : "google", "HSBC", "yahoo", "SCB" ltp: "112", "108.2", "112.4", 105.80" datetime: "1/1/2014 11:01", "1/1/2014 11:01", "1/1/2014 11:01", "1/1/2014 11:01" TABLE : share_percentage company_code: "google", "HSBC", "yahoo", "SCB" total_share: "12000", "20000", "5000", "18000" </code></pre> <p>and my code:</p> <pre><code> SELECT company.sector_id, SUM(eod_stock.ltp * share_percentage.total_share) AS Market_CAP FROM company INNER JOIN (SELECT max(datetime) as lastTime,company_code FROM eod_stock GROUP BY company_code) as LAST_TRADE ON LAST_TRADE.company_code = company.code INNER JOIN eod_stock on eod_stock.datetime = Last_trade.lastTime and eod_stock.company_code = company.code INNER JOIN share_percentage on share_percentage.company_code = company.code GROUP BY company.sector_id; </code></pre> <p>I know my sql is not ok but what I wan to achieve is something like 8.3 as final result. i know i can code it with php by breaking the operation but wanted to achieve it from sql if its possible to save time.</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