T-SQL递归以便查找一段时间内的事务

发布于 2024-10-07 06:00:16 字数 1590 浏览 3 评论 0原文

我已经尝试过,但我无法弄清楚这一点。我有一个表交易(transaction_ID、transaction_Person_ID、Transaction_Date 等)。 我想要的是返回去年每周超过 3 笔交易的所有 transaction_person_ID。这意味着我必须检查 1-1-10 到 7-1-10,看看某人在这 7 天内是否有超过 3 笔交易,然后检查 2-1-10 到 8-1-10,然后检查 3-1 -10 到 9-1-10 等等 我现在需要使用递归选择,但我所写的内容没有产生正确的时间范围。 到目前为止我写的是这个

WITH Dates AS (
        SELECT
         [Date] = CONVERT(DATETIME,'01/01/2010')
        UNION ALL SELECT
         [Date] = DATEADD(DAY, 1, [Date])
        FROM
         Dates
        WHERE
         Date < '12/31/2010'
)

SELECT transaction_person_Id FROM transactions
JOIN DATES
ON transactions.transaction_date = dates.date
where transactions.Transaction_Date between dateadd(DAYOFYEAR,-7,dates.date) and dates.date
group by transaction_person_Id
having count(transaction_person_ID) >= 4
OPTION (MAXRECURSION 2000)

非常感谢

PS: 简而言之,我需要做的就是

 select transaction_person_ID from transactions
    where Transaction_Date between '2010-01-01' and '2010-01-07'
    group by transaction_person_Id
    having count(transaction_person_ID) >= 4

这个

 select transaction_person_ID from transactions
    where Transaction_Date between '2010-01-02' and '2010-01-08'
    group by transaction_person_Id
    having count(transaction_person_ID) >= 4

。 。 。 。 。 直到它消失之前

 select transaction_person_ID from transactions
    where Transaction_Date between '2010-12-25' and '2010-12-31'
    group by transaction_person_Id
    having count(transaction_person_ID) >= 4

我需要得到这 365 个查询的结果

I have tried but i am not able to figure this out. I have a table transactions (transaction_ID, transaction_Person_ID, Transaction_Date etc).
What i want is to return all the transaction_person_ID's that have more than 3 transactions per week for the last year. That means i have to check for 1-1-10 to 7-1-10 to see if someone had more than 3 transactions for that 7 day period, then for 2-1-10 to 8-1-10 then 3-1-10 to 9-1-10 etc etc.
I now i need to use a recursive select but i what i have writen does not produce the correct time frame.
What i have written so far is this

WITH Dates AS (
        SELECT
         [Date] = CONVERT(DATETIME,'01/01/2010')
        UNION ALL SELECT
         [Date] = DATEADD(DAY, 1, [Date])
        FROM
         Dates
        WHERE
         Date < '12/31/2010'
)

SELECT transaction_person_Id FROM transactions
JOIN DATES
ON transactions.transaction_date = dates.date
where transactions.Transaction_Date between dateadd(DAYOFYEAR,-7,dates.date) and dates.date
group by transaction_person_Id
having count(transaction_person_ID) >= 4
OPTION (MAXRECURSION 2000)

Thanks a lot

PS:
in simple words what i need to do is this

 select transaction_person_ID from transactions
    where Transaction_Date between '2010-01-01' and '2010-01-07'
    group by transaction_person_Id
    having count(transaction_person_ID) >= 4

then

 select transaction_person_ID from transactions
    where Transaction_Date between '2010-01-02' and '2010-01-08'
    group by transaction_person_Id
    having count(transaction_person_ID) >= 4

.
.
.
.
.
until it goes

 select transaction_person_ID from transactions
    where Transaction_Date between '2010-12-25' and '2010-12-31'
    group by transaction_person_Id
    having count(transaction_person_ID) >= 4

i need to have the results of these 365 queries

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小耗子 2024-10-14 06:00:16

这将给出一个包含人员和周的结果集,而不是 360 个结果集

WITH Weeks
     AS (
        SELECT
           CONVERT(DATETIME,'01 Jan 2010') AS WeekStartMidnight, 
           CONVERT(DATETIME,'08 Jan 2010') AS WeekEndMidnight
        UNION ALL
        SELECT
           DATEADD(day, 1, WeekStartMidnight),
           DATEADD(day, 1, WeekEndMidnight)
        FROM
            Weeks
        WHERE
           WeekEndMidnight < '31 Dec 2010'
)
SELECT
   t.transaction_person_Id,
   w.WeekStartMidnight, w.WeekEndMidnight
FROM
   weeks w
   JOIN
   transactions t ON t.Transaction_Date >= w.WeekStartMidnight AND t.Transaction_Date < w.WeekEndMidnight
GROUP BY
   t.transaction_person_Id
HAVING
   count(*) >= 4 --COUNT(t.transaction_person_Id) = same
OPTION
   (MAXRECURSION 365)

如果您想要 360 个结果集,则这是一个循环,在“周”派生表中每行使用 WHILE 或 CURSOR

This will give one result set with person and weeks, rather then 360 results sets

WITH Weeks
     AS (
        SELECT
           CONVERT(DATETIME,'01 Jan 2010') AS WeekStartMidnight, 
           CONVERT(DATETIME,'08 Jan 2010') AS WeekEndMidnight
        UNION ALL
        SELECT
           DATEADD(day, 1, WeekStartMidnight),
           DATEADD(day, 1, WeekEndMidnight)
        FROM
            Weeks
        WHERE
           WeekEndMidnight < '31 Dec 2010'
)
SELECT
   t.transaction_person_Id,
   w.WeekStartMidnight, w.WeekEndMidnight
FROM
   weeks w
   JOIN
   transactions t ON t.Transaction_Date >= w.WeekStartMidnight AND t.Transaction_Date < w.WeekEndMidnight
GROUP BY
   t.transaction_person_Id
HAVING
   count(*) >= 4 --COUNT(t.transaction_person_Id) = same
OPTION
   (MAXRECURSION 365)

If you want 360 results sets, it's a loop using WHILE or a CURSOR per row in the "weeks" derived table

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文