1

我在 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 子句。我哪里错了?

4

4 回答 4

1

您错过了第 10 行末尾的“)”和 之后的“,” Last_Transaction_Date。尝试这个:

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
于 2013-10-11T10:51:43.103 回答
0

您的查询中实际上存在三个问题:

  1. Last_Transaction_Date 后缺少逗号。
  2. 选择平衡时结束括号。
  3. 在计算余额时,您使用了 pm.Cost_per_Item。一旦您执行 GROUP BY,所有选择语句列都会根据结果分组行进行计算。我使用了 sum 但您可以根据需要使用任何其他聚合函数。

正确的查询是:

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, -- error 1
     (SELECT ((SUM(Transaction_Amount)* sum(pm.Cost_per_Item))-SUM(Transaction_Amount))) AS 
      Balance -- error 2 and error 3
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 ;

希望这可以帮助!!!

于 2013-10-11T11:07:37.320 回答
0

您在 LastTransactionDate 之后缺少一个逗号,并且您的 SELECT 的最后一列看起来格式不正确,即

(SELECT ((SUM(Transaction_Amount)*pm.Cost_per_Item)-SUM(Transaction_Amount)) AS Balance
于 2013-10-11T10:51:16.693 回答
0

至少你在这一行的末尾缺少一个“,”:

GROUP BY Users_Id,Product_Id) AS Last_Transaction_Date
于 2013-10-11T10:48:54.337 回答