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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:07:54+00:00 2026-05-21T02:07:54+00:00

I have a table like: id month cost —————— 1 Jan 200 1 Mar

  • 0

I have a table like:

id   month   cost
------------------
1    Jan      200
1    Mar      204
1    May      200
1    Dec      201

I need an output like( order by month including the other months of a year-displaying all 12 months):

to   month   cost
------------------
1    Jan      200
NULL Feb      NULL
1    Mar      204
....
....
....
1    Dec      201

any idea or solution how to do this in TSQL?

Thanks!

edit:: month is extracted from a datetime value.
in real world i’ll have to show previous 12 months from last month in a DESC order! any suggestion for that?

  • 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-21T02:07:55+00:00Added an answer on May 21, 2026 at 2:07 am

    How about this?
    The result contains month and year, but you can strip it as you want.

    ;with months
    as
    (
    select dateadd(month, -1, dateadd(day, datediff(day, 0, getdate()), 0)) as m
    union all
    select dateadd(month, -1, m)
    from months
    where   m > dateadd(month, -12, getdate())
    )
    
    -- Testdata
    ,yourTable(id,somedate,cost)
    as
    (
        select 1, '2011-01-03', 200
        union all
        select 1, '2011-03-06', 204
        union all
        select 1, '2010-05-09', 200
        union all
        select 1, '2010-05-19', 201
        union all
        select 1, '2010-12-02', 201
    )
    -- end testdata
    
    select  yt.id
            ,datename(month,coalesce(yt.somedate, m.m)) as [month]
            ,datename(year,coalesce(yt.somedate, m.m)) as [year]
            --,yt.cost
            ,sum(yt.cost) as cost
    from    months m
    left join yourTable yt
        on  datepart(year, yt.someDate) = DATEPART(year, m.m)
        and datepart(month, yt.someDate) = DATEPART(month, m.m)
    group by
        yt.id
        ,datename(month,coalesce(yt.somedate, m.m))
        ,datename(year,coalesce(yt.somedate, m.m))
        ,m.m
    order by m.m desc
    

    Edit: Altered solution to support sum.
    Remove the group by-section and alter the comment of cost, to get the old solution.

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

Sidebar

Related Questions

I have a master table like CityName, Month, Temperature A,Jan,20 .... A,Dec,1 B,Jan,30 ....
I need help with SQL. I have an sqlite table like so; CREATE TABLE
I need some help to build SQL Query. I have table having data like:
I have a table MonthlyShipments that looks like: partnumber | quantity | month |
I have a table that looks like this: ------------------------------------------------------------------- CUSTNUM (INT), ITEMNUM (INT), MONTH
I have a table that look like that projectID , year , jan ,
Imagine I have this table: Month | Person | Value ---------------------- Jan | P1
I have a table that looks like this: Month Site Val 2009-12 Microsoft 10
I need some help to build SQL Query. I have table having data like:
I have a table that looks like this: Month | Year | Rate 2011

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.