?
Solved

SQL Query Get Data from Previous Date

Posted on 2014-09-22
5
Medium Priority
?
125 Views
Last Modified: 2014-10-21
I am looking to get previous data where the date's corresponding data is null.  Basically I want to pre-fill the rate from previous business days for the weekend and holidays where it did not exist in the rate table; therefore I joined a calendar table to ensure it returns a date value.

In the example shown below for 'gb' and 'eu' rates, I want to fill in for the weekends from that previous Friday's data.  Is this possible without a stored procedure?


SQL QUERY
select distinct a.DateKey as CalendarDate
, fx.NumeratorCurrCode as ISO_Curr
, fx.SpotRate
, fx.Mod_SpotRate
from AdvDateList a
left outer join (select NumeratorCurrCode, DenominatorCurrCode
				, AsOfDate, SpotRate, 1/SpotRate as Mod_SpotRate -- Mod_SpotRate
				from APX.FxRateHistory fx
				where NumeratorCurrCode in ('gb','eu')
				and DenominatorCurrCode = 'us'
				) fx on a.DateKey = fx.AsOfDate
where a.DateKey between (getdate()-6) and (getdate()-1) -- returns previous 5 days

Open in new window



ORIGINAL RESULTS                  
Date      ISO_Curr      SpotRate      Mod_SpotRate
9/17/2014      eu      0.7773      1.286504567
9/17/2014      gb      0.6144      1.627604167
9/18/2014      eu      0.7739      1.292156609
9/18/2014      gb      0.6101      1.639075561
9/19/2014      eu      0.7794      1.283038235
9/19/2014      gb      0.614      1.628664495
9/20/2014      NULL      NULL      NULL
9/21/2014      NULL      NULL      NULL

                  
                  
EXPECTED RESULTS                  
Date      ISO_Curr      SpotRate      Mod_SpotRate
9/17/2014      eu      0.7773      1.286504567
9/17/2014      gb      0.6144      1.627604167
9/18/2014      eu      0.7739      1.292156609
9/18/2014      gb      0.6101      1.639075561
9/19/2014      eu      0.7794      1.283038235
9/19/2014      gb      0.614      1.628664495
9/20/2014      eu      0.7794      1.283038235
9/20/2014      gb      0.614      1.628664495
9/21/2014      eu      0.7794      1.283038235
9/21/2014      gb      0.614      1.628664495
0
Comment
Question by:KANEDA 0149
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40338129
Is this SQL Server? (or what dbms is this for?) and what version? (this is important as solution options can change by version)

& it's a good idea to include this info in any SQL related question
0
 

Author Comment

by:KANEDA 0149
ID: 40339005
Hi Paul, my apologizes.  It is SSMS 2008.
0
 

Author Comment

by:KANEDA 0149
ID: 40339011
SSMS 2008 R2 actually
0
 
LVL 41

Accepted Solution

by:
Sharath earned 2000 total points
ID: 40393756
try this.
;WITH cte 
     AS (SELECT DISTINCT a.DateKey            AS CalendarDate, 
                         fx.NumeratorCurrCode AS ISO_Curr, 
                         fx.SpotRate, 
                         fx.Mod_SpotRate 
           FROM AdvDateList a 
                LEFT OUTER JOIN (SELECT NumeratorCurrCode, 
                                        DenominatorCurrCode, 
                                        AsOfDate, 
                                        SpotRate, 
                                        1 / SpotRate AS Mod_SpotRate 
                                   FROM APX.FxRateHistory fx 
                                  WHERE NumeratorCurrCode IN ( 'gb', 'eu' ) 
                                    AND DenominatorCurrCode = 'us') fx 
                             ON a.DateKey = fx.AsOfDate 
          WHERE a.DateKey BETWEEN ( Getdate() - 6 ) AND ( Getdate() - 1 )) 
SELECT t1.CalendarDate, 
       COALESCE(t1.ISO_Curr, t2.ISO_Curr)         ISO_Curr, 
       COALESCE(t1.SpotRate, t2.SpotRate)         SpotRate, 
       COALESCE(t1.Mod_SpotRate, t2.Mod_SpotRate) Mod_SpotRate 
  FROM cte t1 
       LEFT JOIN cte t2 
              ON t2.CalendarDate = Dateadd(dd, CASE Datepart(dw, t1.CalendarDate) 
                                                 WHEN 7 THEN -1 
                                                 WHEN 1 THEN -2 
                                                 ELSE 0 
                                               END, t1.CalendarDate) 
                 AND t2.ISO_Curr = COALESCE(t1.ISO_Curr, t2.ISO_Curr); 

Open in new window


here is an example:
http://sqlfiddle.com/#!3/618128/14
0
 

Author Closing Comment

by:KANEDA 0149
ID: 40394374
Thank you Sharath, that worked exactly as I needed.
0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Earlier I wrote an article about the new lookup functions (http://www.experts-exchange.com/A_3433.html) that ship with SQL Server 2008 R2.  In this article I’m going to show you another new feature of SSRS 2008 R2, this time in the vis…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question