简体   繁体   English

将两个不同表中的两列相乘

[英]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.我真正想要的是让HoldingReport 表中的共享仅来自不同的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.例如,如果 CustomerServiceId = 2,则股票应乘以他们拥有的任何股票的当前价值,并仅对 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.请注意, CustomerServiceId看起来像一个数字,所以我删除了单引号。 One use single quotes for string and date constants.一种对字符串和日期常量使用单引号。 (Of course, if it is really a string, then keep the single quotes!) (当然,如果真的是字符串,那就保留单引号!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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