Difference between two rows of a table.
Hi All,
I have a requirement where I need to calculate the difference of a column, but the values are in two rows.
Its exactly like this, I am having a table where in I have stored incidents with their status and their modified dates
example:
Incident_Id : Status : Modified_Date(in seconds from a specific date)
1 : 1 : 9080890
1 : 2 : 9080999
1 : 3 : 9081900
2 : 1 : 10000900
2 : 2 : 10001000
2 : 3 : 10002000
Now the requirement is I have to show the time spent by each incident in each status.
ie for status 1 I need the difference between modified date of status 2 and modified date of status 1 likewise
for status 2 need the difference between modified date of status 3 and modified date of status 2 and so on.
the actual result I need is like this
Incident_Id : Status : Timespent
1: 1: (modified_date(status2) - modified_date(status1))
Please help me to write the query for this.
Regards,
Tauceef
if only you have incremental status (example = 1,2,3, ... )
WITH t (Incident_Id ,Status , Modified_Date) AS (
SELECT 1 , 1 , 9080890 FROM dual UNION ALL
SELECT 1 , 2 , 9080999 FROM dual UNION ALL
SELECT 1 , 3 , 9081900 FROM dual UNION ALL
SELECT 2 , 1 , 10000900 FROM dual UNION ALL
SELECT 2 , 2 , 10001000 FROM dual UNION ALL
SELECT 2 , 3 , 10002000 FROM dual )
SELECT t1.Incident_Id, t1.Status, t1.Modified_Date , t2.Modified_Date-t1.Modified_Date
FROM t t1
LEFT JOIN t t2
ON t2.Incident_Id = t1.Incident_Id
AND t2.status = t1.status+1
ORDER BY 1,2or
there is no need to know what is the next status number :
WITH t (Incident_Id ,Status , Modified_Date) AS (
SELECT 1 , 1 , 9080890 FROM dual UNION ALL
SELECT 1 , 2 , 9080999 FROM dual UNION ALL
SELECT 1 , 3 , 9081900 FROM dual UNION ALL
SELECT 2 , 1 , 10000900 FROM dual UNION ALL
SELECT 2 , 2 , 10001000 FROM dual UNION ALL
SELECT 2 , 3 , 10002000 FROM dual )
SELECT t1.Incident_Id,
t1.Status,
t1.Modified_Date ,
(LEAD(t1.Modified_Date) OVER (PARTITION BY t1.Incident_Id ORDER BY t1.status)) - t1.Modified_Date
FROM t t1Edited by: Ramin Hashimzadeh on May 15, 2013 1:32 PM
Similar Messages
-
I need to retrieve the a set of rows in between two rows from a table.
consider employees table and primary key employee_id.
With out using EMPLOYEE_ID column in the where clause or between clause, I need to get the records between 104 and 116 or a set of records between two rows.
Can any one help me in this... i know this is simple but am just a fresher to oracle development... help me grow....
Thanks,
Arunya at last i got the out put... thank guys for thinking with me....
SELECT rownum, employee_id FROM (SELECT rownum, employee_id FROM employees ORDER BY employee_id)
WHERE ROWNUM <=8
MINUS
SELECT rownum, employee_id FROM (SELECT rownum, employee_id FROM employees ORDER BY employee_id) WHERE ROWNUM <= 4 -
How to get difference between two rows for a column field?
hi, all,
Could anyone show me what query statement is to get the difference betweem two rows for two column fields?
The tables and its records are like this:
id, begin, end
p1 21 30
p2 45 60
p3 120 150
I would like to have the query result like this
id, diff
p1 15 --- which is 45 minus 30
p2 60 --- which is 120 minus 60
and so on...
thank you in advance.
RaffyYou can use the LAG function to access values from previous rows:
with q as (select 'p1' id, 21 v_start, 30 v_end from dual
union all
select 'p2', 45, 60 from dual
union all
select 'p3', 120, 150 from dual)
select id, v_start, v_end, v_start - lag (v_end, 1, 0)
over (order by id) v_diff from q
ID,V_START,V_END,V_DIFF
p1,21,30,21
p2,45,60,15
p3,120,150,60
See the SQL Language doc
http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions075.htm -
Insert table in between two rows of the table in Web dynpro abap.
Hi All,
I have a requirment like this,
while click on a hide/show button from the first row and last column of the table, want to display a table(or add some rows to insert data) in between the rows of the table and insert data in the new table .
please help me wheather it is possible or not?
Thanks,
Raju.hi raju....
if you want to insert rows.... then read the index of the row clicked and then read the node that is bound to the table and insert an empty row at that index and bind it back again....
if you want another table then make use of table popin.
--regards,
alex b justin -
How to find the difference between 2 rows in a table
Hi all
I have a table say emp which has only 2 rows
emp
empno name age sex deptno
1 X 20 M 10
2 Y 21 M 20
The output should be
empno name age deptno
1 X 20 10
2 Y 21 20
Since the sex are same , so it is not included in the output.
thanks
HariHi there rajkumar,
Although your lead solution is more elegant than the one presented below,
I believe the complete answer it would be:
with mytable as (
SELECT ROW_NUMBER() OVER (ORDER BY EMPNO) POS, Z.* FROM (
select 1 empno, 'X' name, 20 age, 'M' sex, 10 deptno from dual
union
select 2, 'Y', 21, 'M', 20 from dual) Z
SELECT
DECODE(A.EMPNO,B.EMPNO,NULL,A.EMPNO) EMPNO,
DECODE(A.NAME,B.NAME,NULL,A.NAME) NAME,
DECODE(A.AGE,B.AGE,NULL,A.AGE) AGE,
DECODE(A.SEX,B.SEX,NULL,A.SEX) SEX,
DECODE(A.DEPTNO,B.DEPTNO,NULL,A.DEPTNO) DEPTNO
FROM
(SELECT * FROM mytable WHERE POS = 1) A,
(SELECT * FROM mytable WHERE POS = 2) B
UNION
SELECT
DECODE(B.EMPNO,A.EMPNO,NULL,B.EMPNO) EMPNO,
DECODE(B.NAME,A.NAME,NULL,B.NAME) NAME,
DECODE(B.AGE,A.AGE,NULL,B.AGE) AGE,
DECODE(B.SEX,A.SEX,NULL,B.SEX) SEX,
DECODE(B.DEPTNO,A.DEPTNO,NULL,B.DEPTNO) DEPTNO
FROM
(SELECT * FROM mytable WHERE POS = 1) A,
(SELECT * FROM mytable WHERE POS = 2) B
Your query indeed returns NULL in the column for which values are the same, but still, you get just the first row.
Anyway, the question is how to dinamically select the columns that hold NULL values, in the newly generated result. Because i believe the request is like this, if column holds equal values on both rows, then don't select the column (maybe i'm wrong).
If this is the case then just by one sql it will not solve your problem, it needs to be more, with sqlplus and some other scripts checking the values in the columns, if it's not null then set an operator, and in the end (in sqlplus)
select operator1, operator2 etc. from mytable.
Hope it helps,
Michael.
[All the above presented is based on the fact that you shall always have TWO rows to be compared, having more or less it will fail!]
Message was edited by:
kjt -
Need to find the Difference between two table
Hello ,
I have stucked in program as below scenario:-
I have two tables of huge data of same structure in a same schema.I need to find the difference exact values in tables.
By using MINUS we can find the difference between two table ,i need to find the what exact difference in the there values with colunm and value at that column.
Example TableA
Col1 col2 col3 col4 col5.... col50
10 ABC 2001 EE 444 TT
40 XYZ 3002 RR 445 TT3
80 DEF 6005 YY 446 YY8
TableB
Col1 col2 col3 col4 col5.... col50
10 ABC 2001 EE 444 TT
40 XYZ 3002 RR 445 TT3
81 DEF 6005 Yu 447 YY8
I need to the out put like this :-
The Diffence between two table is
TableA.COL1=80 TableB.Col1=81, Different
TableA.Col4=YY TableB.col4=Yu,Different
TableA.Col5=446TableB.col5=447,Different
Please suggest me to write the pl/sql program for the same
thanx in advance
KKThanx friends for all your efforts
I have a sample code for the same,this will compare the two tables for single row in each table .
what r the modification needed for the multiple rows of values in the two tables??
Please suggest!!
CREATE OR REPLACE PROCEDURE test_compare
IS
TYPE t_col
IS
TABLE OF VARCHAR2 (30)
INDEX BY PLS_INTEGER;
l_col t_col;
j NUMBER := 0;
l_sql VARCHAR2 (2000);
col1 VARCHAR2 (30);
col2 VARCHAR2 (30);
val1 NUMBER;
val2 NUMBER;
status VARCHAR2 (30);
CURSOR c1
IS
SELECT column_id, column_name
FROM all_tab_columns
WHERE table_name = 'TEST1';
BEGIN
FOR i IN c1
LOOP
j := j + 1;
l_col (j) := i.column_name;
END LOOP;
FOR k IN 1 .. j
LOOP
l_sql :=
'SELECT '
|| ''''
|| l_col (k)
|| ''''
|| ', '
|| 'TEST2.'
|| l_col (k)
|| ', '
|| ''''
|| l_col (k)
|| ''''
|| ', '
|| 'TEST1.'
|| l_col (k )
|| ', '
|| 'DECODE(TEST2.'
|| l_col (k)
|| ' -TEST1.'
|| l_col (k)
|| ', 0, ''NO CHANGE'', ''CHANGED'') FROM TEST2, TEST1';
EXECUTE IMMEDIATE l_sql INTO col1, val1,col2, val2, status;
IF status = 'CHANGED'
THEN
DBMS_OUTPUT.put_line( 'TEST2.'
|| col1
|| '='
|| val1
|| ', TEST1.'
|| col2
|| '='
|| val2
|| ', '
|| status);
END IF;
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ('Error:- ' || SQLERRM);
END;
/ -
Hi All,
I have two table with similar structure and data, one is on disk and the other is in memory. I somehow calculated the difference between a row size of on disk and in memory table and found that the row size of in memory is 700 Bytes more than the disk based
tables.
aaAs others mentioned, memory optimized tables and disk based tables have different structures in SQL Server 2014.
For memory optimized tables, the number of indexes on table also contribute to the size. You can calculate the exact size of rows and thus the table size using the formula given in the below articles
Table and Row Size in Memory-Optimized Tables
Estimate the Size of a Table
Krishnakumar S -
Difference between current row and previous row in a table
Hi All,
I am having a problem with the query. Can some of please help me?
I need to get difference between current row and previous row in a table. I have a table, which have data like bellow.
TABLEX
================
Name Date Items
AAA 01-SEP-09 100
BBB 02-SEP-09 101
CCC 03-SEP-09 200
DDD 04-SEP-09 200
EEE 05-SEP-09 400
Now I need to get output like bellow...
Name Date Items Diff-Items
AAA 01-SEP-09 100 0
BBB 02-SEP-09 101 1
CCC 03-SEP-09 200 99
DDD 04-SEP-09 200 0
EEE 05-SEP-09 400 200
Can some one help me to write a query to get above results?
Please let me know if you need more information.
Thanks a lot in advance.
We are using Oracle10G(10.2.0.1.0).
Thanks
Asif, nvl (items - lag (items) over (order by dt), 0)like in
SQL> with test as
2 (
3 select 'AAA' name, to_date('01-SEP-09', 'dd-MON-rr') dt, 100 items from dual union all
4 select 'BBB' name, to_date('02-SEP-09', 'dd-MON-rr') dt, 101 items from dual union all
5 select 'CCC' name, to_date('03-SEP-09', 'dd-MON-rr') dt, 200 items from dual union all
6 select 'DDD' name, to_date('04-SEP-09', 'dd-MON-rr') dt, 200 items from dual union all
7 select 'EEE' name, to_date('05-SEP-09', 'dd-MON-rr') dt, 400 items from dual
8 )
9 select name
10 , dt
11 , items
12 , nvl (items - lag (items) over (order by dt), 0)
13 from test
14 ;
NAM DT ITEMS NVL(ITEMS-LAG(ITEMS)OVER(ORDERBYDT),0)
AAA 01-SEP-09 100 0
BBB 02-SEP-09 101 1
CCC 03-SEP-09 200 99
DDD 04-SEP-09 200 0
EEE 05-SEP-09 400 200
SQL> -
Add new row in table between two rows - OATableBean
Hi,
I have a requirement where I need to add a new row between two rows. Lets say I have a table with 5 rows. I am envisioning having an icon (table switcher) on the row #1, which when clicked will insert/create a new row between rows #1 and #2 like #1.5, something that can be achieved in core forms. In core forms, when we click the add button, it immediately creates a new row before the current row ( and pushes other rows down). Is that possible with OATableBean. Please let me know if you have any ideas.
Thanks,
Ravi.Hi,
I used following code to insert the row in the end
public void createNewRow()
XXEGASRLinesVOImpl vo =getXXEGASRLinesVO1();
Row row1 ;
int i;
Row row[] = vo.getAllRowsInRange();
row1 = vo.createRow(); //vo.createRowSet("10");
vo.insertRowAtRangeIndex(row.length,row1);//.insertRow();
row1.setNewRowState(Row.STATUS_INITIALIZED);
Check if it works for you, you have to modify it to meet your requirement.
Regards,
Reetesh Sharma -
How to find the structural difference between two tables
Hi all,
How to find the structural difference between two tables .
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
PL/SQL Release 11.1.0.7.0 - Production
CORE 11.1.0.7.0 Production
TNS for 32-bit Windows: Version 11.1.0.7.0 - Production
NLSRTL Version 11.1.0.7.0 - Production
Thanks,
P Prakashyou could try something similar to this, for each table pair that you want to compare:
SELECT 'TABLE_A has these columns that are not in TABLE_B', DIFF.*
FROM (
SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH
FROM all_tab_columns
WHERE table_name = 'TABLE_A'
MINUS
SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH
FROM all_tab_columns
WHERE table_name = 'TABLE_B'
) DIFF
UNION
SELECT 'TABLE_B has these columns that are not in TABLE_A', DIFF.*
FROM (
SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH
FROM all_tab_columns
WHERE table_name = 'TABLE_B'
MINUS
SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH
FROM all_tab_columns
WHERE table_name = 'TABLE_A'
) DIFF;that's assuming, column_name, data_type and data_length are all you want to compare on. -
Find the difference between two internal table
how can i see the difference between two interal tables?
The requirement is as follows
1. We have a transparent table, which stores the employee data with EMP ID as key.
2. We load the transp table data into a interal table (B).
3. We get data from legecy system as file and it gets loaded into another internal table (A) (this also has the same EMP ID key and this will have latest addition/update to those emplyees).
Now we need to seperate out these data into three interal table Inserted (I), Deleted (D) and Updated (U).
We want to do followign things
I = A - B
D = B - A
Both A and B will have around 40k records. Hence we are trying to avoid the looping.
Please suggest the best option for us.
Thank you in advance.
Raghavendra>
RAGHAV URAL wrote:
> how can i see the difference between two interal tables?
> The requirement is as follows
>
> 1. We have a transparent table, which stores the employee data with EMP ID as key.
> 2. We load the transp table data into a interal table (B).
> 3. We get data from legecy system as file and it gets loaded into another internal table (A) (this also has the same EMP ID key and this will have latest addition/update to those emplyees).
>
> Now we need to seperate out these data into three interal table Inserted (I), Deleted (D) and Updated (U).
>
> We want to do followign things
> I = A - B
> D = B - A
>
> Both A and B will have around 40k records. Hence we are trying to avoid the looping.
>
> Please suggest the best option for us.
>
> Thank you in advance.
> Raghavendra
Hi Raghavendra,
Currently as of my knowledge, these operations are only possible through LOOPs. But LOOPign can be really fast here if you properly utilize the SORTING, READ with BINARY SEARCH and FIELD-SYMBOLS usage. I would say:-
Steps for Insert:-
SORT: A, B.
LOOP AT A ASSIGNING <WA_A>.
READ TABLE B WITH TABLE KEY key = <WA_A>-key BINARY SEARCH.
IF SY-SUBRC NE 0.
APPEND <WA_A> TO I.
ENDIF.
ENDLOOP.
Steps for Delete:-
SORT: A, B.
LOOP AT B ASSIGNING <WA_B>.
READ TABLE A WITH TABLE KEY key = <WA_B>-key BINARY SEARCH.
IF SY-SUBRC NE 0.
APPEND <WA_B> TO D.
ENDIF.
ENDLOOP.
Regards,
Ravi. -
How to get the difference between two columns in a column group
Hi All,
My first time here and really new to programming. I would like to get the difference between 2 columns that are inside
a column group.
Here is my sample table below: The Column Group is PeriodNumber and can only choose 2. like 1 and 2.. I would like to have a third row which will simply calculate the difference between the amounts in PeriodNumber 1 and 2.
PeriodNumber
Account 1 2
1) Cash 10,000 15,000
2) Receivables 12,000 11,500
3) Equipment 5,000 5,500
Total Assets 27,000 32,000Hi yabgestopa,
From your description, you want to get the difference between two columns in a column group. After testing it in my environment, we can use custom code to achieve your requirement. For more details, you can refer to the following steps:
Copy the custom code below and paste it to your report. (Right-click report>Report Properties>Code)
Dim Shared Num1 As Integer
Dim shared Num2 As Integer
Public Function GetAmount(Amount as Integer, Type as String)
If Type = "1" Then
Num1=Amount
Else
Num2=Amount
End If
Return Amount
End Function
Public Function GetDif()
Return Num1-Num2
End function
Right-click the second column to insert a third column with Outside Group-Right.
Then use the expressions below in the matrix.
=Code.GetAmount(Fields!Amount.Value,Fields!PeriodNumber.Value)
=code.GetAmount(Sum(Fields!Amount.Value),Fields!PeriodNumber.Value)
=Code.GetDif()
The report looks like below.
If you have any questions, please feel free to ask.
Thanks,
Katherine Xiong
Katherine Xiong
TechNet Community Support -
Get Differences between two databases
Hello there,
I need to check if there are differences between 2 databases, both on 10g. As I do not want to check all tables, triggers, procedures and so on manually I wonder whether there is a script where I just need to give the db_names for checking both databases?
best regards,
JörnHi there,
first, I need to know structural differences like row datatypes, indexes or triggers. I don't need to know what exactly is different just which objects.
Second, I would also like content - differences between two tables, so the exact table data compared to each other. But this is not as important as the first one.
best regards
Jörn -
Calculate the variance between two row
Post Author: jane
CA Forum: WebIntelligence Reporting
Hi,
I'm new to BO and wondering whether there is a way to calculate the variance between two rows. I'm working on a report which requires to display the change of two day's data. For instance, I have a table in a format as
Date
Sales Amount
3/12/2008
2300.00
2/12/2008
1280.00
Changed
1020.00
I'd like to display the "Changed" column. Is there a way to do that in BO InfoView? In Excel, you can use the formular by identify the two cells.
Thanks!Post Author: Vinl
CA Forum: WebIntelligence Reporting
I am having the same problem and am steping outside of the box and cannot progress.
Seems to me that you are kind of saying that the only way to get around this is to create an individual object in Designer? I cannot find a way of cementing values as a variable in order for a variance to be created.
I am trying to do it at column level mind you, which should not make that much difference....
Simple SQL database. I have created an object called "Year" which takes a date and converts it to a year. I then have another object created "Total sales", put the 2 in a table get the right results...try and create a year on year variance from that...no chance...
Used to work ok...? -
Find the difference between two columns in an ssrs matrix ? MSCRM
Hi All,
I am working in reporting part of our project (On-line MSCRM 2013) & in reporting services.
I am trying to create report using fetch xml based. Below is the snap what we required the result.
Kindly help me, how to get the difference in both column. (Its a matrix table where year is grouped).
We need difference between both year Like (Plan Revenue of 2013 & Plan Revenue of 2014 difference in Plan Revenue Diff section) and same for Actual
Revenue.
https://social.microsoft.com/Forums/en-US/054d5ca4-0d38-4dc6-84a8-88866cc228fe/find-the-difference-between-two-columns-in-an-ssrs-matrix-mscrm?forum=crmdevelopment
Thanks,
Mohammad ShariqueHi Bro,
I used parametrized option for year and done the report,Currently we are getting values in Difference column now i want to show
that value in percentage. How can we show the percentage based on that value. Means i want to show the Difference in Percentage.
Kindly help me i tried but getting some issue. Below i am mentioning the code and snap with result.
Below expression using to showing Plan Revenue in Percentage for year.
=
Sum(IIF(Fields!new_year.Value =Parameters!StartYear.Value,cdec(Fields!new_planrevenueValue.Value/1000), cdec(0)))
- Sum(IIF(Fields!new_year.Value =Parameters!EndYear.Value,cdec(Fields!new_planrevenueValue.Value/1000), cdec(0)))
/IIF(Sum(IIF(Fields!new_year.Value = Parameters!StartYear.Value,cdec(Fields!new_planrevenueValue.Value/1000), cdec(0)))>0,
(Sum(IIF(Fields!new_year.Value = Parameters!StartYear.Value,cdec(Fields!new_planrevenueValue.Value/1000), cdec(0))))
,1)
)*100))
Result issue is as below in snap with highlighted in red colour.
Kindly help me on this issue also :)
Maybe you are looking for
-
Update for garage band 3.0.4
I can;t find the appropriate update for this version of garage band. I have seen somewhere someone suggested to find a garage band 4 before going to 4.1.1, otherwise it'll be impossible to upgrade, but I never found a free download for version 4.0.0
-
Unreferenced() never called when implementing Unreferenced.
I have a remote object called DataFacade which implements the Unreferenced interface. public class DataFacade extends UnicastRemoteObject implements DataInterface, Unreferenced { // some methods public void unreferenced() { System.out.println("dummy"
-
How to make multiple repeating pages for XML child nodes
I have a schema that has many levels, and I am trying to have some of the child data print on separate pages. <As> <A id=1> <Bs> <B id=1 name="B1"> <Cs> <C id=1 name="B1C1" /> <C id=2 name="B1C2" /> <C id=3 name="B1C3" /> </Cs> </B> <B id=2 name="B2"
-
Firefox deleted my downloads can they be restored?
I run Firefox 28.1.0 latest version. my tablet is a nook HD + software 2.20 I accidentally delete and yes on the downloads page. It was a complete accident. I have a neuromuscular problem. Is there any way to fix it? Thanks for answering in advance.
-
Is there an Itunes virus?
Something weird is going on with my music library...when I play a song, the song duplicates and deletes the next song in my library. Also, a lot of my songs have a date added of 3/26/05 or 3/26/06. What is going on??? I'm freaked out because my ipod