Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


get previous value


get previous value

Author
Message
ekknaveen
ekknaveen
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 343
Hi,

I am having the below records

Date value
01-01-2013 10
02-01-2013 20
03-01-2013 0
04-01-2013 0
05-01-2013 30
06-01-2013 40
07-01-2013 0
08-01-2013 50

I want to display the value 20 for the dates 3rd and 4th and value 40 for the date 07th.
In general, for any date if the value is 0 then get the privoud non zero value.

Can anyone pls help me to get this one

Thanks
Naveen
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8957 Visits: 19012
;WITH SampleData (Date, value) AS (
   SELECT '01-01-2013', 10 UNION ALL
   SELECT '02-01-2013', 20 UNION ALL
   SELECT '03-01-2013', 0 UNION ALL
   SELECT '04-01-2013', 0 UNION ALL
   SELECT '05-01-2013', 30 UNION ALL
   SELECT '06-01-2013', 40 UNION ALL
   SELECT '07-01-2013', 0 UNION ALL
   SELECT '08-01-2013', 50
)
SELECT s.[Date], value = ISNULL(NULLIF(s.value,0), x.value)
FROM SampleData s
CROSS APPLY (
   SELECT TOP 1 si.value
   FROM SampleData si
   WHERE si.[Date] <= s.[Date]
      AND si.value > 0
   ORDER BY si.[Date] DESC
) x
ORDER BY s.[Date]



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search