请教外联连接SQL语句问题
本帖最后由 cbcman 于 2012-11-09 11:39:03 编辑 请教一下,从a集合里查询的值,如何放到b集合里面使用作为条件呢??
例如下面的代码:a集合里面拿到一个字段名是 FirstDate 的日期(不只一条记录),我想把这些字段值放到b集合里面作为筛选条件,下面代码肯定是错的,有办法实现吗??谢谢
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
where YearMonthDay between a.FristDate between @StartDate
) b on a.SchoolID=b.SchoolID
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
--表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
-- ...
declare @StartDate varchar(20) --这个外面传进来的值。
SELECTA1.ID,
A1.SchoolID,
A1.FirstDate,
B1.TotalMoney
FROMtable1 AS A1 WITH(NOLOCK) LEFT JOIN
(
SELECTB.SchoolID,
SUM(B.TotalMoney) AS TotalMoney
FROMtable1 AS A WITH(NOLOCK) INNER JOIN
table2 AS B WITH(NOLOCK) ON A.SchoolID=A.SchoolID
WHEREB.YearMonthDay BETWEEN A.FristDate
ANDCAST(@StartDate AS DATETIME)
GROUP BY B.SchoolID
) AS B1 ON A1.SchoolID=B1.SchoolID