我有一個表,公司在這裏爲以下結構:如何找到組的總和並在mysql中查找權重?
+------------+--------------+-----------+--------+-----------------------------+
| ticker | total_shares | institute | public | sector |
+------------+--------------+-----------+--------+-----------------------------+
| 1JANATAMF | 200000000 | 30.00 | 45.00 | Mutual Funds |
| 8THICB | 5000000 | 0.00 | 0.00 | Mutual Funds |
| AAMRATECH | 55369395 | 28.00 | 42.00 | IT Sector |
| ABB1STMF | 162615643 | 46.44 | 33.56 | Mutual Funds |
| ABBANK | 532459152 | 54.86 | 30.01 | Bank |
| ACI | 28661768 | 37.73 | 28.23 | Pharmaceuticals & Chemicals |
| ACIFORMULA | 45000000 | 22.77 | 11.21 | Pharmaceuticals & Chemicals |
| ACIZCBOND | 267170 | 60.00 | 40.00 | Corporate Bond |
| ACTIVEFINE | 69000000 | 20.80 | 64.42 | Pharmaceuticals & Chemicals |
| AFCAGRO | 50000000 | 48.11 | 21.60 | Pharmaceuticals & Chemicals |
| AFTABAUTO | 95732422 | 47.49 | 22.01 | Engineering |
| AGNISYSL | 51798982 | 16.33 | 70.92 | IT Sector |
| AGRANINS | 24157650 | 18.30 | 43.05 | Insurance |
| AIBL1STIMF | 100000000 | 76.31 | 13.69 | Mutual Funds |
| AIMS1STMF | 41450000 | 0.00 | 0.00 | Mutual Funds |
| AL-HAJTEX | 12691843 | 4.19 | 71.76 | Textile |
| ALARABANK | 946958501 | 10.07 | 46.88 | Bank |
+------------+--------------+-----------+--------+-----------------------------+
另一個表價格:當然
+------------+--------+--------+------------+
| ticker | open | high | price_date |
+------------+--------+--------+------------+
| AAMRATECH | 29.80 | 30.20 | 2014-05-04 |
| ABBANK | 27.50 | 27.50 | 2014-05-04 |
| ACI | 235.00 | 244.50 | 2014-05-04 |
| ACIFORMULA | 86.00 | 87.60 | 2014-05-04 |
| ACTIVEFINE | 69.00 | 70.00 | 2014-05-04 |
| BANGAS | 509.50 | 509.50 | 2014-05-04 |
| BANKASIA | 18.00 | 18.40 | 2014-05-04 |
+------------+--------+--------+------------+
數據已經縮短爲目的。現在我想找到開放的加權價格。公式爲
A = open*((total_share*(institue/100)+(total_share*(public/100))
他們的總和A按行業類似總和所有股票屬於共同基金。然後劃分單個股票代碼,例如1JANATAMF /共同基金的總和以找出權重。
我已經使用這個查詢:
select c.ticker,
p.open*(c.total_shares*(c.institute/100)+c.total_shares*(c.public/100)) as cap
from company as c
left join price as p
on p.ticker =c.ticker
where p.price_date = '2014-05-04';
,但不能找到一種方法,通過部門來獲得總和,其重量。
編輯:
輸出會像
定單,A/ 其中,A是根據股票配製的值
輸出將(A與由扇區分組總和)如:
+------------+--------------+--------------+
| ticker | sum(a) | weight |
+------------+--------------+--------------+
| 1JANATAMF | 5700000000 | 150078540 |
您的權重計算有些困惑,例如(1JANATAMF)字符串值如何可以通過數值(如扇區和)整除,您可以清除它嗎? –
您誤解了我從我上面的查詢或由A得到的數字結果。例如1JANATAMF的A說50並且它屬於共同基金有許多其他公司在共同基金部門如1JANATAMF,我想要所有按行業的總和A那麼通過這種方式計算重量A /該部分的總和。 –
請編輯您的問題並提供您想要的給定數據的結果。 –