34,838
社区成员




declare @sql1 nvarchar(1000)
set @sql1 = 'SELECT [month] ,sum([count]) as age1 FROM (SELECT Convert(nvarchar(2),d.CheckTime,101) as [month] ,COUNT(DATEDIFF(yy, P.Birthday, GETDATE()))as [count]
FROM Deals as D INNER JOIN Houses as H ON D.HouseID = H.HouseID inner join Peoples P on D.BuyID = P.PeopleID '
set @sql1 = @sql1 + ' where Convert(nvarchar(4),d.CheckTime,102) = '''+@year+''' '
if @cishu = '1'
begin
if @term <> ''
set @sql1 = @sql1 + ' and h.XiangMu like ''%'+@term+'%'' '
end
else
begin
if @term <> ''
set @sql1 = @sql1 + ' and (h.XiangMu like ''%'+@term+'%'' or replace((h.lu + h.Nong + ''弄''),'' '','''') like ''%'+@term+'%'' )'
end
set @sql1 = @sql1 + ' group by DATEDIFF(yy, P.Birthday, GETDATE()),Convert(nvarchar(2),d.CheckTime,101) having DATEDIFF(yy, P.Birthday, GETDATE()) < 18) AS 临时表 group by [month]'
if @month <> ''
set @sql1 = @sql1 + ' having [month] = '''+@month+''' '
if @season <> ''
begin
if @season = '1'
set @sql1 = @sql1 + ' having [month] between 1 and 3 '
if @season = '2'
set @sql1 = @sql1 + ' having [month] between 4 and 6 '
if @season = '3'
set @sql1 = @sql1 + ' having [month] between 7 and 9 '
if @season = '4'
set @sql1 = @sql1 + ' having [month] between 10 and 12 '
end
exec sp_executesql @sql1
select a.month,age1,age2
from (select month,age1 from ...) a
join (select month,age2 from ...) b on a.month=b.month
select a.month,b.age1,b.age2
from (select month ,......) a
left join (select month ,......) b on.a.month = b.month