Query to find the previous activity in STM

I have a data base to reflect a State of Transition (STM) Machine. It contains 3 tables: activity, node, and Transition.

Tables of knots and Transition are the configuration of the STM. They define the different possible States and the possible transitions between States.

The way that I take through the STM is recorded in the activity Table. A record is created for each visited node.

I need a query that uses the id of my current activity to find my previous activity, i.e. the activity that precedes me directly. The following rules apply:
-My Id is meaningless
-It should work for different configurations of the transition and the node tables, but:
-The starting node is always the node "A".
-There is no recursive transition, for example ('B', 'B');
-There is no circular transitions, for example ('B', 'C') and ('C', 'B')
-Transition and node tables will never change between the creation of the first activity and the execution of the query.
-The path reflected in the activity table is always a valid path.
-My current activity is always the last activity.

For all data below, find the previous activity of activity with id = 4. The correct answer is "C".
DROP TABLE Transition;
DROP TABLE Activity;
DROP TABLE Node;

CREATE TABLE Node
(
     Id VARCHAR2(1) PRIMARY KEY
);

CREATE TABLE Activity
(
     Id Number(8,0) PRIMARY KEY,
     Node_Id VARCHAR2(1)
);

CREATE TABLE Transition
(
     FromNode_Id VARCHAR2(1),
     ToNode_Id VARCHAR2(1)
);

ALTER TABLE Activity
ADD FOREIGN KEY
(Node_Id) REFERENCES Node(Id);

ALTER TABLE Transition
ADD FOREIGN KEY
(FromNode_Id) REFERENCES Node(Id);

ALTER TABLE Transition
ADD FOREIGN KEY
(ToNode_Id) REFERENCES Node(Id);

INSERT INTO Node VALUES ('A');
INSERT INTO Node VALUES ('B');
INSERT INTO Node VALUES ('C');
INSERT INTO Node VALUES ('D');

INSERT INTO Transition VALUES ('A','B');
INSERT INTO Transition VALUES ('B','C');
INSERT INTO Transition VALUES ('B','D');
INSERT INTO Transition VALUES ('C','D');

INSERT INTO Activity VALUES (1,'A');
INSERT INTO Activity VALUES (2,'B');
INSERT INTO Activity VALUES (3,'C');
INSERT INTO Activity VALUES (4,'D');
Desired output:
ID
-
C

Hello

Assuming that all Activity_id tells us nothing on the way, but all lines of activity together to form a single path, we can get results this way:

WITH     all_paths    AS
(
     SELECT     'A' || SYS_CONNECT_BY_PATH (a.Node_id, '/')
              || '/'     AS node_id_path
     FROM     Transition  t
     JOIN     Activity    a  ON  a.Node_Id  = t.ToNode_Id
     WHERE     LEVEL             = (
                            SELECT  COUNT (*)
                         FROM     activity
                         ) - 1
     START WITH  t.FromNode_Id  = 'A'
     CONNECT BY  t.FromNode_Id  = PRIOR t.ToNode_Id
)
SELECT       REGEXP_SUBSTR ( node_id_path
                 , '([^/]+)/' || (
                                    SELECT  Node_Id
                             FROM    activity
                             WHERE   Id     = 4  -- target_id
                                )
                         || '/'
               , 1
               , 1
               , NULL
               , 1
               )     AS prev_id
FROM       all_paths
;

This means, there is some character (I used "/" above) that we know never appears in Activity.Node_Id.

Since there are no loops in Transition, it cannot be more than 1 way that involves all activity lines. If there are N lines in operation, this full path will be one that extends to LEVEL = N - 1. (It is not N, N - 1, because the join between the activity and the Transition will always leave 1 line in activity with Node_Id = 'A'). As soon as we have the full path (which is node_id_path in 1 row, produced by all_paths), we just need to guess what was the Node_Id, which corresponds to the id of the target (4 in this example) and find the previous of the delimited node_id_path node_id.

Tags: Database

