子查询返回多行

时间:2011-09-18 21:56:03

标签: sql-server

我知道这是一个旧的,但真的坚持这个。

我的子过程就是这个谎言。

   ALTER PROCEDURE [dbo].[sp_GETSOMETHING]
    --@OrgID 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 

有什么方法可以重写我最顶层的子查询吗?

提前致谢

1 个答案:

答案 0 :(得分: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,您期望哪一行?