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

  • SEARCH
  • Home
  • 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 6982887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:20:50+00:00 2026-05-27T18:20:50+00:00

I have 2 tables. Table Accs contains 9 million rows (3 columns: acc_id, month,

  • 0

I have 2 tables. Table “Accs” contains 9 million rows (3 columns: acc_id, month, year).
First I need to extract the records that contain the partial entry of account number,
and then in these records to find the complete match, if not – then the first partial match

WITH t AS (
  SELECT a.acc_id,
         t1.as,
         t1.cust,
         t1.curr,
         t1.code,
         t1.depart,
         t1.sdate,
         t1.stype,
         t1.amount,
         t1.s_id
  FROM table1 t1 
  LEFT JOIN Accs a 
  ON SUBSTR(a.acc_id,7,12)=t1.curr||LPAD(t1.code,4,'0')||LPAD(t1.depart,3,'0')
  WHERE t1.sdate='20.11.2011' AND t1.stype='A' AND a.month=11 ANd a.year=2011)
SELECT MAX(t.s_id),
       (CASE WHEN t.as='000000' 
             THEN (CASE WHEN ac2.acc_id IS NOT NULL THEN ac2.acc_id ELSE t.acc_id END) 
             ELSE t.cust||t.curr||LPAD(t.code,4,'0')||LPAD(t.depart,3,'0') END) acc_id
FROM t 
LEFT JOIN (SELECT t.acc_id FROM t) ac2 
ON SUBSTR(ac2.acc_id,1,6)='000'||LPAD(t.depart,3,'0')
GROUP BY      
       (CASE WHEN t.as='000000' 
             THEN (CASE WHEN ac2.acc_id IS NOT NULL THEN ac2.acc_id ELSE t.acc_id END) 
             ELSE t.cust||t.curr||LPAD(t.code,4,'0')||LPAD(t.depart,3,'0') END)

This query takes a long time. Whether correctly I do?

  • 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-27T18:20:50+00:00Added an answer on May 27, 2026 at 6:20 pm

    First attempt

    Can’t really test it without having your actual table structure and data, but I’ve made a few minor changes that do sometimes have big impact.

    First of all, I changed the LEFT JOIN in the with part to an INNER JOIN. Since you use values of a in the WHERE clause, it would function as an inner join anyway, and it is usually a lot faster, especially with this amount of data and proper indexes.

    I changed the inner CASE to NVL, because that’s essentially what it does. Don’t know if that would speed things up.

    Moved the string concatenation from the outer query to the with part.

    These are just small changes that may have anywhere from no to quite some effect. At least you can try these before altering the table structure itself, although it may be a good idea to do that anyway.

    WITH t AS 
    (
      SELECT 
        a.acc_id,
        t1.as,
        t1.cust,
        t1.curr,
        t1.code,
        t1.depart,
        t1.sdate,
        t1.stype,
        t1.amount,
        t1.s_id,
        t1.cust || t1.curr || LPAD(t1.code, 4, '0') || LPAD(t1.depart, 3, '0') as groupfield
      FROM 
        table1 t1 
        INNER JOIN Accs a
          ON SUBSTR(a.acc_id, 7, 12) = t1.curr || LPAD(t1.code, 4, '0') || LPAD(t1.depart, 3, '0')
      WHERE 
        t1.sdate = '20.11.2011' AND t1.stype = 'A' AND a.month = 11 ANd a.year = 2011
    )
    SELECT
      MAX(t.s_id),
      (CASE WHEN t.as = '000000' THEN 
        NVL(ac2.acc_id, t.acc_id)
      ELSE
        t.groupfield
      END) acc_id
    FROM 
      t 
      LEFT JOIN t ac2 on ac2 
        ON SUBSTR(ac2.acc_id, 1, 6) = '000' || LPAD(t.depart, 3, '0')
    GROUP BY      
      (CASE WHEN t.as = '000000' THEN 
        NVL(ac2.acc_id, t.acc_id)
      ELSE
        t.groupfield
      END)
    

    Second attempt

    After looking a little more into your query, I wonder if you cannot just make it a single/simple query instead of using with. I think by first inner joining Accs and then left joining Accs again with the extra condition, you’re a good way to go really.

      SELECT 
        MAX(t1.s_id) AS s_id,
        CASE WHEN t.as = '000000' THEN
          NVL(a2.acc_id, a.acc_id)
        ELSE
          t1.cust || t1.curr || LPAD(t1.code, 4, '0') || LPAD(t1.depart, 3, '0')
        END AS acc_id
      FROM 
        table1 t1 
        INNER JOIN Accs a
          ON SUBSTR(a.acc_id, 7, 12) = t1.curr || LPAD(t1.code, 4, '0') || LPAD(t1.depart, 3, '0')
          AND a.month = 11 AND a.year = 2011
        LEFT JOIN Accs a2
          ON SUBSTR(a2.acc_id, 7, 12) = t1.curr || LPAD(t1.code, 4, '0') || LPAD(t1.depart, 3, '0')
          AND a2.month = 11 AND a2.year = 2011
          AND SUBSTR(a2.acc_id, 1, 6) = '000' || LPAD(t1.depart, 3, '0')
      WHERE 
        t1.sdate = '20.11.2011' AND t1.stype = 'A'
      GROUP BY 
        CASE WHEN t.as = '000000' THEN
          NVL(a2.acc_id, a.acc_id)
        ELSE
          t1.cust || t1.curr || LPAD(t1.code, 4, '0') || LPAD(t1.depart, 3, '0') as groupfield
        END AS acc_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables Table FOO FooUniqueID| Year| Name| Worth| --------------------------- 1 2008 Bob
Let's say I have two tables: Table: Color Columns: Id, ColorName, ColorCode Table: Shape
I have two tables TABLE A and TABLE B. TABLE A contain 1 million
I need help to build SQL query. I have 2 tables table PROJECT and
I have 3 tables: Table: Company , Columns: Id, Name Table: User , Columns:
I have 2 tables: Table A is a category table. Columns are cid, catname
I have two tables Table : 1, Name : NdcAwp, Columns-Ndc, AwpUnitCost Table :
I have three tables Table T1: Columns {a,b,c}, combination {a,b} is unique in Table
I have two tables Table public.tags_to_entities Column | Type | Modifiers --------+---------+----------- tid |
I have 2 tables: Table with counters names(t1): Counter_ID Counter_Name 1 Apples 2 Nuts

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.