2 回答
TA贡献1752条经验 获得超4个赞
--使用group by
select cname,left( convert(varchar(7),createdate,120),4), right(convert(varchar(7),createdate,120),2),sum(salaryOneDay) from #table
group 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 #table
group 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 #table
group by cname,convert(varchar(7),createdate,120)
SELECT * FROM #table2
DROP TABLE #table
DROP TABLE #table2
- 2 回答
- 0 关注
- 626 浏览
添加回答
举报