Identify the missing lines for the hierarchy

Oracle 10.2.0.1
Windows XP

create table vill (identification number (5), area varchar2 (20), subarea1 varchar2 (20), subarea2 varchar2 (20), location varchar2 (20));
Insert in the vill values(1,'AREA1','SUBAREA1','SUBAREA2','AREA1');
Insert in the vill values(2,'AREA1','SUBAREA1','SUBAREA2','SUBAREA1');
Insert in the vill values(3,'AREA1','SUBAREA1','SUBAREA2','SUBAREA2');
Insert in the vill values(4,'AREA1','SUBAREA1','SUBAREA2','VILL1');
Insert in the vill values(5,'AREA1','SUBAREA1','SUBAREA2','VILL2');
Insert in the vill values(6,'AREA1','SUBAREA1','SUBAREA2','VILL3');
Insert in the vill values(7,'AREA1','SUBAREA1','SUBAREA2','VILL4');
-Before is a perfect set of lines because it has all the lines in the area, subarea1 and subarea2.
Insert in the vill values(8,'AREA2','SUBAREA1','SUBAREA2','AREA2');
Insert in the vill values(9,'AREA2','SUBAREA1','SUBAREA2','SUBAREA1');
Insert in the vill values(10,'AREA2','SUBAREA1','SUBAREA2','VILL5');
Insert in the vill values(11,'AREA2','SUBAREA1','SUBAREA2','VILL6');
Insert in the vill values(12,'AREA2','SUBAREA1','SUBAREA2','VILL7');
Insert in the vill values(13,'AREA2','SUBAREA1','SUBAREA2','VILL8');

Insert in the vill values(14,'AREA3','SUBAREA1','SUBAREA2','AREA3');
Insert in the vill values(15,'AREA3','SUBAREA1','SUBAREA2','SUBAREA2');
Insert in the vill values(16,'AREA3','SUBAREA1','SUBAREA2','VILL9');
Insert in the vill values(17,'AREA3','SUBAREA1','SUBAREA2','VILL10');
Insert in the vill values(18,'AREA3','SUBAREA1','SUBAREA2','VILL11');
Insert in the vill values(19,'AREA3','SUBAREA1','SUBAREA2','VILL12');

Insert in the vill values(20,'AREA4','SUBAREA1','SUBAREA2','SUBAREA1');
Insert in the vill values(21,'AREA4','SUBAREA1','SUBAREA2','SUBAREA2');
Insert in the vill values(22,'AREA4','SUBAREA1','SUBAREA2','VILL13');
Insert in the vill values(23,'AREA4','SUBAREA1','SUBAREA2','VILL14');
Insert in the vill values(24,'AREA4','SUBAREA1','SUBAREA2','VILL15');
Insert in the vill values(25,'AREA4','SUBAREA1','SUBAREA2','VILL16');
commit;
Select * from city;
        ID AREA                 SUBAREA1             SUBAREA2             LOCATION
---------- -------------------- -------------------- -------------------- --------------------
         1 AREA1                SUBAREA1             SUBAREA2             AREA1
         2 AREA1                SUBAREA1             SUBAREA2             SUBAREA1
         3 AREA1                SUBAREA1             SUBAREA2             SUBAREA2
         4 AREA1                SUBAREA1             SUBAREA2             VILL1
         5 AREA1                SUBAREA1             SUBAREA2             VILL2
         6 AREA1                SUBAREA1             SUBAREA2             VILL3
         7 AREA1                SUBAREA1             SUBAREA2             VILL4
         8 AREA2                SUBAREA1             SUBAREA2             AREA2
         9 AREA2                SUBAREA1             SUBAREA2             SUBAREA1
        10 AREA2                SUBAREA1             SUBAREA2             VILL5
        11 AREA2                SUBAREA1             SUBAREA2             VILL6
        12 AREA2                SUBAREA1             SUBAREA2             VILL7
        13 AREA2                SUBAREA1             SUBAREA2             VILL8
        14 AREA3                SUBAREA1             SUBAREA2             AREA3
        15 AREA3                SUBAREA1             SUBAREA2             SUBAREA2
        16 AREA3                SUBAREA1             SUBAREA2             VILL9
        17 AREA3                SUBAREA1             SUBAREA2             VILL10
        18 AREA3                SUBAREA1             SUBAREA2             VILL11
        19 AREA3                SUBAREA1             SUBAREA2             VILL12
        20 AREA4                SUBAREA1             SUBAREA2             SUBAREA1
        21 AREA4                SUBAREA1             SUBAREA2             SUBAREA2
        22 AREA4                SUBAREA1             SUBAREA2             VILL13
        23 AREA4                SUBAREA1             SUBAREA2             VILL14
        24 AREA4                SUBAREA1             SUBAREA2             VILL15
        25 AREA4                SUBAREA1             SUBAREA2             VILL16

25 rows selected.
Power required:
        ID AREA                 SUBAREA1             SUBAREA2             LOCATION
---------- -------------------- -------------------- -------------------- --------------------
        26 AREA2                SUBAREA1             SUBAREA2             SUBAREA2
        27 AREA3                SUBAREA1             SUBAREA2             SUBAREA1
        28 AREA4                SUBAREA1             SUBAREA2             AREA4
Why?

