Solved

SQL Query Get Data from Previous Date

Posted on 2014-09-22
5
118 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

A recent questions about how to add SSRS named instances, couldn't find any that talks about SQL server 2008, anyway I decided to help by creating some screen shots. The installation is straightforward, you just pop the SQL server 2008 installati…
It is helpful to note: This is a cosmetic update and is not required, but should help your reports look better for your boss.  This issue has manifested itself in SSRS version 3.0 is where I have seen this behavior in.  And this behavior is only see…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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