3

在上一个问题(请参阅:SQL Keeping count of occurrences)中,我需要计算一个变量的出现次数。

提供的代码如下:

SELECT 
    [Date], Code, 
    [Count] = COUNT(*) OVER (PARTITION BY Code ORDER BY [Date] ROWS UNBOUNDED PRECEDING)
FROM dbo.YourTable
ORDER BY [Date];

但是,现在我需要对该代码进行改进:

假设我有下表:

   Date          | Code
   ------------------------
   2010/01/01    | 25
   2010/01/01    | 22
   2010/01/01    | 23
   2010/01/01    | 25
   2010/01/02    | 23
   2010/01/02    | 23
   2010/01/03    | 23
   2010/01/04    | 23
   2010/01/05    | 23
   2010/01/06    | 23
   2010/01/07    | 23
   .....
   2013/03/02    | 21

现在,我需要计算特定时间段内出现的次数。所需的输出如下(为简单起见,假设时间范围为 n=2 天)

    Date         | Code  |  Occurrences
   ------------------------------------
   2010/01/01    | 25    |      1
   2010/01/01    | 22    |      1
   2010/01/01    | 23    |      1
   2010/01/01    | 25    |      2
   2010/01/02    | 23    |      2
   2010/01/02    | 23    |      3
   2010/01/03    | 23    |      3 -> We are not considering the occurence in 2011/01/01 as it is out of the scope now
   2010/01/04    | 23    |      2 -> Considers only occurrences in 01/03 and 01/04 
   2010/01/05    | 23    |      2
   2010/01/06    | 23    |      2
   2010/01/07    | 23    |      2
   .....
   2013/03/02    |  21   |      1

也就是说,我需要知道代码“x”在过去“n”个月内出现在我的表中的次数。

这是在 SQL Server 2012 中运行的。

先感谢您。

4

4 回答 4

3

将选项与CTECROSS APPLY运算符和ROW_NUMBER排名函数一起使用

 ;WITH cte AS
 (
  SELECT [Date], Code
  FROM dbo.YourTable  
  GROUP BY [Date], Code
  )
  SELECT c.Date, c.Code, o.Occurrences
  FROM cte c 
    CROSS APPLY (
                 SELECT t2.[Date], t2.Code,
                        ROW_NUMBER() OVER(PARTITION BY c.[Date] ORDER BY t2.[Date]) AS Occurrences        
                 FROM dbo.YourTable t2
                 WHERE c.Code = t2.Code
                   AND DATEDIFF(day, t2.[Date], c.[Date]) BETWEEN 0 AND 1
                 ) o
  WHERE c.Code = o.Code AND c.[Date] = o.[Date] 
  ORDER BY c.[Date]

SQLFiddle上的演示

为了提高性能,请使用此索引

CREATE INDEX x ON dbo.YourTable([Date], Code)
于 2013-04-09T07:50:48.123 回答
0

试试这个

     SELECT * FROM 
            (SELECT 
            ROW_NUMBER() OVER (PARTITION BY Code,[Date] ORDER BY [Date]) AS NO,
            [Date], Code, 
            COUNT(1) OVER (PARTITION BY Code ,[Date]) AS [Count] 
        FROM dbo.YourTable Where date between dateadd(m, -n, GETDATE()) and GETDATE() ) 
AS T1 WHERE No = 1
        ORDER BY [Date];

或者试试这个。

 SELECT * FROM 
                (SELECT 
                ROW_NUMBER() OVER (PARTITION BY Code,[Date] ORDER BY [Date]) AS NO,
                [Date], Code, 
                COUNT(1) OVER (PARTITION BY Code ,[Date]) AS [Count] 
            FROM dbo.YourTable Where 
    Code = 25 And
    date between dateadd(m, -n, GETDATE()) and GETDATE() ) AS T1 WHERE No = 1
            ORDER BY [Date];
于 2013-04-08T10:26:00.040 回答
0

请试试:

SELECT 
    *, 
    ROW_NUMBER() OVER (PARTITION BY Code ORDER BY Code) Occurrences 
FROM YourTable
ORDER BY Occurrences
于 2013-04-08T10:34:02.593 回答
0

试试这个查询

查询 1

select 
  count(*) as CountOccurance,
  code
from 
  tbl 
where 
  code = 21 and 
  date between dateadd(m, -5, SYSDATETIME ()) and SYSDATETIME ()
group by 
  code

结果

| COUNTOCCURANCE | CODE |
-------------------------
|              1 |   21 |

如果要计算所有代码值,请删除 where 子句。

SQLFIDDLE

希望这可以帮助

于 2013-04-08T10:35:09.297 回答