SQL确定是否已经过了多个连续月份

时间:2011-10-31 13:41:13

标签: sql sql-server sql-server-2005 sql-server-2008

我有3列1表示代理ID,1表示它们是哪个级别(初级,中级,高级),并且表示他们达到该级别的月末日期。

如果样本数据如下所示

360123, Beginner, 1/22/2011
360123, Null, 2/22/2011
360123, Beginner, 3/22/2011
360123, Intermediate, 4/22/2011
360123, Beginner, 5/22/2011
360123, Beginner, 6/22/2011

如何设计一个查询,告诉我哪些代理商从3月22日到6月22日连续4个月制定了所有初学者目标?

哇,谢谢大家的帮助!

在第一个月是你达到其中一个目标的任何一个月的6个月内的4个月内怎么样?

4 个答案:

答案 0 :(得分:1)

以下示例数据和示例。基本上,按ID分组并搜索不同月份的计数。

DECLARE @T table (id int, lvl varchar(100), datefield smalldatetime)
INSERT INTO @T
VALUES
(360123, 'Beginner', '1/22/2011'),
(360123, Null, '2/22/2011'),
(360123, 'Beginner', '3/22/2011'),
(360123, 'Intermediate', '4/22/2011'),
(360123, 'Beginner', '5/22/2011'),
(360123, 'Beginner', '6/22/2011')

SELECT ID
FROM @T
WHERE Lvl = 'Beginner'
AND datefield BETWEEN '3/1/2011' AND '6/30/2011 23:59:59'
GROUP BY ID
HAVING COUNT(DISTINCT(MONTH(DateField))) = 4

答案 1 :(得分:0)

(Select id from table_name where date ='6/22/2011' and level ='Beginner')
intersect
(Select id from table_name where date ='5/22/2011' and level ='Beginner')
intersect

(Select id from table_name where date ='4/22/2011' and level ='Beginner')
intersect 

(Select id from table_name where date ='3/22/2011' and level ='Beginner')

答案 2 :(得分:0)

SELECT id
FROM TableX
WHERE level = 'Beginner'
  AND datefield >= '2011-03-22' 
  AND datefield < '2011-06-23'
GROUP BY id
HAVING COUNT(DISTINCT YEAR(datefield), MONTH(datefield)) = 4
   AND ( YEAR(MAX(datefield))*12+MONTH(MAX(datefield)) )
     - ( YEAR(MIN(datefield))*12+MONTH(MIN(datefield)) ) = 4 - 1

答案 3 :(得分:0)

主要技巧是创建“人工”Id以将其用作递归CTE中的锚点:

DECLARE @T TABLE(         Id INT,         Lvl VARCHAR(100),         Datefield SMALLDATETIME);

INSERT  INTO @T
VALUES (360123, 'Beginner', '1/22/2011'),
(360123, NULL, '2/22/2011'),
(360123, 'Beginner', '3/22/2011'),
(360123, 'Intermediate', '4/22/2011'),
(360123, 'Beginner', '12/22/2011'),
(360123, 'Beginner', '01/22/2012');

DECLARE @BeginTime AS DATETIME = '1/22/2011';

DECLARE @EndTime AS DATETIME = '6/22/2012';

WITH   M
AS     (SELECT DISTINCT Id,
                        MONTH(datefield) AS Mth,
                        YEAR(datefield) AS Yr,
                        ROW_NUMBER() OVER (PARTITION BY Id ORDER BY YEAR(datefield), MONTH(datefield)) AS RN
        FROM   @T AS T
        WHERE  Lvl = 'Beginner'
               AND T.Datefield >= @BeginTime
               AND T.Datefield <= @EndTime),
       C (Id, RN, MonthsInARow, Mth, Yr)
AS     (SELECT M.Id,
               RN,
               CAST (1 AS INT),
               Mth,
               Yr
        FROM   M
        WHERE  RN = 1
        UNION ALL
        SELECT M.Id,
               M.RN,
               CASE 
               WHEN M.Mth = C.Mth + 1
                         OR (M.Mth = 1
                             AND C.Mth = 12
                             AND M.Yr = C.yr + 1) THEN C.MonthsInARow + 1 ELSE 1 
               END,
               M.Mth,
               M.Yr
        FROM   M
               INNER JOIN
               C
               ON M.Id = C.Id
                  AND M.RN = C.RN + 1)
SELECT [C].[Id],MAX([C].[MonthsInARow]) MAXMonth
FROM   C
GROUP BY [Id]