How do I in the sql and pl/sql?

I have the following data in a table:

Cust_Id Relation_ID
1 2
1 3
2 3
1 v
5 7
6 7
7 8
8 9

for customer 1 is connected to client 2 somehow (could be the husband, for example) and is also linked to the customer 3. Client 2 is related to the customer, 3 3 client is then linked to the customer 4. Similarly, visitors 5,6,7,8 and 9 are all connected, but they are in no way related to customers 1,2,3 and 4.

I need to divide these data into related groups. So clients 1,2,3 and 4 in 1 (say Group 1), and customers 5,6,7,8 and 9 would be in another (say Group 2). Of course, this is a simple example, but my real data are more than that!
In theory, client 1 might also be in another 'group', say with the customers, 10, 11 and 12, that would not be associated with anyone in the Group 1. This would then form another group (say 3).

How I did it and communicate the results in sql/pl sql?

Hello

Tracey.Sheppard wrote:
My apologies - I'm one of these groups a little bit, which would have confused the issue further.

Groups must be:

Customer 195229 (father) Group: (father is 195229 no 195226!)
195229
195224
201147
195228
195226

Son, daughter, and mother of group
195224
195228
195229
195226

Group of friends of 11807
11807
201147

A group of friend of 201147
201147
195229
11807

For each customer, the group is their cust_id more people that they are directly related.

If a group consists of a node and all other nodes connected directly.
That is, x and are in the same group if (and only if) at least one of the following is true:
(1) cust_id = x and reln_id = y for a relationship line.
(2) reln_id = x and cust_id = y for a relationship line, or
(3) x = y
Is this fair?

If I do:

Select distinct (decode(cust_id,'195226',reln_id,cust_id))
in the relationship where (cust_id = ' 195226 'or reln_id = ' 195226')

I get:

