T-SQL迭代检查历史模式

时间:2011-07-26 15:00:39

标签: tsql iteration

如何根据项目的先前历史进行选择?

例如,我正在尝试选择一个val为1的项目,之前的val值为10,之前的val值为1.唯一应该满足以下数据的itemId是itemId = 3。

    WITH Items (itemId, date, val) AS (
      SELECT 1, CONVERT(datetime, '5/1/2011'), 6 UNION ALL
      SELECT 2, CONVERT(datetime, '5/1/2011'), 5 UNION ALL
      SELECT 3, CONVERT(datetime, '5/1/2011'), 1 UNION ALL
      SELECT 1, CONVERT(datetime, '6/1/2011'), 1 UNION ALL
      SELECT 2, CONVERT(datetime, '6/1/2011'), 10 UNION ALL
      SELECT 3, CONVERT(datetime, '6/1/2011'), 10 UNION ALL
      SELECT 1, CONVERT(datetime, '7/1/2011'), 1 UNION ALL
      SELECT 2, CONVERT(datetime, '7/1/2011'), 1 UNION ALL
      SELECT 3, CONVERT(datetime, '7/1/2011'), 1 
    )
    select distinct itemId from Items where val = 1
    --and a previous date val = 10
    --and a previous previous date val = 1

3 个答案:

答案 0 :(得分:0)

这将得到你所追求的具体答案。但是,不确定它在抽象意义上是多么有用。您是否总是在寻找具有3个值的项目,1-10-1?

SELECT
   A.itemID
FROM
   Items A
   INNER JOIN Items B ON A.itemID = B.itemID AND B.val = 10
   INNER JOIN Items C ON B.itemID = C.itemID AND C.val = 1
WHERE
   A.val = 1

答案 1 :(得分:0)

由于您具有在先前日期的值要求,您必须使用ItemId和日期的ROW_NUM订购。

然后你在Id上加上两个自联接加上RowNum加入到它之前的那个(实际上是前一个日期),然后将WHERE子句添加到1-10 -1

WITH Items_Original (itemId, date, val) AS 
(
    SELECT 1, CONVERT(datetime, '5/1/2011'), 6 UNION ALL
    SELECT 2, CONVERT(datetime, '5/1/2011'), 5 UNION ALL
    SELECT 3, CONVERT(datetime, '5/1/2011'), 1 UNION ALL
    SELECT 1, CONVERT(datetime, '6/1/2011'), 1 UNION ALL
    SELECT 2, CONVERT(datetime, '6/1/2011'), 10 UNION ALL
    SELECT 3, CONVERT(datetime, '6/1/2011'), 10 UNION ALL
    SELECT 1, CONVERT(datetime, '7/1/2011'), 1 UNION ALL
    SELECT 2, CONVERT(datetime, '7/1/2011'), 1 UNION ALL
    SELECT 3, CONVERT(datetime, '7/1/2011'), 1 
),
Items As
(
    Select 
        ROW_NUMBER() OVER(ORDER BY ItemId, Date) AS RowNum, * 
        FROM Items_Original
) 
select * 
from Items I1
    Inner JOIN Items I2
        On I1.itemId = I2.itemId
        And I1.RowNum = I2.RowNum-1
    Inner JOIN Items I3
        On I2.itemId = I3.itemId
        And I2.RowNum = I3.RowNum-1
Where 1=1
    And I1.val = 1
    And I2.val = 10
    And I3.val = 1

答案 2 :(得分:0)

 WITH Items (itemId, date, val) AS (
      SELECT 1, CONVERT(datetime, '5/1/2011'), 6 UNION ALL
      SELECT 2, CONVERT(datetime, '5/1/2011'), 5 UNION ALL
      SELECT 3, CONVERT(datetime, '5/1/2011'), 1 UNION ALL
      SELECT 1, CONVERT(datetime, '6/1/2011'), 1 UNION ALL
      SELECT 2, CONVERT(datetime, '6/1/2011'), 10 UNION ALL
      SELECT 3, CONVERT(datetime, '6/1/2011'), 10 UNION ALL
      SELECT 1, CONVERT(datetime, '7/1/2011'), 1 UNION ALL
      SELECT 2, CONVERT(datetime, '7/1/2011'), 1 UNION ALL
      SELECT 3, CONVERT(datetime, '7/1/2011'), 1 
    ),
cte as
(
    select *, row_number() over(partition by itemId order by date) [rn]
    from Items t
)

select distinct a.itemId
from cte a
join cte b on b.rn = a.rn + 1 and b.itemId = a.itemId
join cte c on c.rn = b.rn + 1 and c.itemId = a.itemId
where a.val = 1 and b.val = 10 and c.val = 1