错误计算字段(SSRS图表)

时间:2018-03-12 12:59:50

标签: sql sql-server reporting-services

我的SQL查询需要一些帮助。

我现在正在构建一个SSRS仪表板,我在其上创建了一个图表,显示所有票数的百分比,其中得分高于8且低于7。

我想图表会动态显示数据。

当用户选择一个或多个月时,图表应显示每个月的%。

因为多值参数是昏迷分区,所以我创建了一个修复问题的程序。

下面我附上了程序。

我看到当用户只选择一个月时,%显示正确。

但是如果用户在多值参数中选择1月和2月,则查询显示错误的数据。

它向我展示了这一点。

SUMMARY MONTH NAME  MONTH   YEAR
67     January      1       2018
71     January      1       2018
63     January      1       2018
68     January      1       2018
70     February     2       2018
75     February     2       2018
67     February     2       2018
71     February     2       2018

哪个错误应该只有2行或记录显示不是4。

我不知道如何解决这个问题。

我是SSRS和SQL的新手,感谢任何帮助。

应该显示的正确值是:

67, January, 1, 2018 
71, February,2, 2018

在程序下面。

ALTER Procedure dbo.Procedure1

--@MONTH varchar(50)

AS

BEGIN

create table ##temp ([SUMMARY] float,[MONTH NAME] Varchar (50),[MONTH] Varchar (50),[YEAR] INT)

--declare @MONTH VARCHAR(150) = '1,2',
--declare @var_MONTH varchar(50), @SUPPORT_GROUP varchar(50) = 'xxx_DACH'

declare @MONTH VARCHAR(150) = '1,2',

@var_MONTH varchar(50), @SUPPORT_GROUP varchar(50) = 'xxx_DACH'


SET @var_MONTH=Replace(@MONTH,',',''',''')

print @var_MONTH

Declare @SQL NVARCHAR(MAX)

SET @SQL ='

SELECT ROUND (100*(T2.[FCRR]-T1.[FCRR])/T3.[FCRR],0) AS [SUMMARY], T1.[MONTH_NAME] as [MONTH NAME], T1.[MONTH_NUMBER] as [MONTH], T1.[YEARS] as [YEAR]

FROM

--DETRACTORS
    (SELECT cast(count(*) AS FLOAT) AS [FCRR]
        ,DATENAME(MONTH, [TICKET_CLOSED]) AS [MONTH_NAME]
        ,MONTH([TICKET_CLOSED]) AS [MONTH_NUMBER]
        ,YEAR([TICKET_CLOSED]) AS [YEARS]
    FROM dbo.[PL_Survey]
    WHERE MONTH([TICKET_CLOSED]) IN ('''+@var_MONTH+''') and [QUESTION_3_SCORE] <7 AND SUPPORT_GROUP IN ('''+@SUPPORT_GROUP+''')
    GROUP BY Month([TICKET_CLOSED])
        ,YEAR([TICKET_CLOSED])
        ,DATENAME(MONTH, [TICKET_CLOSED])) AS T1,


 --PROMOTORS 
      (SELECT cast(count(*) AS FLOAT) AS [FCRR]
        ,DATENAME(MONTH, [TICKET_CLOSED]) AS [MONTH_NAME]
        ,MONTH([TICKET_CLOSED]) AS [MONTH_NUMBER]
        ,YEAR([TICKET_CLOSED]) AS [YEARS]
    FROM dbo.[PL_Survey]
    WHERE MONTH([TICKET_CLOSED]) IN ('''+@var_MONTH+''') AND SUPPORT_GROUP IN ('''+@SUPPORT_GROUP+''') and [QUESTION_3_SCORE] > 8
    GROUP BY Month([TICKET_CLOSED])
        ,YEAR([TICKET_CLOSED])
        ,DATENAME(MONTH, [TICKET_CLOSED])) AS T2,


--CASES COUNT
      (SELECT cast(count(*) AS FLOAT) AS [FCRR]
        ,DATENAME(MONTH, [TICKET_CLOSED]) AS [MONTH_NAME]
        ,MONTH([TICKET_CLOSED]) AS [MONTH_NUMBER]
        ,YEAR([TICKET_CLOSED]) AS [YEARS]
    FROM dbo.[PL_Survey]
    WHERE MONTH([TICKET_CLOSED]) IN ('''+@var_MONTH+''')
    GROUP BY Month([TICKET_CLOSED])
        ,YEAR([TICKET_CLOSED])
        ,DATENAME(MONTH, [TICKET_CLOSED])) AS T3'

Print @SQL


Execute( @SQL )

Select * From ##Temp

drop table ##Temp

END


--Execute dbo.Procedure1 '1,2'

1 个答案:

答案 0 :(得分:0)

我可以解决这个问题。

我的查询错误。

这里的解决方案:)

ALTER Procedure dbo.Procedure1

@MONTH varchar(50)

AS

BEGIN

create table ##temp ([SUMMARY] float,[MONTH NAME] Varchar (50),[MONTH] Varchar (50),[YEAR] INT)

declare @var_MONTH varchar(50), @SUPPORT_GROUP varchar(50) = 'xxx_DACH'

--declare @MONTH VARCHAR(150) = '1,2',

--@var_MONTH varchar(50), @SUPPORT_GROUP varchar(50) = 'xxx_DACH'


SET @var_MONTH=Replace(@MONTH,',',''',''')

--print @var_MONTH

Declare @SQL NVARCHAR(MAX)

SET @SQL ='

SELECT cast (100* (tt.[PROMOTORS] - tt.[DETRACTORS]) / tt.[CASES COUNT] as float) as [Summary], [MONTH_NAME] as [MONTH NAME], [MONTH_NUMBER] AS [MONTH], [YEARS] AS [YEAR]
FROM 

(SELECT 
    DATENAME(MONTH, [TICKET_CLOSED]) AS [MONTH_NAME]
    ,MONTH([TICKET_CLOSED]) AS [MONTH_NUMBER]
    ,YEAR([TICKET_CLOSED]) AS [YEARS]
    ,SUM(CASE WHEN SUPPORT_GROUP IN ('''+@SUPPORT_GROUP+''') THEN 1 ELSE 0 END) AS [CASES COUNT]
    ,SUM(CASE WHEN [QUESTION_3_SCORE] < 7 AND SUPPORT_GROUP IN ('''+@SUPPORT_GROUP+''') THEN 1 ELSE 0 END) [DETRACTORS]
    ,SUM(CASE WHEN [QUESTION_3_SCORE] > 8 AND SUPPORT_GROUP IN ('''+@SUPPORT_GROUP+''') THEN 1 ELSE 0 END) [PROMOTORS]
FROM 
    dbo.[PL_Survey]
WHERE 
    MONTH([TICKET_CLOSED]) IN ('''+@var_MONTH+''') AND SUPPORT_GROUP IN ('''+@SUPPORT_GROUP+''')  
 GROUP BY 
    MONTH([TICKET_CLOSED])
    ,YEAR([TICKET_CLOSED])
    ,DATENAME(MONTH, [TICKET_CLOSED])) as tt'

Print @SQL


Execute( @SQL )

Select * From ##Temp

drop table ##Temp

END


--Execute dbo.Procedure1 '1,2'

现在我的SSRS正在运行:)