SSRS矩阵表报告。合并行

时间:2017-04-21 05:58:04

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

这就是我拿桌子的方式。

+------+---------+-----------+---------+
| Cstmr| morning | afternoon |  night  |
+------+---------+-----------+---------+
| ab   | N/A     | N/A       |    car  |
|      | N/A     | N/A       |    11pm |
|      | bike    | N/A       |    N/A  |
|      | 9am     | N/A       |    N/A  |
|      | N/A     | cycle     |    N/A  |
|      | N/A     | 2pm       |    N/A  |
| ac   | N/A     | N/A       |    car  |
|      | N/A     | N/A       |    11pm |
|      | bike    | N/A       |    N/A  |
|      | 9am     | N/A       |    N/A  |
+------+---------+-----------+---------+

我想让它像这样显示。

+------+---------+-----------+---------+
| Cstmr| morning | afternoon |  night  |
+------+---------+-----------+---------+
| ab   | bike    | cycle     |    car  |
|      | 9am     | 2pm       |    11pm |
| ac   | bike    | N/A       |    car  |
|      | 9am     |           |    11pm |
+------+---------+-----------+---------+

Cstmr是客户。早上:早上6点到12点。下午:中午12点至晚上10点。晚上:晚上10点至早上6点。对于客户'ab',自行车在上午9点提供,周期为下午2点,车在晚上11点提供。根据时间数据应显示在各自的列中。如果在那段时间内没有提供车辆,它应显示N / A.

And this is how parent and child groups are formed.

1 个答案:

答案 0 :(得分:0)

恕我直言,从项目的角度来看,这是一个很好的问题?当数据没有正常化时。

其次&#34的含义是什么;这就是我获得这张桌子的方式。" 这是实际表的方式,或者是一些查询后得到的结果集。

第三,尝试使用此脚本和其他示例数据,并告诉我是什么问题。 如果上面的脚本正确但速度慢,那么我们可以为任何商业需求应用分页。

declare @t table(Cstmr varchar(20),morning varchar(20)
,afternoon varchar(20), night varchar(20))

insert into @t VALUES
('ab','NA','NA','car')
,( null, 'NA','NA','11pm ')
,( null, 'bike','NA','NA  ')
,( null, '9am','NA','NA  ')
,( null, 'NA','cycle','NA  ')
,( null, 'NA','2pm','NA  ')
,('ac','NA','NA','car  ')
,( null,'NA','NA','11pm ')
,( null,'bike','NA','NA  ')
,( null,'9am','NA','NA  ')

;

WITH CTE
AS (
    SELECT Cstmr
        ,morning
        ,afternoon
        ,night
        ,ROW_NUMBER() OVER (
            ORDER BY (
                    SELECT NULL
                    )
            ) rn
    FROM @t
    )
    ,CTE1
AS (
    SELECT cstmr
        ,morning
        ,afternoon
        ,night
        ,rn
        ,CASE 
            WHEN morning = 'NA'
                THEN 0
            WHEN isdate(morning) = 0
                THEN 1
            ELSE 2
            END rnm
        ,CASE 
            WHEN afternoon = 'NA'
                THEN 0
            WHEN isdate(afternoon) = 0
                THEN 1
            ELSE 2
            END rnaf
        ,CASE 
            WHEN night = 'NA'
                THEN 0
            WHEN isdate(night) = 0
                THEN 1
            ELSE 2
            END rnn
    FROM cte c
    WHERE rn = 1

    UNION ALL

    SELECT CASE 
            WHEN c.cstmr IS NULL
                THEN c1.cstmr
            ELSE c.cstmr
            END
        ,c.morning
        ,c.afternoon
        ,c.night
        ,c.rn
        ,CASE 
            WHEN c.morning = 'NA'
                THEN 0
            WHEN isdate(c.morning) = 0
                THEN 1
            ELSE 2
            END rnm
        ,CASE 
            WHEN c.afternoon = 'NA'
                THEN 0
            WHEN isdate(c.afternoon) = 0
                THEN 1
            ELSE 2
            END rnaf
        ,CASE 
            WHEN c.night = 'NA'
                THEN 0
            WHEN isdate(c.night) = 0
                THEN 1
            ELSE 2
            END rnn
    FROM cte c
    INNER JOIN cte1 c1 ON c.rn = c1.rn + 1
    )
SELECT m.Cstmr
    ,m.morning
    ,m.rnm
    ,a.afternoon
    ,n.night
FROM cte1 m
LEFT JOIN cte1 a ON a.cstmr = m.cstmr
    AND m.rnm = a.rnaf
LEFT JOIN cte1 n ON n.cstmr = m.cstmr
    AND m.rnm = n.rnn
WHERE m.morning <> 'NA'