日期:2014-05-17 浏览次数:20568 次
DECLARE m INT,
SET m=month(getdate())+1;
INSERT INTO ycjh_table(productName,accountName,ycjh1,ycjh2,ycjh3,ycjh4)
SELECT * FROM e__jhmx e LEFT JOIN Product p ON p.ProductId=e.c__wlh
LEFT JOIN Account a ON a.accountId =e.c__kh GROUP BY p.ProductName,a.accountName,e.c__ycjh+m?
--我想分组的是用e.c__ycjh3_1,e.c__ycjh3_2,e.c__ycjh3_3,e.c__ycjh3_4,这种有啥好方法吗?
DECLARE @m INT
declare @i int
SET @m=month(getdate())+1;
set @i = 1
declare @sql1 varchar(1000)
declare @sql2 varchar(1000)
declare @sql varchar(8000)
set @sql1 = ''
set @sql2 = ''
set @sql = ''
while @i <= 4
begin
set @sql1 = @sql1 + ',ycjh'+CAST(@i as varchar)
set @sql2 = @sql2 + ',e.c__ycjh'+cast(@m as varchar)+'_'+ CAST(@i as varchar)
set @i = @i + 1
end
set @sql = 'INSERT INTO ycjh_table(productName,accountName' + @sql1 +
' select p.ProductName,a.accountName ' + @sql2 +
' FROM e__jhmx e LEFT JOIN Product p ON p.ProductId=e.c__wlh
LEFT JOIN Account a ON a.accountId =e.c__kh
GROUP BY p.ProductName,a.accountName' + @sql2
--select @sql
exec(@sql)