组合两个已经运行的 ORACLE 查询
我想就以下两个问题寻求帮助。他们单独工作就像一个魅力,现在我希望他们一起工作。
它们都提供会员卡数据库。
第一个根据用户的唯一卡号(%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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:假设 ag_tranzakcio.id 字段是一个可靠的递增键,这可以工作:
EDIT: Assuming the ag_tranzakcio.id field is a reliable incrementing key, this could work:
修改后的要求是仅返回每张卡的最近交易。这可以通过使用分析函数 RANK() 对交易进行排序,然后应用包装查询来过滤计算出的排名来完成。
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.