矩阵中列之间的差异

时间:2014-09-03 19:03:03

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

这是我原来的表:

customers   shopname    custimport
6247        Bison       2014-05-28  
5753        Brownies    2014-05-28  
14315       Falkners    2014-05-28  
6392        Bison       2014-08-31  
5483        Brownies    2014-08-31  
14395       Falkners    2014-08-31  

在我的报告中,我制作了这个矩阵

           [custimport]
[shopname] [Sum(customers)]

我知道只有2个custimport日期,我需要总和(客户)之间的差异。基本上,我需要找到商店在2个进口日期之间获得/丢失的客户数量。我不确定用什么表达来实现这个目标。

作为参考,这是现在的矩阵:

       5/28/2014    8/31/2014
Bison       6247    6392
Brownies    5753    5483
Falkners    14315   14395

这就是我想要的结果:

       5/28/2014    8/31/2014   diff
Bison       6247    6392        145
Brownies    5753    5483        -270
Falkners    14315   14395       80

2 个答案:

答案 0 :(得分:0)

以下内容应该是矩阵的数据集。

    SELECT ORI.SHOPNAME,
           ORI.CUSTIMPORT,
           B.CUSTOMERS-A.CUSTOMERS AS DIFF
    FROM
      (SELECT SHOPNAME,
              CUSTIMPORT
       FROM CUSTOMER_TABLE) ORI
    INNER JOIN
      (SELECT CUSTOMERS,
              SHOPNAME,
              CUSTIMPORT
       FROM CUSTOMER_TABLE)A ON ORI.SHOPNAME=A.SHOPNAME
    AND A.CUSTIMPORT=ORI.CUSTIMPORT
    INNER JOIN
      (SELECT CUSTOMERS,
              SHOPNAME,
              CUSTIMPORT
       FROM CUSTOMER_TABLE)B ON A.SHOPNAME=B.SHOPNAME
    AND A.CUSTIMPORT <> B.CUSTIMPORT
    AND ORI.SHOPNAME=B.SHOPNAME
    ORDER BY A.CUSTIMPORT

答案 1 :(得分:-1)

编辑:

这个表达对我有用:

=Sum(Fields!Customers.Value) - Previous(Sum(Fields!Customers.Value))

仅限SQL:

SELECT [shopname], [2014-05-28], [2014-08-31], [2014-05-28] - [2014-08-31] 'Diff'
FROM #Test
PIVOT
(
Sum(customers)
FOR custimport in ([2014-05-28], [2014-08-31])
) AS PVT_Table