Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6530105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:42:57+00:00 2026-05-25T09:42:57+00:00

I’d like to ask for help with the two queries below. They work like

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:42:58+00:00Added an answer on May 25, 2026 at 9:42 am
    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.