ssrs表达式“没有不满意客户的天数”无法使其工作

fumotvh3  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(362)

我必须在我的ssrs表中得到一个表达式,以实现没有不满意客户的天数。
现在我有一个这样的表达: =RunningValue(IIF(Fields!SATISFACTION_LEVEL.Value <> "Dissatisfied",1,0),Sum, "DataSet1") 这给我的行数,其中包含一个满意的水平,而不是不满意。

我的问题是,我似乎无法计算没有不满意客户的天数。我找不到计算天数的办法。本质上这就是它应该做的。如果当天有客户不满意的记录,不要算。如果没有不满意的顾客,记录下来。
这将需要为本年度迄今为止,也为前一年。
我真的很感激任何帮助这个表达!
谢谢
更新更多信息:
数据集结构如下:

_______________________________________
| satisfaction_level     | Date       |
---------------------------------------    
| Satisfied              | 07/20/2020 |
| dissatisfied           | 07/20/2020 |
| Satisfied              | 07/20/2020 |
| Highly Satisfied       | 07/20/2020 |
| Satisfied              | 07/20/2020 |
| Satisfied              | 07/21/2020 |
| Satisfied              | 07/21/2020 |
| Highly Satisfied       | 07/21/2020 |

预期功能-2020年7月20日当天有1名不满意的客户(不理货),2020年7月21日当天没有不满意的客户(理货)。导致没有不满意客户的总天数。我希望这有助于进一步解释所需的结果。

axr492tv

axr492tv1#

把ssr放在一边,现在,计算天数的问题是,很难计算一行没有的时间。例如,如果我有许多响应记录分布在一周内,但它们只落在其中4天,当我们按天分组时,查询只能返回记录集中存在的天的结果:

DECLARE @Responses as Table 
(
    ENTRY_TIME DateTimeOffset, SATISFACTION_LEVEL VARCHAR(20)
)
INSERT INTO @Responses
VALUES 
('2020-01-4', 'Dissatisfied'),
('2020-01-4', 'Dissatisfied'),
('2020-01-1', 'Satisfied'),
('2020-01-5', 'Dissatisfied'),
('2020-01-5', 'Satisfied'),
('2020-01-2', 'Dissatisfied')

SELECT 
  fn.DATE
  , DATENAME(WEEKDAY, fn.DATE) as [Day]
  , SUM(CASE SATISFACTION_LEVEL WHEN 'Dissatisfied' THEN 1 ELSE 0 END) as [Dissatisfied]
FROM @Responses
CROSS APPLY (SELECT CAST(ENTRY_TIME as Date) as [DATE]) as fn
GROUP BY fn.DATE
ORDER BY fn.DATE
DATE       Day                            Dissatisfied
---------- ------------------------------ ------------
2020-01-01 Wednesday                      0
2020-01-02 Thursday                       1
2020-01-04 Saturday                       2
2020-01-05 Sunday                         1

(4 rows affected)

我们可以通过生成一系列的记录集来解决这个问题,我们可以将真实世界的数据与之结合起来,从而确保每天都有一行。
这可以通过使用一个递归的cte来实现,在查询下面的分组数据结果是连接到系列数据中的,你可以用多种不同的方法来实现,你甚至可以透视数据 SATISFACTION_LEVEL 列响应,这只是为了说明在将数据格式化为ssrs报告之前在sql中预处理数据的技术:

DECLARE @From Date = '2019-12-30';
DECLARE @To Date = '2020-01-05';
;
WITH [Sequence] ([Date])
as
(
    SELECT @From
    UNION ALL
    SELECT DATEADD(DAY, 1, [Date]) FROM [Sequence]
    WHERE [Date] < @To 
)
, [GroupedByDay]
as
(
    SELECT 
      fn.DATE
      , SUM(CASE SATISFACTION_LEVEL WHEN 'Dissatisfied' THEN 1 ELSE 0 END) as [Dissatisfied]
      , SUM(CASE SATISFACTION_LEVEL WHEN 'Satisfied' THEN 1 ELSE 0 END) as [Satisfied]
    FROM @Responses
    CROSS APPLY (SELECT CAST(ENTRY_TIME as Date) as [DATE]) as fn
    GROUP BY fn.DATE
)

SELECT 
    c.[Date]
    , DATENAME(WEEKDAY, c.[DATE]) as [Day]
    , ISNull([Dissatisfied],0) as [Dissatisfied]
    , ISNULL([Satisfied],0) as [Satisfied]
FROM [GroupedByDay] g
RIGHT OUTER JOIN [Sequence] c ON g.[DATE] = c.[Date]
ORDER BY c.[Date]
Date       Day                            Dissatisfied Satisfied
---------- ------------------------------ ------------ -----------
2019-12-30 Monday                         0            0
2019-12-31 Tuesday                        0            0
2020-01-01 Wednesday                      0            1
2020-01-02 Thursday                       1            0
2020-01-03 Friday                         0            0
2020-01-04 Saturday                       2            0
2020-01-05 Sunday                         1            1

(7 rows affected)

没有关于你的模式和当前查询的具体信息,这是我所能提供的最好的信息,但是每天的数据应该足以让你在ssrs中把这一年的结果分组。。。
如果需要,也可以直接在sql中执行:)

更新:仅返回没有不满意客户的总天数的示例:

DECLARE @From Date = '2019-12-30';
DECLARE @To Date = '2020-01-05';
;
WITH [Sequence] ([Date])
as
(
    SELECT @From
    UNION ALL
    SELECT DATEADD(DAY, 1, [Date]) FROM [Sequence]
    WHERE [Date] < @To 
)
, [GroupedByDay]
as
(
    SELECT 
      fn.DATE
      , SUM(CASE SATISFACTION_LEVEL WHEN 'Dissatisfied' THEN 1 ELSE 0 END) as [Dissatisfied]
      , SUM(CASE SATISFACTION_LEVEL WHEN 'Satisfied' THEN 1 ELSE 0 END) as [Satisfied]
    FROM @Responses
    CROSS APPLY (SELECT CAST(ENTRY_TIME as Date) as [DATE]) as fn
    GROUP BY fn.DATE
)
, [InjectedMissingDays]
as
(
    SELECT 
        c.[Date]
        , DATENAME(WEEKDAY, c.[DATE]) as [Day]
        , ISNull([Dissatisfied],0) as [Dissatisfied]
        , ISNULL([Satisfied],0) as [Satisfied]
    FROM [GroupedByDay] g
    RIGHT OUTER JOIN [Sequence] c ON g.[DATE] = c.[Date]
)
--Overall 
SELECT COUNT(1) as [Days with No Dissatisfied Customers] FROM [InjectedMissingDays] WHERE Dissatisfied = 0
Days with No Dissatisfied Customers
-----------------------------------
4

(1 row affected)

相关问题