如何计算?

时间:2015-05-01 07:33:56

标签: c# sql sql-server count group-by

我希望根据某些条件获取网站ID。条件如下:

  • 我们每隔5分钟就会将数据上传到db。
  • 如果我们在6分钟内没有收到任何数据,我们会考虑丢失1个数据字符串。
  • 就像我们没有收到过去1小时的数据一样,我们可以说缺少10个数据字符串

我想计算两个日期之间每个网站ID缺失字符串的数量。

示例数据库:

Sl_No  Site_Id  Uploaded_Data  Uploaded_Date
-----  -------  -------------  -------------
 1      ST001       xyz        4/30/2015 5:47:25 PM
 2      ST002       xyz        4/30/2015 5:47:25 PM
 3      ST003       xyz        4/30/2015 5:47:25 PM
 4      ST001       xyz        4/30/2015 5:52:36 PM
 5      ST002       xyz        4/30/2015 5:53:20 PM
 6      ST003       xyz        4/30/2015 5:54:25 PM
 7      ST001       xyz        4/30/2015 5:57:15 PM
 8      ST002       xyz        4/30/2015 5:58:25 PM
 9      ST003       xyz        4/30/2015 6:00:25 PM
 10     ST001       xyz        4/30/2015 6:02:05 PM
 11     ST002       xyz        4/30/2015 6:03:30 PM
 12     ST003       xyz        4/30/2015 6:05:25 PM

根据上述条件,我必须得到如下的计数:

SiteID Count
------ -----
ST001   0
ST002   1
ST003   2

请帮我找一个合适的解决方案。我是sql和c#的新手。

3 个答案:

答案 0 :(得分:2)

您可以使用Group By子句通过SQL获取结果。

SELECT SiteID, Count(*) as Count FROM Table WHERE Uploaded_Date BETWEEN Date1 AND Date2

这将根据您的where条件为您提供SiteID和Count的列表。此外,如果您需要找到缺失的计数,可以进行减法以找到缺失的计数。

E.g。如果您试图找到一小时的缺失计数并且您的上传间隔是5分钟,那么您知道计数应该是12,通过简单的数学运算Total Minutes / 5.使用此计数12来减去计数可在表格中找到。

SELECT SiteID, (DATEDIFF ( minute , startdate , enddate ) / 5) - Count(*) as MissingCount FROM Table WHERE Uploaded_Date BETWEEN startdate AND enddate

答案 1 :(得分:2)

您可以ROW_NUMBER()使用PARTITION BY和自我加入来获得结果。像这样的东西。 SQL Fiddle

示例数据

DECLARE @table TABLE(Sl_No  INT, Site_Id  CHAR(5),Uploaded_Data  CHAR(3),Uploaded_Date DATETIME)

insert into @table values

 (1      ,'ST001'       ,'xyz'        ,'4/30/2015 5:47:25 PM'),
 (2      ,'ST002'       ,'xyz'        ,'4/30/2015 5:47:25 PM'),
 (3      ,'ST003'       ,'xyz'        ,'4/30/2015 5:47:25 PM'),
 (4      ,'ST001'       ,'xyz'        ,'4/30/2015 5:52:36 PM'),
 (5      ,'ST002'       ,'xyz'        ,'4/30/2015 5:53:20 PM'),
 (6      ,'ST003'       ,'xyz'        ,'4/30/2015 5:54:25 PM'),
 (7      ,'ST001'       ,'xyz'        ,'4/30/2015 5:57:15 PM'),
 (8      ,'ST002'       ,'xyz'        ,'4/30/2015 5:58:25 PM'),
 (9      ,'ST003'       ,'xyz'        ,'4/30/2015 6:00:25 PM'),
 (10     ,'ST001'       ,'xyz'        ,'4/30/2015 6:02:05 PM'),
 (11     ,'ST002'       ,'xyz'        ,'4/30/2015 6:03:30 PM'),
 (12     ,'ST003'       ,'xyz'        ,'4/30/2015 6:05:25 PM');

<强>查询

DECLARE @startDate DATETIME
DECLARE @endDate DATETIME
SELECT @startDate = '4/30/2015 5:47:25 PM', @endDate = '4/30/2015 6:05:25 PM'
 ;WITH CTE AS 
 (
 SELECT * ,ROW_NUMBER()OVER(PARTITION BY site_id ORDER BY Uploaded_Date) rn
 FROM @table
 WHERE Uploaded_Date Between @startDate AND @endDate
 )
 SELECT C1.site_id,SUM(CASE WHEN DATEDIFF(minute,C1.Uploaded_Date,C2.Uploaded_Date) > 5 THEN  DATEDIFF(minute,C1.Uploaded_Date,C2.Uploaded_Date)/5 ELSE 0 END) as mindiff
 FROM CTE C1
 INNER JOIN CTE C2
 ON C1.rn + 1 = C2.rn
 AND C1.site_id = C2.site_id
 GROUP BY C1.site_id

答案 2 :(得分:0)

尝试这样的事情:

SELECT Site_ID,COUNT(*)
FROM YOURTABLE
WHERE DATEDIFF(MINUTE,Uploaded_Date,GETDATE()) <= 60
GROUP BY Site_ID
HAVING DATEDIFF(MINUTE,MAX(Uploaded_Date),GETDATE()) >= 6

取回所有有一行或多行6分钟或更长时间的网站;以及不超过60分钟的计数。