将两列sql相乘

时间:2017-11-29 17:37:52

标签: sql

我创建了一个名为marks的视图,其中包含按城市分组的标记

+---------+----+
|city     |mark|
|---------|----|
|paris    |-1  |
|london   |-0.2|
|new york |-0.1|
+---------+----+

我还有另一个视图factors,如下所示:

+---------+-------+
|factor1  |factor2|
|---------|-------|
|0.2      |0.5    |
+---------+-------+

我希望将第一个表中的每个标记乘以factor1以获得此结果:

+---------+--------+
|city     |new_mark|
|---------|--------|
|paris    |-1*0.2  |
|london   |-0.2*0.2|
|new york |-0.1*0.2|
+---------+--------+

我试过了:

SELECT 
    city, ((SELECT factor1 FROM factors) * mark * 1.0) AS new_mark
FROM 
    marks
GROUP BY
    1

但没有成功,我收到以下错误:

  

错误:列" marks.mark"必须出现在GROUP BY子句中或用于聚合函数

我可以得到一些帮助吗?

提前致谢

1 个答案:

答案 0 :(得分:1)

我会将子查询放在from子句中,并且不需要group by

SELECT m.city, (f.factor1 * m.mark * 1.0) as new_mark
FROM marks m CROSS JOIN
     (SELECT factor1 FROM factors) f;