我在 SQL 服务器中创建了 3 个名为 t_user_master、t_product_master 和 t_transaction 的表。t_user_master 通过列 Users_id 与 t_transaction 链接,t _product_master 通过列 Products_id 与 t_transaction 链接。现在我必须生成和输出包含 users_name(来自t_user_master),Product_name(from t_product_master) 与它链接,以便查看哪些使用订购了哪些产品等等。这是我的代码片段-
SELECT um.Users_Name,
pm.Product_Name,
(SELECT SUM(Transaction_Amount)
FROM t_transaction
WHERE Transaction_Type = 'Order'
GROUP BY Users_Id,
Product_Id) AS Ordered_quantity,
(SELECT SUM(Transaction_Amount)
FROM t_transaction
WHERE Transaction_Type = 'Payment'
GROUP BY Users_ID,
Product_Id) AS Amount_Paid,
(SELECT MAX(Transaction_Date)
FROM t_transaction
GROUP BY Users_Id,
Product_Id) AS Last_Transaction_Date,
(SELECT ( ( SUM(Transaction_Amount) * pm.Cost_per_Item ) - SUM(Transaction_Amount) ) )AS Balance
FROM t_user_master um
JOIN t_transaction tr
ON um.Users_ID = tr.Users_ID
JOIN t_product_master pm
ON tr.Product_ID = pm.Product_ID
GROUP BY um.Users_Name,pm.Product_Name
我已经说明了与语法相关的所有错误,但现在我面临另一个问题。执行后我收到此消息 - 列 't_product_master.Cost_Per_Item' 在选择列表中无效,因为它不包含在聚合函数或GROUP BY 子句。我哪里错了?