SQL statement for the click stream analysis
I want to divide the clicks of the user session. A session is defined to include all of the clicks a certain user that occur within 60 seconds of each other:create table clickstream
( t date
, userid number(10) );
insert into clickstream values (to_date('5-9-2009 10:00:00','dd-mm-yyyy hh24:mi:ss'), 2);
insert into clickstream values (to_date('5-9-2009 10:00:24','dd-mm-yyyy hh24:mi:ss') , 2);
insert into clickstream values (to_date('5-9-2009 10:01:23','dd-mm-yyyy hh24:mi:ss') , 2);
insert into clickstream values (to_date('5-9-2009 10:02:40','dd-mm-yyyy hh24:mi:ss') , 2);
insert into clickstream values (to_date('5-9-2009 0:58:24','dd-mm-yyyy hh24:mi:ss') , 7);
insert into clickstream values (to_date('5-9-2009 2:30:33','dd-mm-yyyy hh24:mi:ss') , 7);
commit;
The output should be:Time t Userid Session
10:00:00 2 0
10:00:24 2 0
10:01:23 2 0
10:02:40 2 1
00:58:24 7 0
02:30:33 7 1
This query divides the clickstream sessions:select to_char(t,'hh24:mi:ss') t
, userid
, count(*) over
( partition by userid order by t RANGE between (1/(24*60)) preceding and current row) count
from clickstream
order by userid,t
T USERID COUNT
-------- ---------- ----------
10:00:00 2 1
10:00:24 2 2
10:01:23 2 2
10:02:40 2 1
00:58:24 7 1
02:30:33 7 1
Because when the COUNT = 1 a new session is started by a given user, but I do not know how to proceed?
Seems to me, you can do a bit simpler:
SQL> select userid, t, nvl (sum (s) over (partition by userid order by t), 0) sessionid
from (select userid,
t,
case when t - lag(t) over (partition by userid order by t) > 1 / (24 * 60) then 1 end s
from clickstream
order by userid, t)
/
USERID T SESSIONID
---------- --------------------- ----------
2 05.09.2009 10:00:00 0
2 05.09.2009 10:00:24 0
2 05.09.2009 10:01:23 0
2 05.09.2009 10:02:40 1
2 05.09.2009 10:02:50 1
2 05.09.2009 10:05:40 2
7 05.09.2009 00:58:24 0
7 05.09.2009 02:30:33 1
8 rows selected.
Tags: Database
Similar Questions
-
SQL statement for the hierarchy of the Organization
Nice day
I was wondering if there is a script/sql statement that would generate the hierarchy of the Organization (similar to the organization hierarchy editor) in Oracle EBS HRMS 11.5.10
Thank you
ElmerI was wondering if there is a script/sql statement that would generate the hierarchy of the Organization (similar to the organization hierarchy editor) in Oracle EBS HRMS 11.5.10
Please see if (how to extract HR organization hierarchies in SQL? [REF 463359.1]) help.
Thank you
Hussein -
SQL statement for the materialized view?
Can someone please tell me what SQL statement creates a materialized view?Syntactically, Yes. Otherwise Yes too (as long as you don't mind the Cartesian join with State B).
Why not just try and see what happens?Concerning
Etbin
-
Build SQL statements for the conversion error
Hello
I started a conversion after having dropped any object that has been marked blocking conversion (and refreshed + re-read the database).
But the conversion fails with:
Hello
At the revealid of track that he was opening and closing of session trigger failed.
I disabled the trigger, and now the conversion has kciked off as expected.
Thank you.
BR,
Kim
-
Single SQL query for the analysis of the date of customs declaration under the table of Stock codes
Dear all,
Please tell us a single SQL query for the below,
We have a Table of Stock as shown below,
STOCK_TABLE
ITEM_CODE
(item code)
BAT_NO
(lot no.)
TXN_CODE
(transaction code)
DOC_NO
(number)
BOE_DT
(date of the customs declaration)
I1
B1
I1
I2
I3
B70
I4
B80
I5
B90
T102
1234
JULY 2, 2015
I6
B100
We have to find the date of customs declaration (i.e. the date when the items have come under this particular table) for items that are not attached to any document (that is, who have TXN_CODE, DOC_NO and BOE_DT fields with a NULL value).
For each item in the table of actions, which is not attached to any document, the customs declaration date is calculated as follows.
- If (code section, lot number) combination is present under HISTORY_TABLE, the date of customs declaration will receive the UPDT_DT, the transaction code (TXN_CODE) is an IN or transactions (which can be analyzed from the TRANSACTIONS table).
- If (code section, lot number) combination is NOT currently at the HISTORY_TABLE (or) the transaction code respective to item - batch number combination code is an operation then customs declaration date will be the date of the document (DOC_DT) that we receive from one of the 3 tables IN_TABLE_HEAD that contains the element of that particular lot.
- If the case 1 and case 2 fails, our customs declaration date will be the last date of document (DOC_DT) that we receive from one of the 3 tables IN_TABLE_HEAD containing that particular item and the BAT_NO in expected results will be that corresponding to this document, as appropriate, to another NULL.
- If the case 1 or case 2 is successful, the value of the last field (in the output expected, shown further below) BATCH_YN will be 'Y', because it fits the lot. Otherwise it will be 'n'.
-
Pinning on SQl statement in the library cache
Hello
Do to pin frequently used SQL statement to the shared pool...
I know that dbms_shared_pool.keep does the job for PL/SQL, but on simple SQL statements.
Experts from the clues?
Thank you
KingSee more of scripts:
-- Get address and hash_value of SQL statement select address, hash_value from v$sql where sql_text like 'your query to keep'; ------------------------------------------------------------------------------------------------ 00000000858CA2B0 2239673969 exec dbms_shared_pool.keep('00000000858CA2B0, 2239673969','C');
I think you want to pin the SQL statement that is specific to the moment analysis of long abovid.
I see no reason to pin the SQL more.Dion Cho
-
Is it possible to make the condition depending on the sql statements in the data models
Hi all
Is it possible to include the condition based on sql statements in the data models.
For example
Is something like this? Also, the good doc is available for ' how to take full advantage of the "data models" in BI Publisher?if (some parameter is not null) <sqlstatement name="STATEMENT_1"> ... </sqlstatement> else <sqlstatement name="STATEMENT_2"> ... </sqlstatement>
Thank you
-SookieHello Sookie,
I couldn't find the time to get a data model of demonstration of work for you, but I'll try to explain.First, write a PL/SQL package. Make sure that you set all the parameters of model of data such as a global variable in the default PL/SQL package.
CREATE OR REPLACE
package as employee
function BeforeReportTrigger return Boolean;
query_text varchar (2000);
number of p_DEPTNO;
END;
/CREATE OR REPLACE
package as body employee
function BeforeReportTrigger return Boolean IS
StartIF (p_DEPTNO = 10) THEN
query_text: = select col1, col2, col3 from HR.
elsif (p_DEPTNO = 20) THEN
query_text: = select col1, col2, col3 hr_history.
on the other
query_text: = select col1, col2, col3 hr_history1.
end if;
Returns true;
end;
/Use this package in the default package in your data model. Check the "defaultPackage ="employee"in the following data model header.
Sample data model
------------------------------
--
--
--
--
Before running the query SQL, data engine reads the "before the release of the report" and all the texte_requete argument based on the p_DeptNo value. When executing the Q1, engine sqlQuery analyze the query ' & quert_text and replace it with the actual value. For example if the p_deptno = 10, the query will be "select col1, col2, col3 from HR.
Try it...
-
Try to run a SQL statement through the window of OEM 12 c run SQL. Statement fails
All,
I have a 11g database. When it is initially installed, I installed it in $ORACLE_BASE = / orabin/app/oracle/product and $ORACLE_HOME = $ORACLE_BASE/11gDB_R2. This is the version 11.2.0.1 to give birth to 11.2.0.3.
Recently, I installed 11.2.0.4 in $ORACLE_BASE/11.2.0.4. Then, I upgraded my database at 11.2.0.4. Everything works fine.
All my scripts on the linux host logon changed to point my environment at $ORACLE_BASE/11.2.0.4 House and my $PATH when I connect to the linux host is: /orabin/app/oracle/product/11.2.0.4/bin:/orabin/app/oracle/product/11.2.0.4/OPatch:/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/oracle/bin
On my OEM Console, the database appears as 11.2.0.4 and me can manage very well.
However, when I try to use the window of the OEM Console to execute SQL to execute any type of SQL statement, it fails.
The failure error is:
Could not launch/orabin/app/oracle/product/11gDB_R2/bin/sqlplus-s/nolog: no such file or directory
It would be a mistake since the 11gDB_R2 directory tree (the 11.2.0.3 software) has been removed from the host, now that the database is set to level and working as 11.2.0.4.
I wonder if somewhere in the OEM software, there is always a pointer to the old ORACLE_HOME? I can't find anything in the .bashrc or other shell scripts that are executed when a Shell session on the host computer which were still the reference to the old directory of 11gDB_R2 so I'm a bit puzzled.
I have 8 guests, and all have been improved in the same way at 11.2.0.4. For some databases, the OEM window run SQL is executing the SQL code.
Someone has some ideas on this?
Go to the Configuration of the analysis of the database (when you set the password) and update of the Oracle home. Also do this for all targets listener on this host as well.
-
Functions in pipeline for the csv data analysis?
Hi all
I currently have a pl/sql procedure that is used to load and parse a CSV file in a table of database within the Apex.
Downloading csv files are quite large (nearly 1 million rows or more) and there is a time of significant waiting for the course ends. I tried both Wizard 4.2 data that was very slow loading and the apex plugin excel2collection who timed out/never finished.
I heard functions in pipeline and how they can offer great time savings for insert instructions where the database lines have no interconnect/dependencies to each other.
My question is, would the data through pipes to offer me a gain with my time insert statements, and if so someone could help me to implement? The current procedure is listed below, less any code validation etc. for readability. The CSV is first uploaded to a table in a BLOB file before be analyzed by the procedure.
-- Chunk up the CSV file and split into a line at a time rawChunk := dbms_lob.substr(bloContent, numChunkLength, numPosition + numExtra); strConversion := strConversion || utl_raw.cast_to_varchar2(rawChunk); numLineEnd := instr(strConversion,chr(10),1); --This will return 0 if there is no chr(10) in the String strColumns := replace(substr(strConversion,1,numLineEnd -numTrailChar),CHR(numSpacer),','); strLine := substr(strConversion,1,numLineEnd); strLine := substr(strLine,1,length(strLine) - numTrailChar); -- Break each line into columns using the delimeter arrData := wwv_flow_utilities.string_to_table (strLine, '|'); FOR i in 1..arrData.count LOOP --Now we concatenate the Column Values with a Comma strValues := strValues || arrData(i) || ','; END LOOP; --Remove the trailing comma strValues := rtrim(strValues,','); -- Insert the values into target table, one row at a time BEGIN EXECUTE IMMEDIATE 'INSERT INTO ' || strTableName || ' (' || strColumns || ') VALUES (' || strValues || ')'; END; numRow := numRow + 1; --Keeps track of what row is being converted -- We set/reset the values for the next LOOP cycle strLine := NULL; strConversion := null; strValues := NULL; numPosition := numPosition + numLineEnd; numExtra := 0; numLineEnd := 0; END IF; END LOOP;
Apex-user wrote:
Hi Chris,
I'm trying to expand your code to use more tou both current columns, but having trouble with the format here...
- While (l_clob) dbms_lob.getlength > l_off and l_off > 0 loop
- l_off_new: = instr (l_clob, c_sep, l_off, c_numsep);
- line (csv_split_type)
- substr (l_clob, l_off, instr (l_clob, c_sep, l_off)-l_off)
- , substr (l_clob, instr (l_clob, c_sep, l_off) + 1, l_off_new - instr (l_clob, c_sep, l_off) - 1)
- ));
- l_off: = l_off_new + 2; -to switch c_sep and line (10 sep
How can I add more columns to this code? I'm mixed with all segments of substr and instr.
I've done a rewrite on it (12 sec for 50,000 lines, 4 columns ~ 7 MB, 2.2 sec for 10,000 lines)
create or replace function get_csv_split_cr (blob p_blob)
return csv_table_split_type
pipelined
as
c_sep constant varchar2 (2): = "";
c_line_end constant varchar2 (1): = Chr (10);
l_row varchar2 (32767).
number of l_len_clob;
number of l_off: = 1;
CLOB l_clob;
-below is used only for the call of dbms_lob.converttoclob
l_src_off pls_integer: = 1;
l_dst_off pls_integer: = 1;
number of l_ctx: = dbms_lob. DEFAULT_LANG_CTX;
number of l_warn: = dbms_lob. WARN_INCONVERTIBLE_CHAR;
Start
DBMS_LOB.CREATETEMPORARY (l_clob, true);
DBMS_LOB.converttoclob (l_clob, p_blob, dbms_lob.lobmaxsize, l_src_off, l_dst_off, dbms_lob. DEFAULT_CSID, l_ctx, l_warn);
-Attention: hypothesis that there is at least a 'correct' csv-line
-should perhaps find a better guard condition
-Hypothesis: last column ends with the separator
l_len_clob: = length (l_clob);
While l_len_clob > l_off and l_off > 0 loop
l_row: = substr (l_clob, l_off, instr (l_clob, c_line_end, l_off)-l_off);
line (csv_split_type)
-start of the first; occurrence - 1
substr (l_row, 1, instr (l_row, c_sep) - 1)
-first; second occurrence; accident - first; occurrence
, substr (l_row, instr (l_row, c_sep, 1, 1) + 1, instr (l_row, c_sep, 1, 2) - instr (l_row, c_sep, 1, 1) - 1)
-second; third occurrence; occurrence - second; occurrence
, substr (l_row, instr (l_row, c_sep, 1, 2) + 1, instr (l_row, c_sep, 1, 3) - instr (l_row, c_sep, 1, 2) - 1)
- and so on
, substr (l_row, instr (l_row, c_sep, 1, 3) + 1, instr (l_row, c_sep, 1, 4) - instr (l_row, c_sep, 1, 3) - 1)
));
l_off: = l_off + length (l_row) + 1; -to switch c_sep and line (10 sep
end loop;
return;
end;
You must change the csv_split_type also.
Update: I had to correct, combined version of two upward.
-
The use of objects multi-state for the next stage of the course 9
In my old clumsy e-Learning authoring tool, I could create a slide with several steps. You would click the button Next to display text and graphics. When you get to the last step, the next step button disappears, so that you know that you have completed. How can I make this work with multi-state Cap 9 objects? I created an object with 3 States multi-state. I created a button with an action go to Next State. It is fine until you get to the last State. If you click the button again, it starts again with the first State.
Yes, I can create several buttons. But I really just want a button called next step which disappears after posting the last State. The user would know that she had displayed all the steps.
Any suggestions are welcome. Thank you.
Jeanne M
Here is a sample script:
This has been implemented for 6 States (for a container of text, the Normal State is an empty form).
There are 2 user variables:
- v_counter has a default value of 0
- v_max is the number of States, you can replace it with the literal '6' or you need to
The first decision "done" check if the counter reaches the maximum value (v_max). If this is the case all States should have been disclosed, and it should end the loop. That's why this decision will disable the button used to change States (SB_StateTrigger) and here also to change its status to an invisible state (AfterLoop). Upon return to the slide, the State will be back to Normal (visible), and simply activate the button again. You can replace this with a simple action to hide as well if you want.
If the first decision is not true, there are still reports to be shown and the counter is incremented.
All 6 other decisions are similar (I collapsed 2 of them): they will test the value of the counter and show the associated State. I used to state3 on a common standard. This makes it possible to use exactly the same advanced also action if the total number of States is less than 6. Simply change the value of v_max.
-
FDM generated SQL statement in the log file?
Hello everyone, hopefully a simple question:When you perform an import using FDM, I know that behind the scenes of a SQL SELECT statement statement composed and performed on the database server. My question: the actual SQL statement is retained in a newspaper somewhere (so that we can see it?) In our case, it's an Oracle DB server and we use the ERPI adapter; not sure if it's relevant. We would like to enter this SQL statement and use it as a starting point for other things.
Thanks in advance for anything you can offer.
The statement SQL is not written in the journal of the adapter. The only things that are registered are what show in the file card log if you enable loggin to the adapter source ERPi in action 'Get Data'.
-
pl/sql code for the problem
Hi all
Can someone please tell me how to write pl/sql code to the below question:
I have two tables has temp and temp b both have the code and the reading of time columns. I want if temp one more time temp b for a particularcode of reading then reading time of tempb must obtain updates of reading time (reading time to temp b should get updated ony if the temp one reading is largest temp b and code exist in the tables). If the code as code1 which exist in temp one but doesnot exist in temp b so that records should get inserted into table b temp.
For Ex. Temp a code1, 10:00; Code1, 11:00; B temp is code1, and code 2.10 AM:code 2.9 AM 09:00; then once the program run the tempb should have code1, 11:00; Code2, 10:00
Thanks in advance.Hello
You needn't PL/SQL for this; a MERGE statement will do.
If you want to do it in PL/SQL, always to use MERGE:BEGIN MERGE INTO tempb dst USING ( SELECT code , MAX (reading_time) AS reading_time FROM tempa GROUP BY code ) src ON (src.code = dst.code) WHEN MATCHED THEN UPDATE SET dst.reading_time = src.reading_time WHERE dst.reading_time < src.reading_time WHEN NOT MATCHED THEN INSERT (dst.code, dst.reading_time) VALUES (src.code, src.reading_time) ; END; /
-
SQL statement causes the database to hang
Guys,
a DBA told me that there is something wrong with this SQL statement and causes a blockage of database...
Please check.
SELECT field1 FROM table1 WHERE Field1 LIKE ' %% ' AND field2 = '123456' ORDER BY Field2 ASC
Tell me, what is the problem with statement above. The LIKE operator is?
Thanks for the help!Are not specifically related to your question, but having a clause such as ' where Field2 ='123456 "and then you place your order on Field2 is not really going to do much for you, all values will be identical!
Carl
-
'Go to the State' vs 'Go to the next State' for the selected button state
I have a series of image galleries in my publication that uses MSOs and buttons to "Go to another" - the thumbnails then have a small edge to have indicated that they are 'selected' - it works very well. The border is added to the click for each thumbnail button state.
I have a similar scenario is a YouTube embed of the video and an article as a MSO with two States: a State is the vid to youtube and the other State is the article of the text. I have a set place button to switch between these two States. This button is set to "Go to the next State" so he'll just between the two States MSO. Is there any way to have a selected State for this button so that the words under the button change to 'play' video in 'see article' while keeping this button set to "Go to the next State" - the only way I can think to do is with two buttons.
I realize, this is a very specific use case and I do not expect the features, but if there is a solution I am curious to know if anyone else has come across this.
Thanks in advance for your help.
Implement for the buttons in the different States. Then add an invisible button on it to the next State meetings.
Bob
-
PL/SQL function for the addition of "days."
I created a function that, at a date and a number, adds many days to the date. The problem is that there are parameters to include Saturday, Sunday or Monday as these days. Here is an example of the call
DAYSBETWEEN('01-Aug-11',5,0,0,0) will give 5 calendar days since August 1, 2011 (that you would not exclude Saturday, Sunday or Monday)
DAYSBETWEEN (August 1, 11 ', 5, 1,1,0) would give you 5 working days since August 1, 2011
DAYSBETWEEN('01-Aug-11',5,0,1,1) would give you 5 days of August 1, 2011 spring break Sunday and Monday.
My current role is:
This works for a small set of values for the date, but when there is a bigger set of dates, it takes hours to complete because of the time loop and multiple or statements.create or replace FUNCTION DAYSBETWEEN( DAYIN IN DATE , ADDNUM IN NUMBER , EXSAT IN NUMBER , EXSUN IN NUMBER , EXMON IN NUMBER ) RETURN DATE IS dtestart DATE; intcount NUMBER; holidays NUMBER; BEGIN dtestart := DAYIN; intcount :=1; WHILE intcount <= ADDNUM LOOP dtestart := dtestart + 1; IF NOT((EXSAT = 1 AND TO_CHAR(dtestart, 'd')= 7) OR (EXSUN=1 AND TO_CHAR(dtestart, 'd')= 1) OR (EXMON=1 AND TO_CHAR(dtestart, 'd')= 2)) THEN intcount := intcount + 1; END IF; END LOOP; RETURN dtestart; END DAYSBETWEEN;
Y at - it another game of logic, I could use to improve this query, using possibly not the while loop / or statements?Hi again:
This is the query, reformatted in the form of a function, exectuing the same thing I did in the above query:
SQL> CREATE OR REPLACE FUNCTION daysbetween 2 ( 3 dayin IN DATE , 4 addnum IN NUMBER , 5 exsat IN NUMBER , 6 exsun IN NUMBER , 7 exmon IN NUMBER ) 8 9 RETURN DATE 10 11 IS 12 13 dtestart DATE; 14 intcount NUMBER; 15 holidays NUMBER; 16 17 BEGIN 18 19 WITH all_dates AS 20 ( 21 SELECT LEVEL AS days_to_add 22 , dayin 23 , dayin + LEVEL AS new_dt 24 , addnum 25 , exsat 26 , exsun 27 , exmon 28 FROM dual 29 CONNECT BY LEVEL <= addnum * 2 30 ) 31 , exclusions AS 32 ( 33 SELECT ROW_NUMBER() OVER ( ORDER BY new_dt) ordering 34 , addnum 35 , new_dt 36 , TO_CHAR ( new_dt, 'DY' ) 37 FROM all_dates 38 WHERE 1=1 39 AND TO_CHAR ( new_dt, 'DY' ) != DECODE ( exsat, 1, 'SAT', 'XXX') 40 AND TO_CHAR ( new_dt, 'DY' ) != DECODE ( exsun, 1, 'SUN', 'XXX') 41 AND TO_CHAR ( new_dt, 'DY' ) != DECODE ( exmon, 1, 'MON', 'XXX') 42 ) 43 SELECT MAX( new_dt ) 44 INTO dtestart 45 FROM exclusions 46 WHERE ordering <= addnum; 47 48 RETURN dtestart; 49 50 END daysbetween; 51 / Function created. SQL> SELECT daysbetween ( DATE '2011-08-01', 150, 1, 0, 1) FROM DUAL; DAYSBETWE --------- 26-FEB-12 1 row selected.
The only variable not factored is holiday, but I saw that you were for them in your LOOP function either.
Due to the nature and variability holiday, it is better to have a table with the listed holidays, and those who could be elminated of charges together in the same 'exculsions' query where I used the TO_CHAR and DECODE to exclude the Fri, sat and Sun.
HISTORY_TABLE
ITEM_CODE | BAT_NO |
TXN_CODE
DOC_NO
UPDT_DT
I1
B1
T1
1234
JANUARY 3, 2015
I1
B20
T20
4567
MARCH 3, 2015
I1
B30
T30
7890
FEBRUARY 5, 2015
I2
B40
T20
1234
JANUARY 1, 2015
TRANSACTION
TXN_CODE | TXN_TYPE |
T1 | IN |
T20 |
OFF
T30
ALL THE
T50
IN
T80
IN
T90
IN
T60
ALL THE
T70
ALL THE
T40
ALL THE
IN_TABLE_HEAD_1
H1_SYS_ID (primary key) | TXN_CODE | DOC_NO |
DOC_DATE
H1ID1
T1
1234
JANUARY 1, 2015
H1ID2
T70
1234
FEBRUARY 1, 2015
IN_TABLE_ITEM_1
I1_SYS_ID |
H1_SYS_ID
(foreign key referencing H1_SYS_ID in IN_TABLE_HEAD_1)
ITEM_CODE
I1ID1
H1ID1
I1
I1ID2
H1ID1
I100
I1ID3
H1ID2
I3
IN_TABLE_BATCH_1
B1_SYS_ID | TXN_CODE DOC_NO (now in IN_TABLE_HEAD_1) | BAT_NO |
B1ID1
T1
1234
B1 / can be empty
B1ID2
T70
1234
B70
IN_TABLE_HEAD_2
H2_SYS_ID (primary key) | TXN_CODE |
DOC_NO
DOC_DATE
H2ID1
T30
4567
FEBRUARY 3, 2015
H2ID2
T60
1234
JANUARY 3, 2015
IN_TABLE_ITEM_2
I2_SYS_ID | H2_SYS_ID (foreign key referencing H2_SYS_ID in IN_TABLE_HEAD_2) | ITEM_CODE |
I2ID1 | H2ID1 |
I1
I2ID2
H2ID1
I200
I2ID3
H2ID2
I2
IN_TABLE_BATCH_2
B2_SYS_ID |
I2_SYS_ID
(foreign key referencing I2_SYS_ID in IN_TABLE_ITEM_2)
BAT_NO
B2ID1
I2ID1
B30 / null
B2ID2
I2ID2
B90
B2ID2
I2ID3
B60
IN_TABLE_HEAD_3
H3_SYS_ID (primary key) | TXN_CODE | DOC_NO | DOC_DATE |
H3ID1 |
T50
1234
JANUARY 2, 2015
H3ID2
T80
1234
JANUARY 3, 2015
H3ID3
T90
1234
JANUARY 4, 2015
H3ID4
T40
1234
AUGUST 5, 2015
IN_TABLE_ITEM_3
I3_SYS_ID |
H3_SYS_ID
(foreign key referencing H3_SYS_ID in IN_TABLE_HEAD_3)
ITEM_CODE
BAT_NO
I3ID1
H31D1
I2
B50
I3ID2
H3ID2
I4
B40
I3ID3
H3ID3
I4
I3ID4
H3ID4
I6
There is no IN_TABLE_BATCH_3
Please find below the expected results.
OUTPUT
ITEM_CODE | BAT_NO | TXN_CODE | DOC_NO |
BOE_DT
BATCH_YN
I1
B1
T1
1234
JANUARY 3, 2015
THERE
I1
B30
T30
7890
FEBRUARY 5, 2015
N
I2
B60
T60
1234
JANUARY 3, 2015
N
I3
B70
T70
1234
FEBRUARY 1, 2015
THERE
I4
T90
1234
JANUARY 4, 2015
N
I6
T40
1234
AUGUST 5, 2015
N
Controls database to create the tables above and insert the records.
CREATE TABLE stock_table()item_code VARCHAR2()80),bat_no VARCHAR2()80),txn_code VARCHAR2()80),
doc_no VARCHAR2 (80), boe_dt DATE );
INSERT EN stock_table
VALUES ('I1', 'B1', '', '', '');
INSERT EN stock_table
VALUES ('I1', '', '', '', '');
INSERT IN stock_table
VALUES ('I2', '', '', '', '');
INSERT EN stock_table
VALUES ('I3', 'B70', '', '', '');
INSERT EN stock_table
VALUES ('I4', 'B80', '', '', '');
INSERT EN stock_table
VALUES ('I5', 'B90', 'T102', '1234', '02-JUL-2015');
INSERT EN stock_table
VALUES ('I6', 'B100', '', '', '');
SELECT *
FROM stock_table
CREATE TABLE history_table()item_code VARCHAR2()80),bat_no VARCHAR2()80),txn_code VARCHAR2()80),
doc_no VARCHAR2 (80), updt_dt DATE );
INSERT IN history_table
VALUES ('I1', 'B1', 'T1', '1234', '03-JAN-2015');
INSERT IN history_table
VALUES ('I1', 'B20', 'T20', '4567', '03-MAR-2015');
INSERT IN history_table
VALUES ('I1', 'B30', 'T30', '7890', '05-FEB-2015');
INSERT IN history_table
VALUES ('I2', 'B40', 'T20', '1234', '01-JAN-2015');
SELECT *
FROM history_table
CREATE TABLE transaction1()txn_code VARCHAR()80),txn_type VARCHAR()80));
INSERT INTO transaction1
VALUES ('T1', 'IN');
INSERT INTO transaction1
VALUES ('T20', 'OUT');
INSERT INTO transaction1
VALUES ('T30', 'ALL');
INSERT INTO transaction1
VALUES ('T40', 'ALL');
INSERT INTO transaction1
VALUES ('T50', 'IN');
INSERT INTO transaction1
VALUES ('T60', 'ALL');
INSERT INTO transaction1
VALUES ('T70', 'ALL');
INSERT INTO transaction1
VALUES ('T80', 'IN');
INSERT INTO transaction1
VALUES ('T90', 'IN');
SELECT *
FROM transaction1
CREATE TABLE in_table_head_1()h1_sys_id VARCHAR2()80) PRIMARY KEY,txn_code VARCHAR2()80),
doc_no VARCHAR2 (80), doc_dt DATE );
CREATE TABLE in_table_head_2()h2_sys_id VARCHAR2()80) PRIMARY KEY,txn_code VARCHAR2()80),
doc_no VARCHAR2 (80), doc_dt DATE );
CREATE TABLE in_table_head_3()h3_sys_id VARCHAR2()80) PRIMARY KEY,txn_code VARCHAR2()80),
doc_no VARCHAR2 (80), doc_dt DATE );
INSERT IN in_table_head_1
VALUES ('H1ID1', 'T1', '1234', '01-JAN-2015');
INSERT IN in_table_head_1
VALUES ('H1ID2', 'T70', '1234', '01-FEB-2015');
INSERT IN in_table_head_2
VALUES ('H2ID1', 'T30', '4567', '03-FEB-2015');
INSERT IN in_table_head_2
VALUES ('H2ID2', 'T60', '1234', '03-JAN-2015');
INSERT IN in_table_head_3
VALUES ('H3ID1', 'T50', '1234', '02-JAN-2015');
INSERT IN in_table_head_3
VALUES ('H3ID2', 'T80', '1234', '03-JAN-2015');
INSERT IN in_table_head_3
VALUES ('H3ID3', 'T90', '1234', '05-JAN-2015');
INSERT IN in_table_head_3
VALUES ('H3ID4', 'T40', '1234', '05-AUG-2015');
CREATE TABLE in_table_item_1()i1_sys_id VARCHAR2()80) PRIMARY KEY,
h1_sys_id VARCHAR2 (80) REFERENCES in_table_head_1()h1_sys_id),item_code VARCHAR2()80));
CREATE TABLE in_table_item_2()i2_sys_id VARCHAR2()80) PRIMARY KEY,
h2_sys_id VARCHAR2 (80) REFERENCES in_table_head_2()h2_sys_id),item_code VARCHAR2()80));
CREATE TABLE in_table_item_3(i3_sys_id VARCHAR2(80) PRIMARY KEY,
h3_sys_id VARCHAR2 (80) REFERENCES in_table_head_3()h3_sys_id),item_code VARCHAR2()80),
bat_no VARCHAR2 (80));
INSERT IN in_table_item_1
VALUES ('I1ID1', 'H1ID1', 'I1');
INSERT IN in_table_item_1
VALUES ('I1ID2', 'H1ID1', 'I100');
INSERT IN in_table_item_1
VALUES ('I1ID3', 'H1ID2', 'I3');
INSERT IN in_table_item_2
VALUES ('I2ID1', 'H2ID1', 'I1');
INSERT IN in_table_item_2
VALUES ('I2ID2', 'H2ID1', 'I200');
INSERT IN in_table_item_2
VALUES ('I2ID3', 'H2ID2', 'I2');
INSERT IN in_table_item_3
VALUES ('I3ID1', 'H3ID1', 'I2','B50');
INSERT IN in_table_item_3
VALUES ('I3ID2', 'H3ID2', 'I4','B40');
INSERT IN in_table_item_3
VALUES ('I3ID3', 'H3ID3', 'I4','');
INSERT IN in_table_item_3
VALUES ('I3ID4', 'H3ID4', 'I6','');
SELECT *
FROM in_table_item_1
SELECT *
FROM in_table_item_2
SELECT *
FROM in_table_item_3
CREATE TABLE in_table_batch_1()b1_sys_id VARCHAR2()80) PRIMARY KEY,
txn_code VARCHAR2 (80), doc_no VARCHAR2 (80), bat_no VARCHAR2 (80));
CREATE TABLE in_table_batch_2()b2_sys_id VARCHAR2()80) PRIMARY KEY,
i2_sys_id VARCHAR2 (80) REFERENCES in_table_item_2()i2_sys_id),bat_no VARCHAR2()80));
INSERT IN in_table_batch_1
VALUES ('B1ID1', 'T1', '1234', 'B1');
INSERT IN in_table_batch_1
VALUES ('B1ID2', 'T70', '1234', 'B70');
INSERT IN in_table_batch_2
VALUES ('B2ID1', 'I2ID1', 'B30');
INSERT IN in_table_batch_2
VALUES ('B2ID2', 'I2ID2', 'B90');
INSERT IN in_table_batch_2
VALUES ('B2ID3', 'I2ID3', 'B60');
Please advise a solution for the same.
Thank you and best regards,
Séverine Suresh
very forced (question subfactoring used to allow easy testing/verification - could work with these test data only)
with
case_1 as
(select s.item_code,
s.bat_no,
h.txn_code,
h.doc_no,
h.updt_dt boe_dt,
cases where s.bat_no = h.bat_no then 'Y' else ' n end batch_yn.
cases where h.txn_code is not null
and h.doc_no is not null
and h.updt_dt is not null
then 'case 1' '.
end refers_to
from (select item_code, bat_no, txn_code, doc_no, boe_dt
of w_stock_table
where bat_no is null
or txn_code is null
or doc_no is null
or boe_dt is null
) s
left outer join
w_history_table h
On s.item_code = h.item_code
and s.bat_no = h.bat_no
and exists (select null
of w_transaction1
where txn_code = nvl (s.txn_code, h.txn_code)
and txn_type in ('IN', 'ALL')
)
),
case_2 as
(select s.item_code,
NVL (s.bat_no, h.bat_no) bat_no.
NVL (s.txn_code, h.txn_code) txn_code.
NVL (s.doc_no, h.doc_no) doc_no.
NVL (s.boe_dt, h.updt_dt) updt_dt.
cases where s.bat_no = h.bat_no then 'Y' else ' n end batch_yn.
cases where h.txn_code is not null
and h.doc_no is not null
and h.updt_dt is not null
then 'case 2'.
end refers_to
from (select item_code, bat_no, txn_code, doc_no, boe_dt
of case_1
where refers_to is null
) s
left outer join
w_history_table h
On s.item_code = h.item_code
and exists (select null
of w_transaction1
where txn_code = nvl (s.txn_code, h.txn_code)
and txn_type in ('IN', 'ALL')
)
and not exists (select null
of case_1
where item_code = h.item_code
and bat_no = h.bat_no
and txn_code = h.txn_code
and doc_no = h.doc_no
and updt_dt = h.updt_dt
)
),
case_31 as
(select s1.item_code,
NVL (S1.bat_no, W1.bat_no) bat_no.
NVL (S1.txn_code, W1.txn_code) txn_code.
NVL (S1.doc_no, W1.doc_no) doc_no.
NVL (S1.updt_dt, W1.doc_dt) updt_dt.
cases where s1.bat_no = w1.bat_no then 'Y' else ' n end batch_yn.
cases where w1.txn_code is not null
and w1.doc_no is not null
and w1.doc_dt is not null
then "case 31'.
end refers_to
from (select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn, refers_to
of case_2
where refers_to is null
) s1
left outer join
(select i1.item_code, h1.txn_code, h1.doc_no, h1.doc_dt, b1.bat_no
of w_in_table_item_1 i1
inner join
w_in_table_head_1 h1
On i1.h1_sys_id = h1.h1_sys_id
inner join
w_in_table_batch_1 b1
On h1.txn_code = b1.txn_code
and h1.doc_no = b1.doc_no
) w1
On s1.item_code = w1.item_code
),
case_32 as
(select s2.item_code,
NVL (S2.bat_no, W2.bat_no) bat_no.
NVL (S2.txn_code, W2.txn_code) txn_code.
NVL (S2.doc_no, W2.doc_no) doc_no.
NVL (S2.updt_dt, W2.doc_dt) updt_dt.
cases where s2.bat_no = w2.bat_no then 'Y' else ' n end batch_yn.
cases where w2.txn_code is not null
and w2.doc_no is not null
and w2.doc_dt is not null
then "case 32'.
end refers_to
from (select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn, refers_to
of case_2
where refers_to is null
) s2
left outer join
(select i2.item_code, h2.txn_code, h2.doc_no, h2.doc_dt, b2.bat_no
of w_in_table_item_2 i2
inner join
w_in_table_head_2 h2
On i2.h2_sys_id = h2.h2_sys_id
inner join
w_in_table_batch_2 b2
On i2.i2_sys_id = b2.i2_sys_id
) w2
On s2.item_code = w2.item_code
),
case_33 as
(select s3.item_code,
w3.bat_no,
NVL (S3.txn_code, w3.txn_code) txn_code.
NVL (S3.doc_no, w3.doc_no) doc_no.
NVL (S3.updt_dt, w3.doc_dt) updt_dt.
cases where s3.bat_no = w3.bat_no then 'Y' else ' n end batch_yn.
cases where w3.txn_code is not null
and w3.doc_no is not null
and w3.doc_dt is not null
then "case 33'.
end refers_to
from (select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn, refers_to
of case_2
where refers_to is null
) s3
left outer join
(select i3.item_code, h3.txn_code, h3.doc_no, h3.doc_dt, i3.bat_no
of w_in_table_item_3 i3
inner join
w_in_table_head_3 h3
On i3.h3_sys_id = h3.h3_sys_id
) w3
On s3.item_code = w3.item_code
)
Select item_code, bat_no, txn_code, doc_no, boe_dt, batch_yn
of case_1
where refers_to is not null
Union of all the
Select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn
of case_2
where refers_to is not null
Union of all the
Select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn
from (select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn,
ROW_NUMBER() over (partition by item_code of updt_dt desc order) rn
from (select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn
of case_31
where refers_to is not null
Union of all the
Select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn
of case_32
where refers_to is not null
Union of all the
Select item_code, bat_no, txn_code, doc_no, updt_dt, batch_yn
of case_33
where refers_to is not null
)
)
where rn = 1
ITEM_CODE | BAT_NO | TXN_CODE | DOC_NO | BOE_DT | BATCH_YN |
---|---|---|---|---|---|
I1 | B1 | T1 | 1234 | JANUARY 3, 2015 | THERE |
I1 | B30 | T30 | 7890 | FEBRUARY 5, 2015 | N |
I2 | B60 | T60 | 1234 | JANUARY 3, 2015 | N |
I3 | B70 | T70 | 1234 | FEBRUARY 1, 2015 | THERE |
I4 | - | T90 | 1234 | JANUARY 5, 2015 | N |
I6 | - | T40 | 1234 | AUGUST 5, 2015 | N |
Concerning
Etbin
Maybe you are looking for
-
Firefox crashes at startup.
I uninstalled with the Windows Control Panel and with Revo Uninstaller and then reinstalled Firefox several times. It always crashes at launch. I can't access a safe mode depending on some of the recommendations on other threads. Firefox worked fine
-
Firefox crashes when you open new tab
Crash-ID: bp-681f1751-fc5b-4f14-a30b-f93ed2150407 I tried:Clean install (with the removal of C:prog... \Mozilla)Uninstallation and removal of extensionsFirefox restarted in safe mode without addons/extensions.Uninstalling/updating flash/adobe Can I s
-
Cannot add a folder to the list of shared folders
I am in the Prefs system - Sharing - sharing files and when I click on "Add" and choose a folder in my user profile, as 'Documents', and click 'Add', nothing happens. The file is not added to the list. Any ideas?
-
Easier way to send e-mail?
Hi, I want one to have the "Send feedback" option in my application. The sample application for email (message name) is very complicated and requires read permission and delete the email that may scare users. Is there a simple way that requires no C+
-
After the upgrade to windows 8.1 says file history offline reader
My windows 8 upgraded to 8.1 en from there it says that I need to reconnect my drive as I've changed nothing except to do a disk clean up nothing changed. 24 of Firefox does not connect to internet