TSQL子查询相关

时间:2013-06-22 07:05:59

标签: sql-server tsql subquery

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])

然后它给了我一个错误

  

子查询返回多个值

此代码应返回多个值

那么我应该怎么做呢>?

1 个答案:

答案 0 :(得分:2)

如果您想要带回多个结果,您应该考虑使用 join 而不是子查询。 Inner Join看起来像这样:

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

这将产生尽可能多的行,方法是根据ON条件匹配每个表中的行(例如,如果Employee中有两行,BusinessEntityID为1,以及PersonBusinessEntityID为1的三行,上面将在结果集中生成六行BusinessEntityID为1)