2011-09-18 168 views
-1

我知道這是一箇舊的,但真的堅持這一點。子查詢返回多個行

我的子進程內去domething liek這

ALTER PROCEDURE [dbo].[sp_GETSOMETHING] 
    [email protected] nCHAR(10) 
AS 
    SELECT A.TENANT, 
      A.LEASEE, 
      A.SUITEE, 
      A.TRADEDATES, 
      A.Area, 
     A.YTDSalesThisYr 

FROM (

SELECT 

LEAS.TENTID AS TENANT, 
LEAS.LEASID AS LEASEE, 
SUIT.SUITID AS SUITEE, 
TB_EMEA_CM_SHIS.TRADEDATE AS TRADEDATES, 
(SELECT TOP 1 SQFT FROM SSQF INNER JOIN SUIT 
ON (SSQF.BLDGID = SUIT.BLDGID AND SSQF.SUITID = SUIT.SUITID) 
      WHERE SSQF.SQFTTYPE = 'NLA' AND SSQF.EFFDATE <=GETDATE() 
    ORDER BY SSQF.EFFDATE DESC)AS Area, 

--SUM(TB_EMEA_CM_SHIS.AMOUNT) AS YTDSalesThisYr 
(select SUM(A.AMOUNT) FROM TB_EMEA_CM_SHIS A WHERE A.TRADEDATE BETWEEN (
SELECT DATEADD(month,-11,(select TOP 1 ENTITY.YEAREND + '01' from ENTITY 
INNER JOIN BLDG ON ENTITY.ENTITYID = BLDG.ENTITYID)) 
)AND GETDATE()) as YTDSalesThisYr ---- i am getting the problem here.. 

FROM 

dbo.leas 
     inner join TB_EMEA_CM_SHIS on 
      (TB_EMEA_CM_SHIS.LEASID = LEAS.LEASID 
      AND TB_EMEA_CM_SHIS.BLDGID = LEAS.BLDGID) 

INNER JOIN BLDG ON 
      BLDG.BLDGID = LEAS.BLDGID 
INNER JOIN SUIT ON 
      (BLDG.BLDGID = SUIT.BLDGID 
      AND LEAS.SUITID = SUIT.SUITID) 
INNER JOIN ENTITY ON 
      ENTITY.ENTITYID = BLDG.BLDGID 

GROUP BY TB_EMEA_CM_SHIS.LEASID, TB_EMEA_CM_SHIS.BLDGID, 
TB_EMEA_CM_SHIS.TRADEDATE, LEAS.TENTID,LEAS.LEASID,SUIT.SUITID 
)A 

任何方式來重寫子查詢我已經在上面?

在此先感謝

+0

什麼版本的SQL Server和什麼,確切地說是這個問題? – Wil

+2

這完全取決於你如何格式化你自己的代碼,但是如果你發佈它並想獲得一些幫助,你可能會發現格式良好的代碼片段吸引了更多的關注。 –

回答

1

也許改變:

SELECT DATEADD(month,-11,(select TOP 1 ENTITY.YEAREND ... 

...到...

SELECT TOP 1 DATEADD(month,-11,(select TOP 1 ENTITY.YEAREND ... 

但是,所有這些TOP 1到處都是指向東西在你的數據真的搞砸了模型。如果您使用TOP 1而沒有ORDER BY,您希望哪一行?