How the group using SQL for the desired output.

Hi all

I am currently using oracle 10.2.0.4.0

Create a table script:
CREATE TABLE FORTEST
( gpno VARCHAR2(10 BYTE),
  classnumber  VARCHAR2(10 byte),
  age_min NUMBER,
  age_max NUMBER,
  amount NUMBER)
INSERT statement:
insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
('G123' , 01,0,29,1) 
insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
('G123' , 01,30,35,2) 
insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
('G123' , 01,36,40,3) 
insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
('G123' , 02,0,29,1) 
insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
('G123' , 02,30,35,2) 
insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
('G123' , 02,36,40,5) 
insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
('G123' , 03,0,29,1) 
insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
('G123' , 03,30,35,2) 
insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
('G123' , 03,36,40,3) 
insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
('G124' , 01,0,29,1) 
insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
('G124' , 01,30,35,2) 
insert into fortest  (GPNO,classnumber,age_min,age_max,amount) values
('G124' , 01,36,40,3) 
power required:
gpno    classnumber    age_min    age_max    amount
G123    1,3                0        29        1
G123    1,3                30       35        2
G123    1,3                36       40        3
G123    2                  0        29        1
G123    2                  30       35        2
G123    2                  36       40        5
G124    1                  0        29        1
G124    1                  30       35        2
G124    1                  36       40        3
as for gpno g123, classnumber 1 and 3, the rates are the same in all the age_min and age_max they need to be grouped.
even if gpno 123 classnumber 2 has the same rates as the classesnumber 1 and 3 for the age groups 0 to 29 and 30 to 35,
rates are different for ages 36 to 40. so it should not be placed together. How can I do this in SQL

any help is appreciated.

Thanks in advance.

Hello

Thorny problem!

Unfortunately, LISTAGG was created to the Oracle 11.2. About half of the complexity here is the aggregation of chain, i.e. forming the list of the classnumbers, as '1.3', using only functions available in Oracle 10.2.

Here's a solution:

WITH     got_gpno_classnumber_cnt   AS
(
     SELECT     gpno, classnumber, age_min, age_max, amount
     ,     COUNT (*) OVER ( PARTITION BY  gpno
                                  ,            classnumber
                      )   AS gpno_classnumber_cnt
     FROM    fortest
--     WHERE     ...     -- If you need any filtering, this is where it goes
)
,     pairs          AS
(
     SELECT    a.gpno
     ,       a.classnumber
     ,       MIN (b.classnumber)
                OVER ( PARTITION BY  a.gpno
                          ,                    a.classnumber
                  )     AS super_classnumber
     FROM       got_gpno_classnumber_cnt  a
     JOIN       got_gpno_classnumber_cnt  b  ON   a.gpno     = b.gpno
                                  AND  a.age_min     = b.age_min
                                AND  a.age_max     = b.age_max
                                AND  a.amount     = b.amount
                                AND  a.gpno_classnumber_cnt
                                        = b.gpno_classnumber_cnt
     GROUP BY  a.gpno
     ,            a.classnumber
     ,       b.classnumber
     HAVING       COUNT (*)     = MIN (a.gpno_classnumber_cnt)
)
,     got_rnk          AS
(
     SELECT DISTINCT
             gpno, classnumber, super_classnumber
     ,     DENSE_RANK () OVER ( PARTITION BY  gpno
                               ,                    super_classnumber
                               ORDER BY          classnumber
                       )         AS rnk
     FROM    pairs
)
,     got_classnumbers     AS
(
     SELECT     gpno, classnumber, super_classnumber
     ,      SUBSTR ( SYS_CONNECT_BY_PATH (classnumber, ',')
                   , 2
                 )     AS classnumbers
     FROM     got_rnk
     WHERE     CONNECT_BY_ISLEAF = 1
     START WITH     rnk             = 1
     CONNECT BY     rnk             = PRIOR rnk + 1
          AND     gpno             = PRIOR gpno
          AND     super_classnumber  = PRIOR super_classnumber
)
SELECT DISTINCT
       g.gpno
,       c.classnumbers
,       g.age_min
,       g.age_max
,       g.amount
FROM       got_gpno_classnumber_cnt  g
JOIN       got_classnumbers         c  ON   c.gpno        = g.gpno
                             AND  c.classnumber  = g.classnumber
ORDER BY  g.gpno
,            c.classnumbers
;

Out (just as you requested):

GPNO       CLASSNUMBERS       AGE_MIN    AGE_MAX     AMOUNT
---------- --------------- ---------- ---------- ----------
G123       1,3                      0         29          1
G123       1,3                     30         35          2
G123       1,3                     36         40          3
G123       2                        0         29          1
G123       2                       30         35          2
G123       2                       36         40          5
G124       1                        0         29          1
G124       1                       30         35          2
G124       1                       36         40          3

Tags: Database

