Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

sql server - Dynamic table/output each month for report

I have a table and report I need to create and I'm not sure how to wrap my head around how to make it display in the correct order each month for the output.

Using SQL Server 2012 and SSRS 2016 as the output, I need to create a rolling report that displays the last 12 months with their corresponding values. Each month the previous 12th month will drop off.

What's the best table design to approach something like this and how do you control the output to drop off the previous 12th month and keep it rolling?

Sample of desired output would be something like below but next month I need to drop off Dec - 15 and add Jan - 16 but have the columns sorted in a descending order so the previous month is always the last month in the report.

-- Desc     |   DEC - 15 |  Jan - 16 | Feb - 16 | restofmonths| Nov 16 | Dec 16|
********************************************************************************
-- Loss     |   1,000   |   2500    | 1700      | 123         |  4565  |  3433 | 
-- Expense  |   2,000   |   3200    | 900       | 456         |  1223  |  4445 |
-- Reserve  |   3,000   |   3300    | 400       | 789         |  4747  |  4444 |
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to use a matrix.

In your dataset, add 2 columns (if not already present). The first is the column header and the second is the column sort order. Something like this

    [Header] = LEFT(DATENAME(MONTH, DateValue), 3) + ' - ' + RIGHT(YearNum, 2)
,   [HeaderSort] = CONVERT(VARCHAR, YEAR(DateValue)) + RIGHT('0' + CONVERT(VARCHAR, DATEPART(MONTH, DateValue)), 2)

Set the matrix column group to your Header value and set the sort order to your HeaderSort value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...