-1
SELECT DT.TenantDescription
    ,DT.PropertyNumber
    ,DT.UnitNo
    ,DT.AdressLn1
    ,DT.AddressLn2
    ,DT.AddressSituation
    ,(
        CASE 
            WHEN DT.TransactionCode = 1
                THEN DT.New_TransactionValue
            ELSE 0
            END
        ) AS RentDue
    ,(
        CASE 
            WHEN DT.TransactionCode = 2
                THEN DT.New_TransactionValue
            ELSE 0
            END
        ) AS OTHERSUMSDUE
    ,(
        CASE 
            WHEN DT.TransactionCode = 3
                THEN DT.New_TransactionValue
            ELSE 0
            END
        ) AS ARREARSBFWD
    ,(
        CASE 
            WHEN DT.TransactionCode = 4
                THEN DT.New_TransactionValue
            ELSE 0
            END
        ) AS ARREARSCFWD
    ,(
        CASE 
            WHEN DT.TransactionCode = 5
                THEN DT.New_TransactionValue
            ELSE 0
            END
        ) AS IRRECOVERABLERENT
    ,(
        CASE 
            WHEN DT.TransactionCode > 5
                THEN DT.New_TransactionValue
            ELSE 0
            END
        ) AS Expenditure
FROM (
    SELECT * * (
            SELECT New_TenantNameOnly
            FROM New_Rentmaster Rm
            WHERE Rm.New_rentmasterId = PD.new_rentmasterid
            ) AS TenantDescription
        ,* * (
            SELECT [New_UnitNumber]
            FROM new_propertyunits NPU
            WHERE NPU.[New_propertyunitsId] = PD.[new_unitnumberid]
            ) AS UnitNo
        ,(
            SELECT New_AddressLine1
            FROM New_address
            WHERE New_addressId = (
                    SELECT New_addressid
                    FROM New_PropertyMaster PM
                    WHERE PM.new_propertymasterid = PD.[new_propertynumbernameid]
                    )
            ) AS AdressLn1
        ,(
            SELECT New_AddressLine2
            FROM New_address
            WHERE New_addressId = (
                    SELECT New_addressid
                    FROM New_PropertyMaster PM
                    WHERE PM.new_propertymasterid = PD.[new_propertynumbernameid]
                    )
            ) AS AddressLn2
        ,(
            SELECT TT.[New_TransactionTypeCode] AS TransactionCode
            FROM New_transactiontype TT
            WHERE [New_transactiontypeId] = PD.[new_transactioncodenameid]
            ) AS TransactionCode
        ,(
            SELECT New_PropertyNumber
            FROM New_PropertyMaster PM
            WHERE PM.new_propertymasterid = PD.[new_propertynumbernameid]
            ) AS PropertyNumber
        ,(
            SELECT New_UnitAddressIdName
            FROM New_propertyunits NPU
            WHERE NPU.[New_propertyunitsId] = PD.[new_unitnumberid]
            ) AS AddressSituation
        ,PD.New_TransactionValue
    FROM New_PropertyDetails PD
    ) AS DT

上面的代码工作正常

现在我想对上面的列进行更改(名称别名为TenantDescription),我想用下面的代码替换上面的列

Select New_TenantNameOnly from New_Rentmaster Rm    
  New_TenantNumber IN 
     (Select MAX(New_TenantNumber) from new_rentmaster GROUP BY [New_Unit_No],[New_Propety_Number])

然后它给了我一个错误

子查询返回多个值

此代码应该返回多个值

那我该怎么办>??

4

1 回答 1

2

如果你想返回多个结果,你应该考虑使用连接而不是子查询。内连接看起来像这样:

SELECT *
FROM HumanResources.Employee AS e
INNER JOIN Person.Person AS p
ON e.BusinessEntityID = p.BusinessEntityID

这将根据条件匹配每个表中的行来生成尽可能多的行ON(例如,如果有两行在1 中EmployeeBusinessEntityID三行在Person1BusinessEntityID中,以上将在结果集中产生六行哪里BusinessEntityID是 1)

于 2013-06-22T07:35:55.310 回答