简体   繁体   中英

Multiplying two columns from two different tables

I'm trying to sum multiply two columns from two different tables but I keep getting different results. What I really want is to have the shares from the HoldingReport table to be only from a distinct CustomerServiceId. For example If CustomerServiceId = 2 then the shares should be multiplied with the current value of whatever stock they have and summed for only those columns with CustomerServiceId = 2.

These are the two tables:

在此处输入图像描述

在此处输入图像描述

And the query I have is:

SELECT SUM(CurrentValue*Shares) AS TotalAUM
FROM Stocks,HoldingReport
WHERE CustomerServiceId = '2'

Presumably, the query you want is:

SELECT SUM(s.CurrentValue * hr.Shares) AS TotalAUM
FROM Stocks s JOIN
     HoldingReport hr
     ON s.stocksid = hr.stocksid
WHERE hr.CustomerServiceId = 2;

Note that CustomerServiceId looks like a number, so I removed the single quotes. One use single quotes for string and date constants. (Of course, if it is really a string, then keep the single quotes!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM