执行脚本时出错

时间:2018-02-09 16:58:37

标签: sql sql-server

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

错误:{{1}}

如何更改查询以使其正常工作?我需要客户的账户金额。

enter image description here

1 个答案:

答案 0 :(得分:1)

这一点:

 (SELECT Cards.Id, 
               (SELECT [Sum] FROM Accounts WHERE Accounts.CardId = Cards.Id) AS [SumFromAccounts]
        FROM Cards WHERE Clients.Id = Cards.ClientId) AS CardsId

您无法在此类型的选择中返回多个值。

尝试使用APPLY:

    SELECT ClientId,
           CONCAT(FirstName,' ', MiddleName,' ', LastName) as FullName,
           SexId,
           Birthday,
           (CASE WHEN (SELECT COUNT(*) FROM ChildBirthdays WHERE ChildBirthdays.Client_Id = Clients.Id) = 0 THEN 0 ELSE 1 END) AS HasChild,
           CalculatedProperties_OrdersCount,
           CalculatedProperties_LastVisit,
           CalculatedProperties_TotalSpent,
           (CASE WHEN (CalculatedProperties_OrdersCount > 0) THEN CONVERT(decimal(10,2),CalculatedProperties_TotalSpent / CalculatedProperties_OrdersCount) ELSE 0 END) AS AverageSpent,
           Cards.CardsId,
           Cards.[SumFromAccounts]

    FROM Clients

OUTER APPLY (SELECT Cards.Id AS CardsId,  
                   (SELECT [Sum] FROM Accounts WHERE Accounts.CardId = Cards.Id) AS [SumFromAccounts]
            FROM Cards WHERE Clients.Id = Cards.ClientId) AS Cards