I think I need a LEAD/LAG function?
Hello
Oracle 10.2.0.4
I have a table that contains an atrribute used over time. Number of lines does not change the value and is there only to maintain integrity between the lines of other tables.
CREATE THE EMP_ATTR TABLE (EMPID VARCHAR2 (8) NOT NULL, ASOFDATE DATE NOT NULL, ATTR VARCHAR2 (13));
Values inserted INTO the EMP_ATTR ("'001', January 1, 2010 ', ' XX');
INSERT INTO EMP_ATTR VALUES ('001', FEBRUARY 2, 2011 "," YY");
INSERT INTO EMP_ATTR VALUES ('001', MARCH 4, 2011 "," YY");
INSERT INTO EMP_ATTR VALUES ('002', JANUARY 1, 2010 "," TT");
INSERT INTO EMP_ATTR VALUES (' '002', 1ST FEBRUARY 2010 TT ', ' ');
INSERT INTO EMP_ATTR VALUES (' '002', 1 DECEMBER 2013 ', ' TT');
INSERT INTO EMP_ATTR VALUES (' '003', JANUARY 1, 2010 ', ' EW');
INSERT INTO EMP_ATTR VALUES (' '003', MARCH 2, 2010 ', ' RT');
INSERT INTO EMP_ATTR VALUES (' '003', 1 NOVEMBER 2013 EW ', ' ');
INSERT INTO EMP_ATTR VALUES (' '003', 1 DECEMBER 2013 EW ', ' ');
What I have to do is to analyze the table, and for each return line when the person assigned this attribute, without going through another attribute assigned in the meantime
So I need something that will return
EMPID, ATTR, ASOFDATE, FIRST_ASOFDATE
001, XX, JANUARY 1, 2010, JANUARY 1, 2010
001, YY, FEBRUARY 2, 2011, FEBRUARY 2, 2011
001, YY, March 4, 2011, February 2, 2011 <-as this was awarded in February and the line of March has not changed the attribute
002, TT, JANUARY 1, 2010, JANUARY 1, 2010
002, TT, FEBRUARY 1, 2010, JANUARY 1, 2010
002, TT, DECEMBER 1, 2013, JANUARY 1, 2010
003, EW, JANUARY 1, 2010, JANUARY 1, 2010
003, RT, MARCH 2, 2010, MARCH 2, 2010
003, EW, November 1, 2013, November 1, 2013 <-EW was previously assigned in 2010, but they have a RT line between Mar-2010 so it's seen as a reboot
003, EW, December 1, 2013, November 1, 2013 <-there was no intermediate attribute inherit so assignment NOV forward
Can be
SQL > select empid, asofdate, attr, asofdate_first
25 years of emp_attr
model 26
27 partition by (empid)
size 28 per (row_number() over (partition by asofdate order empid) rno)
29 measures (asofdate, attr, sysdate asofdate_first, ' ' attr_prev)
30 rules
(31)
32 attr_prev [any] = attr [cv () - 1],
33 asofdate_first [any] = decode (attr_prev [cv ()], attr [cv ()], [cv () - 1] asofdate_first, asofdate [cv ()])
34)
35 order by empid, asofdate;
EMP ASOFDATE TO ASOFDATE_
--- --------- -- ---------
001-1 JANUARY 10 XX 1 JANUARY 10
001 2 FEBRUARY 11 AA 2 FEBRUARY 11
001 4 MARCH 11 AA 2 FEBRUARY 11
002 1 JANUARY 10 TT 1 JANUARY 10
002 1 FEBRUARY 10 TT 1 JANUARY 10
002 1 DECEMBER 13 TT 1 JANUARY 10
003 1 JANUARY 10 EW 1 JANUARY 10
003 2 MARCH 10 RT 2 MARCH 10
1ST NOVEMBER 13 003 EW 1 NOVEMBER 13
1ST DECEMBER 13 003 EW 1 NOVEMBER 13
10 selected lines.
Tags: Database
Similar Questions
-
Oracle version: 11.0.2.0.2.0
Dear all
I have a question where I am using tables Tab1 and Tab2
Struncture of tab1 is
Store_no NUMBER,
Sell_date DATE
Sell quantity number,
number of item_no
to tab2
Number of Item_no
date date1,
number of item_price
I need know the total price of the article sold for 2 days (current as previous)
Select the store, sell_date, item_no, sell_qty * item_price, lag (sell_qty * item_price, 1) on the partition by (store_no, item_no order of sell_date)
of tab1, tab2
where tab1.item_no = tab2.item_no
and sell_date = date1
Question1) tables have millions of lines... The lag function is causing the query to run for hours...
Could you please suggest if we capture these sorts of queries where Lag/lead are used.
Your query does not much sense. Analytical functions are applied to already recovered lines, and since you have sell_date = date1, all you get is one day sales. All you need is:
Select the store
Max (sell_date),
tab1.item_no,
sum (case sell_date when date1 then sell_qty * end item_price) day_sales_amount,.
sum (case sell_date when date1 - sell_qty then 1 * item_price end) prev_day_sales_amount
of tab1.
tab2
where tab1.item_no = tab2.item_no
and sell_date in the fields (date1, date1 - 1).
Store group,
tab1.item_no;
SY.
-
Hi all
I want to understand the inner workings of the Lag function in Oracle, which could help to solve my problem.
I have the table as the structure below.
P_ID ENTRY_TIMESTAMP SIZE C_ID 15287 1 NOVEMBER 15 05.45.00 PM 30 92 15295 06.00.00 PM 1ST NOVEMBER 15 15 92 15306 06.00.00 PM 1ST NOVEMBER 15 1 92 Now, when I write a query as below:
Select the size, CURR_TIMESTAMP TO_TIMESTAMP (TO_CHAR (ENTRY_TIMESTAMP, 'YYYYMMDDHH24MI'), 'YYYYMMDDHH24MI'),
TO_TIMESTAMP (TO_CHAR (LAG (ENTRY_TIMESTAMP) OVER (PARTITION BY ORDER BY ENTRY_TIMESTAMP ASC C_ID), 'YYYYMMDDHH24MI'), 'YYYYMMDDHH24MI') LAG_TIMESTAMP
OF Entry_log
where ENTRY_TIMESTAMP BETWEEN to_date (1 November 15 18.00.00','DD-MON-YY HH24.MI.) SS') AND to_date (1 November 15 18.00.00','DD-MON-YY HH24.MI.) SS')
Query will lead to give Lag_timestamp as 06.00.00 PM for size = 1 but will be NULL for size = 15.
Why is that Oracle will never lag_timestamp = 06.00.00 PM for size = 15.
Could someone please explain this behavior.
I'm using Oracle 11 g.
Thank you
I had a small mistake in the above query. now, I fixed it
-
How can I use the lead/lag in this query
I wrote this query that gives me the comparative data based on this week and next week. How is the lead/Lag used in this query.
Output is like this.WITH CURRENT_WEEK AS ( SELECT QPAQ.YEAR YEAR, QPAQ.SEASON SEASON, REGEXP_SUBSTR (QPAQ.SERIES_NAME, '[^/]+') ACC_SERIES, TO_NUMBER (QPAQ.WEEK) WEEK, MAX (QPAQ.FAILURES) FAILURES FROM QAR_PLAN_ACC_QTY QPAQ, QAR_PLAN_THRESHOLD_LST QPTL WHERE QPTL.CATEGORY_ID = 7 AND QPAQ.YEAR = QPTL.YEAR AND QPAQ.SEASON = QPTL.SEASON AND QPAQ.SERIES_NAME = QPTL.MODEL_SERIES GROUP BY QPAQ.YEAR, QPAQ.SEASON, REGEXP_SUBSTR (QPAQ.SERIES_NAME, '[^/]+'), TO_NUMBER (QPAQ.WEEK) ORDER BY REGEXP_SUBSTR (QPAQ.SERIES_NAME, '[^/]+')), LAST_WEEK AS ( SELECT QPAQ.YEAR YEAR, QPAQ.SEASON SEASON, REGEXP_SUBSTR (QPAQ.SERIES_NAME, '[^/]+') ACC_SERIES, TO_NUMBER (QPAQ.WEEK + 1) WEEK, MAX (QPAQ.FAILURES) FAILURES FROM QAR_PLAN_ACC_QTY QPAQ, QAR_PLAN_THRESHOLD_LST QPTL WHERE QPTL.CATEGORY_ID = 7 AND QPAQ.YEAR = QPTL.YEAR AND QPAQ.SEASON = QPTL.SEASON AND QPAQ.SERIES_NAME = QPTL.MODEL_SERIES GROUP BY QPAQ.YEAR, QPAQ.SEASON, REGEXP_SUBSTR (QPAQ.SERIES_NAME, '[^/]+'), TO_NUMBER (QPAQ.WEEK) ORDER BY REGEXP_SUBSTR (QPAQ.SERIES_NAME, '[^/]+')) SELECT CURRENT_WEEK.YEAR, CURRENT_WEEK.SEASON, CURRENT_WEEK.ACC_SERIES, CURRENT_WEEK.WEEK, CURRENT_WEEK.FAILURES, (CURRENT_WEEK.FAILURES - LAST_WEEK.FAILURES) FAILURES_COMPARE FROM LAST_WEEK, CURRENT_WEEK WHERE CURRENT_WEEK.WEEK = LAST_WEEK.WEEK(+) ORDER BY CURRENT_WEEK.WEEK;
Edited by: BluShadow January 6, 2012 13:26YEAR SEASON MODEL WEEK FAILURES Failures_COMPARE 1 2011 SUMMER VGP-BMS15 49 10 2 2011 SUMMER VGP-BMS15 50 28 18 3 2011 SUMMER VGP-BMS15 51 30 2 4 2011 SUMMER VGP-BMS15 52 40 10
addition of {noformat}{noformat} tags. Please read {message:id=9360002} to learn to do this yourself in future.
You would dump the subquery LAST_WEEK together. Then replace the calculation of your failure with
current_week.failure - lag(current_week.failure) over (order by current_week.year, current_week.week) as failures_compare
I guess you might want to think about renaming the subquery as well...
Cheers, APC
Published by: APC on 6 January 2012 13:41
-
I'm buying a new MacBook to replace my mid-2010 MacBook Pro 2.4 GHz Intel core 2 duo, 4 GB of memory, which always runs, but becomes slow. I don't think I need retina display (if I have a lot of photos and films on the subject), but I was told that the MacBook Pro without screen retina was an old system that Apple could not support much longer. Is this true?
Barbara cfrompa says:
but I was told that the MacBook Pro without screen retina was an old system that Apple could not support much longer. Is this true?
Apple only can answer such a question, and the terms of use for these forums discourage us discuss what Apple plans to do.
I too have a MBP 2010. I installed an SSD and that increased performance. Consider that as an option.
You may experience other problems that slows down your MBP. Before commit you the important cash for a new MBP, examine the potential software problems that may be responsible for the poor performance. With the SSD, my 2010 works better than the day he came out of the box.
Ciao.
-
My battery status shows the service battery. I have reset the alert status about 6 months ago, but he will not be reset again. Thinking I need a new battery Apple!
Batteries are no longer available on the Apple online store but can be at your nearest Apple Retail Store, you must check with your store. A better option is probably buy one yourself and replace it. Buy a quality like this http://eshop.macsales.com/shop/apple_laptop/batteries/MacBook_13_Unibody battery
Avoid imitations cheap as those who are sold on auction sites.
-
I need a printer duplex function your advertising is not clear analysis - recommendations?
I need a printer duplex function your advertising is not clear analysis - recommendations?
Hello
The mentioned models there drivers for Mac OS X 10.11
Please confirm with the link:
Hope that helps.
Please click the "accept as solution" if it helped to solve your problem and "Bravo Thumbs up" as an encouragement.
-
HP Officejet Pro 8600 Plus: I think I need to uninstall and reinstall the printer
My printer started to print only if I turn it on, then go back on it. I think I need to uninstall and reinstall the printer. I have the CD, but not sure of updates. This all started when my cable company came out to solve my problem of modem and saw that I was running my wireless printer. He said that I should never do that, so I went back to USB. When I did, the printer would not analyze. I've switched back to wireless, but now I have the power problem. Help!
Hello
Try the procedure described in this doc connecting the printer after change of your wireless network.
-
Can I install PS elements 13 and PS Cc on my PC? I want to be able to use my skills and according to the needs of the image function.
Different, different programs installed in different folders
-
LAG function alternative queries
Dear all,
WITH t AS
(SELECT 1 AS num, "test1" AS status
OF THE DOUBLE
UNION ALL
SELECT 2 AS num, "test2" LIKE status
OF THE DOUBLE
UNION ALL
SELECT 3 AS num, "test3" LIKE status
OF THE DOUBLE
UNION ALL
SELECT the 4 ACEs num, "test4" LIKE status
OF THE DOUBLE
UNION ALL
SELECT 5 AS num, "test5" LIKE status
THE DOUBLE)
SELECT the State, LAG (status 1) OVER (ORDER BY num) AS previous_status
T
Please help me to write search above without LAG function like oracle forms10g unsupported feature LAG.
Hello, you can join the table itself for the results. This will work only if you have a number in ascending order.
WITH t AS
(SELECT 1 AS num, "test1" AS status
OF THE DOUBLE
UNION ALL
SELECT 2 AS num, "test2" LIKE status
OF THE DOUBLE
UNION ALL
SELECT 3 AS num, "test3" LIKE status
OF THE DOUBLE
UNION ALL
SELECT the 4 ACEs num, "test4" LIKE status
OF THE DOUBLE
UNION ALL
SELECT 5 AS num, "test5" LIKE status
THE DOUBLE)
T1.status, t2.status SELECT previous_status
T t1, t t2
where t2.num (+) = t1.num - 1
order of t1.status
Best regards, Andrei
-
try to follow the tutorial of Muse to apply a store online, downloaded lesson files will not open, but an alert says my version of Muse needs update. The function of update in Muse said that my version is the most recent; 2014.3!
I expect that you use OS X 10.8 or earlier? Muse 2015.0 and later requires OS X 10.9 or higher.
-
LAG function to get the value of the measurement for parent
Hello
I'm interested if there is no function in OLAP DML, which is similar to the function of TROLLING. LAG function returns the measured value for the previous dimension member (or a few members of the back dimension). I would like to know if this function exists which does not return a meaningless measurement for the dimension member value, but the measured value for the parent of the live dimension member.
Thanks in advance
Peter_parentrel relationship identifies the parent of each Member of the dimension of a hierarchy. use:
the time - dimension
time_parentrel - Member parent expression
time_parentrel (time_hierlist 'CALENDAR') - expression for parent along the CALENDAR hierarchySales - measure
Sales (Time time_parentrel) - expression returning sales for the parent of the current Member along the existing hierarchy.
sales (time time_parentrel (time_hierlist 'EXERCISE'))-expression returning the sales for the parent of the current Member along the FISCAL hierarchy.You can search the user's Guide the OLAP DML for many examples of the use of the OLAP DML commands/functions/objects in expressions.
All of these objects are automatically created/maintained while retaining the dimension from the front end (MN).For example:
(11g) OLAP DML users guide notions-> about-> OLAP DML objects given OLAP DML-> objects that support the use of hierarchical Dimensions section. -
Row1-row2(without lead and lag function)
Hello
I wanted less row1 - row2 can I could use at least conditions he same for 1000 lines and should not use the features of lead and lag
Thank youIt if self explaining IMHO
with data as ( select 9 r from dual union all select 7 from dual union all select 6 from dual union all select 3 from dual ) , rdata as ( select r, row_number() over (order by r desc) rn from data ) select d1.r - nvl(d2.r, '0') res from rdata d1, rdata d2 where d1.rn +1 =d2.rn(+) RES 2 1 3 3
Concerning
-
Hello
If you have a database table called X, and it looks like this:
OrderID Transtype amount
----------------------------------------------
1 53 10
1 57 10
2 53 20
2 55 10
2 57 10
3 53 30
3 57 40
How can I get this result using the advance functions or a delay:
OrderID Transtype amount Diff
-------------------------------------------------------------
1 53 10
1 57 10 0 <--10-10
2 53 20
2 55 10
2 57 10 0 < - 20/10 / 10
3 53 30
3 57 40 - 10 <--30-40
The arrows are only for the explanation of the calculation.
Concerning
/ Nicklaswith tab as ( select 1 a, 53 b, 10 c from dual union all select 1, 57, 10 from dual union all select 2, 53, 20 from dual union all select 2, 55, 10 from dual union all select 2, 57, 10 from dual union all select 3, 53, 30 from dual union all select 3, 57, 40 from dual ) select a, b, c, case when lead(c) over (partition by a order by b) is null then 2*first_value(c) over (partition by a order by b) - sum(c) over (partition by a order by b) else null end diff from tab; A B C DIFF ---------- ---------- ---------- ---------- 1 53 10 1 57 10 0 2 53 20 2 55 10 2 57 10 0 3 53 30 3 57 40 -10 7 rows selected.
-
Need help with analytical function (LAG)
The requirement is as I have a table with described colums
col1 County flag Flag2
ABC 1 Y Y
XYZ 1 Y Y
XYZ 1 O NULL
xyz *2* N N
XYZ 2 Y NULL
DEF 1 Y Y
DEF 1 N NULL
To get the columns Flag2
1 assign falg2 as indicator for rownum = 1
2 check the colm1, count of current line with colm1, Earl of the previous line. The colm1 and the NTC are identical, should assign null...
Here's the query I used to get the values of Flag2
SELECT colm1, count, flag
BOX WHEN
LAG(Count, 1,null) OVER (PARTITION BY colm1 ORDER BY colm1 DESC NULLS LAST) IS NULL
and LAG(flag, 1, NULL) PLUS (SCORE FROM colm1 ORDER BY colm1, cycle DESC NULLS LAST) IS NULL
THEN the flag
END AS Flag2
FROM table1
but the query above returns the o/p below which is false
col1_ County flag Flag2
ABC 1 Y Y
XYZ 1 Y Y
XYZ 1 O NULL
xyz *2* N NULL
XYZ 2 Y NULL
DEF 1 Y Y
DEF 1 N NULL
Thank you
Published by: user9370033 on April 8, 2010 23:25Well, you have not enough explained your full requirement in this
1 assign falg2 as indicator for rownum = 1
2 check the colm1, count of current line with colm1, Earl of the previous line. The colm1 and the NTC are identical, should assign null...as you say not what Flag2 must be set on if com1 and cnt are not the same as the previous row.
But how about this as my first guess what you mean...
SQL> with t as (select 'abc' as col1, 1 as cnt, 'Y' as flag from dual union all 2 select 'xyz', 1, 'Y' from dual union all 3 select 'xyz', 1, 'Y' from dual union all 4 select 'xyz', 2, 'N' from dual union all 5 select 'xyz', 2, 'Y' from dual union all 6 select 'def', 1, 'Y' from dual union all 7 select 'def', 1, 'N' from dual) 8 -- END OF TEST DATA 9 select col1, cnt, flag 10 ,case when lag(col1) over (order by col1, cnt) is null then flag 11 when lag(col1) over (order by col1, cnt) = col1 and 12 lag(cnt) over (order by col1, cnt) = cnt then null 13 else flag 14 end as flag2 15 from t 16 / COL CNT F F --- ---------- - - abc 1 Y Y def 1 Y Y def 1 N xyz 1 Y Y xyz 1 Y xyz 2 Y Y xyz 2 N 7 rows selected. SQL>
Maybe you are looking for
-
Can I use 10W with my iPhone charger 5
I just bought an IPad 2 Air. Can I use the charger 10W with my other devices like the iPhone 5?
-
Upgrade Vista SP2 stops at step 3-0%
HelloMy Dell XPS M1330 laptop is running Vista and I've updated with SP2. All went well until it restarted and began to step 3. It says 0% and nothing happens. After a minute or two, the computer restarts and returns to the same step it over again an
-
Cannot create the Documents folder. No error, it just does not respond.
How is it that I can't create a folder in my documents once I installed skydrive? Does that mean if I click "record" to make a new folder in my documents he won't answer more after that I have to install/activate sky player. Original title: how to c
-
Hello I'm starting to read about ASA 55xx in Cisco's Web site. But after a good read, I have a few questions... In Cisco Docs on ASA55xx, I see the "Maximum simultaneous AnyConnect or VPN sessions without client" and "Maximum simultaneous site-to-sit
-
PowerEdge 1900 w / PERC5 / i or SAS5 / I under compatibility Win 2008
I have a PowerEdge 1900 (internal SATA only now) that I plan on the deployment of Windows Server 2008 Standard (64 bit) on short time. When I bought the camera, not to not order any controller RAID; before installing Win2008, I would add one of the s