组合两个已经运行的 ORACLE 查询

发布于 2024-12-03 03:51:08 字数 1432 浏览 1 评论 0原文

我想就以下两个问题寻求帮助。他们单独工作就像一个魅力,现在我希望他们一起工作。

它们都提供会员卡数据库。

第一个根据用户的唯一卡号(%1111%%2222%%3333%)获取与用户相关的各种信息。下面的示例)并返回提供卡号的按照自定义顺序的行。

SELECT cardnumber, first_name || ' ' || last_name 
FROM (
     SELECT cardnumber, first_name, last_name, c.OrderNo
     FROM ag_cardholder ch, (SELECT '%1111%' cardmask, 1 OrderNo from dual
                        UNION ALL
                        SELECT '%2222%', 2 OrderNo from dual
                        UNION ALL
                        SELECT '%3333%', 3 OrderNo from dual
                        ) c
     WHERE ch.cardnumber LIKE c.cardmask
     Order by c.OrderNo
) t

第二个代码根据卡号和类型返回卡的当前积分余额最后用于写入点的终端的在卡上。有两种类型的终端对实际余额的计算方式不同(其中一种需要余额列中的数字,另一种需要求和 余额和总积分列)。

SELECT *
FROM
(SELECT
cardnumber,
(SELECT last_name || ' ' || first_name FROM ag_cardholder WHERE cardnumber = '1111'),
CASE
WHEN terminal_id LIKE 'AGHUPR9%' THEN card_balance
WHEN terminal_id LIKE 'AGHUPR7%' THEN card_balance + total_points
END
FROM ag_tranzakcio
WHERE cardnumber = '1111'
ORDER BY tran_date DESC)
WHERE ROWNUM = 1

我想要是一个执行这两项任务的查询:按提供的顺序获取卡号,并按相同的顺序返回一些用户信息以及当前余额。我们将非常感谢您的帮助。

更新:不幸的是,我对 ORACLE 还不够熟悉,甚至无法想出一个反复试验的解决方案,所以我现在依赖于您的洞察力。

I'd like to ask for help with the two queries below. They work like a charm separately, now I want them to work together.

Both of them serve a loyalty card database.

The first one gets various information related to a user based on their unique cardnumber (%1111%, %2222%, %3333% in the example below) and returns the rows in the custom order that the cardnumbers were provided in.

SELECT cardnumber, first_name || ' ' || last_name 
FROM (
     SELECT cardnumber, first_name, last_name, c.OrderNo
     FROM ag_cardholder ch, (SELECT '%1111%' cardmask, 1 OrderNo from dual
                        UNION ALL
                        SELECT '%2222%', 2 OrderNo from dual
                        UNION ALL
                        SELECT '%3333%', 3 OrderNo from dual
                        ) c
     WHERE ch.cardnumber LIKE c.cardmask
     Order by c.OrderNo
) t

The second code returns the current point balance of a card based on the cardnumber and the type of the terminal last used to write the points on the card. There are two types of terminals that calculate the actual balance differently (one of them needs the number in the balance column, the other needs to sum the balance and total_points columns).

SELECT *
FROM
(SELECT
cardnumber,
(SELECT last_name || ' ' || first_name FROM ag_cardholder WHERE cardnumber = '1111'),
CASE
WHEN terminal_id LIKE 'AGHUPR9%' THEN card_balance
WHEN terminal_id LIKE 'AGHUPR7%' THEN card_balance + total_points
END
FROM ag_tranzakcio
WHERE cardnumber = '1111'
ORDER BY tran_date DESC)
WHERE ROWNUM = 1

What I want is a query that does both of these tasks: gets the cardnumbers in the provided order and returns some user information along with the current balance in the same order. Your help would be much appreciated.

UPDATE: Unfortunately I'm not familiar enough with ORACLE to come up with even a trial-and-error solution so I depend on your insight now.

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

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

发布评论

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

