Solved

SQL Query Get Data from Previous Date

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to use Variables  and Custom code in SSRS report and Assembly reference to use compile shared code in SSRS. Its big question for all who are working with SSRS. It is easy to create assembly and refer in SSRS report, still there are some steps…
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…

726 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