2013-06-22 220 views
-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 

上面這段代碼工作正常TSQL子查詢相關

現在我想對上述柱變化(名字化名爲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]) 

然後它給了我一個錯誤

子查詢返回更多的T漢一個值

此代碼應該返回多個值

所以,我應該做些什麼>?

+1

歡迎來到StackOverflow!正確格式化您的代碼。 – peterm

回答

2

如果你想要帶回多個結果,你應該考慮使用加入而不是子查詢。一個Inner Join看起來是這樣的:

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

這將產生如可通過從基於所述ON條件(例如,如果有兩排在Employee具有1 BusinessEntityID,並且每個表匹配的行來產生儘可能多的行三行PersonBusinessEntityID爲1,以上將在結果集中產生六行,其中BusinessEntityID爲1)

+0

與此同時,我正在尋找一個答案,根據業務需求使用SubQueries而不是聯接 – OneThinker

+2

@OneThinker - 由於select子句中的子查詢*不能返回多個值,因此必須在「產生我想要的結果「以及」我使用子查詢有一個**業務**要求「 - 因爲我無法理解其中的第二個是如何合理的,我會選擇第一個。 –