我想学习 PowerBI,所以我下载了它,将 PowerBI 连接到使用此测试数据集的 MySQL 数据库:
http://learn.evermight.net/johnlai/demostore.sql
该数据集有表格invoice,invoice_item,customer,address,product。
我想使用 PowerBI 给我一个数据表,它产生与这个查询相同的结果:
SELECT customer_id, CONCAT(first_name,last_name),
(SELECT SUM(qty*unit_cost)
FROM invoice_item WHERE invoice_id IN
(SELECT invoice_id FROM invoice WHERE invoice.customer_id = customer.customer_id))
AS PurchAmt
FROM customer
GROUP BY customer_id
ORDER BY PurchAmt DESC;
+-------------+------------------------------+-------------+
| customer_id | CONCAT(first_name,last_name) | PurchAmt |
+-------------+------------------------------+-------------+
| 10 | YuriPreb | 13082458.70 |
| 7 | SylvaiBelange | 13031101.01 |
| 9 | YmnuaKrish | 13004262.97 |
| 8 | AmnaChowdury | 12843937.12 |
| 3 | FlorenceIel | 12786294.27 |
| 11 | GordonBiyas | 12722387.44 |
| 5 | SabrinaFabo | 12675754.67 |
| 4 | AlishaMml | 12662484.31 |
| 1 | JayBobbin | 12537697.14 |
| 12 | SimulaUmmea | 12376986.77 |
| 2 | JayisonGam | 12313863.63 |
| 6 | FabriceLabe | 12176486.31 |
+-------------+------------------------------+-------------+
使用 PowerBI 连接到数据库后,我在 PowerBI 中定义了两个新列:
Cost = 'demostore invoice_item'[qty]*'demostore invoice_item'[unit_cost]
FullName = CONCATENATE('demostore customer'[first_name],'demostore customer'[last_name])
然后我点击了字段的复选框customer.customer_id,customer.FullName,invoice_item.Cost,这就是我得到的:
为什么 PowerBI 中的结果与我的 SQL 查询不同?例如,MySQL 说 Fabrice 有 12176486.31 但 PowerBI 说 11286529.84 ?
更新
我注意到,如果我选中标记以下字段invoice.customer_id,invoice_item.Cost,并且我不总结invoice.customer_id如下图所示:
然后客户 6(即 Fabrice)显示与我的 MySQL 查询相同的数量。这是怎么回事?