DECODE (C
--------
195224
195228
195229

so 195226 group is foregoing plus 195226.

I don't think it's useful to think of "the 195226 group." Haven't you said that an id can be member of groups of two or more systematically? Aren't say you, below, that "195226 group' is equivalent to 'of 195224 group', and that the two are the same as"195228 group?

The same query, but with 295228 gives:

DECODE (C
--------
195224
195226
195229

so when 195228 is added, the group is the same. Here in a single group.

You are on the right track. Associate each node with all nodes connected directly to it, as well as to himself. It's that the subquery in the query below got_memebers. Note the similarity between got_members and the definition I gave to a group above.

WITH     got_members     AS
(
     SELECT     cust_id  AS id1,   reln_id  AS id2     FROM relation     UNION
     SELECT     reln_id  AS id1,   cust_id  AS id2     FROM relation     UNION
     SELECT     cust_id  AS id1,   cust_id  AS id2     FROM relation     UNION
     SELECT     reln_id  AS id1,   reln_id  AS id2     FROM relation
)
,     got_r_num     AS
(
     SELECT     id1
     ,     id2
     ,     ROW_NUMBER () OVER ( PARTITION BY  id1
                               ORDER BY          id2
                       )  AS r_num
     FROM     got_members
)
SELECT DISTINCT      SUBSTR ( SYS_CONNECT_BY_PATH (id2, ',')
                       , 2
               ) AS grp
FROM           got_r_num
WHERE           CONNECT_BY_ISLEAF     = 1
START WITH      r_num          = 1
CONNECT BY      r_num          = PRIOR r_num + 1
     AND      id1          = PRIOR id1
;

The main query uses String aggregation to obtain a unique identifier for each group. (The unique ID used above is the list separated by commas of the members in alphabetical order.) Got_r_num of the subquery is useful for making string using CONNECT BY aggregation. If you use another method of aggregation of the chain, you shouldn't have this subquery. (On my system, I would use the STRAGG function defined by the user, that is found in)
http://asktom.Oracle.com/pls/asktom/f?p=100:11:0:P11_QUESTION_ID:2196162600402
.)

Please test (and correct, if necessary) any code before you post.
INSERT statements you posted all contain errors: the word "INSERT INTO * TABLE * relationship...» ». Reln_to and reln_of are each defined as VARCHAR2 (4), but almost all the values you are trying to use more than 4 characters.

With the data for this example:

CREATE TABLE relation (
cust_id varchar2(8),
reln_to varchar2(8),
reln_id varchar2(8),
reln_of varchar2(8) );

INSERT INTO relation(cust_id, reln_to, reln_id, reln_of) VALUES('195228', 'MOTHER',   '195226', 'SON');
INSERT INTO relation(cust_id, reln_to, reln_id, reln_of) VALUES('195229', 'FATHER',   '195226', 'SON');
INSERT INTO relation(cust_id, reln_to, reln_id, reln_of) VALUES('195229', 'FATHER',   '195224', 'DAUGHTER');
INSERT INTO relation(cust_id, reln_to, reln_id, reln_of) VALUES('195226', 'BROTHER',  '195224', 'SISTER');
INSERT INTO relation(cust_id, reln_to, reln_id, reln_of) VALUES('195228', 'WIFE',     '195229', 'HUSBAND');
INSERT INTO relation(cust_id, reln_to, reln_id, reln_of) VALUES('195228', 'DAUGHTER', '195224', 'MOTHER');
INSERT INTO relation(cust_id, reln_to, reln_id, reln_of) VALUES('11807',  'FRIEND',   '201147', 'FRIEND');
INSERT INTO relation(cust_id, reln_to, reln_id, reln_of) VALUES('201147', 'FRIEND',   '195229', 'FRIEND');

the above query gives these results:

GRP
-----------------------------------------------------
11807,195229,201147
195224,195226,195228,195229
11807,201147
195224,195226,195228,195229,201147

This assumes that the output will only contain related items. What happens if you have some entitiy which is not linked to another entity in your application? If you want to include "Islands" like this in your output, you must include them in the data somehow. The best way is probably to have a separate table that had a line per node, if not he was connected to all other nodes. Another way would be to have a line in the relationship that binds each otherwise node not connected to itself.

Tags: Database

Similar Questions

  • How do we improve the SQL Loader for EBS?

    Hi all

    We would like to use some of the new features like the FILLING and the EXPRESSION in the 10g version of SQL * Loader with the concurrent Manager.

    EBS 11i version SQL Loader is 8i.

    How do we improve the SQL Loader for EBS?

    Thanks in advance!

    If you are referring to executable files under ORACLE_HOME 8.0.6, then it cannot be migrated to 10 g / 11 g - upgrade developer 6i with Oracle Applications 11i [125767.1 ID]

    For executable files of database, please visit:

    Interoperability Notes Oracle EBS 11i with Oracle Database 11 g 2 (11.2.0).) [ID 881505.1]
    Oracle Applications Release 11i with Oracle 10 g Release 2 (10.2.0) [ID 362203.1]

    Thank you
    Hussein

  • How can I get the second and third highest salary from the emp table

    How can I get the second and third highest salary from the emp table in the ecah Department
    SQL> ed
    Wrote file afiedt.buf
    
      1  select empno, ename, sal, deptno
      2  from (
      3    select empno, ename, sal, deptno, dense_rank() over (partition by deptno order by sal desc) as rn
      4    from emp
      5    )
      6* where rn in (2,3)
    SQL> /
    
         EMPNO ENAME             SAL     DEPTNO
    ---------- ---------- ---------- ----------
          7782 CLARK            2450         10
          7934 MILLER           1300         10
          7566 JONES            2975         20
          7876 ADAMS            1100         20
          7499 ALLEN            1600         30
          7844 TURNER           1500         30
    
    6 rows selected.
    
    SQL>
    
  • How do I get the toolbar and the sidebar to display in the Finder?

    How do I get the toolbar and the sidebar to display in the finder window? The choices are grayed out in the view.

    Hello!

    You clicked on dock finder, take the cursor to the top and clicked on display and then click on hide menu bar .it toolbar is hidden.

    See the side bar, tool bar again click on view - once again, click Show toolbar.

    Thank you!

  • I need to reinstall my Linux Mint, therfore also Thunderbird. How can I save the addresses and all my messages (copy and again after installation Thunderbird)

    I need to reinstall my Linux Mint 17 companion, therfore also Thunderbird. How can I save the addresses and all my messages (copy and again after Thunderbird installation)?

    You will need to backup your Thunderbird profile.
    _ http://KB.mozillazine.org/Thunderbird: _FAQs_:_Backing_Up_and_Restoring #Manually_back_up_the_profile

  • How can I restart the date and time?

    The date and time on my task bar have become desensitized. How can I restart the date and time?

    I can't access the same system for the date and time preferences.

    Have you tried restarting your Mac?

    See you soon,.

    GB

  • How can I keep the dock and menu bar in the window

    How can I keep the dock and menu bar in the window

    First of all, go to System Preferences and select general:

    Under the dark menu and dock option, uncheck "hide and show the menu bar automatically".

    Go back and select Dock Prefs system:

    Make sure that "automatically hide / show the dock" is not checked.

  • How can I change the date and time on my HP officejet pro

    How can I change the date and time on my HP Officejet pro?

    I'll be gald to help you in this situation.

    What officejet do you have?

    You can go to the settings on the printer screen.

    1. click on Setup

    2. click on Preferences

    3. choose the Date and time

  • How can I change the n and m?

    I signed up for xbox live and put the wrong email address (* address email is removed from the privacy *). How can I change the n and m?

    Hello

     
    The problem is related only to the Xbox. Please visit the link below to find a community that will support what ask you:
    http://support.Xbox.com/en-us/contact-us
  • I've lost the last two digits of my valid key for xp pro sp2 and notebook, how do I get the key and support? where?

    I've lost the last two digits of my valid key for xp pro sp2 and notebook, how do I get the key and support? where?

    Hi Jonathan,.

    Please go to the Microsoft Community Forums.
    After the description of the question, I understand that you have lost the last 2 digits of the product key.
    Let me go ahead and help you with the issue.
     
    Please answer these questions, which will help us to help you best.
    1. How did you get the product key?
    2. the operating system come preinstalled on the computer?
     
    I suggest you for the link and check if that helps:
    How to identify, locate, and replace a product key
     
    See also:

    I hope this helps. If you have any other queries/issues related to Windows, write us and we will be happy to help you further.
  • I bought a used laptop. How can I clean the computer and only the program that the value of the factory. It's a dv4000 HP clubhouse with windows xp

    I bought a used laptop and the previous ovner couldn't remember his password since it's been more than a year she opened a session.  How can I clean the computer and restore it to that only programs that comes with it when buying or facotry settings.  It's a HP Pavilion dv4000 with windows xp?  I have not all discs.

    Go here: http://h10025.www1.hp.com/ewfrf/wc/document?docname=c00006110&tmp_task=useCategory&cc=us&dlc=en&lang=en&lc=en&product=464513 and click on the link that best matches your computer (e.g., Presario, Pavilion and HP Compaq Business PC manufactured and sold in 2006 or later).

    Assuming that your computer is 2006 or later, following the links eventually will lead you to the following directions:


    Reformatting a hard drive should be considered a last resort to solve a problem with a computer system. The hard drive can be reformatted and reinstalled the operating system, using the system PC Suite hard disk recovery. Reformatting the hard drive will overwrite all data on the disk. To preserve the data, back up the data of the user as; letters, leaves of calculation, graphics and emails to an external storage device before you reformat the disk. If the PC does not work in the Windows operating system, see reformatting since the recovery disks .
    Do the following to reformat the hard drive and reinstall the operating system.
    1. Connect the PC to the power adapter.
    2. Close all programs.
    3. Click Start , click principally made programs , select System Restore and click on the PC recovery .
    4. When the recovery options are offered, select the option to perform the Recovery of PC and click Next .
    5. The PC will restart.
    6. When the recovery of the PC of welcome message, click OK to continue.
    7. In the system recovery Panel, select System Recovery . By default, the system will perform a complete reformat and will destroy all user data.
    8. Restart the PC when prompted.
    As alternative method for the steps above, click Advanced Options , select the destructive recovery option and click Next to begin the reformatting process.
    The PC is in original factory State and all data in the system, such as the place and the date must be reconfigured.

    NOTE: any software "trial offer" has been included in the original factory image, it will be reinstalled, but it won't work (the test period will have expired).  If so any trial software is a security antivirus software or others, before connect you to the Internet, you must uninstall (using an uninstall tool provided by the manufacturer if available antivirus software) and then install your own anti-virus software.  So, you should download the uninstallers necessary and installers (or buy the software on CD) using another machine before starting this project.

    NOTE 2: See the following important tips from MS - MVP PA supporter (once the restore is complete, click Start > run > winver > OK to determine what Service Pack you have):

    HOW TO get WinXP SP1 or SP2 fully patched after a 'clean install': http://groups.Google.com/group/Microsoft.public.WindowsXP.General/MSG/a066ae41add7dd2b

    1. download & save the installer for WinXP SP3 on your desktop:
    http://www.Microsoft.com/downloads/details.aspx?FamilyId=5b33b5a8-5E7...

    [Yes, you can jump SP2 if you run WinXP SP1.]

    2. read & take into account:
    http://msmvps.com/blogs/harrywaldron/archive/2008/05/08/Windows-XP-SP...

    3 connected as long as administrator, if necessary, double-click the saved file to install WinXP SP3.  Follow the prompts. Be patient and restart twice when the installation ends.

    4. make the resolution method 2 here (believe me):
    http://support.Microsoft.com/kb/943144

    5. go to http://windowsupdate.microsoft.com . Install all required software, and then click CONTINUE. Select CUSTOM and scan | Install the critical updates of security offered. Still, follow all the instructions.

    [I do NOT recommend install IE7 through Windows Update.]

    6. make sure that automatic updates is enabled; FC.
    http://support.Microsoft.com/kb/306525

    Other references:

    Free unlimited installation and compatibility support is available for Windows XP, but only for the Service Pack 3 (SP3), until April 14-09. Chat and e-mail support is available only in the United States and the Canada.

    Go to http://support.microsoft.com/oas/default.aspx?gprid=1173 . Select "Windows".
    XP"and select"Windows XP Service Pack 3 "

    5 steps to help protect your new computer before going online
    [installation clean = new computer]
    http://www.Microsoft.com/protect/computer/advanced/XPPC.mspx

    Measures to help prevent spyware
    http://www.Microsoft.com/protect/computer/spyware/prevent.mspx

    How TO get Windows XP Gold fully patched: http://groups.Google.com/group/Microsoft.public.windowsupdate/MSG/3f5afa8ed33e121c

    1. download & save the WinXP SP2 installer on your desktop:
    http://www.Microsoft.com/downloads/details.aspx?FamilyID=049c9dbe-3b8...

    [Yes, you can jump SP1]

    1B. download & save the installer for WinXP SP3 on your desktop:
    http://www.Microsoft.com/downloads/details.aspx?FamilyId=5b33b5a8-5E7...

    2. read & take into account:
    http://msmvps.com/blogs/harrywaldron/archive/2008/05/08/Windows-XP-SP...

    3. administrator, if necessary, double-click the saved file to install Windows XP * SP2 *.  Follow the prompts. Be patient and restart twice when the installation ends.

    3B. logged on as administrator, if necessary, double-click the saved file to install WinXP SP3.  Follow the prompts. Be patient and restart twice when the installation ends.

    4. make the resolution method 2 here (believe me):
    http://support.Microsoft.com/kb/943144

    5. go to http://windowsupdate.microsoft.com . Install all required software, and then click CONTINUE. Select CUSTOM and scan | Install the critical updates of security offered.  Still, follow all the instructions.

    [I do NOT recommend install IE7 through Windows Update.]

    6. make sure that automatic updates is enabled; FC.
    http://support.Microsoft.com/kb/306525

    References with dubbing:

    Free unlimited installation and compatibility support is available for Windows XP, but only for the Service Pack 3 (SP3), until April 14-09. Chat and e-mail support is available only in the United States and the Canada.  Reach
    http://support.Microsoft.com/OAS/default.aspx?gprid=1173 | Select "Windows".
    XP"and select"Windows XP Service Pack 3 "

    Protect your PC!
    http://www.Microsoft.com/athome/security/computer/default.mspx

    Learn how to protect your PC by taking the three simple steps
    http://www.Microsoft.com/downloads/details.aspx?FamilyId=3AD23728-497...

  • How can I recover the files and programs before a system restore? my document folders are all empty and my programs will not open!

    How can I recover the files and programs before a system restore?  my document folders are all empty and my programs will not open!

    I know they are there and hidden somewhere, but this restoration took my settings of the computer to 2004! We lost all our data, our software that we use to operate our company at home, our quickbooks that has all our financial data, family photos, videos, Outlook will work not so no e-mail, all turned in time and 6 years ' worth of documents have disappeared.

    What happeneded was that I was trying to upgrade to quickbooks software when I got an error, I cancelled out of the facility, then restarted and that's when I got an error hal.dll something? I thought about a system restore would allow me to go back in time to just before when the error occurred, but he basically took my computer back to factory settings, so all the programs that I installed and paid hundreds of dollars for the past six years has disappeared, Microsoft Office and very expensive construction estimating software that we wil have to pay once AGAIN for If I can't get this program working properly.

    I am a fool! I can't believe what's happening.

    Someone at - it instructions on how I can retrieve either the programs and data that I need specifically or how to go and seek shade from the files I read online on and restore my computer for about 12 hours ago?

    Thank you all!

    Sally in Georgia

    Looks like you did a system rather than a system restore recovery.  If you have inserted a disc supplied with your machine and booted from it, you probably erased the disc and installed the original operating system provided with your computer.  Usually, a step in this recovery is to do a full format or quick of your drive.  If you did a full format, your data is permanently lost.  With a quick format, your data may still be there, somewhere but inaccessible by normal means.  Applications must generally be installed, in order to recover the files probably will not help unless you have the original installation media.  Your other data files (images, videos, financial data) may be salvageable, but at this point, it would be advisable to take your computer or your disk to a business/professional who is experienced with data disc recovery.  It will be cheap and there is no guarantee.

    Impatient, remember investigate you a backup strategy for your computer and critical data.  Hard drives can go wrong with little or no warning.

    HTH,
    JW

  • How do you set the time and date on fax for hp officejet 4622

    How do you set the date and time on the fax for hp officejet head 4622

    Hi Chrisharrison23,

    Date and time should be under configuration tools or fax setting in the printer control panel.

    OfficeJet 4620:

    1. click on the key icon on the control panel of the printer

    2. go to tools

    3 scroll to the date and time

    I would like to know if you are able to locate the setting?

    Officejet 4622: Set the date and time

     
  • My old MSN e-mail account was hacked and blocked. I created a new Windows Live Hotmail account. How can I access the emails and blocked my old account contact information?

    My old MSN e-mail account was hacked and blocked. I created a new hotmail account. How can I access the emails and blocked my old account contact information?  Thanks for any help!

    original title: hacked E-mail

    Hi BigByrd,

    I'm afraid it's almost impossible to access the old MSN your Hotmail account is newly created account.
    See you soon ~
  • How can I reset the time and date on Windows Vista?

    How can I reset the time and date on Windows Vista

    http://www.ehow.com/how_2056384_change-date-time-Windows-Vista.html

    http://www.Vistax64.com/tutorials/114989-date-time-change.html

    Read the information in one of the links above.

    See you soon.

    Mick Murphy - Microsoft partner

  • There was a pre installed windows 7 on my device. How can I download the windows and write on a dvd.

    * O.T. > window 7

    Hello

    When I bought my laptop, there was a pre installed windows 7 on it with the product key.

    Unfortunately I lost my recovery DVDs as I can't do any other DVD recovery because of a problem with windows.

    I want to re install the window, so first I download my original windows from Microsoft.

    Please let me know how I can download the windows and write on a dvd.

    Thnaks

    Microsoft provides no support for pre-installed OS.  You must obtain from the computer manufacturer.

Maybe you are looking for