Similar Questions

  • How can I use statistics for all the tables in a schema in SQL Developer? and how long will it take on average?

    Hello

    How can I use statistics for all the tables in a schema in SQL Developer? and how long will it take on average?

    Thank you

    Jay.

    Select the connection and right-click on it and select schema statistics collection

  • How can I use SQL to check the Version of Oracle Forms and object of the request?

    Hi all:

    How can I use SQL to check the Version of Oracle Forms and object of the request?





    Concerning
    Terry

    Terry,

    See the following threads/docs for the version of forms.

    How to find the developer version
    Re: How to find the developer version

    Note: 466890.1 - Script to find the Apache, Java, Jinitiator, version of forms and details of the JVM for Oracle E-Business Suite 11i
    Note: 392793.1 - how to get Forms Oracle Oracle Applications 11i Command Line Version
    Note: 468311.1 - Script to find the Apache, Java, JRE, Forms for Oracle E-Business Suite R12 version

    What purpose are you talking?

    Thank you
    Hussein

  • How can I use SQL to check if the receipt is taken into account?

    Hi all:
    How can I use SQL to check if the reception of the AR is taken into account? Because there are so many recipes to check, I can not open to see the details one by one.

    My environment is: 11.5.9
    database: Oracle 9.2.0.8

    Terry,

    Run the script (11i: Oracle receivables data reception Data Collection Test [215969.1 ID]) and see if it helps.

    Thank you
    Hussein

  • How can I use SQL/CUBE to figure out total?

    How can I use SQL/CUBE to figure out total?

    Thank you.
    -JC

    Of course, you can do this by using the cube. But cube will produce all the possible combinations of summary. We can filter the unnecessary summariies having clause using the group_id function which is a binary encoding of the columns that are summarized. Below for example, as applied on the table SH.sales.

    Select channel_id, promo_id, prod_id, sum (amount_sold)
    from the sale
    Group of cube (channel_id, promo_id, prod_id)
    having grouping_id (channel_id, promo_id, prod_id) (3, 5, 6, 7)

  • My mobile version of lightroom 30-day trial has expired, but I have Adobe Creative cloud with a full membership. How can I use lightroom for mobile in the future?

    My mobile version of lightroom 30-day trial has expired, but I have Adobe Creative cloud with a full membership. How can I use creative photography cloud adobe?

    Hello

    Please see: -.

    The use of Lightroom Mobile is included with Creative Cloud Student and Teacher Edition?

    Or

    Mobile Lightroom not available for education memberships | Kivuto

    You can also refer to the document:-Adobe Lightroom for mobile FAQ

    I hope that answers your query!

  • How many groups of disks for + DATA?

    Hi all

    Made Oracle recommends that a group of disks asm wholesale/shared for all databases?
    In our case we're going to 11.2 and 10 g rac against 11.2 ask...

    Am I right to say that I need to set the compatibility of the asm to 10 in order to be able to use the same disk?
    It is a good idea? Or should I create another group of disks for the db 10g?
    I guess that there are features that are not available when compatibility is reduced to 10g...

    Yes, it is possible but not recommended because the ASM would distribute the e/s in such a way to disc 1 would get 4 times requests for disk 2, which is probably not what you want in terms of performance.

    Bjoern

  • How can I use two DVI and HDMI outputs for monitors?

    I have a 55 inch LG TV connected to my PC via an HDMI cable. I recently bought a HP 2311 x flatscreen monitor and want to connect using DVI.  But when both are plugged into the PC automatically turns off the DVI output, and switches automatically to use the HDMI that my TV is turned on.  Is it possible that I can have the two outputs working both on my pc?

    Hello

    I suggest you to send your query to this link for assistance.

    http://experts.Windows.com/f/

  • How to run wfstatus.sql for i-expenses

    How to run wfstatus.sql i-expenses in EBS 11i.

    Select this check box, hope it helps.

    How to find and solve the reports on Internet expenditures within workflow [ID 185004.1]

    How wfstatus.sql to run on a statement of expenses Internet [134895.1 ID]

  • Change the font color in a report using SQL for APEX 5.0 queries

    Hello

    I'm testing APEX 5.0 in my test environment to see if I can migrate my internal applications of 4.2 to 5.0.

    How can I change the font color in a field based the SQL query? I know how to do it in 4.2, but I can't find the same in 5.0.

    Thank you

    Well, I used to put html tags in the select text, getting the value of depending on the CASE, so I had to change the display column attribute in the report as "Display in text form (space special characters...)".

    I found an old post here: https://tylermuth.wordpress.com/2007/12/01/conditional-column-formatting-in-apex/ and this method still worked for me in APEX 5.0

    Thanks anyway

  • Query using SQL for the agenda of the source page does not value populated insert

    Display as: text field
    Type of page element: query SQL
    Value Expression: select sysdate double

    Element of the page is 'UPDATE_DATE '.

    The goal is to 'automatically fill the box' this field with sysdate to the user has to when you complete the form. Problem is that it is automatically generated and I can even see the value when I click on the Sessions, however, when I click on CREATE to insert the row, he returned, "ORA-01400: cannot insert NULL in...» »

    Hello

    Apart from "Type of Source should be the column of database and the Source value must be the name of a column of a table.", you can also use the default: SYSDATE as a PL/SQL Expression

    Greetings,
    Roel
    http://roelhartman.blogspot.com/
    You can reward this response in marking it as useful or Correct ;-)

  • Where to find or how to create LKM SQL FOR SQL?

    Hello

    I m very big thing back to ODI.

    My problem is that I have to map a table in my DB ORACLE in a CSV file. So I created an Interface in my editor ODI and
    1 checked 'target different stop.
    2. now, it says "staging area is different from the target. In order to load data from the staging area to the target, you must select a multiple on the target connection revenge or a LKM on the staging area. »
    3. in my goal, I use an IKM SQL append

    Can anyone tell
    1. how this 'IKM SQL append"multi connection

    or

    2 should. what LKM I use for my rest area. And where can I find that LKM. (Right now I have that LKM Sql file I have some previous draft.)


    Thanks in advance :)

    Vikas.

    Hi Vikas

    You can import the default KM (LKM, IKM, sewing, RKM) to oracleodi/xmlreference and then duplicate the imported KM (LKM or IKM). Then see all the steps inside he make changes in the steps needed if you want according to your requirement. Do not try to encode any value in your KM.

    Thank you

  • How can I use FNDLOAD for XML reports and data defnitions

    I program simultaneous registerd bound reports xml and data defnitions in instance of staging and managed to switch to DEV... After updating of this stage and when I download the details of DEV and transferred to the program only simultaneous scene I can download... Can we use the command FNDLOAD for definitions of data or the data model for the XML report, FNDLOAD used for the simultaneous program, he transferred only the details of the program at the same time.

    Hello

    Use XDOLoader.

    Note: 469585.1 - how to use XDOLoader?
    https://metalink2.Oracle.com/MetaLink/PLSQL/ml2_documents.showDocument?p_database_id=not&P_ID=469585.1

    Kind regards
    Hussein

  • How to write a SQL for this condition?

    I have a table with columns v_sub, v_visit, and T_DATE and the structure is like this

    v_sub v_visit T_DATE
    1 visit-1 01 - mar - 09
    1 visit-2 05 - mar - 09
    1 visit-3 17 - mar - 09
    2-visit-1-4 February 09
    2 visit-2 12 - mar - 09
    2 visit-3 20 - mar - 09

    I want to write a query that control weather it is in chronological order or not. (for v_sub, v_visit and T_DATE must be in chronological order as above)

    and I want to check the condition as below:

    v_sub v_visit T_DATE
    1 visit-1 01 - mar - 09
    1 visit-2 5 February 09
    1 visit-3 17 - mar - 09
    2-visit-1-4 February 09
    2 visit-2 12 January 09
    2 visit-3 20 - mar - 09


    Thanks in advance

    Use the LAG function to get previous date

    SQL> -- sample data
    SQL> with t
      2  as
      3  (
      4     select 1 v_sub, 'visit-1' v_visit, to_date('01-mar-09','dd-mon-yy') v_date from dual union all
      5     select 1, 'visit-2', to_date('05-mar-09','dd-mon-yy') from dual union all
      6     select 1, 'visit-3', to_date('17-mar-09','dd-mon-yy') from dual union all
      7     select 2, 'visit-1', to_date('04-feb-09','dd-mon-yy') from dual union all
      8     select 2, 'visit-2', to_date('12-mar-09','dd-mon-yy') from dual union all
      9     select 2, 'visit-3', to_date('20-mar-09','dd-mon-yy') from dual
     10  )
     11  -- end of sample data
     12  select v_sub, v_visit, v_date, lag(v_date) over(partition by v_sub order by v_visit, v_date) v_previous_date
     13    from t
     14  /
    
         V_SUB V_VISIT V_DATE    V_PREVIOU
    ---------- ------- --------- ---------
             1 visit-1 01-MAR-09
             1 visit-2 05-MAR-09 01-MAR-09
             1 visit-3 17-MAR-09 05-MAR-09
             2 visit-1 04-FEB-09
             2 visit-2 12-MAR-09 04-FEB-09
             2 visit-3 20-MAR-09 12-MAR-09
    
    6 rows selected.
    

    Now you can check if the previous_date is less than T_DATE

  • How to add PL/SQL for AppModule processes

    Hi community I try to add a PL/SQL APPMODULE process with the answer for the view controller.

    Thank you.

    Hi Peter

    Use this guide [http://download-west.oracle.com/docs/cd/B32110_01/web.1013/b25947/bcadvgen.htm#sm0297]

    Good luck

Maybe you are looking for