Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8525831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:05:46+00:00 2026-06-11T08:05:46+00:00

I have got a table where i store transactions for contracts like so: Id_Contract[int]

  • 0

I have got a table where i store transactions for contracts like so:

Id_Contract[int] | Month[DateTime] | Amount[int]
------------------------------------------------
        1          2012-01-01 00:00:00.000      500     
        1          2012-03-01 00:00:00.000      450 
        2          2012-09-01 00:00:00.000      300     
        3          2012-08-01 00:00:00.000      750 

The user should be able to chose the timespan of the query
what i want to archive is that if the user chooses a timespan from 01/2012 – 03-2012 he gets the result:

Id_Contract[int] | Jan 2012 | Feb 2012 | Mar 2012
--------------------------------------------------
    1                   500                       450

Do you have any suggestions how i could solve this?

Best regards,
r3try

EDIT: THANKS FOR THE ANSWERS SO FAR!
When i googled for my problem i also stumbled across pivoting, but i havent found an example so far that really solves my issue (because basically every example gives the specific possibilities for the column entries, but in my example it can be like ‘March 2012’, ‘April 2012’, …, ‘January 2013’, …)

Just to give you guys some more background info on what im planning to do with the date i got from the sql query:
I have got an asp.net webforms site with a gridview that contains some data on contracts… now this table should be expanded by the payment information on that specific contract (basically a join on Id_Contract but ‘pivoted’).
If the user chooses March 2012 – May 2012 the Gridview should contain the normal data on the contract + 3 columns on payment information (March, April, May).
In the DB there are only entries stored which have already an inserted value.
-> i hope that explanation makes things a little bit clearer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:05:48+00:00Added an answer on June 11, 2026 at 8:05 am

    Your problem can be solve using Dynamic Pivoting. Please look into this article

    Try this

    DECLARE @t TABLE(Id_Contract INT, Dt DATETIME,Amount INT)
    INSERT INTO @t SELECT 1,'2012-01-01 00:00:00.000',500
    INSERT INTO @t SELECT 1,'2012-03-01 00:00:00.000',450
    INSERT INTO @t SELECT 2,'2012-09-01 00:00:00.000',300
    INSERT INTO @t SELECT 3,'2012-08-01 00:00:00.000',750
    
    DECLARE @cols AS VARCHAR(MAX), @query  AS VARCHAR(MAX);
    
    SELECT 
        Id_Contract
        , LEFT(DATENAME(month,Dt),3) + ' ' + DATENAME(Year,Dt) AS Month_Year_Name
        ,Amount
    INTO #Temp
    FROM @t 
    WHERE Dt BETWEEN  '01/01/2012' AND '03/31/2012'
    
    SELECT  @cols = STUFF(( SELECT DISTINCT 
                                   '],[' +   t2.Month_Year_Name
                            FROM    #Temp AS t2
                            ORDER BY '],[' + t2.Month_Year_Name
                            FOR XML PATH('')
                          ), 1, 2, '') + ']'
    
    SET @query = 'SELECT Id_Contract, ' + @cols + ' FROM 
                (
                    SELECT
                         Id_Contract
                        , Amount
                        , Month_Year_Name
                    FROM #Temp
               ) x
                PIVOT 
                (
                     MAX(amount)
                     FOR Month_Year_Name in (' + @cols + ')
                ) p '
    EXECUTE(@query)
    DROP TABLE #Temp
    

    // Result

    Id_Contract Jan 2012    Mar 2012
    1           500         450
    

    Edit

    For your test data,

    DECLARE @t TABLE(Id_Contract INT, Dt DATETIME,Amount INT) 
    INSERT INTO @t SELECT 1,'2012-01-01 00:00:00.000',500 
    INSERT INTO @t SELECT 1,'2012-03-01 00:00:00.000',450 
    INSERT INTO @t SELECT 2,'2012-03-01 00:00:00.000',450 
    INSERT INTO @t SELECT 3,'2012-08-01 00:00:00.000',750 
    

    the output is

    Id_Contract Jan 2012    Mar 2012
    1              500          450
    2              NULL         450
    

    Let me know if it satisfies the requirement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a table that stores inventory for a store that looks like this
I've got a table that looks like this: Foo FooId : int (PK) BarId
I have got a table [newsletter] in the db, which saves the email address,
I have got a table in an SQL Server 2008 R2 database. It contains
The problem is: I have got a table of columns (A, B, C) where
I have got a simple MySQL table and primary index (id) is not numbered
I got a table where in got allocated space of 3gig but have no
I've got a table that people have been inserting into getting the primary key
I've got a table containing many rows. The rows are guaranteed to have been
I've got a table recording views of programs. Each program can have two different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.