LAG & LEAD functions... Any Way to Retrieve the 1st non-NULL Values?
My question is this... Has anyone found an elegant way of getting the LAG & LEAD functions to move to the 1st NON-NULL value within the partition, rather than simply using a hard-coded offset value?
Here's some test data...
IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp
CREATE TABLE #temp (
BranchID INT NOT NULL,
RandomValue INT NULL,
TransactionDate DATETIME
PRIMARY KEY (BranchID, TransactionDate)
INSERT #temp (BranchID,RandomValue,TransactionDate) VALUES
(339,6, '20060111 00:55:55'),
(339,NULL, '20070926 23:32:00'),
(339,NULL, '20101222 10:51:35'),
(339,NULL, '20101222 10:51:37'),
(339,1, '20101222 10:52:00'),
(339,1, '20120816 12:02:00'),
(339,1, '20121010 10:36:00'),
(339,NULL, '20121023 10:47:53'),
(339,NULL, '20121023 10:48:08'),
(339,1, '20121023 10:49:00'),
(350,1, '20060111 00:55:55'),
(350,NULL, '20070926 23:31:06'),
(350,NULL, '20080401 16:34:54'),
(350,NULL, '20080528 15:06:39'),
(350,NULL, '20100419 11:05:49'),
(350,NULL, '20120315 08:51:00'),
(350,NULL, '20120720 11:48:35'),
(350,1, '20120720 14:48:00'),
(350,NULL, '20121207 08:10:14')
What I'm trying to accomplish... In this instance, I'm trying to populate the NULL values with the 1st non-null preceding value.
The LAG function works well when there's only a single null value in a sequence but doesn't do the job if there's more than a singe NULL in the sequence.
For example ...
SELECT
t.BranchID,
t.RandomValue,
t.TransactionDate,
COALESCE(t.RandomValue, LAG(t.RandomValue, 1) OVER (PARTITION BY t.BranchID ORDER BY t.TransactionDate)) AS LagValue
FROM
#temp t
Please note that I am aware of several methods of accomplishing this particular task, including self joins, CTEs and smearing with variables.
So, I'm not looking for alternative way of accomplishing the task... I'm wanting to know if it's possible to do this with the LAG function.
Thanks in advance,
Jason
Jason Long
I just wanted to provide a little follow-up now that I had a little time to check up and digest Itzik’s article and tested the code posed by Jingyang.
Turns out the code posted by Jingyang didn’t actually produce the desired results but it did get me pointed in the right direction (partially my fault for crappy test data that didn’t lend itself to easy verification). That said, I did want to post the version
of the code that does produce the correct results.
IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp
CREATE TABLE #temp (
BranchID INT NOT NULL,
RandomValue INT NULL,
TransactionDate DATETIME
PRIMARY KEY (BranchID, TransactionDate)
INSERT #temp (BranchID,RandomValue,TransactionDate) VALUES
(339,6, '20060111 00:55:55'), (339,NULL, '20070926 23:32:00'), (339,NULL, '20101222 10:51:35'), (339,5, '20101222 10:51:37'),
(339,2, '20101222 10:52:00'), (339,2, '20120816 12:02:00'), (339,2, '20121010 10:36:00'), (339,NULL, '20121023 10:47:53'),
(339,NULL, '20121023 10:48:08'), (339,1, '20121023 10:49:00'), (350,3, '20060111 00:55:55'), (350,NULL, '20070926 23:31:06'),
(350,NULL, '20080401 16:34:54'), (350,NULL, '20080528 15:06:39'), (350,NULL, '20100419 11:05:49'), (350,NULL, '20120315 08:51:00'),
(350,NULL, '20120720 11:48:35'), (350,4, '20120720 14:48:00'), (350,2, '20121207 08:10:14')
SELECT
t.BranchID,
t.RandomValue,
t.TransactionDate,
COALESCE(t.RandomValue,
CAST(
SUBSTRING(
MAX(CAST(t.TransactionDate AS BINARY(4)) + CAST(t.RandomValue AS BINARY(4))) OVER (PARTITION BY t.BranchID ORDER BY t.TransactionDate ROWS UNBOUNDED PRECEDING)
,5,4)
AS INT)
) AS RandomValueNew
FROM
#temp AS t
In reality, this isn’t exactly a true answer to the original question regarding the LAG & LEAD functions, being that it uses the MAX function instead, but who cares? It still uses a windowed function to solve the problem with a single pass at the data.
I also did a little additional testing to see if casting to BINARY(4) worked across the board with a variety of data types or if the number needed to be adjusted based the data… Here’s one of my test scripts…
IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp
CREATE TABLE #Temp (
ID INT,
Num BIGINT,
String VARCHAR(25),
[Date] DATETIME,
Series INT
INSERT #temp (ID,Num,String,Date,Series) VALUES
(1, 2, 'X', '19000101', 1), ( 2, 3, 'XX', '19000108', 1),
(3, 4, 'XXX', '19000115', 1), ( 4, 6, 'XXXX', '19000122', 1),
(5, 9, 'XXXXX', '19000129', 1), ( 6, 13, 'XXXXXX', '19000205', 2),
(7, NULL, 'XXXXXXX', '19000212', 2),
(8, NULL, 'XXXXXXXX', '19000219', 2),
(9, NULL, 'XXXXXXXXX', '19000226', 2),
(10, NULL, 'XXXXXXXXXX', '19000305', 2),
(11, NULL, NULL, '19000312', 3), ( 12, 141, NULL, '19000319', 3),
(13, 211, NULL, '19000326', 3), ( 14, 316, NULL, '19000402', 3),
(15, 474, 'XXXXXXXXXXXXXXX', '19000409', 3),
(16, 711, 'XXXXXXXXXXXXXXXX', '19000416', 4),
(17, NULL, NULL, '19000423', 4), ( 18, NULL, NULL, '19000430', 4),
(19, NULL, 'XXXXXXXXXXXXXXXXXXXX', '19000507', 4), ( 20, NULL, NULL, '19000514', 4),
(21, 5395, NULL, '19000521', 5),
(22, NULL, NULL, '19000528', 5),
(23, 12138, 'XXXXXXXXXXXXXXXXXXXXXXX', '19000604', 5),
(24, 2147483647, 'XXXXXXXXXXXXXXXXXXXXXXXX', '19000611', 5),
(25, NULL, 'XXXXXXXXXXXXXXXXXXXXXXXXX', '19000618', 5),
(26, 27310, 'XXXXXXXXXXXXXXXXXXXXXXXXX', '19000618', 6),
(27, 9223372036854775807, 'XXXXXXXXXXXXXXXXXXXXXXXXX', '19000618', 6),
(28, NULL, NULL, '19000618', 6),
(29, NULL, 'XXXXXXXXXXXXXXXXXXXXXXXXX', '19000618', 6),
(30, 27310, NULL, '19000618', 6)
SELECT
ID,
Num,
String,
[Date],
Series,
CAST(SUBSTRING(MAX(CAST(t.[Date] AS BINARY(4)) + CAST(t.Num AS BINARY(4))) OVER (ORDER BY t.[Date] ROWS UNBOUNDED PRECEDING), 5,4) AS BIGINT) AS NumFill,
CAST(SUBSTRING(MAX(CAST(t.[Date] AS BINARY(4)) + CAST(t.Num AS BINARY(4))) OVER (PARTITION BY t.Series ORDER BY t.[Date] ROWS UNBOUNDED PRECEDING), 5,4) AS BIGINT) AS NumFillWithPartition,
CAST(SUBSTRING(MAX(CAST(t.[Date] AS BINARY(4)) + CAST(t.Num AS BINARY(8))) OVER (ORDER BY t.[Date] ROWS UNBOUNDED PRECEDING), 5,8) AS BIGINT) AS BigNumFill,
CAST(SUBSTRING(MAX(CAST(t.[Date] AS BINARY(4)) + CAST(t.Num AS BINARY(8))) OVER (PARTITION BY t.Series ORDER BY t.[Date] ROWS UNBOUNDED PRECEDING), 5,8) AS BIGINT) AS BIGNumFillWithPartition,
CAST(SUBSTRING(MAX(CAST(t.ID AS BINARY(4)) + CAST(t.String AS BINARY(255))) OVER (ORDER BY t.ID ROWS UNBOUNDED PRECEDING), 5,255) AS VARCHAR(25)) AS StringFill,
CAST(SUBSTRING(MAX(CAST(t.ID AS BINARY(4)) + CAST(t.String AS BINARY(25))) OVER (PARTITION BY t.Series ORDER BY t.ID ROWS UNBOUNDED PRECEDING), 5,25) AS VARCHAR(25)) AS StringFillWithPartition
FROM #Temp AS t
Looks like BINARY(4) is just fine for any INT or DATE/DATETIME values. Bumping it up to 8 was need to capture the largest BIGINT value. For text strings, the number simply needs to be set to the column size. I tested up to 255 characters without a problem.
It’s not included here, but I did notice that the NUMERIC data type doesn’t work at all. From what I can tell, SS doesn't like casting the binary value back to NUMERIC (I didn't test DECIMAL).
Thanks again,
Jason
Jason Long
Similar Messages
-
iphone 3 dropped in salt water. Appears non-functional - won't turn on. Nothing on SIM card -- had that checked. Is there ANY way to retrieve the photos and contacts. There is no backup of them.
leave the phone in rice for >24 hours to see if it will recover
you can check itunes as well to see if there are backups there from the top of the screen hit edit (itunes on mac) > preferences > devices , and see if a backup is listed
also check icloud.com to see if you had any of the information backed up there (only possible if have have a 3gs or higher)
also there are data recovery companies that can recover data even from wrecked hard drives but its not cheap, if your interested in that I would say lookup a company called drivesavers, I believe they are authorized to work on even in warranty iphones
good luck -
i live in a home with two other people, and one housemate tried to sync up his content from iTunes and iCloud onto another housemate's new iPad. the iTunes library only contains my content. is there any way to retrieve the other person's iCloud? Can they go out of my account? Their content is saved on their iPads, but can another iCloud be on the same home computer?
The syncing of music is one way, computer to phone. See this helpful document from a fellow user. Credit goes to the author.
https://discussions.apple.com/docs/DOC-3141 -
When I updated my iPad to ios5, I lost all my calendar entries. Is ther any way to retrieve the information? This was before iCloud was up and running.
try and restore you ipad maybe that would work
-
I upgraded to Yosemite from Snow Leopard and lost my address book. Is there any way to retrieve the information?
Transfer iTunes purchases by:
iTunes Store: Transferring purchases from your iPhone, iPad, or iPod to a computer
Transfer other music by using a third-party programlike one of those discussed here:
Copy music from Ipod to new computer...: Apple Support Communities
You can also rredownload mos iTunes purchases by:
Downloading past purchases from the App Store, iBookstore, and iTunes Store -
I upgraded to Yosemite and have lost my address book. Is there any way to retrieve the information it contained?
There is nothing in the contact app and the backup seems to have disappeared, too.
-
My computer crashed, I don't have the serial number for lightroom 4. Is there any way to retrieve the serial number. I can't remember where I purchased this.
Lfiles37 if the serial number has been registered then it will be available under your account. You can find more details at Find your serial number quickly - http://helpx.adobe.com/x-productkb/global/find-serial-number.html.
-
After syncing my iphone I found that all of my notes were erased from the phone. Is there any way to retrieve the notes now that they have been erased? I had some very important notes that I have to be able to find. Anyone know how?
I think those contacts still in iphone database. So you can back up you iphone with itunes to get iphone database off your iphone. Then access the SQLite batabse.
http://support.apple.com/kb/HT1766
All data included with the backup is stored in a SQLite database format with the data within various folders not easily accessible.There are some 3rd party utilities that provide for extracting select data from an iPhone's backup and iphone database such as this one.
http://www.datarecoveryforiphone.com/ -
Type your questionI am trying to deactivate a version and transfer it to another computer, however I have lost the card with the serial number. Is there any way of retrieving the serial number?
Hello Irene,
you should have a look at Adobe's database, to see what's stored/saved about your accounts. Here you will find general infos about your Adobe Account
https://www.adobe.com/account.html and to find your serial number have a look there http://helpx.adobe.com/x-productkb/global/find-serial-number.html
Hans-Günter -
I had an iPhone 4 that got stolen and just got a new one, so after I had taken important photos I plugged the phone into iTunes and it accidentally restored to my old iPhone data and uploaded all that information, deleting the new photos before I had a chance to back them up. I don't suppose there is any way of retrieving this lost data?
If these are photos that were synced to your phone, the only way to extract them from your phone is third-party software, like this:
http://www.wideanglesoftware.com/touchcopy/index.php -
My phone was stolen in Italy, had carrier cancel service. any way to retrieve photos I took?? I cloud says offline. Not very good with technical stuff so any help would be appreciated. Thank you
My phone was stolen in Italy, had carrier cancel service. any way to retrieve photos I took?? I cloud says offline. Not very good with technical stuff so any help would be appreciated. Thank you
-
Is there a way to retrieve a Note, which I have mistakenly sent to the garbage can instead of the to the envelope?
Thank you very much,
DespondentTrue, I assumed it was created in Notes. Check your trash for the email account you created it in.
-
Any way to set the starting hit counter value
I am moving some static pages to a blog template and would like to retain the hit counter values on the new pages. Is there a way to set the starting number other then reloading the page 25,000 times
thanksWelcome to the Apple Discussions. Not with the Apple counter. However, if you use a 3rd party counter like StatCounter you can set it to any number you want. You can also set it so it will not count your visits. To install see Old Toad's Tutorial #13 - Adding a StatCounter as an HTML Snippet.
OT -
Is there any way to retrieve the solitaire game I lost from my iPod Classic?
I have lost the solitaire game that came with my iPod. Does anyone know how to retrieve it?
Hello Emily,
How did you remove a default game that came with your iPod? Either way, it may be a good idea to restore your iPod via iTunes and start from scratch.
B-rock -
I entered a ton of changes on a document and accidentally didn't save it. I was working on it for over 30 minutes...I assumed there would be a temp file somewhere on my mac - but I can't find it. Help....please
Nope, unless saved it's gone.
Maybe you are looking for
-
How to include Sap Work item in email by FSCM workflow WS01700044
Hi, In FSCM Dispute Management, we have a requirment to include the sap work item in the email being sent by the workflow - WS01700044. The underlying task in it is TS01707928. This Task sends an email to the outlook as well as work item to SAP inbox
-
IPhoto 11 malfunctions again and again...
I am running iPhoto 11 version 9.4.2 on a 2012 MacBook Pro 15. Every time I start up iPhoto it says it needs to upgrade, then I click on upgrade and it says it needs to repair it - I then let it repair it and after a 10-15 min beachball spin it then
-
Creating a Global Role using weblogic.Admin command
Hi, Does anyone have an example of creating a global role using the weblogic.Admin commands? I think I have to use the INVOKE command with the DefaultRoleMapper and createRole method, but I'm not quite sure what the rest of the syntax is. Thanks, Gab
-
Unexpected error when accessing OCS Files
Everytime we try to access OCS Files (after being idle for sometime), we get this unexpected error. After a series of clicking on the report error, the workspace eventually appears. This is the log being reported to our sysadmin: Unexpected Error Enc
-
How can I edit a pdf file? It is a form and I need to insert some text. Please help.