评论(2

给妤﹃绝世温柔 2024-12-10 03:51:08
SELECT cardnumber, name, balance, OrderNo
FROM
(
    SELECT 
    ch.cardnumber cardnumber, 
    ch.first_name || ' ' || ch.last_name name, 
    CASE WHEN t.terminal_id LIKE 'AGHUPR9%' THEN t.card_balance 
         WHEN t.terminal_id LIKE 'AGHUPR7%' THEN t.card_balance + t.total_points 
    END balance,
    rank () over (partition by ch.cardnumber order by t.tran_date DESC) rank,
    c.OrderNo as OrderNo
    FROM ag_cardholder ch
    JOIN (SELECT '%1111%' cardmask, 1 OrderNo from dual
                                UNION ALL
                                SELECT '%2222%', 2 OrderNo from dual
                                UNION ALL
                                SELECT '%3333%', 3 OrderNo from dual
                                ) c ON ch.cardnumber like c.cardmask
    JOIN ag_tranzakcio t ON ch.cardnumber = t.cardnumber 
)
WHERE rank = 1
ORDER BY OrderNo

编辑:假设 ag_tranzakcio.id 字段是一个可靠的递增键,这可以工作:

SELECT cardnumber, name, balance, OrderNo
FROM
(
    SELECT 
    ch.cardnumber cardnumber, 
    ch.first_name || ' ' || ch.last_name name, 
    CASE WHEN t.terminal_id LIKE 'AGHUPR9%' THEN t.card_balance 
         WHEN t.terminal_id LIKE 'AGHUPR7%' THEN t.card_balance + t.total_points 
    END balance,
    rank () over (partition by ch.cardnumber order by t.id DESC) rank,
    c.OrderNo as OrderNo
    FROM ag_cardholder ch
    JOIN (SELECT '%1111%' cardmask, 1 OrderNo from dual
                                UNION ALL
                                SELECT '%2222%', 2 OrderNo from dual
                                UNION ALL
                                SELECT '%3333%', 3 OrderNo from dual
                                ) c ON ch.cardnumber like c.cardmask
    JOIN ag_tranzakcio t ON ch.cardnumber = t.cardnumber 
)
WHERE rank = 1
ORDER BY OrderNo
SELECT cardnumber, name, balance, OrderNo
FROM
(
    SELECT 
    ch.cardnumber cardnumber, 
    ch.first_name || ' ' || ch.last_name name, 
    CASE WHEN t.terminal_id LIKE 'AGHUPR9%' THEN t.card_balance 
         WHEN t.terminal_id LIKE 'AGHUPR7%' THEN t.card_balance + t.total_points 
    END balance,
    rank () over (partition by ch.cardnumber order by t.tran_date DESC) rank,
    c.OrderNo as OrderNo
    FROM ag_cardholder ch
    JOIN (SELECT '%1111%' cardmask, 1 OrderNo from dual
                                UNION ALL
                                SELECT '%2222%', 2 OrderNo from dual
                                UNION ALL
                                SELECT '%3333%', 3 OrderNo from dual
                                ) c ON ch.cardnumber like c.cardmask
    JOIN ag_tranzakcio t ON ch.cardnumber = t.cardnumber 
)
WHERE rank = 1
ORDER BY OrderNo

EDIT: Assuming the ag_tranzakcio.id field is a reliable incrementing key, this could work:

SELECT cardnumber, name, balance, OrderNo
FROM
(
    SELECT 
    ch.cardnumber cardnumber, 
    ch.first_name || ' ' || ch.last_name name, 
    CASE WHEN t.terminal_id LIKE 'AGHUPR9%' THEN t.card_balance 
         WHEN t.terminal_id LIKE 'AGHUPR7%' THEN t.card_balance + t.total_points 
    END balance,
    rank () over (partition by ch.cardnumber order by t.id DESC) rank,
    c.OrderNo as OrderNo
    FROM ag_cardholder ch
    JOIN (SELECT '%1111%' cardmask, 1 OrderNo from dual
                                UNION ALL
                                SELECT '%2222%', 2 OrderNo from dual
                                UNION ALL
                                SELECT '%3333%', 3 OrderNo from dual
                                ) c ON ch.cardnumber like c.cardmask
    JOIN ag_tranzakcio t ON ch.cardnumber = t.cardnumber 
)
WHERE rank = 1
ORDER BY OrderNo
心如荒岛 2024-12-10 03:51:08

修改后的要求是仅返回每张卡的最近交易。这可以通过使用分析函数 RANK() 对交易进行排序,然后应用包装查询来过滤计算出的排名来完成。

select cardnumber
       , card_holder
       , txn_amount
from 
    (
    SELECT ch.cardnumber
           , ch.first_name || ' ' || ch.last_name  as card_holder
           , CASE
                WHEN txn.terminal_id LIKE 'AGHUPR9%' THEN txn.card_balance
                WHEN txn.terminal_id LIKE 'AGHUPR7%' THEN txn.card_balance + txn.total_points
            END as txn_amount
           , rank () over (partition by ch.cardnumber order by txn.tran_date DESC) as rnk       
            , c.orderno
    FROM ag_cardholder ch
         join    (SELECT '%1111%' cardmask, 1 OrderNo from dual
                            UNION ALL
                            SELECT '%2222%', 2 OrderNo from dual
                            UNION ALL
                            SELECT '%3333%', 3 OrderNo from dual
                            ) c 
            on (ch.cardnumber LIKE c.cardmask)
         join ag_tranzakcio txn
            on (ch.cardnumber = txn.cardnumber)
    )        
where rnk=1       
order by orderno, cardnumber  
/

The revised requirement is to return only the most recent transaction for each card. This can be done by using the analytic function RANK() to sort the transactions, and then applying a wrapping query to filter on the calculated rank.

select cardnumber
       , card_holder
       , txn_amount
from 
    (
    SELECT ch.cardnumber
           , ch.first_name || ' ' || ch.last_name  as card_holder
           , CASE
                WHEN txn.terminal_id LIKE 'AGHUPR9%' THEN txn.card_balance
                WHEN txn.terminal_id LIKE 'AGHUPR7%' THEN txn.card_balance + txn.total_points
            END as txn_amount
           , rank () over (partition by ch.cardnumber order by txn.tran_date DESC) as rnk       
            , c.orderno
    FROM ag_cardholder ch
         join    (SELECT '%1111%' cardmask, 1 OrderNo from dual
                            UNION ALL
                            SELECT '%2222%', 2 OrderNo from dual
                            UNION ALL
                            SELECT '%3333%', 3 OrderNo from dual
                            ) c 
            on (ch.cardnumber LIKE c.cardmask)
         join ag_tranzakcio txn
            on (ch.cardnumber = txn.cardnumber)
    )        
where rnk=1       
order by orderno, cardnumber  
/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文