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

Posted on 2011-10-20
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.


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  
day6          50
Question by:keplan
    LVL 142

    Accepted Solution

    you will need to have a calendar table:

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

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

    Open in new window


    Author Closing Comment

    Thanks for your answer, it is excellent.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    Review MS SQL cluster diagram 9 60
    sql calculate reminders 11 52
    Testing connection to sql 7 35
    Tracking a Database Lock - SQL 2014 10 28
    Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now