求救SQL语句,怎么实现统计 Insert 表2Select MemberID,Year(AddTime),Month(AddTime),Count(1) from 表1 Group By MemberID,Year(AddTime),Month(AddTime) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 应该有好多中方法。我的方法是在表二中写个插入的触发器。当向表二插入MemberID,Years ,Months时。在触发器中写个select 语句将表一的统计出来再插入进去就可以了。具体的代码你先自己实现吧。如果有问题我可以帮你写一下。 --建立测试环境Create Table 表1(ID Int, MemberID Int, Phone Varchar(15), AddTime Varchar(10))Create Table 表2(MemberID Int, Years Int, Months Int, Sums Int)--插入数据Insert 表1 Values(1,1,'13000000001','2005-5-20')Insert 表1 Values(1,1,'13000000003','2005-5-24')Insert 表1 Values(1,3,'13000000004','2005-5-24')Insert 表1 Values(1,3,'13000000005','2005-6-1')--测试Insert 表2Select MemberID,Year(AddTime),Month(AddTime),Count(1) from 表1 Group By MemberID,Year(AddTime),Month(AddTime)Select * from 表2 --删除测试环境Drop Table 表1,表2--结果/*MemberID Years Months Sums1 2005 5 23 2005 5 13 2005 6 1*/ 建立测试 数据Create Table table1(ID int,MemberID int,Phone varchar(20),AddTime datetime)insert table1 Values(1,1,13000000001,'2005-5-20')insert table1 Values(1,1,13000000003,'2005-5-24')insert table1 Values(1,3,13000000004,'2005-5-24')insert table1 Values(1,3,13000000005,'2005-6-1')Create Table table2(MemberID int,Years varchar(10),Months varchar(10),Sums int )执行语句insert table2(MemberID,Years ,Months,Sums) select memberID,year(addtime),month(addtime),count(1) from table1 group by memberID,year(addtime),month(addtime)select * from table2清除测试结果drop table table1drop table table2 执行SQL2000DTS包调度时报错,请高手指点! 查询刘同学没选的课程的课程号 批量更新问题 高手请进 急求SQL语句,在线等!!!!! SQL中如何使distinct显示所有字段? 晕啊~这两天一分也抢不着,都怪这版的高手太多! 超难度算法,up有分!! SQL 两不同列的表匹配两列 如何优化查询 视图使用union all后出现的奇怪问题(高手请进,点这,说的就是你) 连接本机sqlserver出现"ssl安全性错误" database里的数据库显示"no items"了.怎么回事?
Create Table 表1
(ID Int,
MemberID Int,
Phone Varchar(15),
AddTime Varchar(10))
Create Table 表2
(MemberID Int,
Years Int,
Months Int,
Sums Int)
--插入数据
Insert 表1 Values(1,1,'13000000001','2005-5-20')
Insert 表1 Values(1,1,'13000000003','2005-5-24')
Insert 表1 Values(1,3,'13000000004','2005-5-24')
Insert 表1 Values(1,3,'13000000005','2005-6-1')
--测试
Insert 表2
Select MemberID,Year(AddTime),Month(AddTime),Count(1) from 表1
Group By MemberID,Year(AddTime),Month(AddTime)Select * from 表2
--删除测试环境
Drop Table 表1,表2
--结果
/*
MemberID Years Months Sums
1 2005 5 2
3 2005 5 1
3 2005 6 1
*/
Create Table table1
(ID int,
MemberID int,
Phone varchar(20),
AddTime datetime
)insert table1 Values(1,1,13000000001,'2005-5-20')
insert table1 Values(1,1,13000000003,'2005-5-24')
insert table1 Values(1,3,13000000004,'2005-5-24')
insert table1 Values(1,3,13000000005,'2005-6-1')Create Table table2
(
MemberID int,
Years varchar(10),
Months varchar(10),
Sums int )执行语句insert table2(MemberID,Years ,Months,Sums) select memberID,year(addtime),month(addtime),count(1) from table1 group by memberID,year(addtime),month(addtime)select * from table2清除测试结果
drop table table1
drop table table2