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 7071487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:41:38+00:00 2026-05-28T05:41:38+00:00

I have 2 tables which are Account tbl and Customer tbl, the structure is

  • 0

I have 2 tables which are Account tbl and Customer tbl, the structure is like below:

Account tbl

Customer_ID    Account_ID    Parent_Account_ID    
3780952        3780952       3780952
3780997        3780997       3780997
3781004        3781004       3780997       

Customer tbl (Customer_Group have different value, but im only interest on Personal)

Customer_ID      Customer_Group
3781004          Personal
3780997          Personal
3780952          Personal      

The rule to determine PS/NonPS, Principle, Supp as per below:

**PS/NonPs**
Customer_ID equal to Parent_Account and Parent_Account is unique (not exist more than 1)  then NonPs.
Customer_ID equal to Parent_Account and Parent_Account is non unique OR -   Customer_ID is not equal to Parent_Account then PS    
**Principle** 
IF NonPS then Principle is Null
IF PS - If Customer_ID equal to Parent_Account then Principle is Y else N
**Supp**
IF NonPS then Supp is Null
IF PS - If Customer_ID not equal to Parent_Account then supp is Y else N

The final output should be like this

Customer_ID    Account_ID    Parent_Account_ID   PS/NonPS   Principle   Supp
3780952        3780952       3780952             NonPS       Null        Null
3780997        3780997       3780997             PS          Y           N
3781004        3781004       3780997             PS          N           Y   

I alredy tried many times but still cant get the output..anyone can help ?

  • 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-28T05:41:38+00:00Added an answer on May 28, 2026 at 5:41 am

    With CASE and analytic functions:

    SQL> WITH myData AS (
      2    SELECT 3780952 Customer_ID, 3780952 Account_ID, 3780952 Parent_Account_ID
      3      FROM DUAL
      4    UNION ALL SELECT 3780997, 3780997, 3780997 FROM DUAL
      5    UNION ALL SELECT 3781004, 3781004, 3780997 FROM DUAL
      6  )
      7  SELECT v.*,
      8         CASE WHEN ps = 'PS' AND customer_id = parent_account_id  THEN 'Y'
      9              WHEN ps = 'PS' THEN 'N'
     10         END "Principle",
     11         CASE WHEN ps = 'PS' AND customer_id != parent_account_id  THEN 'Y'
     12              WHEN ps = 'PS' THEN 'N'
     13         END "Supp"
     14    FROM (SELECT m.*,
     15                 CASE WHEN customer_id = parent_account_id
     16                       AND COUNT(*) OVER (PARTITION BY parent_account_id) = 1
     17                      THEN 'NonPS'
     18                      ELSE 'PS'
     19                 END ps
     20            FROM myData m) v;
    
    CUSTOMER_ID ACCOUNT_ID PARENT_ACCOUNT_ID PS    P S
    ----------- ---------- ----------------- ----- - -
        3780952    3780952           3780952 NonPS
        3781004    3781004           3780997 PS    N Y
        3780997    3780997           3780997 PS    Y N
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables something like the following: Customer (CustomerID, AddressState) Account (AccountID, CustomerID,
I have two tables which looks something like this Table Queue int ID; string
(I am using PostgreSQL) I have a table which stores transactions to an account.
I have 3 tables which need to be linked in an SQL statement (I'm
I have 2 tables which I want to query from. The first table (lets
I have two tables which are used to store details of different types of
I have two tables which I want to join together using a left outer
I have two tables which have some transactional stuff stored in them. There will
I have three tables which are defined as: class User(Base): __tablename__ = 'users' id
I use Hibernate with Java. I have two tables which are associated with foreign

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.