[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Derive information from a transaction table and unpivote the information on SQL 2008

Posted on 2011-10-20
2
Medium Priority
?
151 Views
Last Modified: 2012-05-12
I've a table which got the data as below example: I need to organize the data according to the
sample below. I really appreciate if you can help me to write a SQL query to achieve this.


Table:

process_date              transaction_from_date                         transaction_to_date           rate            

DAY4                                         DAY1                           DAY3                100.00
DAY6                                              ---                        --                                       50.00  

Require data format:

DATE         rate
day1         100    
day2         100  
day3         100  
day4
day5
day6          50
-
-
-
-
-
-
day31
0
Comment
Question by:keplan
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 37005300
you will need to have a calendar table:
http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html

from there, you can left join to a (inline) view like this:

select c.date
  , sum(t.rate)
 from calendar c
 left join yourtable t
    on t.transaction_from_date <= c.date
  and t.transaciton_to_date >= c.date
  group by c.date

Open in new window

0
 

Author Closing Comment

by:keplan
ID: 37021170
Thanks for your answer, it is excellent.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

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