Similar Questions

  • bougth a second hand iPhone 6 how find the previous owner using the serial number?

    I bought a second hand iPhone 6 and he holds a lock to iCloud how to find the previous of this iPhone owner so that I can email him?

    I don't have a serial number of the phone how to use it so that I can follow the former owner?

    Unfortunately, you can not. Take it to where you bought it for a refund.

    See you soon

    Pete

  • After you have configured remote access on Server 2003, I am unable to find the 'users Active Directory & computers'.

    am setting up remote access on the MS 2003 Server following the white paper, but can not find the 'users Active Directory & computers' to set the ip this part has been renamed or hidden somewhere?

    original title: MS Server 2003

    Post in the Windows Server Forums:
    http://social.technet.Microsoft.com/forums/en-us/category/WindowsServer/

  • SQL query to find the total number of source based nonsource passangersbetween source and destination station and passenger station on the same chekindate

    Hello

    SQL query to find the total number of source based nonsource passangersbetween source and destination station and passenger station on the same chekindate.

    Please help on this script and let me know if you need more details.

    ---

    You use a SELECT statement.  Let me know if you need more details.

  • What is the query to find the name of all applications for all EBS R12.1.3 modules?

    What is the query to find the name of all applications for all EBS R12.1.3 modules?

    With regard to:

    Mr. Shahzad Saleem

    Try:

    SELECT * FROM fnd_concurrent_programs_vl;

  • Query to find the Pages and the list of permissions associated with a particular role in PS

    I want a query to find the Pages and the list of permissions associated with specific roles in PS.

    For example, if we see the role of manager accounts payable, it conatins Pages and the list of permissions.

    But, to get everything in EXCEL sheet by Manuel priocess's BIG job. So, can someone give me the query.

    Please try under queries

    The roles assigned to the list of Perm:

    SELECT B.ROLENAME

    OF PSCLASSDEFN A, PSROLECLASS B

    WHERE (A.CLASSID = B.CLASSID

    AND A.CLASSID =: 1).

    List of Perm pages can access:

    SELECT B.MENUNAME, B.BARNAME, B.BARITEMNAME, B.PNLITEMNAME, C.PAGEACCESSDESCR, B.DISPLAYONLY

    OF PSCLASSDEFN A, PSAUTHITEM B, PSPGEACCESSDESC C

    WHERE (A.CLASSID = B.CLASSID

    AND A.CLASSID =: 1

    AND B.BARITEMNAME > ' '

    AND B.AUTHORIZEDACTIONS = C.AUTHORIZEDACTIONS)

  • Is there a way to get the first Pro CS6 with still now I can't find the previous, offered versions with the subscription of the CC 2015?

    Is there a way to get the first Pro CS6 with still now I can't find the previous, offered versions with the subscription of the CC 2015? What is recommended for creating DVDs with interactive menus?

    Instructions again, cloud or version https://forums.adobe.com/thread/1992717 serial number
    -a of notes on different versions of cloud and step by step on these differences
    -contains information about the separate library download which is necessary
    -CS6 is the last reminder, see here #8 why http://forums.adobe.com/thread/1337952?tstart=0

    Still and 10 Windows https://forums.adobe.com/thread/2015461 use the compatibility mode of Windows 8
    problem/solution to install Encore CS6 https://forums.adobe.com/thread/1934087

  • query to find the dependent task, attached to the task on a response in OIM 11 g

    can someone help me to do a sql query to find the dependent task, attached to the task on a response in OIM 11 g

    Published by: user13331347 on Sep 3, 2012 14:09

    Use under query to find the dependent task in OIM 11 g: -.

    Select pkg.pkg_name, mil.mil_name, rsc.rsc_data, rsc.sta_key, sta.sta_status, sta.sta_bucket, mil2.mil_name
    pkg pkg, tos tos, mil mil, mil mil2, rsc rsc, sta sta, rgm the rgm
    where pkg.pkg_key = tos.pkg_key
    and tos.tos_key = mil.tos_key
    and mil.mil_key = rsc.mil_key
    and rsc.sta_key = sta.sta_key
    and rgm.rsc_key = rsc.rsc_key
    and rgm.mil_key = mil2.mil_key
    order of pkg.pkg_name, mil.mil_name, rsc.rsc_data, sta.sta_status, mil2.mil_name

  • Query to find the coordinates of employee salary

    Hello

    Could someone help write the query to find the salary of the employee details.

    Thanks in advance.

    This should help you get started:

    SELECT papf.full_name
    papf.email_address
    ppp.proposed_salary_n salary
    OF per_pay_proposals ppp
    per_all_assignments_f ADP
    per_all_people_f women's wear
    WHERE ppp.assignment_id = paaf.assignment_id
    AND paaf.assignment_type = 'E '.
    AND paaf.primary_flag = 'Y '.
    AND paaf.person_id = papf.person_id
    AND nvl (papf.current_employee_flag, 'n') = 'Y '.
    AND trunc (sysdate) BETWEEN
    PPP.change_date AND ppp.date_to
    AND trunc (sysdate) BETWEEN
    PAAF.effective_start_date AND paaf.effective_end_date
    AND trunc (sysdate) BETWEEN
    PAPF.effective_start_date AND papf.effective_end_date;

  • Find the previous quarter

    Hello
    I'm looking for a function that will help me find the previous quarter, then the format base quarter is YYYY | » Q'|| Q.
    For example, when quater_base = 2011Q 1, I need the function returns quarter_previous = 2010Q 4.

    I think I need to convert YYYY | » Q'|| Q on a date and then subtract from 91 days or use add months, but I need help conversion YYYY | » Q'|| Q for a date.
    Thank you very much in advance for your help.
    TERI

    Hi, Teri,

    Welcome to the forum!

    user13426561 wrote:
    Hello
    I'm looking for a function that will help me find the previous quarter, then the format base quarter is YYYY | » Q'|| Q.
    For example, when quater_base = 2011Q 1, I need the function returns quarter_previous = 2010Q 4.

    I think I need to convert YYYY | » Q'|| Q on a date and then subtract 91 days or use add months,.

    Q1 is normally 90 days long. If you subtract 91 days March 1, you return of the last two quarters.
    Q2 is always long for 92 days. If you subtract 91 days from May 31, you have not changed shifts.
    Better use ADD_MONTHS.

    but I need help conversion YYYY | » Q'|| Q for a date.

    If s is a string of 6 characters such as '2011 Q 1' (i.e. Q1 2011), then

    ADD_MONTHS ( TRUNC ( TO_DATE ( SUBSTR (s, 1, 4)
                        , 'YYYY'
                        )
                 , 'YEAR'
                 )
            , 3 * ( TO_NUMBER (SUBSTR (s, 6, 1))
               - 1
               )
            )
    

    is the start DATE of this quarter.
    Subtract 3 months ago, and convert it to a string such as "2010 Q 4":

    TO_CHAR ( ADD_MONTHS ( TRUNC ( TO_DATE ( SUBSTR (s, 1, 4)
                               , 'YYYY'
                               )
                        , 'YEAR'
                        )
                   , 3 * ( TO_NUMBER (SUBSTR (s, 6, 1))
                      - 2     -- NOTE: subtracting 2, to get previous quarter
                      )
                     )
         , 'YYYY"Q"Q'
         )
    

    These strings come Is it possible that you could change the strings to have a month ('01', '04', '07' or 10') instead of the district, or to include both? This work would be much simpler!

    It's a shame that you can't use 'Q' in TO_DATE.
    You might consider a user-defined function to perform the conversion above. It will make your code much cleaner, but it will be slower.

    Thank you very much in advance for your help.
    TERI

    Published by: Frank Kulash, January 4, 2011 18:30
    Corrected the two expressions

  • Windows 7 Image Backup - where can I find the previous image after new backup?

    My previous hard drive died, and I replaced it.

    Good news: I did a backup of the system before the death of completed HDD.  The image is under "WindowsImageBackup" folder on my external drive.

    Then I replaced the HARD drive, picked up at the factory reset.

    Before restoring the image backup, I took a step extra stupid: I thought that I should save its current state before as I have restore the previous state in case any thing wrong, so I started to sign--> system and security--> backup and restore--> backup now

    Bad news: the new backup image sub-folder of "WindowsImageBackup" even erased my previous image!  They have different timestamps, so different names, but the previous image has disappeared after a new creation.  In fact, I killed the new backup process immediately after that I was disappearing from the previous image.  He has not finished, but I can't find my previous image!

    BTW, there is a lot of space on my drive.  It is only 20% used!

    Are there ways I can retrieve my previous image?

    BTW, I use Windows 7 Home Premium.  By default, all parameters are as I do factory reset.

    Suggestions and help is very appreciated!

    Hello

    With the publication of the description, I understand that you have accidentally deleted the system image backup file. I've certainly you will help answer your query.

    I wish to inform you that there is no way to get the previous system back image - which has already been replaced however, you can try a few steps to get the old image to the top, although there is no guarantee that it will work but still, you can try and see if it helps.

    First of all, I suggest you to rename the WindowsImageBackup to WindowsImageBackup. Old then follow the screenshot and select the option highlighted in yellow color select another backup to restore files of.

    Hope that this help, please write us back for any further assistance on this point, we will be happy to help you further.

  • find the previous owner info (icloud)

    Hi, how can I find the name and surname of the previous owner of my second hand iphone?

    I have the apple ID, but find my iphone is on and I do not know password!

    Thank you

    You can't get from Apple or anyone here. You must contact the former owner, either through the person, you bought the phone you can not. Confidentiality issues prohibit Apple or the cellular carrier to release any of this information. If the person you bought isn't the former owner, then there is the possibility that the unit you have purchased has been stolen.

  • Where can I find the previous version of the BIOS to 2.54?

    I accidentally updated the BIOS for my Lenovo E430. I want to go back to the BIOS, so I need the previous version. No one knows the number of the previous version and where I can find it on the Lenovo Web site?

    Nice day.

    Please take a look at the support site for your machines:

    http://support.Lenovo.com/us/en/products/laptops-and-netbooks/ThinkPad-edge-laptops/ThinkPad-edge-E4...

    If you select the CD or bootable Windows Update link and scroll to the bottom of the resulting page, you'll see a grid of earlier versions, as well as their documentation files.

    As with any update BIOS, please read the documentation carefully.

    I hope this helps a little.

    Kind regards.

  • Query to find the name of the partition

    Hi all

    I'm a little surprised to see that it is not a column I can use to query the name of the partition for a table, in the case of partition of Internval per day...

    I'm looking for a simple query find the name of the partition for the word partition of today ' today... Am I missing something?

    Ask something like...

    Select nom_partition from dba_Tab_partitions where table_name = 'XYZ' and TO_DATE(HIGH_VALUE,'DD-MON-YYYY') = April 2, 2014"

    High value being a long column, lets you convert CHAR or DATE?

    Thanks in advance

    Then, you create a copy of dba_tab_partitions, since you can only convert the LONG data during an insertion type or create the table.

    From Dan article: «just...» If you have a LONG, and you want to appear more than it, you must use TO_LOB to convert it during either an INSERT INTO

    or a CREATE TABLE As . Everything else is just wasting time. "

  • Query to find the sales order

    Hi all
    I need a query to find sales order number against which a serial number is processed.

    Can ny helps an onthis.

    Thank you

    Hello
    Try the following question

    Select wdd.source_document_number
    Of wsn wsh_serial_NUMBERS,.
    wsh_delivery_details JMD
    Where wsn.delivery_detail_id = wdd.delivery_detail_id
    And & wsn.fm_serial_number series
    and nvl (wsn.ti_serial_number, wsn.fm_serial_number)

    Thank you
    Claire

Maybe you are looking for