4

这是报告:报告表

这就是我获得“最近一年的百分比变化”列的百分比的方式。

=((Last(Fields!Quantity.Value,"Child")  -  First(Fields!Quantity.Value)) / First(Fields!Quantity.Value))`

= ((54675 - 55968)/55968 ) = -2.31%'
= ((54675 - 57849)/57849)  = -5.49%'

在这种情况下,它总是需要第一年“2012”并获得相对于其他年份的百分比。如果我输入 2005、2004、2003、2002、2001 年,它将始终采用第一年,并且每增加一年就计算一个百分比。2005 年至 2004 年、2005 年至 2003 年、2005 年至 2002 年等等。我可以有多达 2 列(年)到许多列。

我需要为总计和小计执行此操作,但它不起作用,因为它在不同的范围内。

data is     = row Child group
Sub Total:  = row Parent group
Total:      = row Total group
Year        = Column Period group

查询用于获取结果。

 SELECT     MEMBERSHIP_CODE
, PERIOD, COUNT(DISTINCT ID) AS Distinct_ID
, SUM(QUANTITY) AS Quantity
, '01-Personal' AS Child
, '01-Overall' AS Parent
, 'Total' as Total
  FROM         vf_Sshot AS vfs
INNER JOIN vProd AS vP ON vfs.PRODUCT_CODE = vP.PRODUCT_CODE
INNER JOIN vMem_Type vMT on vMT.Member_Type = vfs.Member_Type
  WHERE  (PERIOD IN ( (SELECT Val from dbo.fn_String_To_Table(@Periods,',',1))))                  
      AND (vMT.MEMBER_TYPE NOT IN ('a','b','c')) 
      AND (vfs.STATUS IN ( 'A', 'D', 'C')) 
      AND (MEMBERSHIP_CODE NOT IN ('ABC', 'DEF' ))
      and vP.PROD_TYPE in ('DUE','MC','SC')
  and vMT.Member_Record = '1'
 GROUP BY MEMBERSHIP_CODE, PERIOD

有任何想法吗?

我将如何产生这个输出?

总计:57,573 58,941 57,573 61,188 57,573 61,175 57,175

在此处输入图像描述

4

1 回答 1

0

这是解决您的问题的最简单方法。在您的查询中,在单独的列上确定最新期间的总和(您可以将查询转换为 CTE,这样您就不必大量更改基本查询):

WITH query AS (
   SELECT     MEMBERSHIP_CODE
     , PERIOD, COUNT(DISTINCT ID) AS Distinct_ID
     , SUM(QUANTITY) AS Quantity
     , '01-Personal' AS Child
     , '01-Overall' AS Parent
     , 'Total' as Total
  ...
UNION
   SELECT
  ...
)
SELECT
   A.MEMBERSHIP_CODE,
   A.PERIOD,
   A.Distinct_ID,
   A.Child,
   A.Parent,
   A.Total,
   A.Quantity,
   B.Quantity AS LastPeriodQuantity
FROM
   query A INNER JOIN
(SELECT *, ROW_NUMBER() OVER(PARTITION BY MEMBERSHIP_CODE, Distinct_ID, Child, Parent ORDER BY PERIOD DESC) as periodOrder FROM query) B ON
A.MEMBERSHIP_CODE = B.MEMBERSHIP_CODE AND
A.DISTINCT_ID = B.DISTINCT_ID AND
A.Parent = B.Parent AND
A.Child = B.Child AND
A.Total = B.Total AND
B.PeriodOrder = 1

然后在所有总计/小计/列上,您将访问一个按与分母相同的规则分组/过滤的列。对于所有单元格,您的表达式可以保持如下: =(Fields!LastPeriodQuantity.Value - Fields!Quantity.Value) / Fields!Quantity.Value

于 2012-10-19T09:19:09.750 回答