本帖最后由 cbcman 于 2012-11-09 11:39:03 编辑

解决方案 »

  1.   

    declare @StartDate varchar(20)   --这个外面传进来的值。 
    select ID,SchoolID,FirstDate,TotalMoney 
    from(     select ID,SchoolID,FristDate from table1) a
    LEFT JOIN(     select SchoolID,sum(TotalMoney) as TotalMoney from talbe2  as X inner join table1 as Y  on x.schoolid=y.schoolid 
    where YearMonthDay between Y.FristDate between @StartDate) b 
    on a.SchoolID=b.SchoolID
      

  2.   


    --表ta(date) tb(date)
    select *
    from ta
    where date in (select date from tb group by date)select *
    from ta a
    where exists (select 1 from tb where a.date = date)select a.*
    from ta a join tb b on a.date = b.date-- ...
      

  3.   

    declare @StartDate varchar(20)   --这个外面传进来的值。  
     select ID,SchoolID,FirstDate,(select sum(TotalMoney) as TotalMoney from talbe2   b   where a.SchoolID=b.SchoolID   and YearMonthDay between a.FristDate between @StartDate ) TotalMoney 
    from table1  a 
      

  4.   

    这样试试:declare @StartDate varchar(20)   --这个外面传进来的值。SELECT A1.ID,
    A1.SchoolID,
    A1.FirstDate,
    B1.TotalMoney
    FROM table1 AS A1 WITH(NOLOCK) LEFT JOIN
    (
    SELECT B.SchoolID,
    SUM(B.TotalMoney) AS TotalMoney
    FROM table1 AS A WITH(NOLOCK) INNER JOIN
    table2 AS B WITH(NOLOCK) ON A.SchoolID=A.SchoolID
    WHERE B.YearMonthDay BETWEEN A.FristDate
    AND CAST(@StartDate AS DATETIME)
    GROUP BY B.SchoolID
    ) AS B1 ON A1.SchoolID=B1.SchoolID