日期:2014-05-18 浏览次数:20566 次
--用户表 CREATE TABLE Test_user ( UserID varchar(50) NOT NULL, UserName varchar(50) NOT NULL, PRIMARY KEY (UserID) ) --服务表 CREATE TABLE Test_Services ( billno INT NOT NULL IDENTITY (1,1), UserID VARCHAR(50) NOT NULL, CreateTM VARCHAR(50), PRIMARY KEY (billno) ) -- 回访表 CREATE TABLE Test_Visit ( billno INT NOT NULL IDENTITY (1,1), UserID VARCHAR(50) NOT NULL, CreateTM VARCHAR(50), PRIMARY KEY (billno) ) -- 插入回访数据 INSERT INTO Test_user (UserID,UserName) VALUES('XiaoE','小二') INSERT INTO Test_user (UserID,UserName) VALUES('ZhangS','张三') INSERT INTO Test_user (UserID,UserName) VALUES('Lis','李四') INSERT INTO Test_Services (UserID,CreateTM) VALUES('XiaoE','2012-03-21 16:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('XiaoE','2012-03-22 16:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('XiaoE','2012-03-23 16:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('XiaoE','2012-03-24 16:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('XiaoE','2012-03-25 16:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('XiaoE','2012-03-26 16:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('XiaoE','2012-03-27 16:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('XiaoE','2012-03-21 16:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('XiaoE','2012-03-22 15:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('XiaoE','2012-03-23 15:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('XiaoE','2012-03-24 15:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('XiaoE','2012-03-25 15:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('XiaoE','2012-03-26 15:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('ZhangS','2012-03-21 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('ZhangS','2012-03-21 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('ZhangS','2012-03-21 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('ZhangS','2012-03-21 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('ZhangS','2012-03-23 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('ZhangS','2012-03-22 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('ZhangS','2012-03-23 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('ZhangS','2012-03-22 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('ZhangS','2012-03-24 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('ZhangS','2012-03-24 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('ZhangS','2012-03-24 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('ZhangS','2012-03-24 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('Lis','2012-03-24 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('Lis','2012-03-24 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('Lis','2012-03-24 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('Lis','2012-03-24 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('Lis','2012-03-25 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('Lis','2012-03-26 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('Lis','2012-03-27 10:04:57') INSERT INTO Test_Services (UserID,CreateTM) VALUES('Lis','2012-03-28 10:04:57')
insert into Test_Visit select distinct b.* from Test_user a cross apply(select top percent 40 * from Test_Services where U