温温酱
--使用group by select cname,left( convert(varchar(7),createdate,120),4), right(convert(varchar(7),createdate,120),2),sum(salaryOneDay) from #tablegroup by cname,convert(varchar(7),createdate,120) --测试脚本CREATE TABLE #table(cid INT IDENTITY(1,1),cname VARCHAR(16),salaryOneDay INT,createdate DATETIME)CREATE TABLE #table2(cname VARCHAR(16),salary INT,years VARCHAR(4),months varchar(2))INSERT INTO #table ( cname, salaryOneDay, createdate ) VALUES ('小明',40,'2012-06-14')INSERT INTO #table ( cname, salaryOneDay, createdate ) VALUES ('小明',30,'2012-07-14')INSERT INTO #table ( cname, salaryOneDay, createdate ) VALUES ('小红',30,'2012-08-14')INSERT INTO #table ( cname, salaryOneDay, createdate ) VALUES ('小红',40,'2012-08-14')select cname,left( convert(varchar(7),createdate,120),4), right(convert(varchar(7),createdate,120),2),sum(salaryOneDay) from #tablegroup by cname,convert(varchar(7),createdate,120)INSERT INTO #table2 ( cname, years, months, salary )select cname,left( convert(varchar(7),createdate,120),4), right(convert(varchar(7),createdate,120),2),sum(salaryOneDay) from #tablegroup by cname,convert(varchar(7),createdate,120)SELECT * FROM #table2DROP TABLE #tableDROP TABLE #table2