简体   繁体   English

sql查询以显示proficy historian中存在的每个标签的平均值

[英]sql query to show average of each tag present in proficy historian

I need to develop ssrs report in which user will provide a month as varchar(2) and year as varchar(4) . 我需要开发ssrs报告,其中用户将以varchar(2)月份,以varchar(4)年份。 When a user clicks on a view report button, an ssrs report should display result as follows. 当用户单击视图报告按钮时,ssrs报告应显示如下结果。 (suppose if user pass month as 12 and year as 2013) (假设用户通过的月份为12,年份为2013)

Date                 TagName               Average
12/01/13             Tag01                   45.23
12/01/13             Tag02                   89.23
12/02/13             Tag01                   2.363
12/02/13             Tag02                   45.23
.
.
.
12/31/13             Tag01                   55.24
12/31/13             Tag02                   95.24

I designed the following query 我设计了以下查询

CREATE TABLE #tempval 
  ( 
     [timestamp] VARCHAR(30), 
     tagname     VARCHAR(300), 
     tagval      DECIMAL(18, 5) 
  ) 

DECLARE @month VARCHAR(2) 
DECLARE @year VARCHAR(4) 

SET @month='08' 
SET @year='2014' 

DECLARE @startdate VARCHAR(30) 
DECLARE @enddate VARCHAR(30) 

SET @startdate=@month + '/01/' + @year 

IF ( CONVERT(INT, @month) >= Month(Getdate()) 
     AND CONVERT(INT, @year) >= Year(Getdate()) ) 
  BEGIN 
      SET @enddate=CONVERT(VARCHAR, (SELECT Getdate())) 
  END 
ELSE 
  BEGIN 
      SET @enddate=CONVERT(VARCHAR, Dateadd(day, -1, Dateadd(month, 1, CONVERT( 
                                                     DATETIME, @startdate 
                                                          )) 
                                    )) 
  END 

DECLARE @query VARCHAR(1000) 
DECLARE @starttime VARCHAR(30) 
DECLARE @endtime VARCHAR(30) 

SET @starttime= CONVERT(VARCHAR, Datepart(month, Dateadd(hour, 22, Dateadd(day, 
                -1, CONVERT( 
                DATETIME, @startdate))))) 
                + '/' 
                + CONVERT(VARCHAR, Datepart(day, Dateadd(hour, 22, Dateadd(day, 
                -1, CONVERT(DATETIME, @startdate))))) 
                + '/' 
                + CONVERT(VARCHAR, Datepart(year, Dateadd(hour, 22, Dateadd(day, 
                -1, CONVERT(DATETIME, @startdate))))) 
                + ' ' 
                + CONVERT(VARCHAR, Datepart(hour, Dateadd(hour, 22, Dateadd(day, 
                -1, CONVERT(DATETIME, @startdate))))) 
                + ':' 
                + CONVERT(VARCHAR, Datepart(minute, Dateadd(hour, 22, Dateadd( 
                day, -1, CONVERT(DATETIME, @startdate))))) 
                + ':' 
                + CONVERT(VARCHAR, Datepart(second, Dateadd(hour, 22, Dateadd( 
                day, -1, CONVERT(DATETIME, @startdate))))) 
SET @endtime=CONVERT(VARCHAR, Datepart(month, Dateadd(hour, 22, CONVERT(DATETIME 
             , @startdate) 
             ))) 
             + '/' 
             + CONVERT(VARCHAR, Datepart(day, Dateadd(hour, 22, CONVERT(DATETIME 
             , @startdate)))) 
             + '/' 
             + CONVERT(VARCHAR, Datepart(year, Dateadd(hour, 22, CONVERT( 
             DATETIME, @startdate)))) 
             + ' ' 
             + CONVERT(VARCHAR, Datepart(hour, Dateadd(hour, 22, CONVERT( 
             DATETIME, @startdate)))) 
             + ':' 
             + CONVERT(VARCHAR, Datepart(minute, Dateadd(hour, 22, CONVERT( 
             DATETIME, @startdate)))) 
             + ':' 
             + CONVERT(VARCHAR, Datepart(second, Dateadd(hour, 22, CONVERT( 
             DATETIME, @startdate)))) 
