SSRS 折线图:x 轴时间尺度,有数据间隙

发布于 2024-10-02 07:47:13 字数 954 浏览 3 评论 0 原文

我有一个数据集,用于计算每小时生产的托盘数量,例如

11/11/2010 22:00 --> 22
2010年11月11日 23:00 --> 12
2010年11月12日 00:00 --> 18
2010/11/12 01:00 --> 19
2010/11/12 03:00 --> 20

您可能会注意到,01:00 和 03:00 之间有一个间隙,因为该小时没有数据。该数据在 SSRS 2005 中使用带有时间尺度 x 轴的图表进行可视化。当图形类型为“柱形”时,完全没有问题,因为 02:00 可视化时没有 (0) 值,并且间隙在图形中清晰可见。当图形类型为“线”或“面积”时,02:00 也会在图形上可视化,但没有 0 值:01:00 和 03:00 的值之间有一条连接线。查看折线图时,人们可以得出结论,02:00 处有生产,但事实并非如此,它只是连接 01:00 的值与 03:00 的值的线。

面积图中相同数据的示例 alt text(原始图像:http://img577.imageshack.us/img577/9616/area.jpg)

和柱形图 alt text(原始图片:http://img577.imageshack.us/img577/7590/column.jpg

应该解释这个问题。

有谁知道如何解决这个问题?谢谢你!

I have a dataset which counts the number of produced pallets per hour, eg

11/11/2010 22:00 --> 22
11/11/2010 23:00 --> 12
11/12/2010 00:00 --> 18
11/12/2010 01:00 --> 19
11/12/2010 03:00 --> 20

As you may notice, there is a gap between 01:00 and 03:00 since there is no data for that hour. This data gets visualised in SSRS 2005 using a graph with a time-scale x-axis. When the graph type is 'Column', there is no problem at all since 02:00 gets visualised with no (0) value and the gap is well visible in the graph. When the graph type is 'Line' or 'Area', 02:00 is visualised on the graph as well but with no 0 value: there is a connection line between the value of 01:00 and 03:00. When looking to the line graph, one could conclude that there was production at 02:00 but this is not true, it is just the line that connects the value of 01:00 with the value of 03:00.

Example of the same data in an Area graph alt text(original image: http://img577.imageshack.us/img577/9616/area.jpg)

and a Column graph alt text(original image: http://img577.imageshack.us/img577/7590/column.jpg)

should explain the problem.

Does anyone know how to resolve this problem? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心 2024-10-09 07:47:13

也许,您现在已经找到了问题的答案。这是针对可能偶然发现这个问题的其他人的。仅当您使用SQL Server 2005 或更高版本作为报表的数据源时,此解决方案才适用。该示例使用通用表表达式 (CTE) 来实现问题中提到的所需结果。下面提到的示例使用 SSRS 2005SQL Server 2008 R2 数据库。 SQL Server 2005 或更高版本 支持 CTE。

分步过程:

  1. 创建一个名为 dbo.TimeScaleData 的表,并使用问题中提供的数据进行填充。请参阅屏幕截图#1SQL 脚本部分下提供了表的创建脚本。

  2. 创建一个 CTE,它将获取表 dbo.TimeScaleData 中的最小和最大日期值,并生成给定范围内每小时的所有时间值。然后在 CTE 和表 dbo.TimeScaleData 之间使用 OUTER APPLY 来获取数据。任何没有匹配记录的时间范围都将被分配值 0。请参阅屏幕截图 #2。该查询将在 SSRS 报告中用于创建数据集。查询在 SSRS DataSet Query 部分中提供。

  3. 屏幕截图 #3 显示 CTE 查询用于创建报告数据集。屏幕截图 #4 - #7 显示如何配置图表控件以使用数据集。

  4. 屏幕截图 #8 显示针对 SQL Server 表中数据的报告输出。

希望有帮助。

SQL 脚本:

CREATE TABLE [dbo].[TimeScaleData](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [DateTimeValue] [datetime] NULL,
    [PalletsProduced] [int] NULL,
 CONSTRAINT [PK_TimeScaleData] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
GO

SSRS 数据集查询:

DECLARE @StartDateTime  AS DATETIME;
DECLARE @EndDateTime    AS DATETIME;

SELECT  @StartDateTime  = MIN(DateTimeValue)
    ,   @EndDateTime    = MAX(DateTimeValue)
FROM    dbo.TimeScaleData;  

WITH CTE(DateTimeRange) AS
(
        SELECT  @StartDateTime AS DateTimeRange
    UNION ALL
        SELECT  DATEADD(HOUR, 1, DateTimeRange)
        FROM    CTE
        WHERE   DATEADD(HOUR, 1, DateTimeRange) <= @EndDateTime
)
SELECT          CTE.DateTimeRange
            ,   COALESCE(TSD.PalletsProduced, 0) AS PalletsProduced
FROM            CTE
OUTER APPLY     (
                    SELECT  PalletsProduced
                    FROM    dbo.TimeScaleData TSD
                    WHERE   TSD.DateTimeValue = CTE.DateTimeRange
                ) TSD;

屏幕截图 #1:

1

屏幕截图 #2:

2

屏幕截图 #3:

3

屏幕截图#4:

4

屏幕截图 #5:

5

屏幕截图 #6:

6

屏幕截图 #7:

7

屏幕截图 #8:

8

Probably, you have found an answer to your question now. This is for others who might stumble upon this question. This solution is only applicable if you are using SQL Server 2005 or above as the data source for the reports. The example uses Common Table Expressions (CTE) to achieve the desired results mentioned in the question. The example mentioned below uses SSRS 2005 and SQL Server 2008 R2 database. CTEs are supported in SQL Server 2005 or above.

Step-by-step process:

  1. Create a table named dbo.TimeScaleData and populate with the data provided in the question. Refer screenshot #1. Create scripts of the table is provided under SQL Scripts section.

  2. Create a CTE that will take the minimum and maximum date values in the table dbo.TimeScaleData and generate all the time values for every hour between the given range. Then use OUTER APPLY between the CTE and the table dbo.TimeScaleData to fetch the data. Any time range that doesn't have a matching record will be assigned the value 0. Refer screenshot #2. This query will be used in the SSRS report to create the dataset. The query is provided SSRS DataSet Query section.

  3. Screenshot #3 shows that the CTE query is being used to create the report dataset. Screenshots #4 - #7 shows how the Chart control is configured to use the dataset.

  4. Screenshot #8 shows the report output against the data in SQL Server table.

Hope that helps.

SQL Scripts:

CREATE TABLE [dbo].[TimeScaleData](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [DateTimeValue] [datetime] NULL,
    [PalletsProduced] [int] NULL,
 CONSTRAINT [PK_TimeScaleData] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
GO

SSRS DataSet Query:

DECLARE @StartDateTime  AS DATETIME;
DECLARE @EndDateTime    AS DATETIME;

SELECT  @StartDateTime  = MIN(DateTimeValue)
    ,   @EndDateTime    = MAX(DateTimeValue)
FROM    dbo.TimeScaleData;  

WITH CTE(DateTimeRange) AS
(
        SELECT  @StartDateTime AS DateTimeRange
    UNION ALL
        SELECT  DATEADD(HOUR, 1, DateTimeRange)
        FROM    CTE
        WHERE   DATEADD(HOUR, 1, DateTimeRange) <= @EndDateTime
)
SELECT          CTE.DateTimeRange
            ,   COALESCE(TSD.PalletsProduced, 0) AS PalletsProduced
FROM            CTE
OUTER APPLY     (
                    SELECT  PalletsProduced
                    FROM    dbo.TimeScaleData TSD
                    WHERE   TSD.DateTimeValue = CTE.DateTimeRange
                ) TSD;

Screenshot #1:

1

Screenshot #2:

2

Screenshot #3:

3

Screenshot #4:

4

Screenshot #5:

5

Screenshot #6:

6

Screenshot #7:

7

Screenshot #8:

8

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文