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.
Raffy
You 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
Similar Messages
-
How to get days between two dates
Hi ,
How to get days between two dates.
Regards,
Ramesh.Hi Ramesh,
REPORT ZDATEDIFF.
DATA: EDAYS LIKE VTBBEWE-ATAGE,
EMONTHS LIKE VTBBEWE-ATAGE,
EYEARS LIKE VTBBEWE-ATAGE.
PARAMETERS: FROMDATE LIKE VTBBEWE-DBERVON,
TODATE LIKE VTBBEWE-DBERBIS DEFAULT SY-DATUM.
call function 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
exporting
i_date_from = FROMDATE
i_date_to = TODATE
I_FLG_SEPARATE = ' '
IMPORTING
E_DAYS = EDAYS
E_MONTHS = EMONTHS
E_YEARS = EYEARS.
WRITE:/ 'Difference in Days ', EDAYS.
WRITE:/ 'Difference in Months ', EMONTHS.
WRITE:/ 'Difference in Years ', EYEARS.
INITIALIZATION.
FROMDATE = SY-DATUM - 60. -
How to get relationship between two views in the reports
How to get relationship between two views in the reports, I am doing a deletion program , it is fully relates to views , how to get relationship between them in the reports
Hi,
Please explain your question in detail...what do you want to read ?
If you want to know about the navigation links between the views then you can use APIs like
wdComponentAPI.getComponentInfo().findInWindows("windowName").getViewUsageByID("Name").getNavigationLinks();
Iterate through the navigationLinkInfo from above collection and can read the other properties .
I haven't tried the above , but it should work !!!
Regards,Anilkumar -
How to get space between two values in a single cell of a table.
hello,
how to get space between two values in a single cell of a table.
thanks a lot.
kailash.sorry i got the answer.
-
How can get difference between 2 dates in the form of days
how can get difference between 2 dates in the form of days
Hi,
Check the following program:
REPORT ZDATEDIFF.
DATA: EDAYS LIKE VTBBEWE-ATAGE,
EMONTHS LIKE VTBBEWE-ATAGE,
EYEARS LIKE VTBBEWE-ATAGE.
PARAMETERS: FROMDATE LIKE VTBBEWE-DBERVON,
TODATE LIKE VTBBEWE-DBERBIS DEFAULT SY-DATUM.
call function 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
exporting
i_date_from = FROMDATE
i_date_to = TODATE
I_FLG_SEPARATE = ' '
IMPORTING
E_DAYS = EDAYS
E_MONTHS = EMONTHS
E_YEARS = EYEARS.
WRITE:/ 'Difference in Days ', EDAYS.
WRITE:/ 'Difference in Months ', EMONTHS.
WRITE:/ 'Difference in Years ', EYEARS.
INITIALIZATION.
FROMDATE = SY-DATUM - 60.
Regards,
Bhaskar -
How to Calculate AGE by getting difference between two Date Fields
HI Gems
I need to calculate AGE from getting difference from two date fields. But when i am trying to wrte fromula as Current date - date1(some date field) then it is showing error.
How can i get values.
Thanks
ManuYou already asked this question:
How to calculate AGE from two different date fields -
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 -
How to check differences between two bpel jar files
Hi,
Can some one please suggest, how to check the differences between two versions of a BPEL process jar files.
Thanks in advance!Hi,
Try this,
SELECT cons.owner AS child_owner, cons.table_name AS child_table,
cons.constraint_name constaint_name,
cons.constraint_type constraint_type, col.owner parent_owner,
col.table_name parent_table, col.column_name column_name
FROM dba_cons_columns col, dba_constraints cons
WHERE cons.r_owner = col.owner
AND cons.r_constraint_name = col.constraint_name
AND col.owner = 'MY_USER'
ORDER BY child_table;Thanks,
Shankar -
How to find differences between two columns
Hello I need the formula to spot differences between two columns and to write on a third one if the same name appear on the two columns.
For example:
rome berlin true
berlin moscow false
chicago toronto true
toronto chicago true
florence
Thanks
LorenzoYou should be able to use COUNTIF where the range is th first column and the condition is the word in the second column. If the result is greater than zero then the word is in both columns.
-
How to caluclate tat between two dates of one timestamp field
Hi,
could some one help how to caluclate days between two dates of single timestamp filed and with this
query
Select * from m_activity_transaction where actn_opp_id in (
Select actn_opp_id from m_activity_transaction where ACTN_ACTV_ID = 218
Group by actn_opp_id
having count(*) > 1 ) and ACTN_ACTV_ID = 218
order by actn_performed_on
iam getting output is
ACTN_ID ACTN_OPP_ID ACTN_PERFORMED_ON
319415 95831 27-JAN-12 11.06.20.000000 AM
315249 95831 08-FEB-12 05.32.54.000000 PM
301927 103509 20-DEC-11 04.01.43.000000 PM
301458 103509 19-DEC-11 04.51.03.000000 PM
294841 115840 10-JAN-12 03.20.12.000000 PM
312062 115840 11-JAN-12 05.17.06.000000 PM
and i nedd to caluclate no.of days between two dates like 27-JAN-12 11.06.20.000000 AM and 08-FEB-12 05.32.54.000000 PM where actn_id is unique AND ACTN_OPP_ID IS NOT UNIQUE.
Thanks in Advance,
vvr.This way?
with data as
select 315249 a, 95831 b, to_timestamp('27-JAN-12 11.06.20.000000 AM', 'DD-MON-RR HH.MI.SS.FF6 AM') dt from dual union all
select 319415, 95831, to_timestamp('08-FEB-12 05.32.54.000000 PM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual union all
select 301927, 103509 , to_timestamp('20-DEC-11 04.51.03.000000 PM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual union all
select 301458 , 103509 , to_timestamp('19-DEC-11 04.01.43.000000 PM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual union all
select 363810 , 144656 , to_timestamp('27-JUN-12 12.43.28.000000 PM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual union all
select 363500 , 144656 , to_timestamp('26-JUN-12 11.41.50.000000 AM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual union all
select 363354 , 144656 , to_timestamp('25-JUN-12 12.41.13.000000 PM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual
select a, b, diff
from (
select a, b, extract( day from (dt - lag(dt) over (partition by b order by dt, a)) ) diff
from data
) tab
where tab.diff is not null;
A B DIFF
319415 95831 12
301927 103509 1
363500 144656 0
363810 144656 1 -
Most elegant way to get difference between two tables - not minus!!!
Hi,
Simplified example of what I am trying to achieve - I have two tables ORIGINAL and REVISED.
My desired result is to compare the two, such that; -
Where data exists in both tables I get the difference between the Budget column, and if there is no difference then I want no rows.
Where data exists in ORIGINAL but not in revised I want the inverse of the current value of the Budget column.
Where data exists in REVISED I want the value from REVISED.
I can see how I can do this, cf below, but is there a more elegant solution??
Data for table ORIGINAL
select '801040' entity, '2186' expense_type, 234000 budget
from dual
union all
select '801040' entity, '3001' expense_type, 1000 budget
from dual
union all
select '801040' entity, 'P132' expense_type, 34000 budget
from dual
union all
select '801040' entity, 'P135' expense_type, 43000 budget
from dualData for table REVISED
select '801040' entity, '2186' expense_type, 235000 budget
from dual
union all
select '801040' entity, 'P132' expense_type, 34000 budget
from dual
union all
select '801040' entity, 'P139' expense_type, 56000 budget
from dualDesired output
ENTITY EXPENSE_TYPE DIFFERENCE
801040 2186 1000
801040 3001 -1000
801040 P135 -43000
801040 P139 56000
5 rows selected.
Current code to achieve this, is there a better way??
select original.entity
, original.expense_type
, (nvl(revised.budget,0) - original.budget) as difference
from original
, revised
where original.entity = revised.entity(+)
and original.expense_type = revised.expense_type(+)
and (nvl(revised.budget,0) - original.budget) != 0
union all
select revised.entity
, revised.expense_type
, revised.budget as difference
from revised
where not exists
(select 'x'
from original
where original.entity = revised.entity
and original.expense_type = revised.expense_type)
and revised.budget != 0Many thanks for your input,
Robert.
Edited by: Robert Angel on 17-Jan-2012 03:31 to change not equals to != - thanks for heads upUse full outer join:
with original as (
select '801040' entity, '2186' expense_type, 234000 budget from dual union all
select '801040' entity, '3001' expense_type, 1000 budget from dual union all
select '801040' entity, 'P132' expense_type, 34000 budget from dual union all
select '801040' entity, 'P135' expense_type, 43000 budget from dual
revised as (
select '801040' entity, '2186' expense_type, 235000 budget from dual union all
select '801040' entity, 'P132' expense_type, 34000 budget from dual union all
select '801040' entity, 'P139' expense_type, 56000 budget from dual
select nvl(o.entity,r.entity) entity,
nvl(o.expense_type,r.expense_type) expense_type,
nvl(r.budget,0) - nvl(o.budget,0) budget
from original o
full join
revised r
on (
r.entity = o.entity
and
r.expense_type = o.expense_type
where nvl(r.budget,0) - nvl(o.budget,0) != 0
ENTITY EXPE BUDGET
801040 2186 1000
801040 3001 -1000
801040 P135 -43000
801040 P139 56000
SQL> SY. -
Find the difference between two dates for the specific month and year
Hi,
I have two dates, start date is 30/12/2012 and end date is 04/01/2013. Using datediff I found the difference of days between two dates. But I find the no of days in January 2013. ie output is 4 instead of 6. I input month and year to find the no of days
for that date. In this case I input Jan 2013. How can I sql this ?I don't understand how most of the answers provided here not analytically solving the problem with many cases possible.
First let me understand you:
You have 2 dates range and you want to calculate day range for specific month and year between the original date range.
declare @for_month int = 1 --January
declare @for_year int = 2013
declare @StartDate date = '2012-12-20'
declare @EndDate date = '2013-01-04'
SELECT
CASE
WHEN (DATEPART(MONTH, @StartDate) = @for_month and DATEPART(MONTH, @EndDate) = @for_month) and ((DATEPART(YEAR, @StartDate) = @for_year or DATEPART(YEAR, @EndDate) = @for_year)) THEN
DATEDIFF(DAY, @StartDate,@EndDate)
WHEN (@StartDate < cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (@EndDate between (cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date))) THEN
DATEDIFF(DAY, DATEADD(MONTH, DATEDIFF(MONTH, -1, @EndDate)-1, 0),@EndDate)
WHEN (@EndDate > cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date)) and (@StartDate between (cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date))) THEN
DATEDIFF(DAY, @StartDate,DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @StartDate) + 1, 0))) + 1
WHEN ((DATEDIFF(DAY, @StartDate, cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date)) >= 0) and (DATEDIFF(DAY, cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date), @EndDate) >= 0)) THEN
DATEDIFF(DAY, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as datetime), DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as datetime)) + 1, 0))) + 1
ELSE
0
END as [DD]
I don't know how you calculate day range between 01/01/2013 and 04/01/2013
is 4, it is actually is 3 but if that is the case, you can add 1 from the condition. -
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,
Tauceefif 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 -
How to get values between two years??
select distinct a.band_no_daftar, a.episod, a.band_nama1, a.penjara,
to_char(b.trkh_semak_layak,'yyyy') as tahun,
c.penj_lokasi
from banduan a, prl_semak_layak b, penjara c
where a.band_no_daftar = b.no_daftar(+)
and a.episod = b.episod(+)
and a.penjara = c.penjara_id
and b.status_layak = 1
and b.trkh_semak_layak between trunc(to_date(:dari,'yyyy')) and to_date(:hingga,'yyyy')
group by c.penj_lokasi, a.band_no_daftar, a.episod, a.band_nama1, a.penjara,
to_char(b.trkh_semak_layak,'yyyy')
order by a.penjarahow i'm going to retrieve records between two years??for example from year 2011 to 2012.CREATE TABLE PRL_SEMAK_LAYAK
NO_DAFTAR VARCHAR2(10 BYTE) NOT NULL,
EPISOD NUMBER(2) NOT NULL,
TL_HUK NUMBER(1),
TL_KLAS NUMBER(1),
TL_JAD4 NUMBER(1),
TL_KESLAIN NUMBER(1),
TL_LP NUMBER(1),
TL_RAY NUMBER(1),
TL_PPI NUMBER(1),
STATUS_LAYAK NUMBER(1),
TRKH_LAYAK_PAROL DATE,
KOD_PEJABAT_PAROL VARCHAR2(10 BYTE),
TRKH_MULA_PRL DATE,
TRKH_TAMAT_PRL DATE,
TEMPOH_T NUMBER(4),
TEMPOH_B NUMBER(4),
TEMPOH_H NUMBER(4),
KLASIFIKASI_PRL NUMBER(1),
TL_ROTAN NUMBER(1),
PECAHAN_KESALAHAN VARCHAR2(3 BYTE),
TERIMA_DOSIR NUMBER(1),
TRKH_DOSIR DATE,
TRKH_SEMAK_LAYAK DATE
Insert into prl_semak layak
(NO_DAFTAR, EPISOD, TL_HUK, TL_KLAS, TL_JAD4, TL_KESLAIN, TL_LP, TL_RAY, TL_PPI, STATUS_LAYAK, TRKH_LAYAK_PAROL, TL_ROTAN, TRKH_SEMAK_LAYAK)
Values
('70201849', 9, 1, 1, 1, 1, 1, 1, 1, 1, TO_DATE('04/06/2013 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 1, TO_DATE('12/24/2012 10:46:40', 'MM/DD/YYYY HH24:MI:SS'));
Insert into prl_semak layak
(NO_DAFTAR, EPISOD, TL_HUK, TL_KLAS, TL_JAD4, TL_KESLAIN, TL_LP, TL_RAY, TL_PPI, STATUS_LAYAK, TRKH_LAYAK_PAROL, TL_ROTAN, TRKH_SEMAK_LAYAK)
Values
('670500066', 5, 1, 1, 1, 1, 1, 1, 1, 1, TO_DATE('04/22/2013 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 1, TO_DATE('12/24/2012 11:03:27', 'MM/DD/YYYY HH24:MI:SS'));
Insert into prl_semak layak
(NO_DAFTAR, EPISOD, TL_HUK, TL_KLAS, TL_JAD4, TL_KESLAIN, TL_LP, TL_RAY, TL_PPI, STATUS_LAYAK, TRKH_LAYAK_PAROL, TL_ROTAN, TRKH_SEMAK_LAYAK)
Values
('71000424', 3, 1, 1, 1, 1, 1, 1, 1, 1, TO_DATE('04/26/2013 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 1, TO_DATE('12/24/2012 11:11:01', 'MM/DD/YYYY HH24:MI:SS'));
Insert into prl_semak layak
(NO_DAFTAR, EPISOD, TL_HUK, TL_KLAS, TL_JAD4, TL_KESLAIN, TL_LP, TL_RAY, TL_PPI, STATUS_LAYAK, TRKH_LAYAK_PAROL, TL_ROTAN, TRKH_SEMAK_LAYAK)
Values
('470900549', 1, 1, 1, 1, 1, 1, 1, 1, 1, TO_DATE('12/20/2012 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 1, TO_DATE('11/08/2010 09:15:59', 'MM/DD/YYYY HH24:MI:SS'));
Insert into prl_semak layak
(NO_DAFTAR, EPISOD, TL_HUK, TL_KLAS, TL_JAD4, TL_KESLAIN, TL_LP, TL_RAY, TL_PPI, STATUS_LAYAK, TRKH_LAYAK_PAROL, TL_ROTAN, TRKH_SEMAK_LAYAK)
Values
('30906760', 2, 1, 1, 1, 1, 1, 1, 1, 1, TO_DATE('12/02/2012 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 1, TO_DATE('11/19/2010 08:54:26', 'MM/DD/YYYY HH24:MI:SS'));
COMMIT;i give you this sample,table creation and some insert values.So now,what i should do in order to retrieve all these records? -
How to give relationship between two tables with comon column with between oprator
Hi Folks,
I am using Sql Server 2008R2. I am getting a problem to establish relationship between two tables.
I have two Tables, 1.Inventory Details Table another one is Inventory Header Table.
Inventory Details Table having a column Card No and inventory Header Table having columns From card No and To Card No.
I want to give relationship between these two tables with Card no. Could you please provide me the Sql Query.
Your help would be greatly appreciated .
Regards
hasthi.
email:[email protected]Hi Raju,
We have two way that we can relate to the table either join or quality condition use following syntax/Query for relating two tables
select * from Inventory_Details ID inner join Inventory_Header IH on ID.CardNo between IH.FrmCardno and IH.ToCardNo
or
Select * from Inventory_Details ID ,Inventory_Header IH where ID.CardNo=IH.CardNo OrSelect * from Inventory_Details ID ,Inventory_Header IH where ID.CardNo between IH.FrmCardno and IH.ToCardNo
Hope this will help you
Niraj Sevalkar
Maybe you are looking for
-
While simulating cenvat in j1iex posting
while simulating cenvat in j1iex for import purchase orders an error is showing no cenvat amount to be posted and no cenvat amount to be reversed. regards balaji
-
even tho the flash player says its installed it still wont work i have tried uninstalling the programs and removing all registry values for them (not all at once) i have tried every troubleshooting tip for both programs to resolve this issue the flas
-
Running xscreensaver in the background after resume from hibernate
I'm using hibernate-script with my tuxonice kernel. One of the things that annoys me about xscreensaver is how when I resume its always running (because the requisite time has passed), so I have to type my password again, having just typed it at BIOS
-
I have documents that were done in MS Publisher 2003, and need to convert to pdf. My work computer has Acrobat 5.0 (yes, I know it is outdated but should work). The way things are installed (which I have no control over) I am printing the documents t
-
Weblogic 8.1 sp4 on Slowwaris 2.7 - Thread dump included
Dear experts, We have a time response problem within our production environment, following is our configuration : Oracle 8.1.7.4 Weblogic 8.1 sp4 Solaris Sparc 2.7 JSP is pre-compiled, though the time response behaviour is rather weird, sometime quic