SSRS显示一列横向

时间:2018-03-27 10:37:15

标签: sql-server reporting-services ssrs-2012

我有一个包含月份名称和月份编号的月份表。如何显示这样的月份表(连续4个月)?

Month table

1 个答案:

答案 0 :(得分:0)

假设您的数据是这样的

CREATE TABLE Table1
    (MonthNum INT, [MonthName] NVARCHAR(20))
;

INSERT INTO Table1
    (MonthNum, [MonthName])
VALUES
    (1, 'January'),
    (2, 'February'),
    (3, 'March'),
    (4, 'April'),
    (5, 'May'),
    (6, 'June'),
    (7, 'July'),
    (8, 'August'),
    (9, 'September'),
    (10, 'October'),
    (11, 'November'),
    (12, 'December')
;

我会在TSQL中尝试解决方案

创建一些列以符合您希望月份名称出现的顺序,我将在这里使用CTE

;WITH cte(Col1, Col2, Col3, Col4)
AS(
    SELECT 1, 4, 7, 10 UNION ALL
    SELECT 2, 5, 8, 11 UNION ALL
    SELECT 3, 6, 9, 12
)

加入月份表

;WITH cte(Col1, Col2, Col3, Col4)
AS(
    SELECT 1, 4, 7, 10 UNION ALL
    SELECT 2, 5, 8, 11 UNION ALL
    SELECT 3, 6, 9, 12
)
SELECT
      T1.[MonthName]
    , T2.[MonthName]
    , T3.[MonthName]
    , T4.[MonthName]
FROM 
    cte     X
LEFT JOIN dbo.Table1 T1 ON T1.MonthNum = X.Col1
LEFT JOIN dbo.Table1 T2 ON T2.MonthNum = X.Col2
LEFT JOIN dbo.Table1 T3 ON T3.MonthNum = X.Col3
LEFT JOIN dbo.Table1 T4 ON T4.MonthNum = X.Col4

得到这个

MonthName   MonthName   MonthName   MonthName
January     April       July        October
February    May         August      November
March       June        September   December