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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:49:01+00:00 2026-06-09T17:49:01+00:00

I have written a SQL query that fills the following columns in a temporary

  • 0

I have written a SQL query that fills the following columns in a temporary table:

CODE | MODULE | MONTH | YEAR | SO_NUM | LABOR_HRS | LABOR_COST

From this table, I need to create a select statement that returns the same data, split up by month, like so:

CODE | MODULE | SO_NUM_MONTH1 | LABOR_HRS_MONTH1 | LABOR_COST_MONTH1 | SO_NUM_MONTH2 | LABOR_HRS_MONTH2 | LABOR_COST_MONTH2 | etc...

Code and module do not vary with month, as you can see. There will always be six months worth of data in the temporary table. However, what gets me is that the month does not always start at 1 (month and year are integers).

I also would like the resulting columns to be named based on the month and year, so in the above example, assuming that the data starting in January 2011, the names might be:

CODE | MODULE | SO_NUM_JAN_2011 | LABOR_HRS_JAN_2011 | LABOR_COST_JAN_2011 | SO_NUM_FEB_2011 | LABOR_HRS_FEB_2011 | LABOR_COST_FEB_2011 | etc...

This project is being developed in SQL Server 2008 R2.

  • 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-09T17:49:02+00:00Added an answer on June 9, 2026 at 5:49 pm

    Here’s some code to get you started. Basically, it creates a table with the fields that you need and you can just drop it and recreate it as necessary. You’ll need to finish adding the rest of the fields to the dynamic SQL part (@CreateTable), and then you’ll need to build some insert and update queries to populate it. I’d use some dynamic sql like what I used to create the table to do those so you get the field names from the variables.

    DECLARE @Month1 VARCHAR(15),
    @Month2 VARCHAR(15),
    @Month3 VARCHAR(15),
    @Month4 VARCHAR(15),
    @Month5 VARCHAR(15),
    @Month6 VARCHAR(15),
    @Year VARCHAR(4)
    
    SELECT @Year = Year(GetDate())
    
    SELECT @Month1 = LEFT(DateName(mm,DateAdd(mm,-6,GetDate())),3) + '_' + @Year, 
    @Month2 = LEFT(DateName(mm,DateAdd(mm,-5,GetDate())),3) + '_' + @Year,
    @Month3 = LEFT(DateName(mm,DateAdd(mm,-4,GetDate())),3) + '_' + @Year,
    @Month4 = LEFT(DateName(mm,DateAdd(mm,-3,GetDate())),3) + '_' + @Year,
    @Month5 = LEFT(DateName(mm,DateAdd(mm,-2,GetDate())),3) + '_' + @Year,
    @Month6 = LEFT(DateName(mm,DateAdd(mm,-1,GetDate())),3) + '_' + @Year
    
    PRINT @Month1
    PRINT @Month2
    PRINT @Month3
    PRINT @Month4
    PRINT @Month5
    PRINT @Month6
    
    DECLARE @CreateTable VARCHAR(MAX)
    
    SELECT @CreateTable = 'CREATE TABLE tblTESTING (
        CODE VARCHAR(10),
        MODULE VARCHAR(10),
        SO_NUM_' + @Month1 + ' VARCHAR(10),
        LABOR_HOURS_' + @Month1 + ' VARCHAR(10),
        LABOR_COST_' + @Month1 + ' VARCHAR(10)
        )'
    
    PRINT ''    
    PRINT @CreateTable
    
    --DROP TABLE tblTesting
    
    EXECUTE(@CreateTable)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL query that is returning a table like the following: id
I have written a SQL query that works just fine, but am having a
I have written an SQL Query that brings the result in below format ResourceName
I have written an sql query to search 2 columns of a database, partNo
I have written PLSQL program that generates table having usually more than 200 columns.
I have written an SQL query that gets unread messages but I think I
I have an SQL query that I have written using CTE. Now, I am
I have written a query in Sql server 2008. select select * from program
As I had written in title, I have SQL query, run on Oracle DB,
I have written a SQL Server stored procedure that includes a DateTime parameter. This

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.