如何在SSRS中订购两年的月份

时间:2017-12-05 13:17:57

标签: reporting-services

我在SQL中有以下表格:

collectiontosearch = input("What event would you like to display? : ")
collection = db[collectiontosearch]
...

在SSRS中,我有一个按月分组的表,有4个参数:1表示Year1,1表示Month1;第2年为1,第2年为1。

例如,如果我选择Year1 = 2015和Month1 = November和December以及Year2 = 2016和Month2 = January和February,我需要显示这样的月份,即使结果具有NULL值:

collection = db.get_database(collectiontosearch)

这是我使用的查询:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

2 个答案:

答案 0 :(得分:0)

处理这类事情的最佳方法是创建日期表。这是多么详细,符合您的要求。在您的情况下,您可能只需要数月和数年,但如果您的数据更详细,则可能需要所有日期。然后,您可以使用此表并将主数据连接到它。

另一种选择是在数据集中构建临时表,其中包含月份和年份。

我并不完全明白你传递的参数是什么,所以我只是猜测一下,希望无论如何这都会给你足够的信息。这假设您只使用了不超过2年的数据,如果没有,那么您可能最好沿着永久日期表路线...

因此,如果您将4个参数传递给2015年的数据集查询; 11; 2016年10(Nov' 15至10月' 16)

您的数据集查询看起来像。

/* uncomment for testing on SSMS only
DECLARE @Year1 int = 2015
DECLARE @Year2 int = 2016
DECLARE @Month1 int = 11
DECLARE @Month2 int = 10
*/

/*Create temp Dates Table*/
DECLARE @d TABLE([Year] int, [Month] int)
INSERT INTO @d 
SELECT @Year1, 1 UNION
SELECT @Year1, 2 UNION
SELECT @Year1, 3 UNION
SELECT @Year1, 4 UNION
SELECT @Year1, 5 UNION
SELECT @Year1, 6 UNION
SELECT @Year1, 7 UNION
SELECT @Year1, 8 UNION
SELECT @Year1, 9 UNION
SELECT @Year1, 10 UNION
SELECT @Year1, 11 UNION
SELECT @Year1, 12 UNION
SELECT @Year2, 1 UNION
SELECT @Year2, 2 UNION
SELECT @Year2, 3 UNION
SELECT @Year2, 4 UNION
SELECT @Year2, 5 UNION
SELECT @Year2, 6 UNION
SELECT @Year2, 7 UNION
SELECT @Year2, 8 UNION
SELECT @Year2, 9 UNION
SELECT @Year2, 10 UNION
SELECT @Year2, 11 UNION
SELECT @Year2, 12


SELECT d.[Year], d.[Month], t.myColumns
    FROM @d d
       LEFT JOIN myTable t on d.[Year] = t.[Year] and d.[Month] = t.[Month]
    WHERE 
       ((t.[Year] *100) + t.[Month]) between (@Year1 *100 + @Month1) and (@Year2 *100 + @Month2) 
    ORDER by d.[Year], d.[Month]

答案 1 :(得分:0)

这是我使用的查询:

SELECT *
FROM
(
SELECT
      [QUARTER]
      ,[CARRIER]
      ,[PLANGROUP]
      ,[UniqueUsers]
      ,[YEAR]
      ,A.[MONTH]
      ,A.[MONTH2]
FROM [ReportUnit].[dbo].[Month_List] A

LEFT JOIN

(SELECT
        [YEAR]
        ,[MONTH2]
        ,[CARRIER]
        ,[PLANGROUP]
        ,[QUARTER]
        ,COUNT(DISTINCT [CONTRACT_NUM]) AS [UniqueUsers]
FROM
(
SELECT *
FROM
(
SELECT
        MIN([YEAR]) AS [YEAR]
        ,MIN([MONTH2]) AS [MONTH2]
        ,DATENAME(MONTH, DATEADD(MONTH, MIN([MONTH2])-1, CAST('2017-01-01' AS datetime))) AS [MONTH]
        ,[CARRIER]
        ,[PLANGROUP]
        ,MIN([QUARTER]) AS [QUARTER]
        ,[CONTRACT_NUM]
FROM MyTable
WHERE
[YEAR] = @YEAR1
AND [PLANGROUP] IN (@ACCOUNT)
GROUP BY [PLANGROUP],[CARRIER],[CONTRACT_NUM]
) Q1
WHERE
[MONTH] IN (@MONTH1) --THIS IS MONTH NAME PARAMETER IN SSRS
--[MONTH2] IN (11,12)

UNION ALL

SELECT *
FROM
(
SELECT
        MIN([YEAR]) AS [YEAR]
        ,MIN([MONTH2]) AS [MONTH2]
        ,DATENAME(MONTH, DATEADD(MONTH, MIN([MONTH2])-1, CAST('2017-01-01' AS datetime))) AS [MONTH]
        ,[CARRIER]
        ,[PLANGROUP]
        ,MIN([QUARTER]) AS [QUARTER]
        ,[CONTRACT_NUM]
FROM MyTable
WHERE
[YEAR] = @YEAR2
AND [PLANGROUP] IN (@ACCOUNT)
GROUP BY [PLANGROUP],[CARRIER],[CONTRACT_NUM]
) Q1
WHERE
[MONTH] IN (@MONTH2)  --THIS IS MONTH NAME PARAMETER IN SSRS
--[MONTH2] IN (1,2,3,4,5,6,7,8,9,10)
) Q1
GROUP BY [YEAR],[MONTH2],[PLANGROUP],[CARRIER],[QUARTER]
) B ON A.MONTH2 = B.MONTH2
) Q4
ORDER BY [PLANGROUP], [YEAR], [MONTH2], [MONTH]

而且我不知道如何在临时表中加入它