Home » SQL & PL/SQL » SQL & PL/SQL » display group name
display group name [message #631827] Tue, 20 January 2015 05:27 Go to next message
sudeshna_bio08
Messages: 11
Registered: December 2013
Location: india
Junior Member
Hi everybody,

I need a query to show immediate begin-total,end-total and posting ledger name.

CREATE TABLE LEDGER_ACC
(
  VA_GL_ACCOUNT_NO                     VARCHAR2(20 BYTE) NOT NULL,
  VA_GL_ACCOUNT_NAME              VARCHAR2(100 BYTE),
  VA_GL_ACCOUNT_TYPE_ID          VARCHAR2(10 BYTE),
  NU_INDENTATION                            NUMBER(5)
);
INSERT INTO LEDGER_ACC VALUES('11000000','Share capital','ACCTYP0003',1);
INSERT INTO LEDGER_ACC VALUES('11200000','Reserve & Surplus','ACCTYP0003',2);
INSERT INTO LEDGER_ACC VALUES('11200110','Capital reserve','ACCTYP0002',3);
INSERT INTO LEDGER_ACC VALUES('11200120','Security Premium','ACCTYP0002',3);
INSERT INTO LEDGER_ACC VALUES('11299999','Reserve & Surplus, Total','ACCTYP0004',2);


Here ledger account type id signifies to
ACCTYP0003 = BEGIN-TOTAL
ACCTYP0002 = POSTING
ACCTYP0004 = END-TOTAL

If account id parameter = '11299999' , query will return details of its begin-total value : '11200000';
If account id parameter = '11200120', query should return its begin-total value : '11200000';
If account id parameter = '11200000', query should return its begin-total value : '11000000';

I have tried with case when:

SELECT CASE WHEN A.VA_GL_ACCOUNT_TYPE_ID = 'ACCTYP0004' THEN
                      (SELECT B.VA_GL_ACCOUNT_NAME
                       FROM LEDGER_ACC  B
                       WHERE B.VA_GL_ACCOUNT_NO LIKE (SELECT RPAD(SUBSTR(A.VA_GL_ACCOUNT_NO,1,INSTR(A.VA_GL_ACCOUNT_NO,'9')-1),
                                                             (LENGTH(SUBSTR(A.VA_GL_ACCOUNT_NO,1,INSTR(A.VA_GL_ACCOUNT_NO,'9')-1)) +
                                                             LENGTH(SUBSTR(A.VA_GL_ACCOUNT_NO,INSTR(A.VA_GL_ACCOUNT_NO,9))))-1,
                                                           '0') ACC_NUM FROM DUAL)||'%'
                        AND B.VA_GL_ACCOUNT_TYPE_ID = 'ACCTYP0003')
            WHEN A.VA_GL_ACCOUNT_TYPE_ID = 'ACCTYP0003' AND A.NU_INDENTATION IN (1,2,3,4,5) THEN
                      (SELECT B.VA_GL_ACCOUNT_NAME
                        FROM LEDGER_ACC  B
                        WHERE B.VA_GL_ACCOUNT_NO LIKE (SELECT RPAD(SUBSTR(A.VA_GL_ACCOUNT_NO,1,INSTR(A.VA_GL_ACCOUNT_NO,0)-2),7,'0')
                                                       FROM DUAL)||'%'
                         AND B.VA_GL_ACCOUNT_TYPE_ID = 'ACCTYP0003')
            WHEN A.VA_GL_ACCOUNT_TYPE_ID = 'ACCTYP0003' AND A.NU_INDENTATION = 0 THEN
                       (SELECT B.VA_GL_ACCOUNT_NAME
                         FROM LEDGER_ACC  B
                         WHERE B.VA_GL_ACCOUNT_NO = (SELECT RPAD(SUBSTR(A.VA_GL_ACCOUNT_NO,1,INSTR(A.VA_GL_ACCOUNT_NO,0)),8,'0')
                                                        FROM DUAL)
                          AND B.VA_GL_ACCOUNT_TYPE_ID = 'ACCTYP0001')
            WHEN A.VA_GL_ACCOUNT_TYPE_ID = 'ACCTYP0002'  AND A.NU_INDENTATION IN (1,3,4) THEN
                       (SELECT B.VA_GL_ACCOUNT_NAME
                         FROM LEDGER_ACC  B
                         WHERE B.VA_GL_ACCOUNT_NO LIKE (SELECT RPAD(SUBSTR(A.VA_GL_ACCOUNT_NO,1,INSTR(A.VA_GL_ACCOUNT_NO,0)-1),7,'0')
                                                       FROM DUAL)||'%'       
                          AND B.VA_GL_ACCOUNT_TYPE_ID = 'ACCTYP0003')
            WHEN A.VA_GL_ACCOUNT_TYPE_ID = 'ACCTYP0002'  AND A.NU_INDENTATION IN (5,6) THEN
                       (SELECT B.VA_GL_ACCOUNT_NAME
                         FROM LEDGER_ACC  B
                         WHERE B.VA_GL_ACCOUNT_NO LIKE (SELECT RPAD(SUBSTR(A.VA_GL_ACCOUNT_NO,1,INSTR(A.VA_GL_ACCOUNT_NO,0)-2),7,'0')
                                                       FROM DUAL)||'%'       
                          AND B.VA_GL_ACCOUNT_TYPE_ID = 'ACCTYP0003')
       END GRP_LEDGER_NAME
FROM  LEDGER_ACC   A
WHERE A.VA_GL_ACCOUNT_NO = '11200120'


It showing the required output.
Is there any way to achieve the same using LAG analytical function?

Please help.

Thanks in adv


Edited by Lalit : Added code tags, please do yourself in future.

[Updated on: Tue, 20 January 2015 05:39] by Moderator

Report message to a moderator

Re: display group name [message #631832 is a reply to message #631827] Tue, 20 January 2015 06:35 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

What you posted is not clear and I think your test case is oversimplified to cover all the cases.

Quote:
If account id parameter = '11299999' , query will return details of its begin-total value : '11200000';
If account id parameter = '11200120', query should return its begin-total value : '11200000';
If account id parameter = '11200000', query should return its begin-total value : '11000000';


Are these account id fixed or are they from your example? Where do these begin-total values come from?
And what should be the result for account id 11200110. Why?
And what does "NU_INDENTATION" have to do with the question?

Post a clear specification.
Post a complete test case.
Post the complete result for this test case.

But before, Please read How to use [code] tags and make your code easier to read.
Also always post your Oracle version, with 4 decimals.

[Updated on: Tue, 20 January 2015 06:36]

Report message to a moderator

Re: display group name [message #631833 is a reply to message #631827] Tue, 20 January 2015 06:41 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Firstly, please use code tags from next time. Read How to use [code] tags


sudeshna_bio08 wrote on Tue, 20 January 2015 16:57

If account id parameter = '11299999' , query will return details of its begin-total value : '11200000';
If account id parameter = '11200120', query should return its begin-total value : '11200000';
If account id parameter = '11200000', query should return its begin-total value : '11000000';



So, you need the previous row's details for VA_GL_ACCOUNT_no sorted by VA_GL_ACCOUNT_NAME, VA_GL_ACCOUNT_no. simply use LEAD.

SQL> column VA_GL_ACCOUNT_NO format a10
SQL> column VA_GL_ACCOUNT_NAME format a20
SQL> column VA_GL_ACCO format a10
SQL> WITH data AS
  2    (SELECT t.*,
  3      lead(va_gl_account_no) over(order by VA_GL_ACCOUNT_NAME, VA_GL_ACCOUNT_no) rn
  4    FROM ledger_acc t
  5    )
  6  SELECT * FROM DATA
  7    WHERE rn = 11299999
  8  /

VA_GL_ACCO VA_GL_ACCOUNT_NAME   VA_GL_ACCO NU_INDENTATION RN
---------- -------------------- ---------- -------------- --------------------
11200000   Reserve & Surplus    ACCTYP0003              2 11299999

SQL>
Re: display group name [message #631836 is a reply to message #631833] Tue, 20 January 2015 07:42 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Where does the following come into play?

Quote:
Here ledger account type id signifies to
ACCTYP0003 = BEGIN-TOTAL
ACCTYP0002 = POSTING
ACCTYP0004 = END-TOTAL


Re: display group name [message #631837 is a reply to message #631836] Tue, 20 January 2015 08:14 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Ah, sorry. I completely missed that. I don't see any relevance though as OP didn't include proper rules for it. When I read the requirement, I thought the rules are what I quoted. I might be wrong, only OP can clarify.

You are right, when I re-read the requirement, it seems unclear.
Re: display group name [message #631838 is a reply to message #631837] Tue, 20 January 2015 09:22 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
@OP, I am curious, can you please explain about your query that you posted. I see a big gap between your requirement and your query attempt. Is there something which you still need to add to the requirement. It would be good if you post the output of your query and explain the difference between your output and what you are expecting.

I believe, that is what Michel tried to point out. We can go ahead to help you, only after you clarify these things.
Previous Topic: Finding SQL Query ...
Next Topic: How to control the inserting the new records when it cross the limits
Goto Forum:
  


Current Time: Thu Aug 27 02:42:28 CDT 2026