Because as I said above that rowset for the euro1 area is perfect because in the location column there are lines for the euro1, SUBAREA1 and SUBAREA2 area. is exists, whereas if we see that there are no lines for AREA2 with the value SUBAREA2 in the place, even regarding the Domain3, there is no line that has the value of the SUBAREA1 location and even for AREA4, there is no line as location = AREA4. It is a table which is having all the names of place with nested values; for example, we can see that vill7 is exist in zone2 including SUBAREA1 (COLUMN NAME) = "SUBAREA1" AND SUBAREA2 (COLUMN NAME) = "SUBAREA2", but I would like to know location = SUBAREA2 then it will say no line, I want to generate all the lines that is to have no value in the column location for said AREA/SUBAREA1/SUBAREA2 column. Table is sorted by ID. Total column lines are 45800.
SQL> select * from vill where area='AREA1' and location='SUBAREA1';

        ID AREA                 SUBAREA1             SUBAREA2             LOCATION
---------- -------------------- -------------------- -------------------- --------------------
         2 AREA1                SUBAREA1             SUBAREA2             SUBAREA1
Ok
SQL> select * from vill where area='AREA2' and location='SUBAREA1';

        ID AREA                 SUBAREA1             SUBAREA2             LOCATION
---------- -------------------- -------------------- -------------------- --------------------
         9 AREA2                SUBAREA1             SUBAREA2             SUBAREA1
It is also ok, but if said
SQL> select * from vill where area='AREA2' and location='SUBAREA2';

no rows selected 
< <-Yes, this line I want to generate, because there should be a line whose location = "SBUAREA2" but why? Because otherwise how do I know what is the hierarchy of the SUBAREA2; While vill 5,6,7 and 8 were SUBAREA2 as "SUBAREA2".

I'm not sure how I am clear in my question, please let me know if I need to provide more details.

Thank you.

user12050217 wrote:
... query should return:

ID AREA                 SUBAREA1             SUBAREA2             LOCATION
---------- -------------------- -------------------- -------------------- --------------------
131 my state             my city              my city              street 2

Sorry, I don't understand that one line. Was it a mistake? I think I understand all the other lines that you want to, but I do not understand why you want the one above. Did you mean

ID AREA                 SUBAREA1             SUBAREA2             LOCATION
---------- -------------------- -------------------- -------------------- --------------------
131 my state             my city              street 2             street 2

?

If this is not the case, why you don't want the row immediately above? Why don't you also:

`       ID AREA                 SUBAREA1             SUBAREA2             LOCATION
---------- -------------------- -------------------- -------------------- --------------------
       131 my state             new city             new city             street 2

?

Assuming it was just a mistake, the following Gets the results you want

WITH     rows_needed     AS
(
     SELECT     area
     ,     area          AS subarea1
     ,     area          AS subarea2
     ,     area          AS location
     FROM     vill
     WHERE     area           != subarea1
     OR     area          != subarea2
     OR     area          != location
    UNION
     SELECT     area
     ,     subarea1
     ,     subarea1     AS subarea2
     ,     subarea1     AS location
     FROM     vill
     WHERE     subarea1     != subarea2
     OR     subarea1     != location
    UNION
     SELECT     area
     ,     subarea1
     ,     subarea2
     ,     subarea2     AS location
     FROM     vill
)
SELECT     (
          SELECT     MAX (id)
          FROM     vill
     ) + ROWNUM          AS id
,     area, subarea1, subarea2, location
FROM     rows_needed
WHERE     (area, subarea1, subarea2, location)
           NOT IN (
                   SELECT  area, subarea1, subarea2, location
                   FROM    vill
                 )
;

except that it is for an indefinite period including the ID will assign to what line. I guess that doesn't matter, as long as 130 to 137 numbers are used (or 131 to 138, if that is what you really want).

When I run your code on the table he returned me the desired output, but when I run it on my actual table that is having 45800 lines, then he messed up some results, because I think that in your code, I should mention the clause ORDER BY ID somewhere... Therefore, in this example I mention different IDS while inserting the lines. Your code worked very well because it is having only 25 rows with ID Ordered, while my actual table is to have 45800 lines in the table, there is no guarantee of ID ordered... so I think that its a problem with the order by clause, ID... but I don't know where I should put this clause

What is the significance of id? Is this just a unique key? That's, I guess that each line must have an id and two rows can have the same id, but, aside from that, it doesn't matter what line has which id.
The query I posted yesterday assumes that subarea1 and subarea2 were functionally dependent on the area. as in the example of data you posted yesterday. I see your last post that's not true, and which explains erroneous results.

As I said yesterday, if you are actually setting up new lines in the table, you must use a sequence to generate identifiers.

Tags: Database

Similar Questions

Maybe you are looking for

  • cable modem-wireless combination or not?

    Hi, I'm going to switch from DSL to cable T - W (the only option in my building). I have an Airport Extreme on my TimeCapsule (it is the 500 GB disk version), that I use with my laptop. The iMac is connected to the router. I looked around at best buy

  • No update OS on Alcatel One Touch fire

    The version of the OS on my Alcatel OTF is 1.1.0.0 - prerelease.If I understand the update of the OS is pushed by my phone provider (Alcatel).I have to be patient with the OS updates, or is there something wrong with the system?Can anyone give inform

  • Processor problem

    I had my 2 Atrix in less than a month ago and there after I did the update OTA ICS. I noticed that after the updated ICS up-to-date CPU is back to his own clock speed is to say 1, 2 GHz and not speed underclocked then usual IE 1 GHz. But this had tak

  • Recovery (F11) does not

    Looking to reinstall Vista on my IdeaPad Y530 and pressing F11 when restarting has no effect. Background: (1) I had installed ubuntu in dual boot, whose reading other posts can interfere with recovery (2) the product code for windows vista on the bot

  • Regarding the 8 Developer Preview window

    Hello Sir, It came I used window Developer 8 in my laptop but its why not active. my friend has also install overview Developer window 8 in our laptop and his show of activated window but not activated why. can you tell me how to activate the window