Row1-row2(without lead and lag function)
HelloI 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 you
It 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
Tags: Database
Similar Questions
-
What is the difference between LEAD and LAG?
Hi all
Please let me know the difference between LEAD and LAG even if both are functions. Analytics, where we can use specific to improve performance.
Thanks in advance.
concerning
PRAMODPramod salvation,
pramod62917 wrote:
Please let me know the difference between LEAD and LAGThe main difference is in which way is 'research ':
-Lead will "transfer" in respect of the order by clause
-Lag goes "back" with regard to the order by clauseSee this example (Basic):
Scott@my11g SQL>ed Wrote file afiedt.buf 1 with t as ( 2 select 'TeamX' team, 1 player_id, 'PlayerA' name from dual 3 union all select 'TeamX' team, 2 player_id, 'PlayerB' name from dual 4 union all select 'TeamX' team, 3 player_id, 'PlayerC' name from dual 5 union all select 'TeamX' team, 4 player_id, 'PlayerD' name from dual 6 union all select 'TeamZ' team, 1 player_id, 'PlayerJ' name from dual 7 union all select 'TeamZ' team, 2 player_id, 'PlayerK' name from dual 8 union all select 'TeamZ' team, 3 player_id, 'PlayerL' name from dual 9 -- end of sample data -- 10 ) 11 select 12 team 13 ,player_id current_play_id 14 ,name current_name 15 ,lead(name) over (partition by team order by player_id) next_name 16 ,lag(name) over (partition by team order by player_id) prev_name 17* from t Scott@my11g SQL>/ TEAM CURRENT_PLAY_ID CURRENT NEXT_NA PREV_NA ----- --------------- ------- ------- ------- TeamX 1 PlayerA PlayerB TeamX 2 PlayerB PlayerC PlayerA TeamX 3 PlayerC PlayerD PlayerB TeamX 4 PlayerD PlayerC TeamZ 1 PlayerJ PlayerK TeamZ 2 PlayerK PlayerL PlayerJ TeamZ 3 PlayerL PlayerK 7 rows selected.
On the first line + (team = TeamX, current_play_id = 1) +, the next_name once PlayerA regarding the order given by +(order by player_id) + team +(partition by team) + playerB. For this line it is not prev_name like no player as an inferior player_id wihtin this team.
On the 3rd line +(team=TeamX, current_play=3) +, the once next_name PlayerC in regards to the order given by the team PlayerD, the previous is PlayerB. -
to fill the gaps with value of lead and the delay and make average and the gap between earned
Thanks in advance
I have table as below
What I need is to fill the gaps with value of lead and the delay and make average and the gap between the valuesID TYPE NUM NAME BEG_MP END_MP VALUE 10001103N 3 1190001 WST 0.000 0.220 10001103N 3 1190002 WST 0.220 0.440 10001103N 3 1190003 WST 0.440 0.820 12800 10001103N 3 1190003 WST 0.820 1.180 12800 10001103N 3 1190004 WST 1.180 1.220 10001103N 3 1190004 WST 1.220 1.300 10001103N 3 1190005 WST 1.300 1.420 14800 10001103N 3 1190005 WST 1.420 1.550 14800 10001103N 3 1190006 WST 1.550 2.030 10001103N 3 1190006 WST 2.030 2.660 10001103N 3 1190007 WST 2.660 2.780
ID TYPE NUM NAME BEG_MP END_MP VALUE 10001103N 3 1190001 WST 0.000 0.220 12800 ---> Lag value 10001103N 3 1190002 WST 0.220 0.440 12800 ---> Lag Value 10001103N 3 1190003 WST 0.440 0.820 12800 10001103N 3 1190003 WST 0.820 1.180 12800 10001103N 3 1190004 WST 1.180 1.220 13800 ---> Avg(12800,14800) 10001103N 3 1190004 WST 1.220 1.300 13800 ---> Avg(12800,14800) 10001103N 3 1190005 WST 1.300 1.420 14800 10001103N 3 1190005 WST 1.420 1.550 14800 10001103N 3 1190006 WST 1.550 2.030 14800 ---> Lead Value 10001103N 3 1190006 WST 2.030 2.660 14800 ---> Lead Value 10001103N 3 1190007 WST 2.660 2.780 14800 ---> Lead Value
create table AVG_TABLE ( ID VARCHAR2(20), TYPE NUMBER, NUM NUMBER, NAME VARCHAR2(10), VALUE NUMBER, BEG_MP NUMBER(6,3), END_MP NUMBER(6,3) ) ; insert into AVG_TABLE (ID, TYPE, NUM, NAME, VALUE, BEG_MP, END_MP) values ('10001103N', 3, 1190001, 'WST', null, 0, .22); insert into AVG_TABLE (ID, TYPE, NUM, NAME, VALUE, BEG_MP, END_MP) values ('10001103N', 3, 1190002, 'WST', null, .22, .44); insert into AVG_TABLE (ID, TYPE, NUM, NAME, VALUE, BEG_MP, END_MP) values ('10001103N', 3, 1190003, 'WST', 12800, .44, .82); insert into AVG_TABLE (ID, TYPE, NUM, NAME, VALUE, BEG_MP, END_MP) values ('10001103N', 3, 1190003, 'WST', 12800, .82, 1.18); insert into AVG_TABLE (ID, TYPE, NUM, NAME, VALUE, BEG_MP, END_MP) values ('10001103N', 3, 1190004, 'WST', null, 1.18, 1.22); insert into AVG_TABLE (ID, TYPE, NUM, NAME, VALUE, BEG_MP, END_MP) values ('10001103N', 3, 1190004, 'WST', null, 1.22, 1.3); insert into AVG_TABLE (ID, TYPE, NUM, NAME, VALUE, BEG_MP, END_MP) values ('10001103N', 3, 1190005, 'WST', 14800, 1.3, 1.42); insert into AVG_TABLE (ID, TYPE, NUM, NAME, VALUE, BEG_MP, END_MP) values ('10001103N', 3, 1190005, 'WST', 14800, 1.42, 1.55); insert into AVG_TABLE (ID, TYPE, NUM, NAME, VALUE, BEG_MP, END_MP) values ('10001103N', 3, 1190006, 'WST', null, 1.55, 2.03); insert into AVG_TABLE (ID, TYPE, NUM, NAME, VALUE, BEG_MP, END_MP) values ('10001103N', 3, 1190006, 'WST', null, 2.03, 2.66); insert into AVG_TABLE (ID, TYPE, NUM, NAME, VALUE, BEG_MP, END_MP) values ('10001103N', 3, 1190007, 'WST', null, 2.66, 2.78); commit;
Hello
Use LEAD and LAG when you know exactly how far is the target line (for example, if you know the desired value is on the next row).
If you don't know exactly how far is the target line, then FIRST_VALUE and LAST_VALUE are more likely to be useful.WITH got_neighbors AS ( SELECT avg_table.* , LAST_VALUE (value IGNORE NULLS) OVER (ORDER BY beg_mp) AS prev_value , LAST_VALUE (value IGNORE NULLS) OVER (ORDER BY beg_mp DESC) AS next_value FROM avg_table ) SELECT id, type, num, name, beg_mp, end_mp , COALESCE ( value , ( NVL (prev_value, next_value) + NVL (next_value, prev_value) ) / 2 ) AS value FROM got_neighbors ORDER BY beg_mp to f ;
Riedelme is correct: LAG LEAD (as well as FIRST_VALUE and LAST_VALUE) can return only the values that are there (or that you give as default values). This means that you can not solve this problem with these functions alone; you need something else (as NVL, above) to provide value when the function does not find it.
-
by using the LAG() and MIN() functions
So, I try to get the smallest number in a column of an alias I use LAG() in. I know, which makes no sense, so here's what im trying to do with a SQL statement:
Select MIN (NUM1 - lag (num1, 1, 0)) over (ORDER BY NUM1) AS J_LAG ORDER BY HEY HEY
Basically I want the smallest number of the column that I run the lag function on. When I try to it says, ' ORA-30484: lack of specification of the window for this function. I tried to put in brackets the minutes like this: MIN (NUM1 - lag (num1, 1, 0)) and as follows: MIN (NUM1 - lag (num1, 1, 0) over (ORDER BY NUM1)) and none of them does not seem to work.
Any ideas?
-Joshualike this ?? Select Min(hey) From (Select num1 - lag(num1,1,0) over(ORDER BY NUM1) As hey From J_LAG)
-
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
-
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.
-
SQL Loader: Help TRIM and Decode functions please
Hello
I have load data from a flat file, for some columns, I need to use the TRIM and DECODE functions. It's a pipe delimited file.
I get a syntax error (one is lower) same error listed for GARNISH.
SQL * Loader-350: at line xx syntax error.
Expected ', 'or') ', found 'DECODE '.
===========
, FINAL_BILL_DATE CHAR (30) "TRIM(:FINAL_BILL_DATE)".
, BUSINESS_ID 'DECODE(:BUSINESS_ID,'B',1,'C',2,'E',3,'G',4,'O',5,'R',6,'T',7,'U',8,'H',9,-1) '.
Can someone please help.
Thank you
JosianeHello josiane.
The error you receive leads me to believe that at some point before the DECODING on the BUSINESS_ID line, probably some line before the FINAL_BILL_DATE line, it is a syntactic error causing the quotes before DECODING actually put an end to some other syntaxes. Without any lines that might actually contribute to this, including details of the header, it's the best I can advise.Hope this helps,
LukePlease check the answer as helpful or response, if it is so. If this is not the case, further clarification.
Try to always provide create table and table insert to help members of the forum to help you. -
Safari hangs and lags while watching the video on youtube
While watching therapy Unbox videos, the video hangs and lags. What's weird, is that only Unbox GAL of video therapy and I have not had this problem with any other channel. Confused! Help!?
Try a reboot.
Make a backup using Time Machine or a cloning program, to ensure that data files can be recovered. Two backups are better than one.
Try to set up another admin user account to see if the same problem persists. If back to my Mac is enabled in system preferences, the guest account will not work. The intention is to see if it is specific to an account or a system wide problem. This account can be deleted later.
Isolate a problem by using a different user account
If the problem is still there, try to start safe mode using your usual account. Disconnect all devices except those necessary for the test. Shut down the computer and then put it up after a 10 second wait. Immediately after hearing the startup chime, hold down the SHIFT key and continue to hold it until the gray Apple icon and a progress bar appear. Startup is considerably slower than normal. This will reset some caches, forces a check for directory and disables all start-up and connection, among other things. When you restart normally, the initial restart may be slower than normal. If the system is operating normally, there may be 3rd party applications that pose a problem. Try to delete/disable the third-party applications after a reboot using the UN-Installer. For each disable/remove, you need to restart if you do them all at once.
-
All drag and drop functionality stopped working in Firefox 16 / Firefox 16.0.2
Note that when it stopped working exactly but all drag and drop functionality work more... images, URLS, tabs... you name it. I was on 16 FF and upgraded to 16.0.2 without help. SafeMode... same behavior. Disable each add-on single and plugin... same thing again. using the default theme.
What's left to try?
I am running Windows XP SP3.
A complete remission to zero of the system got things working again for me. Not ideal, but I'll take it for now.
-
Lost recovery and Partition function.
Hello community HP I'm a beginner so this is my first post on a problem im having with my PC, my PC is a HP Pavilion dv4-4075la Entertainment PC.
Data sheet:
RAM: 4 GB
A4 2.1 GHz AMD Dual Core
OS / Type: Windows 7 Ultimate 64-bit
AMD Radeon HD 6480G
Version of DirectXX 11
Problem: I lost function recovery after a problem with windows so my only option was formatting the entire drive (Partitions included) I lost my recovery Partition and its function, after that I installed Ubuntu 14.04 LTS 64 bit, but it worked not for the games because the graphic card after that I installed Windows 7 Ultimate 64 bit , sound factory OS Windows 7 Home Basic 64 bit, and I want to recovery because HP support Assistant and other HP software don't work properly and im unable to run an uninstall also shutdown my PC due to overheating (90 d) something like that... Sorry for my English im not American.
So I hope to have an answer or assistance pls also hope to see Linux on the market .
See you soon!
Please use the support of HP recovery, you created earlier to return the computer to a factory State. If you create your HP recovery media when you first set up your computer, please contact the official support of HP in your region / country, via the HP portal to support all over the world, to see if the HP recovery support is available for your computer.
Alternatively, if you can still read the Windows product key of 25 characters on the COA Microsoft attached to your computer, you can install Windows 7 Home Basic by using this key. Please see "how to install Windows 7 without the disc" to get instructions on how to download Windows 7, create the installation media and install Windows 7 using the product key 25 character of the COA Microsoft on your computer. This method will not install HP customizations or value-added software and you will have to manually install all drivers from the page of Entertainment Notebook PC drivers HP Pavilion dv4-4075la.
Windows 7 SP1 can be found HERE and HERE. (Download the Windows 7 ISO)
Link to "ei.cfg removal utility" . (Download and use to allow you to install Windows 7 Home Basic Edition)
Direct link to Windows 7 USB/DVD Download tool. (Use to create ISO support)
If you have any other questions, feel free to ask.
Please click the 'Thumbs Up' white LAURELS to show your appreciation
-
Lead and lead state details status
It is a two part question on the State of conduct and lead the details of situation. We are about to revise our current model in SFDC.
First of all, if you are tracking MQL, SAL, SQL, SQO, do you rely on your CRM drive State and details of the state drive to differentiate the prospects who agreed sales or sales qualified by result status such as Open, work, committed? If 'no', how are you going to do it?
Then, here is the revised lead the statutes (in blue) and details of situation lead before us. Are there statutes obvious lack?
Working
Committed
Enrich (culture)
Partners
1st attempt to contact
BANT appeal at the request
Future calendar (contacted but not a SAL)
Application of accepted partner
2nd attempt to contact
In the Conversation
Future needs (contacted but not a SAL)
Partner request denied
3rd attempt to contact
Planning meeting
Future Budget (contacted but not SAL)
No attempt to contact
Awaiting response from the customer
Lack of Contact Info
The Contact Search
Awaiting response from the customer
No answer
Work of PH
Disqualified
Purge
Convert
Prospecting
Competitor
Bad Contact Information
Convert to partner
Account name
Best practices survey
Invalid company information
Converted to Contact (existing possibility)
Enriched Ph
No Contact Center
Duplicate record
Converted to opportunity
REP selected
No decision maker / influencer
No longer works
Outside Service area
Out of business
These recent ACD / IVR purchase
Name of the vendor
Too small
WFO only
Hi, Kim. Thank you for your thoughtful response. Looks like you've been down this road before and your insight is appreciated. Follow a few answers/clarification:
- Open the report - I don't understand the above, but there isn't. Open (without details) is the default value. Our plan is to implement something similar to what you did; after XX days intact, "resume" and return to a flow of feed.
- Work situation - this status allows officials to hold the wires in their queue, without worrying about "recovery" we have SLAS in place that limit the duration of that recording can sit in the work, but these SLA is not forced, rather through reports of aging Rep systemattically. If some representatives sit on the job too long, then they will get a call from their boss. No automatted 'come back' in the case of 'work '.
- Excellent point on the dependecy necessary to lead State detail for lead status = Enrich. I make sure to drive retail for committed State.
- Status of committed - it seems actually a SAL and teams were. In our version of a report for the pipeline reach and waterfall, anything before Engaged is considered a MQL, suspicious or investigation. Change in lead to 'Committed' to 'Open' or 'Work' status indicates that it is accepted sales.
I am happy to know that you (and probably others) rely on status of lead and lead State detail to differentiate what is accepted by sales, and what is simply the collection of dust. I wasn't sure if there was a better way. Now, I am more confident in what we do.
Thank you and have a great Friday.
J
-
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
-
Since the last refresh of my payment for Adobe enjoy cloud data, I was not able to use the software, not Eve open one of the applications, even the payment has been made and saved to my banck account. I have been a month without service and I've not found a way to communicate directly with any responsible for Adpbe, nor received any satisfactory support of any kind. How can solve this problem? Is it possible to have a personal contact to have a personal support, instead of a jungle of pages on the web that will lead you to more of the same pages?
The CC is waiting for bank payment, if payment is declined then CC will be suspended.
Concerning
Stéphane
-
MONTH and YEAR functions. Help, please...
Can someone tell me why the YEAR and MONTH functions only accepts the format dd-MMM-yyyy (e.g. 1 January 2009)
The guest of the calendar will be return only the format dd/mm/yyyy (e.g. 01/01/2009). This makes it pretty useless calendar.
I really need help on this. Does anyone know how to get the value returned by the calendar in a command prompt in a format that can be used by the YEAR and MONTH functions?
Thanks in advanceHere's one without having to resort to assess:
day: substring (' @{Date1} {01/01/1900}' of (locate('/','@{Date1}{01/01/1900}',1) + 1) for (locate ('/ ',' @{Date1} {01/01/1900}', 4)-locate('/','@{Date1}{01/01/1900}',1)-1))
month: substring (' @{Date1} {01/01/1900}' of 1 for (locate ('/ ',' @{Date1} {01/01/1900}', 1)-1))
year: substring (' @{Date1} {01/01/1900}' of (locate ('/ ',' @{Date1} {01/01/1900}', 4) + 1) for 4)in the format DD/MM/YYYY
See also:
http://obiee101.blogspot.com/2009/03/OBIEE-date-prompt-to-day-month-year.htmlconcerning
John
http://obiee101.blogspot.com/Published by: John Minkjan, March 10, 2009 12:20 AM
-
I would like to be able to access the start page of Thunderbird at random times, without disconnecting and then insert it again.
Is this possible?
Under the GO menu is PAGE of STARTING PRINCIPAL.
The shortcut is alt + home page.
Maybe you are looking for
-
Pavilion x 2-10-n109nl: Audio services does not
Hi all, at one point in time and after any particular event, my laptop (Pavilion x 2-10-n109nl with Win10x64) started does not recognize the audio device. It displays a red x is close to the 'Volume' icon in the taskbar notification as if no audio de
-
Hi all About two weeks ago, I finished customizing my new Toshiba Qosmio laptop, and I noticed that the top shortcut keys stopped working as usual. Them, I looked on various forums and sites and found no help. Well, I decided to restore the original
-
It is possible to omit the popup warns "do you want to replace the existing file?
Well, the title says already I have an application where the user can save the results to a log file; These data are added (using OpenFile (file_name, VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII), so the opening of this file will not replace its content, b
-
The CAB ARCHIVES and the BZIP2 ARCHIVE in the C folder are corrupt
original title: archive BZIP2 I ran scans to start and I am told that the CAB ARCHIVE and the ARCHIVE BZIP2 in the C folder are corrupt and I need to now how to fix. I think that is what slows down my lap top. If someone can appreciate it. Denise
-
Original title: widows update does not work. Microsoft Fix - It fails with the error 0 x 80040154. What can I do? Widows Update does not work. Microsoft Fix - It fails with the error 0 x 80040154 The notice reads as follows: "Year error occurred whi