Solved

SQL Query Get Data from Previous Date

Posted on 2014-09-22
5
117 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
  • 3
5 Comments
 
LVL 48

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 40

Accepted Solution

by:
Sharath earned 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This code started out as a fix for a customer that had incoming data that was hunderds of numbers and words long that was to fit in one column. The problem was that the customer did not want to split words or numbers when wrapping in the column. …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

770 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