高效获取最小值、最大值和汇总数据

发布于 2024-10-24 23:12:56 字数 507 浏览 4 评论 0原文

我有一个账户表和一个交易表。在报告中,我需要为每个帐户显示以下内容:

First Purchase Date, 
First Purchase Amount, 
Last Purchase Date, 
Last Purchase Amount, 
# of Purchases, 
Total of All Purchases.

交易表如下所示:

TX_UID
Card_Number
Post_Date
TX_Type
TX_Amount

目前,我继承的查询对于每个元素都有一个子查询。在我看来,必须有一种更有效的方法。我可以使用存储过程来执行此操作,而不是单个查询。

获取单个帐户的所有交易的查询示例如下:

select * from tx_table where card_number = '12345' and TX_Type = 'Purchase'

有什么想法吗?

I have a table of accounts and a table of transactions. In a report I need to show the following for each account:

First Purchase Date, 
First Purchase Amount, 
Last Purchase Date, 
Last Purchase Amount, 
# of Purchases, 
Total of All Purchases.

The transaction table looks like this:

TX_UID
Card_Number
Post_Date
TX_Type
TX_Amount

Currently the query I've inherited has a sub-query for each of these elements. It seems to me that there's got to be a more efficient way. I'm able to use a stored procedure for this and not a single query.

A sample of a query to get all transactions for a single account would be:

select * from tx_table where card_number = '12345' and TX_Type = 'Purchase'

Any ideas?

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

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

发布评论

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

评论(2

单挑你×的.吻 2024-10-31 23:12:56

试试这个:

select tt1.post_date as first_purchase_date,
       tt1.tx_amount as first_purchase_amount,
       tt2.post_date as last_purchase_date,
       tt2.tx_amount as last_purchase_amount,
       tg.pc as purchase_count,
       tg.amount as Total
from (select Card_Number,min(post_date) as  mipd, max(post_date) as mxpd, count(*) as pc, sum(TX_Amount) as Amount from tx_table where TX_Type = 'Purchase' group by card_number) tg
join tx_table tt1 on tg.card_number=tt1.card_number and tg.mipd=tt1.post_date
join tx_table tt2 on tg.card_number=tt2.card_number and tg.mxpd=tt2.post_date
where TX_Type = 'Purchase'

我添加了计数..我第一次没有看到它。

如果您还需要多个 TX_Type 的摘要,则必须从 where 子句中获取它并将其放入组和内部选择连接中。但我猜你只需要购买

try this:

select tt1.post_date as first_purchase_date,
       tt1.tx_amount as first_purchase_amount,
       tt2.post_date as last_purchase_date,
       tt2.tx_amount as last_purchase_amount,
       tg.pc as purchase_count,
       tg.amount as Total
from (select Card_Number,min(post_date) as  mipd, max(post_date) as mxpd, count(*) as pc, sum(TX_Amount) as Amount from tx_table where TX_Type = 'Purchase' group by card_number) tg
join tx_table tt1 on tg.card_number=tt1.card_number and tg.mipd=tt1.post_date
join tx_table tt2 on tg.card_number=tt2.card_number and tg.mxpd=tt2.post_date
where TX_Type = 'Purchase'

I added the count .. I didn't see it first time.

If you need also the summary on multiple TX_Types, you have to take it from the where clause and put it in the group and the inner selection join. But I guess you need only for purchases

古镇旧梦 2024-10-31 23:12:56
;with cte as
(
  select
    Card_Number,
    TX_Type,
    Post_Date,
    TX_Amount,
    row_number() over(partition by TX_Type, Card_Number order by Post_Date asc) as FirstP,
    row_number() over(partition by TX_Type, Card_Number order by Post_Date desc) as LastP
  from tx_table 
)
select 
  F.Post_Date as "First Purchase Date",
  F.TX_Amount as "First Purchase Amount",
  L.Post_Date as "Last Purchase Date", 
  L.TX_Amount as "Last Purchase Amount",
  C.CC as "# of Purchases",
  C.Amount as "Total of All Purchases"
from (select Card_Number, TX_Type, count(*) as CC, sum(TX_Amount) as Amount
      from cte
      group by Card_Number, TX_Type) as C
  inner join cte as F
    on C.Card_Number = F.Card_Number and
       C.TX_Type = F.TX_Type and
       F.FirstP = 1    
  inner join cte as L
    on C.Card_Number = L.Card_Number and
       C.TX_Type = L.TX_Type and
       L.LastP = 1
;with cte as
(
  select
    Card_Number,
    TX_Type,
    Post_Date,
    TX_Amount,
    row_number() over(partition by TX_Type, Card_Number order by Post_Date asc) as FirstP,
    row_number() over(partition by TX_Type, Card_Number order by Post_Date desc) as LastP
  from tx_table 
)
select 
  F.Post_Date as "First Purchase Date",
  F.TX_Amount as "First Purchase Amount",
  L.Post_Date as "Last Purchase Date", 
  L.TX_Amount as "Last Purchase Amount",
  C.CC as "# of Purchases",
  C.Amount as "Total of All Purchases"
from (select Card_Number, TX_Type, count(*) as CC, sum(TX_Amount) as Amount
      from cte
      group by Card_Number, TX_Type) as C
  inner join cte as F
    on C.Card_Number = F.Card_Number and
       C.TX_Type = F.TX_Type and
       F.FirstP = 1    
  inner join cte as L
    on C.Card_Number = L.Card_Number and
       C.TX_Type = L.TX_Type and
       L.LastP = 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文