SQL:我需要选择连续日期中3行相同的位置

时间:2015-10-20 08:53:43

标签: mysql sql sql-server

我有一个表,其中包含PersonID,ItemID,Quantity和DateBought作为列。我需要选择PersonID,ItemID和Quantity相同且DateBought连续几天的行。

以下是一些示例数据

PersonID    ItemID    Quantity    DateBought
1           A         1           2015-02-01
1           A         1           2015-02-02
2           B         2           2015-02-01
2           B         1           2015-02-02
3           C         2           2015-02-01
3           C         2           2015-02-01
4           D         1           2015-02-01
4           E         1           2015-02-02
5           G         7           2015-09-21
5           G         7           2015-09-22

在这种情况下,我想选择PersonID = 1和PersonID = 5,没有其他人(2个有差异量,3个是同一天,4个不同的项目。)

7 个答案:

答案 0 :(得分:0)

使用MySQL GROUP BYORDER BY: -

 SELECT * FROM table_name GROUP BY PersonID, ItemID, Quantity ORDER BY DateBought DEsc;

答案 1 :(得分:0)

我认为这是最简单的查询

SELECT * FROM table_name GROUP BY PersonID, ItemID, Quantity ORDER BY DateBought ASC;

答案 2 :(得分:0)

试试这个:

select * from tablename t1 inner join tablename t2 
on t1.personID = t2.personID and t1.ItemID = t2.ItemID and t1.Quantity= t2.Quantity

答案 3 :(得分:0)

WITH t AS (
  SELECT DateBought d,ROW_NUMBER() OVER(ORDER BY DateBought) i
  FROM @d
  GROUP BY DateBought
)
SELECT MIN(d),MAX(d)
FROM t
GROUP BY DATEDIFF(day,i,d)

通过使用这个,你将从DateBought列获得min_date和max_date,只需要通过使用简单条件从表中获取其他值。

答案 4 :(得分:0)

Tabibitosan方法(参见https://community.oracle.com/message/3996302)应该可以解决这个问题......假设您的数据存储在像“#temp”这样的临时表中,SQL将是:

Intent i = new Intent(this, D.class);
finish();
startActivity(i);

答案 5 :(得分:0)

试试这个

select a.* from sample a,sample b where a.personid=b.personid and a.itemid=b.itemid and a.datebought=(b.datebought)-1
union select b.* from sample a,sample b where a.personid=b.personid and a.itemid=b.itemid and b.datebought=(a.datebought)+1;

输出:

PersonID    ItemID    Quantity    DateBought
1           A         1           2015-02-01
1           A         1           2015-02-02
5           G         7           2015-09-21
5           G         7           2015-09-22

答案 6 :(得分:0)

你走了。

if object_id('tempdb..#Test') is not null
    drop table #Test;
select *
into #Test
from (
          select 1 as PersonId, 'A' as ItemId, 1 as Quantity, cast('2015-02-01' as date) as DateBought
union all select 1 as PersonId, 'A' as ItemId, 1 as Quantity, cast('2015-02-02' as date) as DateBought
union all select 2 as PersonId, 'B' as ItemId, 2 as Quantity, cast('2015-02-01' as date) as DateBought
union all select 2 as PersonId, 'B' as ItemId, 1 as Quantity, cast('2015-02-02' as date) as DateBought
union all select 3 as PersonId, 'C' as ItemId, 2 as Quantity, cast('2015-02-01' as date) as DateBought
union all select 3 as PersonId, 'C' as ItemId, 2 as Quantity, cast('2015-02-01' as date) as DateBought
union all select 4 as PersonId, 'D' as ItemId, 1 as Quantity, cast('2015-02-01' as date) as DateBought
union all select 4 as PersonId, 'E' as ItemId, 1 as Quantity, cast('2015-02-02' as date) as DateBought
union all select 5 as PersonId, 'G' as ItemId, 7 as Quantity, cast('2015-09-21' as date) as DateBought
union all select 5 as PersonId, 'G' as ItemId, 7 as Quantity, cast('2015-09-22' as date) as DateBought)a


select PersonId
, ItemId
, Quantity
, DateBought 
from (
        select 
            PersonId
            , ItemId
            , Quantity
            , DateBought
            ,  abs(datediff(DAY, lead(DateBought) over (Partition by PersonId, ItemId, Quantity order by DateBought desc), DateBought)) as [DateDiff]
        from #Test
    ) Results
where Results.[DateDiff]= 1