SQL Query显示REPORT BUILDER 3.0的多个日期范围之间的差距

时间:2018-02-20 23:30:44

标签: reporting-services ssrs-2008 reportbuilder3.0

我使用SQL Server Management Studio 2008 2 Simple charts: 1. Showing DistinctCount of all visits to a walk in clinic by month/day for the last 30 days; and, 2. Showing the same but counting only those visits with a police escort

在Report Builder 3.0中创建了一个报表

我遇到的问题是,每天都有警察护送人员无法访问 - 所以有些日子没有NULL值,但完全没有!

我已经使用SSRS找到了几个解决这个问题的方法,但是对于我来说,切换到报表生成器(我两者都是新手)太复杂了。有人可以告诉我如何在报表生成器中修复此问题吗?

注意:我无法添加或更改表(我有只读访问权限),因此这些选项已经用完。显示如何添加临时表声音的说明很有趣,但对我来说太复杂了 - 这是两个表的代码(我无法弄清楚如何为两者做一个数据集 - 当我限制第二个时Tablix只是警察,它搞砸了第一个tablix)。

请记住,我是新手,我使用的是Report Builder而不是SQL。感谢。

ServiceByDatesHrly

SELECT
    ClientChartPartition.ClientChartPartitionID AS [ClientChartPartition ClientChartPartitionID]
    ,ClientChartPartition.ClientChartID AS [ClientChartPartition ClientChartID]
    ,ClientChart.ClientChartID AS [ClientChart ClientChartID]
    ,VisitPlan.VisitPlanID
    ,VisitPlan.ClientChartPartitionID AS [VisitPlan ClientChartPartitionID]
    ,VisitPlan.VisitDate
    ,VisitPlan.ServiceTypeDesc
    ,Name.ParticipantID
    ,Name.LastName 
FROM
    ClientChartPartition 
    INNER JOIN
        ClientChart 
        ON ClientChartPartition.ClientChartID = ClientChart.ClientChartID 
    INNER JOIN
        VisitPlan 
        ON ClientChartPartition.ClientChartPartitionID = VisitPlan.ClientChartPartitionID 
    INNER JOIN
        Name 
        ON Name.ParticipantID = ClientChartPartition.ClientChartPartitionID 
    WHERE
    Name.LastName NOT LIKE N'TestClient%' 

PoliceEscort

SELECT
    VisitPlan.VisitPlanID
    ,VisitPlan.ClientChartPartitionID
    ,VisitPlan.VisitDate
    ,VisitPlan.ServiceTypeDesc
    ,FormRelation.ParentRelationID
    ,FormRelation.ChildRelationID
    ,Form.FormID
    ,Field.ParentID
    ,Field.FieldIndexCode
    ,Field.ResultXML
    ,Name.ParticipantID
    ,Name.LastName 
FROM
    VisitPlan 
    INNER JOIN
        FormRelation 
        ON VisitPlan.VisitPlanID = FormRelation.ParentRelationID 
    INNER JOIN
        Form 
        ON FormRelation.ChildRelationID = Form.FormID 
    INNER JOIN
        Field 
        ON Form.FormID = Field.ParentID 
    INNER JOIN
        Name 
        ON VisitPlan.ClientChartPartitionID = Name.ParticipantID 
WHERE
    VisitPlan.ServiceTypeDesc = N'MAPP_REFDATA_VISIT_PLAN_TYPE_WALK_IN_SERVICE' 
    AND Name.LastName NOT LIKE N'TestClient' 
    AND Form.FormTemplateID = N'ab395124-679a-4e7c-b98d-d9a9c11cb2a3'  --Mental Health and Physical Health Screen
    AND Field.FieldIndexCode = N'CRC_0070a' -- Mental Health and Physical Health Screen
    AND Field.ResultXML LIKE N'%<value>2</value>%'  -- Police

1 个答案:

答案 0 :(得分:1)

您将需要某种日期表,然后您只需将现有数据LEFT JOIN加入其中。最好的方法是创建一个永久日期表,但如果这不可能,那么您可以在SSRS数据集中临时创建一个。

假设您当前的数据集查询类似于

SELECT VisitDate, COUNT(*) AS Vists FROM mytable GROUP BY VisitDate

然后我们可以像这样修改它。

WITH tmpDates AS (
  SELECT CAST('2018-01-21' AS DATETIME) AS dt
  UNION ALL
  SELECT DATEADD(dd, 1, dt)
    FROM tmpDates td
   WHERE DATEADD(dd, 1, dt) <= CAST('2018-02-20' AS DATETIME)
  )
SELECT td.dt as VisitDate, COUNT(*) as Visits
  FROM tmpDates td
  LEFT JOIN mytable m on td.dt = m.VisitDate
GROUP BY VisitDate

显然,您可以将日期作为参数传递。

基本上我们在这里做的是创建一个只有UNIONS一堆日期的CTE,每个日期比前一天晚一天。这不是很有效,但对于像你这样的小日期范围来说会很好。然后我们选择所有这些记录,并按日期左键加入原始表格,然后按日期分组/计数。如果原始表中没有特定日期的数据,那么您将返回一行,其中包含日期和0计数。