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

  • Home
  • SEARCH
  • 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 1014933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:19:34+00:00 2026-05-16T10:19:34+00:00

HI, Using SQL server 2005 I have the following query: SELECT contact_id ,YEAR(date_created) AS

  • 0

HI,
Using SQL server 2005 I have the following query:

SELECT 
contact_id      
,YEAR(date_created)         AS giftyear
,SUM(amount_exc_vat)        AS year_total_xGA
,SUM(amount_inc_vat)        AS year_total_inGA
,COUNT(*) AS numGifts 
FROM gifts 
GROUP BY contact_id 
,Year(date_created)

Which returns data that looks like this:

contact_id  |  giftyear  | year_total_xVAT  |year_total_inVAT  |  numGifts
id001      |    2006    |   17.00          |      21.79       |     4
id001      |    2007    |   5.00           |    6.41          |     1
id001       |   2008    |   5.00           |       6.41       |     1

I then want to pivot this data to have the table looking like this instead

contact_id  |  gift_2006  |  2006_excVAT  | 2006_incVAT  | 2007gifts | 2007_excVAT  |  2007_incVAT | gift_2008  |  2008_excvat  |  2008_incvat
id001       |    1        |     17.00     |    21.79     |  1        |    5.00      |  6.41         |    1       |    5.00       |    6.41

So where gift_2006 etc is essentially a CASE statement saying if the contact_id gave a gift in 2006 then assign 1 else 0 – so that for one contact all of the information is contained in one row

Thanks in advance 🙂

  • 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-05-16T10:19:35+00:00Added an answer on May 16, 2026 at 10:19 am
    SELECT 
    contact_id,   
     CASE WHEN COUNT(CASE WHEN YEAR(date_created) = 2006 THEN 1 END) > 0 THEN 1 ELSE 0 END AS gift_2006,
    SUM(CASE WHEN YEAR(date_created) = 2006 THEN amount_exc_vat END) AS [2006_excVAT] ,
    SUM(CASE WHEN YEAR(date_created) = 2006 THEN amount_inc_vat END) AS [2006_incVAT] ,
    CASE WHEN COUNT(CASE WHEN YEAR(date_created) = 2007 THEN 1 END) > 0 THEN 1 ELSE 0 END AS gift_2007,
    SUM(CASE WHEN YEAR(date_created) = 2007 THEN amount_exc_vat END) AS [2007_excVAT] ,
    SUM(CASE WHEN YEAR(date_created) = 2007 THEN amount_inc_vat END) AS [2007_incVAT] ,
    CASE WHEN COUNT(CASE WHEN YEAR(date_created) = 2008 THEN 1 END) > 0 THEN 1 ELSE 0 END AS gift_2008,
    SUM(CASE WHEN YEAR(date_created) = 2008 THEN amount_exc_vat END) AS [2008_excVAT] ,
    SUM(CASE WHEN YEAR(date_created) = 2008 THEN amount_inc_vat END) AS [2008_incVAT] 
    FROM gifts 
    GROUP BY contact_id 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.