我是学习SQL的初学者。我有2张桌子。尝试以最少的交易金额获得客户的名字和姓氏-仅那一行。一整天都在尝试不同的代码,但是一无所获。
表1:订单
Customer_Id TxnDate Amount
-------------------------------
4001 21-Aug-18 245.99
4002 30-Jan-18 49.99
4003 15-Apr-17 204.87
4001 18-Dec-18 130.88
4004 15-May-17 198.33
4006 4-Feb-17 783.65
表2:客户
Customer_Id AcctOpenDate CustomerFirstName CustomerLastName
--------------------------------------------------------------
4001 7-Jan-16 John Doe
4002 15-Apr-15 Ashley Smith
4003 14-May-14 Carter Jones
4004 17-Sep-16 Ika Gaut
4005 18-Aug-14 Gray Show
4006 25-Oct-15 Kathia Kim
查询:
SELECT
C.CustomerFirstName, C.CustomerLastName, O.Amount
FROM
Customers C
WHERE
Amount = (SELECT MIN(Amount) FROM Orders) Leasttxnamt
INNER JOIN
Customers C ON O.Customer_Id = C.Customer_Id;
当前产生的错误:
第15层状态1的第31行,消息102
'Leasttxnamt'附近的语法不正确
答案 0 :(得分:0)
如果我的理解正确,您正在从“订单”表中查找客户明智的最低金额。如果这是正确的,则可以根据需要使用以下脚本-
这在大多数数据库中都是可行的。
SELECT C.CustomerFirstName, C.CustomerLastName, MIN(Amount) min_amount
FROM Customers C
LEFT JOIN Orders O ON O.Customer_Id = C.Customer_Id
GROUP BY C.CustomerFirstName, C.CustomerLastNam
答案 1 :(得分:0)
尝试以下查询:
SELECT Customers.CustomerFirstName ,Customers.CustomerLastName
FROM Customers
JOIN Orders ON Customers.Customer_Id = Orders.Customer_Id
AND Orders.Amount=(
SELECT min(Amount) FROM Orders
WHERE Customers.Customer_Id = Orders.Customer_Id
)
答案 2 :(得分:0)
尝试一下。
返回交易金额最低的客户的姓氏和名字
select CustomerFirstName,CustomerLastName
from Customer
where Customer_Id in (
SELECT DISTINCT Customer_Id
from Orders
where Amount=(select min(Amount) from orders))