multiple transactions within a certain time period, limited by date range

Deadly 提交于 2019-12-24 10:00:12

问题


I have a database of transactions, people, transaction dates, items, etc. Each time a person buys an item, the transaction is stored in the table like so:

personNumber, TransactionNumber, TransactionDate, ItemNumber

What I want to do is to find people (personNumber) who, from January 1st 2012(transactionDate) until March 1st 2012 have purchased the same ItemNumber multiple times within 14 days (configurable) or less. I then need to list all those transactions on a report.

Sample data:

personNumber, TransactionNumber, TransactionDate, ItemNumber
1           |               100|      2001-01-31|        200
2           |               101|      2001-02-01|        206
2           |               102|      2001-02-11|        300
1           |               103|      2001-02-09|        200
3           |               104|      2001-01-01|        001
1           |               105|      2001-02-10|        200
3           |               106|      2001-01-03|        001
1           |               107|      2001-02-28|        200

Results:

personNumber, TransactionNumber, TransactionDate, ItemNumber
1           |               100|      2001-01-31|        200
1           |               103|      2001-02-09|        200
1           |               105|      2001-02-10|        200
3           |               104|      2001-01-01|        001
3           |               106|      2001-01-03|        001

How would you go about doing that?

I've tried doing it like so:

select * 
from (
    select personNumber, transactionNumber, transactionDate, itemNumber,
count(*) over (
    partition by personNumber, itemNumber) as boughtSame)
from transactions
where transactionDate between '2001-01-01' and '2001-03-01')t
where boughtSame > 1

and it gets me this:

personNumber, TransactionNumber, TransactionDate, ItemNumber
1           |               100|      2001-01-31|        200
1           |               103|      2001-02-09|        200
1           |               105|      2001-02-10|        200
1           |               107|      2001-02-28|        200
3           |               104|      2001-01-01|        001
3           |               106|      2001-01-03|        001

The issue is that I don't want TransactionNumber 107, since that's not within the 14 days. I'm not sure where to put in that limit of 14 days. I could do a datediff, but where, and over what?


回答1:


Alas, the window functions in SQL Server 2005 just are not quite powerful enough. I would solve this using a correlated subquery.

The correlated subquery counts the number of times that a person purchased the item within 14 days after each purchase (and not counting the first purchase).

select t.*
from (select t.*,
             (select count(*)
              from t t2
              where t2.personnumber = t.personnumber and
                    t2.itemnumber = t.itemnumber and
                    t2.transactionnumber <> t.transactionnumber and
                    t2.transactiondate >= t.transactiondate and 
                    t2.transactiondate < DATEADD(day, 14, t.transactiondate
             ) NumWithin14Days
      from transactions t
      where transactionDate between '2001-01-01' and '2001-03-01'
     ) t
where NumWithin14Days > 0

You may want to put the time limit in the subquery as well.

An index on transactions(personnumber, itemnumber, transactionnumber, itemdate) might help this run much faster.




回答2:


If as your question states you just want to find people (personNumbers) with the specified criteria, you can do a self join and group by:

create table #tx (personNumber int, transactionNumber int, transactionDate dateTime, itemNumber int)
insert into #tx
values
    (1, 100, '2001-01-31', 200),
    (2, 101, '2001-02-01', 206),
    (2, 102, '2001-02-11', 300),
    (1, 103, '2001-02-09', 200),
    (3, 104, '2001-01-01', 001),
    (1, 105, '2001-02-10', 200),
    (3, 106, '2001-01-03', 001),
    (1, 107, '2001-02-28', 200)

declare @days int = 14

select t1.personNumber from #tx t1 inner join #tx t2 on 
    t1.personNumber = t2.personNumber 
    and t1.itemNumber = t2.itemNumber
    and t1.transactionNumber < t2.transactionNumber
    and datediff(day, t1.transactionDate, t2.transactionDate) between 0 and @days
group by t1.personNumber
-- if more than zero joined rows there is more than one transaction in period
having count(t1.personNumber) > 0 

drop table #tx


来源:https://stackoverflow.com/questions/15123687/multiple-transactions-within-a-certain-time-period-limited-by-date-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!