我试图在SQL中计算需要来自两个不同表格的数字的比率,但它不断吐出错误的答案。我的查询如下:
SELECT t1.category, ROUND((SUM(t1.cost)/SUM(t2.store_sales)),2) AS ratio
FROM table1 AS t1
JOIN table2 AS t2 ON t1.id = t2.id
GROUP BY 1
ORDER BY 2;
返回:
| category | ratio |
+-------------------------+------------------------------+
| N/A | 0.00 |
| Category 1 | 1287.71 |
但是对于类别1,SUM(t1.cost)= 1,367,502和SUM(t2.sales)= 5,819.33,当你将两者分开时,它应该给出234.99的比率。感谢我出错的地方。提前谢谢。