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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:00:13+00:00 2026-06-13T23:00:13+00:00

I was wondering if I could get some ideas or direction on a sql

  • 0

I was wondering if I could get some ideas or direction on a sql query that would output column months. Here is my current query..

select A.assetid, A.Acquisition_Cost, B.modepreciaterate from FA00100 A
inner join FA00200 B on A.assetindex = B.assetindex
where MoDepreciateRate != '0'

I would like to add more columns that look as such:

select assetid, acquisition_cost, perdeprrate, Dec_2012, Jan_2013, Feb_2013....

where Dec_2012 = (acquisition_cost - MoDepreciateRate*(# of months))
and Jan_2013 = (acquisition_cost - MoDepreciateRate*(# of months))

where # of months can be changed.

Any help would be really appreciated. Thank you!

Here is an example of what I would like the output to be with ‘# of months’ = 4

assetid SHRTNAME    Acquisition_Cost    perdeprrate Dec_2012    Jan_2013    Feb_2013    Mar_2013
CS-013  GEH INTEG   17490.14            485.83      17004.31    16518.48    16032.65    15546.82
CS-014  WEB BRD     14560               404.4507    14155.5493  13751.0986  13346.6479  12942.1972
  • 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-13T23:00:15+00:00Added an answer on June 13, 2026 at 11:00 pm

    Try This:

    --setup
    create table #fa00100 (assetId int, assetindex int, acquisitionCost int, dateAcquired date)
    create table #fa00200 (assetIndex int, moDepreciateRate int, fullyDeprFlag nchar(1), fullyDeprFlagBit bit)
    
    insert #fa00100 
          select 1, 1, 100, '2012-01-09'
    union select 2, 2, 500, '2012-05-09'
    insert #fa00200
          select 1, 10, 'N', 0
    union select 2, 15, 'Y', 1
    

    .

    --solution
    create table #dates (d date not null primary key clustered)
    declare @sql nvarchar(max)
    , @pivotCols nvarchar(max)
    , @thisMonth date
    , @noMonths int = 4
    
    set @thisMonth = cast(1 + GETUTCDATE() - DAY(getutcdate()) as date)
    select @thisMonth
    while @noMonths > 0
    begin
        insert #dates select DATEADD(month,@noMonths,@thisMonth) 
        set @noMonths = @noMonths - 1
    end
    
    select @sql = ISNULL(@sql + NCHAR(10) + ',', '') 
    --+ ' A.acquisitionCost - (B.moDepreciateRate * DATEDIFF(month,dateAcquired,''' + convert(nvarchar(8), d, 112) + ''')) ' --Original Line
        + ' case when A.acquisitionCost - (B.moDepreciateRate * DATEDIFF(month,dateAcquired,''' + convert(nvarchar(8), d, 112) + ''')) <= 0 then 0 else A.acquisitionCost - (B.moDepreciateRate * DATEDIFF(month,dateAcquired,''' + convert(nvarchar(8), d, 112) + ''')) end ' --new version
    
    + quotename(DATENAME(month, d) + '_' + right(cast(10000 + YEAR(d) as nvarchar(5)),4))
    from #dates
    
    set @sql = 'select A.assetid
    , A.acquisitionCost
    , B.moDepreciateRate 
    ,' + @sql + '
    from #fa00100 A
    inner join #fa00200 B 
        on A.assetindex = B.assetindex
    where B.fullyDeprFlag = ''N''
    and B.fullyDeprFlagBit = 0
    '
    --nb: B.fullyDeprFlag = ''N'' has double quotes to avoid the quotes from terminating the string
    --I've also included fullyDeprFlagBit to show how the SQL would look if you had a bit column - that will perform much better and will save space over using a character column
    
    print @sql
    exec(@sql)
    
    drop table #dates 
    

    .

        --remove temp tables from setup
    drop table #fa00100
    drop table #fa00200
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering if I could get some advice and direction on this following requirement: Need
I was wondering if i could get some help. What i am trying to
I need to do the following and i was wondering if i could get
Hi sorry still learning here and slow to learning code arguments. Just wondering could
I'm wondering what could be some consequences of reusing the names of built-in types
I was wondering if someone could help me get pointers to solve this problem.
I still often use console output to get ideas what's going on in my
I was wondering if I could get a little bit of guidance with Javascript.
I have some random HTML layouts that contain important text I would like to
i am wondering could i access my app internal storage using emulator. I mean

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.