我在创建SQL查询时遇到问题。
本申请的目的是使每个月的第一个定义两个值之间的差异。
以下是我构建的查询:
SELECT
Date,
Compteur - (SELECT MAX(Compteur)
FROM SnmpDataPages
WHERE IP = t1.ip AND ( Date < t1.Date) AND Compteur IS NOT NULL) AS diff,
IP
FROM
SnmpDataPages AS t1
WHERE
Compteur IS NOT NULL
AND Date BETWEEN dateadd(YEAR, -1, CAST(getdate() AS DATE)) AND CAST(getdate() AS DATE)
ORDER BY
Date, diff DESC
返回此输出:
Date Diff IP
---------------------------------
2014-11-04 5075 149.0.15.40
2014-11-04 1623 149.0.19.177
2014-11-04 1264 149.0.19.77
etc.
此请求适用于“每日”差异,但不适用于月度差异..(我每个月搜索差异第一条记录..)并且分组子查询似乎不可能......
表SnmpDataPage
每天递增,parc中的每个外围设备的ip和那里的计数器(总页数)
示例:
id IP Counter Date Model
-----------------------------------------------
28780 100.0.15.51 140064 2014-10-08 Lexmark
28781 100.0.15.53 243617 2014-10-08 Lexmark
28782 100.0.15.55 24101 2014-10-08 Samsung
28783 100.0.15.56 135907 2014-10-08 Brother
44000 100.0.15.51 200000 2014-11-08 Lexmark
44001 100.0.15.53 250000 2014-11-08 Lexmark
44002 100.0.15.55 24200 2014-11-08 Samsung
44003 100.0.15.56 230000 2014-11-08 Brother
我需要的输出:(计数器(M-(M-1)))
date diff IP
-----------------------------------
2014-11-08 59936 100.0.15.51
2014-11-08 6383 100.0.15.53
etc.
答案 0 :(得分:0)
试试这个让我知道。
CREATE TABLE #tmp
(
ID INT,
IP varchar(50),
[counter] INT,
date DATETIME,
model VARCHAR(500)
)
INSERT INTO #tmp ( ID, IP, counter, date, model ) VALUES (1,'100.0.10.11',140064,'2014-10-08','Lexmark' )
INSERT INTO #tmp ( ID, IP, counter, date, model ) VALUES (1,'100.0.10.11',200000,'2014-10-08','Lexmark' )
SELECT * FROM #tmp
SELECT IP,MONTH(date),MAX([counter])-MIN([counter]),model FROM #tmp
GROUP BY IP,MONTH(date),model
答案 1 :(得分:0)
模拟数据
create table #SnmpDataPage (id int, IPaddress varchar(50), TheCounter int, TheDate datetime, Model varchar(50))
insert into #SnmpDataPage values (28780, '100.0.15.51', 140064, '2014-10-08', 'Lexmark')
insert into #SnmpDataPage values (28781, '100.0.15.53', 243617, '2014-10-08', 'Lexmark')
insert into #SnmpDataPage values (28782, '100.0.15.55', 24101, '2014-10-08', 'Samsung')
insert into #SnmpDataPage values (28783, '100.0.15.56', 135907, '2014-10-08', 'Brother')
insert into #SnmpDataPage values (44000, '100.0.15.51', 200000, '2014-11-08', 'Lexmark')
insert into #SnmpDataPage values (44001, '100.0.15.53', 250000, '2014-11-08', 'Lexmark')
insert into #SnmpDataPage values (44002, '100.0.15.55', 24200, '2014-11-08', 'Samsung')
insert into #SnmpDataPage values (44003, '100.0.15.56', 230000, '2014-11-08', 'Brother')
查询上个月做差异
select #SnmpDataPage.TheDate, #SnmpDataPage.IPaddress, #SnmpDataPage.Model,
#SnmpDataPage.TheCounter - Allrecords.TheCounter as TheDifference
from #SnmpDataPage
join ( select TheDate, TheCounter, IPAddress
from #SnmpDataPage
) Allrecords
on #SnmpDataPage.IPaddress = AllRecords.IPaddress
and datediff(month, #SnmpDataPage.TheDate, AllRecords.TheDate) = -1
<强>结果
TheDate IPaddress Model TheDifference
2014-11-08 100.0.15.51 Lexmark 59936
2014-11-08 100.0.15.53 Lexmark 6383
2014-11-08 100.0.15.55 Samsung 99
2014-11-08 100.0.15.56 Brother 94093
这也可以扩展到前几个月。如果我添加以下测试数据,它将在结果中返回8行,显示前4行中10-08和09-08之间的差异:
insert into #SnmpDataPage values (18780, '100.0.15.51', 40064, '2014-09-08', 'Lexmark')
insert into #SnmpDataPage values (18781, '100.0.15.53', 43617, '2014-09-08', 'Lexmark')
insert into #SnmpDataPage values (18782, '100.0.15.55', 4101, '2014-09-08', 'Samsung')
insert into #SnmpDataPage values (18783, '100.0.15.56', 35907, '2014-09-08', 'Brother')