Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

TSQL Search and Extract Different Activity Sequences in a given set of data

$
0
0

Hello,

I have the following tables

1) EVENT_NAMES (EVENT_NAME_ID, EVENT_NAME)

1 PM
2 PM WASH
3 Backlog

 

2) EVENT_NAME_SEQUENCE(SeqParentKey, Event_Name_ID,Seqorder)

Data looks like below

1  2   1
1  1   2
1  3   3
2  1   1
2  3    2

Event_NAME_Sequence contains the Sequence which I need to find in the RAW data table. Currently there are 2 sequence which I need to find (identified by SeqParentKey). I need to find Seq(PM WASH, PM and BACKLOG) and Seq(PM,Backlog) in RAW DATA TABLE for a particular Equipment

RAW DATA TABLE looks like below

3) EVENT_NAMECHANGE( EQMT, EVENT_NAME, STARTTIME, ENDTIME)
Sample data is as shown below

MACH_101    PM WASH    2014-01-01 10:00:00.000    2014-01-01 11:00:00.000
MACH_101    PM              2014-01-01 11:00:00.000    2014-01-01 15:00:00.000
MACH_101    Backlog      2014-01-01 15:00:00.000    2014-01-01 16:00:00.000
MACH_103     PM WASH    2014-01-01 10:00:00.000    2014-01-01 11:00:00.000
MACH_103     PM              2014-01-01 11:00:00.000    2014-01-01 15:00:00.000
MACH_106    PM               2014-01-01 15:00:00.000    2014-01-01 16:00:00.000
MACH_106    Backlog      2014-01-01 16:00:00.000    2014-01-01 18:00:00.000
MACH_101    PM               2014-01-02 15:00:00.000    2014-01-02 16:00:00.000
MACH_101    Backlog      2014-01-02 16:00:00.000    2014-01-02 18:00:00.000
MACH_900     PM               2014-01-02 15:00:00.000    2014-01-02 16:00:00.000
MACH_900     REPAIR      2014-01-02 16:00:00.000   2014-01-02 18:00:00.000

The records in BOLD is the output I would need

Below are the scripts for table and data

CREATE TABLE EVENT_NAMES
(
EVENT_NAME_ID int,
EVENT_NAME varchar(100)
)

INSERT INTO EVENT_NAMES values(1,'PM')
INSERT INTO EVENT_NAMES values(2,'PM WASH')
INSERT INTO EVENT_NAMES values(3,'Backlog')

CREATE TABLE EVENT_NAME_SEQUENCE
(
SeqParentKey int,
EVENT_NAME_ID int,
SeqOrder int

)



INSERT INTO EVENT_NAME_SEQUENCE values(1,2,1)
INSERT INTO EVENT_NAME_SEQUENCE values(1,1,2)
INSERT INTO EVENT_NAME_SEQUENCE values(1,3,3)
INSERT INTO EVENT_NAME_SEQUENCE values(2,1,1)
INSERT INTO EVENT_NAME_SEQUENCE values(2,3,2)

CREATE TABLE EVENT_NAMECHANGE
( 
EQMT varchar(100), 
EVENT_NAME varchar(100), 
STARTTIME DATETIME,
 ENDTIME DATETIME
 )

INSERT INTO EVENT_NAMECHANGE VALUES('MACH_101','PM WASH','2014-01-01 10:00:00.000','2014-01-01 11:00:00.000')
INSERT INTO EVENT_NAMECHANGE VALUES('MACH_101','PM','2014-01-01 11:00:00.000','2014-01-01 15:00:00.000')
INSERT INTO EVENT_NAMECHANGE VALUES('MACH_101','Backlog','2014-01-01 15:00:00.000','2014-01-01 16:00:00.000')
INSERT INTO EVENT_NAMECHANGE VALUES('MACH_103','PM WASH','2014-01-01 10:00:00.000','2014-01-01 11:00:00.000')
INSERT INTO EVENT_NAMECHANGE VALUES('MACH_103','PM','2014-01-01 11:00:00.000','2014-01-01 15:00:00.000')
INSERT INTO EVENT_NAMECHANGE VALUES('MACH_106','PM','2014-01-01 15:00:00.000','2014-01-01 16:00:00.000')
INSERT INTO EVENT_NAMECHANGE VALUES('MACH_106','Backlog','2014-01-01 16:00:00.000','2014-01-01 18:00:00.000')
INSERT INTO EVENT_NAMECHANGE VALUES('MACH_101','PM','2014-01-02 15:00:00.000','2014-01-02 16:00:00.000')
INSERT INTO EVENT_NAMECHANGE VALUES('MACH_101','Backlog','2014-01-02 16:00:00.000','2014-01-02 18:00:00.000')
INSERT INTO EVENT_NAMECHANGE VALUES('MACH_900','PM','2014-01-02 15:00:00.000','2014-01-02 16:00:00.000')
INSERT INTO EVENT_NAMECHANGE VALUES('MACH_900','Repair','2014-01-02 16:00:00.000','2014-01-02 18:00:00.000')
Any help on creating a Generic logic would help as in future there would be Event sequences added to EVENT_NAME_SEQUENCE table



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>