SET @query='select * from openquery(muri,''set StartTime=''''' 
           + @starttime + ''''',EndTime=''''' + @endtime 
           + 
''''' select * from ihrawdata where tagname=MURISERVER.MURI.DCS.ASSETS.87A.87A_FI_2101.DACA.PV  and samplingmode=rawbytime'')'

EXEC (@query) 

But it displays blank columns but I'm not sure why. 但是它显示空白列,但我不确定为什么。

The easiest way I've found for an SSRS report to query GE Proficy Historian is to use dynamic SQL within a stored proc. 我找到的用于查询GE Proficy Historian的SSRS报告的最简单方法是在存储的proc中使用动态SQL。

The prerequisite is that you have a SQL Server linked server configured using the IhOLEDB.iHistorian provider. 前提条件是您具有使用IhOLEDB.iHistorian提供程序配置的SQL Server链接服务器。 That would look something like this once installed: 安装完成后,看起来会像这样:

GE Historian的链接服务器提供商

You then create a stored proc as normal, I usually have a fairly generic proc which takes most of the arguments you'd want to query a GE Historian with. 然后,您通常会创建一个存储的proc,我通常会使用一个相当通用的proc,该proc接受您要查询GE Historian的大多数参数。 Example stored proc definition: 示例存储过程定义:

CREATE PROCEDURE [utility].[Historian_GetData] @DateFrom DATETIME,
                                           @DateTo DATETIME,
                                           @TagList  VARCHAR(1000),
                                           @Historian NVARCHAR(100),
                                           @SampleInterval NVARCHAR(50),
                                           @SampleMethod NVARCHAR(50),
                                           @DefaultRowCount INT = 5000

However to keep the example simple, here is an anonymous block with some dates which will be used to query. 但是,为了使示例简单起见,这是一个带有一些日期的匿名块,将用于查询。 Essentially, for each of the parameters in the stored proc you'd add them to the dynamic SQL. 本质上,对于存储的proc中的每个参数,您都将它们添加到动态SQL中。

DECLARE @DateFrom NVARCHAR(100) = '2018-05-02 06:00'
DECLARE @DateTo   NVARCHAR(100) = '2018-05-03 06:00'

DECLARE @SQL NVARCHAR(4000)
DECLARE @Result TABLE (TagName NVARCHAR(100) NOT NULL, [Timestamp] DATETIME NOT NULL, Value NVARCHAR(100) NOT NULL, Quality NVARCHAR(100) NOT NULL)

BEGIN
   SET @SQL = 'SELECT tagname, timestamp, value, quality
           FROM OPENQUERY ([Your-Historian],
           ''SET StartTime = "' + @DateFrom + '", EndTime = "' + @DateTo + '",
             SamplingMode = RawByTime
             SELECT tagname, timestamp, value, quality
             FROM ihRawData
             WHERE TagName = XYZ'')';

  INSERT INTO @Result EXEC sp_executesql @SQL

  SELECT TagName,
         [Timestamp],
         [Value],
         Quality
  FROM @Result ORDER BY TagName, [Timestamp]
END

The result for that will look something like this: 结果将如下所示:

SQL结果

So in summary: 因此,总而言之:

  1. Configure your linked server to point to your Historian 配置链接服务器以指向您的Historian
  2. Create your stored proc accepting all the Historian query arguments as parameters. 创建接受所有Historian查询参数作为参数的存储过程。
  3. Execute your dynamic SQL 执行动态SQL
  4. Calculate your result as required, in your case some averages. 根据需要计算结果,在某些情况下为平均值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM