2014-11-05 93 views
0

我在創建SQL查詢時遇到了問題。每月2行之間的SQL Server差異值

本應用程序的目標是使兩個值之間的區別定義每月的第一個。

這裏是我已經建立了查詢:

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是每天增加ip每個每ipheril在公園和有計數器(總頁數)

實施例:

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

請編輯您的問題並提供一些示例數據以及期望的結果。 – 2014-11-05 16:09:20

回答

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') 

查詢做一個diff上月

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') 
+0

真的非常感謝,你救了我的一天,那是我尋找的:) – user2798773 2014-11-06 13:26:37

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