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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:14:43+00:00 2026-05-11T02:14:43+00:00

I’m wondering how others handle this situation… and how to apply the Don’t Repeat

  • 0

I’m wondering how others handle this situation… and how to apply the Don’t Repeat Yourself (DRY) principle to this situation.

I find myself constantly PIVOTing or writing CASE statements in T-SQL to present Months as columns. I generally have some fields that will include (1) a date field and (2) a value field. When I present this back to a user through an ASPX page or Reporting Services I need to have the last right-most 14 columns to have this pattern:

[Year],[Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec],[Total]

Where year is the year as an int and every other field is the value field summed for that month (except for [Total] which is the total value field for the year).

I’d like to find one re-usable way to handle this. Open to all suggestions (T-SQL / ANSI SQL)

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T02:14:44+00:00Added an answer on May 11, 2026 at 2:14 am

    This isn’t exactly what you’re looking for, but I’ve done a lot of repetitive UNPIVOT, and typically, I would code-gen this, with some kind of standardized naming and use CTEs heavily:

    WITH P AS (     SELECT Some Data             ,[234] -- These are stats             ,[235]     FROM Whatever      ) ,FINAL_UNPIVOTED AS (     SELECT Some Data             ,[STAT]     FROM P     UNPIVOT (         STAT FOR BASE IN ([234], [235])      ) AS unpvt     WHERE STAT <> 0 ) SELECT Some Data               ,CONVERT(int, FINAL_UNPIVOTED.[BASE]) AS [BASE]               ,FINAL_UNPIVOTED.[STAT] FROM FINAL_UNPIVOTED 

    You can codegen by inspecting a table or view and using something like this:

    DECLARE @sql_unpivot AS varchar(MAX) SELECT @sql_unpivot = COALESCE(@sql_unpivot + ',', '') + COLUMN_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'whatever' 

    And templatizing the code:

    SET @template = '     WITH P AS (         SELECT Some Data                 ,{@sql_unpivot}                   FROM Whatever          )     ,FINAL_UNPIVOTED AS (         SELECT Some Data                 ,[STAT]         FROM P         UNPIVOT (             STAT FOR BASE IN ({@sql_unpivot})          ) AS unpvt         WHERE STAT <> 0     )     SELECT Some Data                   ,CONVERT(int, FINAL_UNPIVOTED.[BASE]) AS [BASE]                   ,FINAL_UNPIVOTED.[STAT]     FROM FINAL_UNPIVOTED ' SET @sql = REPLACE(@template, '{@sql_unpivot}', @sql_unpivot) 

    etc.

    Of course, it’s possible to run this code dynamically or create and SP and you can swap out a view or table you created temporarily just to pick up metadata for something inline.

    See comments regarding table-valued functions and OUTER APPLY technique.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I don't have much knowledge about the IPv6 protocol, so sorry if the question

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.