仅当上个月有一条记录时才检索去年的数据

发布于 2024-12-22 22:32:42 字数 250 浏览 5 评论 0原文

我有两个表

USERTRANSACTION

我想从 TRANSACTION 表中选择过去一年的数据,如果 USER > 上个月进行的任何交易

USERTRANSACTION 表都使用 USER_ID 连接,

这将如何实现?

I have two tables

USER and TRANSACTION

i want to choose the data from TRANSACTION table for last one year if the USER has made any transaction in the last month

USER and TRANSACTION table are connected using USER_ID

how can it will be achieved?

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

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

发布评论

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

评论(2

无悔心 2024-12-29 22:32:42
select 
  trx_detail_1,
  trx_detail_2
from ( 
  select sum(
           case when trx_date > add_months(sysdate, -1) then 1 else 0 end
         ) over (partition by user_id) sum_user_last_month,
         trx_detail_1,
         trx_detail_2
     from 
         transaction
     where
         trx_date > add_months(sysdate, -12)
)
where 
     sum_user_last_month > 0;
select 
  trx_detail_1,
  trx_detail_2
from ( 
  select sum(
           case when trx_date > add_months(sysdate, -1) then 1 else 0 end
         ) over (partition by user_id) sum_user_last_month,
         trx_detail_1,
         trx_detail_2
     from 
         transaction
     where
         trx_date > add_months(sysdate, -12)
)
where 
     sum_user_last_month > 0;
长伴 2024-12-29 22:32:42

列出上个月活跃用户去年的交易:(

您不需要 USERS 表)

SELECT * 
FROM
TRANSACTIONS
WHERE transaction_date > sysdate - interval '1' year
  and user_id in 
    (select user_id
     from TRANSACTIONS
     where transaction_date > sysdate - interval '1' month);

list last year of transactions for users active in the last month:

(you don't need USERS table)

SELECT * 
FROM
TRANSACTIONS
WHERE transaction_date > sysdate - interval '1' year
  and user_id in 
    (select user_id
     from TRANSACTIONS
     where transaction_date > sysdate - interval '1' month);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文