问题是这样的( 表一 如下 ):
id ip user time mac
1 1.1.1.1 a 2015-6-6 0000-1111-2222-3333
1 1.1.1.1 a 2015-6-6 9999-0000-4444-5555
1 2.2.2.2 a 2015-6-6 7777-1111-2222-66666
1 2.2.2.2 a 2015-6-6 8888-9999-2222-00000
1 7.7.7.7 b 2015-6-6 6666-5555-2222-6666
1 8.8.8.8 b 2015-6-6 33333-99999-2222-2222
表二 数据如下:
id ip time pv
1 1.1.1.1 2015-6-6 123
2 2.2.2.2 2015-6-6 456
3 7.7.7.7 2015-6-6 222
4 8.8.8.8 2015-6-6 323
通过第一表,第二表,ip去重复后,关联表查询SQL这样写的:
select T1.ip,count(*) as macs,T2.pv from mactj T1 JOIN pvip T2 ON T1.ip=T2.ip and T1.[macdate]=T2.[tjtime] where macdate)='时间' group by T1.ip,T2.pv ORDER BY macs desc
得到结果如下:
id ip user macs pvs
1 1.1.1.1 a 2 123
1 2.2.2.2 a 2 456
1 7.7.7.7 b 1 222
1 8.8.8.8 b 1 333
现在想把这个结果,每天早晨通过计划,把前一天这样的数据写到表三(那么在查询时 macdate=' 时间 ' 这个时间变量要用前一天的)
想通过SQ计划定时调用存储过程处理,但是不清楚具体写,麻烦指点,谢谢!
------解决思路----------------------
CREATE PROC 存储过程名
AS
INSERT INTO 表三
select T1.ip,count(*) as macs,T2.pv
from mactj T1 JOIN pvip T2 ON T1.ip=T2.ip and T1.[macdate]=T2.[tjtime]
where macdate=CONVERT(VARCHAR(10),GETDATE()-1,120)--这个是前一开
group by T1.ip,T2.pv
ORDER BY macs desc
------解决思路----------------------
写完存储过程后,需要定时自动执行SQL存储过程
1、打开SQL Server Management Studio,SQL Server代理--作业--点右键--新建作业
2、作业的名称取为:test,并进行说明,若作业的数量太多,以方便区别; 这里写上定时执行存储过程Pr_test 确定保存.
3、点击常规下面的步骤,按箭头指示,新建步骤.
4、取名步骤名称:exec pr_test 注意选择存储过程所在的数据库 录入需要执行的命令:exec pr_test ,确定保存.
若有其它参数命令也可以编写
5、接下来设置计划,自动定时执行的时间. 新建计划,进放计划属性设置窗口.
6、依然要填写计划的名称:exec pr_test 计划类型:重复计划 时间频率:每月的26日的12点30分自动执行一次
持续时间:无结束日期 确定保存.到此基本功能已经设置完成,额外其它功能可以根据需要进行增加.
http://jingyan.baidu.com/article/e9fb46e19a0b0c7520f76679.html
------解决思路----------------------
CREATE TABLE #t_TB ([id] int,[ip] NVARCHAR(10),[user] NVARCHAR(10),[time] DATETIME,[mac] NVARCHAR(40));
INSERT INTO #t_TB VALUES
('1','1.1.1.1','a','2015-6-6','0000-1111-2222-3333'),
('1','1.1.1.1','a','2015-6-6','9999-0000-4444-5555'),
('1','2.2.2.2','a','2015-6-6','7777-1111-2222-66666'),
('1','2.2.2.2','a','2015-6-6','8888-9999-2222-00000'),
('1','7.7.7.7','b','2015-6-6','6666-5555-2222-6666'),
('1','8.8.8.8','b','2015-6-6','33333-99999-2222-2222');
--SELECT * FROM #t_TB;
CREATE TABLE #t_TB2 ([id] int,[ip] NVARCHAR(10),[time] DATETIME,[pv] NVARCHAR(10));
INSERT INTO #t_TB2 VALUES
('1','1.1.1.1','2015-6-6','123'),
('2','2.2.2.2','2015-6-6','456'),
('3','7.7.7.7','2015-6-6','222'),
('4','8.8.8.8','2015-6-6','323');
--SELECT * FROM #t_TB2;
IF OBJECT_ID('dbo.p_Test') IS NOT NULL
BEGIN
DROP PROCEDURE dbo.p_Test;
END
GO
CREATE PROCEDURE dbo.p_Test
AS
BEGIN
IF OBJECT_ID('dbo.T3') IS NULL
BEGIN
SELECT
A.[ID]
, A.[IP]
, A.[USER]
, COUNT(1) OVER(PARTITION BY A.[ID],A.[IP],A.[USER],A.[time] ORDER BY A.[ID],A.[IP],A.[USER],A.[time]) AS [MAC]
, B.[PV]
INTO T3
FROM #t_TB A
INNER JOIN #t_TB2 B ON A.[ID] = B.[ID] AND A.[time] = B.[time]
WHERE DATEADD(DAY,A.[time],GETDATE())=1
;
END
ELSE
BEGIN
INSERT INTO T3( [ID], [IP],[USER],[MAC],[PV])
SELECT
A.[ID]
, A.[IP]
, A.[USER]
, COUNT(1) OVER(PARTITION BY A.[ID],A.[IP],A.[USER],A.[time] ORDER BY A.[ID],A.[IP],A.[USER],A.[time]) AS MAC
, B.PV
FROM #t_TB A
INNER JOIN #t_TB2 B ON A.[ID] = B.[ID] AND A.[time] = B.[time]
WHERE DATEADD(DAY,A.[time],GETDATE())=1
;
END
END