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 877587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:38:02+00:00 2026-05-15T11:38:02+00:00

I’m back with another (possibly) silly question. sorry. I have a pretty complicated query

  • 0

I’m back with another (possibly) silly question. sorry.

I have a pretty complicated query which joins 4 tables and computes the sum of a column based on the other two columns in two tables. the result returned is like this:

Image http://eternalvinay.iocleicester.com/blahblah.png

Now, I want the results to be like the right hand side of the image. the number rows per month/year might change though its 4 for now.

I am creating a temporary table as:
Declare @TmpTable (id int identity, AnsSum float, AnsMonth int, AnsYear int)
to store the values from image –> table1. However, I cant figure out how to convert those rows into the format required by table 2.

So, Any hints on this please?
Thanks so much..

ps: I tried to google and related questions here, no luck.
pss: I am not expecting the exact answer too, i am quite interested to learn new things so if you know where i can learn to do this, a push in the right direction, that would be great too!

  • 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-15T11:38:03+00:00Added an answer on May 15, 2026 at 11:38 am

    You could use cross apply to get all the values in a comma delimted format in a single column. instead of “4” different columns. The problem is this “4” cannot be defined everytime. it may increase or decrease and it is not advisable to have this as columns.

    SELECT DISTINCT AnaMonth, anayear, [DerivedColumn] FROM @TmpTable A
    CROSS APPLY
    (
    SELECT AnaSum + ‘,’ FROM @TmpTable B
    WHERE A.AnaMonth = B.AnaMonth
    AND A.AnaYear = B.AnaYear
    FOR XML PATH(”)
    ) AS C (DerivedColumn)

    You will get [6.0000, 1.000, 8.0000, 5.0000] in one column for month 5 and year 2010 etc … You could use this as a table to query for any particular month.

    Hope this helps

    • 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.