计算和添加几列到SQL仅导致1行
具有以下值
id | broker | stock | long_short | stock_price | shares_owned |
---|---|---|---|---|---|
1 | 1 | amc | long | 14.76 | 8 |
2 | 1 | msft | short | 282.36 | 8 |
3 | 1 | aapl | short | 161.42 | 8 |
4 | 的 | 桌子 | 一个 | 的 | 1 |
我 | : | db | 有 | sqlite | 8 |
6 | 1 | XOM | 短 | 91.72 | 8 |
7 | 1 | BAC | LOC | 37.8 | 8 |
8 | 1 | KO | SHORT | 64.745 | 8 |
9 | 1 | DIS | LONG | 114.42 | 8 |
10 | 1 | VZ | SHOTS | 48.02 | 8 |
11 | 2 | AMC | LONG | 14.76 | 9 |
12 | 3 | AMC | | | SHOT |
获得以下结果的表:
库存| gentregate_value | total_added_up | “曝光%”
- gentregate_value = stock_price * shares_owned
- total_added_up =所有gentregate_value的总和。
- “曝光%” =(((grengregate_value / total_added_up) * 100)
选择库存,sum(total_value)作为gencregate_value(select stock,stock_price,stock_price * sharees_ sharees_ s as at trade from trade from trade from code from code; < /
code>给我一个良好的gentregate_value列。
我可以通过以下操作获得total_added_up值:
from (select Stock, SUM(Total_Value) AS Aggregate_Value
from (select Stock, Stock_Price*Shares_Owned AS Total_Value
from Trades)
group by Stock);
我尝试了以下查询来组合gentregate_value total_added_up和``exposure%'':
from (select Stock, Aggregate_Value, SUM(Aggregate_Value) AS Total_Added_Up
from (select Stock, SUM(Total_Value) AS Aggregate_Value
from (select Stock, Stock_Price*Shares_Owned AS Total_Value
from Trades)
group by Stock));
上述查询将为我提供格式 stock | gentregate_value | total_added_up | “曝光%” ,但是当每个股票都有一排时,它只是一行。
我得到的输出看起来如下。
库存 | gencregate_value | total_added_up | shares_ones_oned |
---|---|---|---|
xom | 733.76 | 15907.616 | 4.6126333 |
我期望的输出。
库存 | share_owned | 汇总extral_added_up | amc |
---|---|---|---|
733.76 | 15907.616 | 4.6126333 | msft |
( | ) | 股票 | to_calculate |
( | 15907.616 | 。 | ) |
mapl | ( | to_calculate | to_calculate) |
依此类推,其余的
我是否过度复杂化,还是有一种更简单的方法来解决我的解决方案?
I have a SQLite DB that has a table which has the following values:
id | Broker | Stock | Long_Short | Stock_Price | Shares_Owned |
---|---|---|---|---|---|
1 | 1 | AMC | Long | 14.76 | 8 |
2 | 1 | MSFT | Short | 282.36 | 8 |
3 | 1 | AAPL | Short | 161.42 | 8 |
4 | 1 | TSLA | Short | 922.672 | 8 |
5 | 1 | FB | Long | 215.48 | 8 |
6 | 1 | XOM | Short | 91.72 | 8 |
7 | 1 | BAC | Long | 37.8 | 8 |
8 | 1 | KO | Short | 64.745 | 8 |
9 | 1 | DIS | Long | 114.42 | 8 |
10 | 1 | VZ | Short | 48.02 | 8 |
11 | 2 | AMC | Long | 14.76 | 9 |
12 | 3 | AMC | Short | 14.76 | 10 |
I need to be able to query the table to get the following result:
Stock | Aggregate_Value | Total_Added_Up | "Exposure %"
- Aggregate_Value = Stock_Price * Shares_Owned
- Total_Added_Up = The sum of all Aggregate_Value's.
- "Exposure %" = ((Aggregate_Value / Total_Added_Up) * 100)
select Stock, SUM(Total_Value) AS Aggregate_Value from (select Stock, Stock_Price*Shares_Owned AS Total_Value from Trades ) group by Stock;
The above query gives me a good Aggregate_Value column.
I can get the Total_Added_Up value by doing:
from (select Stock, SUM(Total_Value) AS Aggregate_Value
from (select Stock, Stock_Price*Shares_Owned AS Total_Value
from Trades)
group by Stock);
I tried the following query to combine Aggregate_Value Total_Added_Up, and "Exposure %":
from (select Stock, Aggregate_Value, SUM(Aggregate_Value) AS Total_Added_Up
from (select Stock, SUM(Total_Value) AS Aggregate_Value
from (select Stock, Stock_Price*Shares_Owned AS Total_Value
from Trades)
group by Stock));
The above query will give me the format Stock | Aggregate_Value | Total_Added_Up | "Exposure %", but it is only one row when there should be a row for every Stock.
The output I am getting looks like the following.
Stock | Aggregate_Value | Total_Added_Up | Shares_Owned |
---|---|---|---|
XOM | 733.76 | 15907.616 | 4.6126333 |
The output I am expecting is.
Stock | Aggregate_Value | Total_Added_Up | Shares_Owned |
---|---|---|---|
AMC | 733.76 | 15907.616 | 4.6126333 |
MSFT | (To_Calculate) | 15907.616 | (To_Calculate) |
AAPL | (To_Calculate) | 15907.616 | (To_Calculate) |
TSLA | (To_Calculate) | 15907.616 | (To_Calculate) |
And so on with the rest of the listed Stocks.
Am I overcomplicating this or is there an easier way to get to my solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
选择库存,sum(total_value)作为gentregate_value(select stock,stock_price*sharees_nephed as total_value as trade by by股票;)
可以转换为
您所做的事情是正确的,只是让它看起来很干净,它将帮助您阅读和调试。
这将为您提供Total_Added_up值。这只是一个变量。我不熟悉sqlite DB,但可能很难在那里使用变量。
因此,在新的
选择
中加入这两个表,您拥有曝光值的所有必需值。只需确保不要在公式中除以0select Stock, SUM(Total_Value) AS Aggregate_Value from (select Stock, Stock_Price*Shares_Owned AS Total_Value from Trades group by Stock;)
Can be converted to
What you're doing is correct, just make it look cleaner, it'll help you read and debug it.
This will give you the Total_Added_Up value. It's only a variable. I am not familiar with SQLite DB, but it might be difficult to use a variable there.
So join those two tables in a new
select
and you have all of the needed values for the Exposure value. Just make sure to not divide by 0 in your formula您可以通过joning总和
我也将其舍入2位数字来完成此操作,以使ot不会变得野外,但是您可以增加数字的数量或获得圆形的杆
db&lt;&gt;&gt;
You can do this in one seLect by joning the total sum
I also rounded it uop to 2 digits, so that ot wouldn't look to wild, but you can increase the numbers of digits or get rod of the rounding
db<>fiddle here