计算和添加几列到SQL仅导致1行

发布于 2025-01-27 01:32:53 字数 6270 浏览 1 评论 0原文

具有以下值

idbrokerstocklong_shortstock_priceshares_owned
11amclong14.768
21msftshort282.368
31aaplshort161.428
4桌子一个1
dbsqlite8
61XOM91.728
71BACLOC37.88
81KOSHORT64.7458
91DISLONG114.428
101VZSHOTS48.028
112AMCLONG14.769
123AMCSHOT

​获得以下结果的表:

库存| 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_valuetotal_added_upshares_ones_oned
xom733.7615907.6164.6126333

我期望的输出。

库存share_owned汇总extral_added_upamc
733.7615907.6164.6126333msft
股票to_calculate
15907.616
maplto_calculateto_calculate)

依此类推,其余的

我是否过度复杂化,还是有一种更简单的方法来解决我的解决方案?

I have a SQLite DB that has a table which has the following values:

idBrokerStockLong_ShortStock_PriceShares_Owned
11AMCLong14.768
21MSFTShort282.368
31AAPLShort161.428
41TSLAShort922.6728
51FBLong215.488
61XOMShort91.728
71BACLong37.88
81KOShort64.7458
91DISLong114.428
101VZShort48.028
112AMCLong14.769
123AMCShort14.7610

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.

StockAggregate_ValueTotal_Added_UpShares_Owned
XOM733.7615907.6164.6126333

The output I am expecting is.

StockAggregate_ValueTotal_Added_UpShares_Owned
AMC733.7615907.6164.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱ヾ落雨殇 2025-02-03 01:32:53

选择库存,sum(total_value)作为gentregate_value(select stock,stock_price*sharees_nephed as total_value as trade by by股票;)

可以转换为

SELECT Stock, 
     SUM(Stock_Price*Shares_Owned) AS Aggregate_Value 
     FROM Trades 
GROUP BY Stock;

您所做的事情是正确的,只是让它看起来很干净,它将帮助您阅读和调试。

这将为您提供Total_Added_up值。这只是一个变量。我不熟悉sqlite DB,但可能很难在那里使用变量。

SELECT SUM(Stock_Price*Shares_Owned) 
FROM Trades

因此,在新的选择中加入这两个表,您拥有曝光值的所有必需值。只需确保不要在公式中除以0

select 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

SELECT Stock, 
     SUM(Stock_Price*Shares_Owned) AS Aggregate_Value 
     FROM Trades 
GROUP BY Stock;

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.

SELECT SUM(Stock_Price*Shares_Owned) 
FROM Trades

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

ぃ弥猫深巷。 2025-02-03 01:32:53

您可以通过joning总和

我也将其舍入2位数字来完成此操作,以使ot不会变得野外,但是您可以增加数字的数量或获得圆形的杆

 选择
``股票',
sum('stock_price` *`shares_owned`)
,total_added_up,round(sum(conkect_price` * sharees_owned`) / total_added_up) * 100,2)为'exposus%''
从股票交叉加入(select seles sum('stock_price` * s shares_owned`)total_added_up从股票
组``股票''
 
库存| sum('stock_price` * s shares_owned`)| total_added_up |接触 %
:---- | ----------------------------------------------:| -----------------:| ------------:
AAPL | 1291.36 | 15907.616 | 8.12
AMC | 398.52 | 15907.616 | 2.51
BAC | 302.4 | 15907.616 | 1.9
dis | 915.36 | 15907.616 | 5.75
FB | 1723.84 | 15907.616 | 10.84
ko | 517.96 | 15907.616 | 3.26
MSFT | 2258.88 | 15907.616 | 14.2
TSLA | 7381.376 | 15907.616 | 46.4
VZ | 384.16 | 15907.616 | 2.41
Xom | 733.76 | 15907.616 | 4.61

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

SELECT
`Stock`,
SUM(`Stock_Price` * `Shares_Owned`)
,Total_Added_Up, ROUND((SUM(`Stock_Price` * `Shares_Owned`) / Total_Added_Up) * 100,2) as 'Exposure %'
FROM stocks CROSS JOIN (SELECT SUM(`Stock_Price` * `Shares_Owned`) Total_Added_Up  FROM stocks) as t1
GROUP BY `Stock`
Stock | SUM(`Stock_Price` * `Shares_Owned`) | Total_Added_Up | Exposure %
:---- | ----------------------------------: | -------------: | ---------:
AAPL  |                             1291.36 |      15907.616 |       8.12
AMC   |                              398.52 |      15907.616 |       2.51
BAC   |                               302.4 |      15907.616 |        1.9
DIS   |                              915.36 |      15907.616 |       5.75
FB    |                             1723.84 |      15907.616 |      10.84
KO    |                              517.96 |      15907.616 |       3.26
MSFT  |                             2258.88 |      15907.616 |       14.2
TSLA  |                            7381.376 |      15907.616 |       46.4
VZ    |                              384.16 |      15907.616 |       2.41
XOM   |                              733.76 |      15907.616 |       4.61

db<>fiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文