注意勞動,多學(xué)習(xí)電氣自動化知識!
配置軟件需要在數(shù)據(jù)庫中存儲數(shù)據(jù)。雖然每個品牌的實現(xiàn)方法不同,但實際實現(xiàn)的想法基本相同,所有配置軟件都有這個功能!我不會說怎么儲存。根據(jù)配置軟件的不同,搜索一下“WINCC將數(shù)據(jù)存儲在SQL server上”、“Intouch將數(shù)據(jù)存儲在SQL server上”等。
。今天我們主要講數(shù)據(jù)庫的規(guī)劃以及將數(shù)據(jù)存儲進數(shù)據(jù)庫后如何展示出來和導(dǎo)出成為Excel或PDF的一種實現(xiàn)方法。另外集合了一些SqlServer,RDLC的知識點。
文章很長,建議收藏,轉(zhuǎn)發(fā)、點贊~效果圖如下,如果做上位機相信會用到:
一、數(shù)據(jù)庫規(guī)劃
打開數(shù)據(jù)庫,建立各種表:
1、變量表
建立一個TAGDESC表,用于存放需要的變量,TAGID用于與其它表映射關(guān)系,TAGNAME用于標示變量名稱,TAGDESC用于存放變量注釋,Locate用于存放歸屬位置。
這個表是自己建的,用于存放注釋,以及變量名和ID對應(yīng)值。以及Locate的區(qū)域注釋。
2、數(shù)值記錄表
建立一個數(shù)值記錄表示例中的表名qd_data_temp,記錄時間,記錄值,區(qū)域值,后面會將ID和變量映射、區(qū)域和區(qū)域注釋映射。
3、區(qū)域?qū)?yīng)表
本例中不使用
二、VS——RDLC報表建立,報表展示方式
打開SQLServerBusinessIntelligence
1、新建項目
2、選擇數(shù)據(jù)源
3、定義數(shù)據(jù)集
數(shù)據(jù)集選項
SELECT N.Tagid_d AS 序號, T.TagDesc AS 注釋, CONVERT(VARCHAR, N.Sampletime_d, 111) AS 日期, DATEPART(hh,
N.Sampletime_d) AS 時間, AVG) AS 值
FROM qd_data_temp AS N INNER JOIN
TagDesc AS T ON N.Tagid_d = T.TagID
WHERE (CONVERT(VARCHAR, N.Sampletime_d, 111) = CONVERT(VARCHAR, @查詢?nèi)掌? 111))
GROUP BY N.Tagid_d, T.TagDesc, CONVERT(VARCHAR, N.Sampletime_d, 111), DATEPART(hh, N.Sampletime_d)
ORDER BY 日期, 序號
(簡單方式
SELECT N.Tagid_d AS 序號, N.Sampletime_d, N.Samplevalue_d, T.TagDesc as 注釋
FROM qd_data_temp AS N INNER JOIN
TagDesc AS T ON N.Tagid_d = T.TagID
)
SQL語句說明:
SELECT 表代號.表列名 AS 列別名......
FROM 數(shù)據(jù)表1名 AS 表代號 INNER JOIN 數(shù)據(jù)表2名AS表代號 表1.關(guān)聯(lián)項=表2.關(guān)聯(lián)項
選擇矩陣
4、數(shù)據(jù)展示樣式
下一步,直到完成
5、參數(shù)設(shè)置
設(shè)置參數(shù)數(shù)據(jù)類型為日期型。
6、最終效果
效果如下,可以導(dǎo)出為EXCEL,也可以是導(dǎo)出 PDF
三、SQL常用語句
1、其他可用語句:
SQL SELECT INTO 選取一部分創(chuàng)建一個新表
實例:
SELECT A表.列,B表.列
INTO 新表名
FROM A表
INNER JOIN 表1 ON 表1.列=表2.列
WHERE 條件
2、視圖的使用
CREATE VIEW
CREATE VIEW DATATEST AS
SELECT N.Tagid_d AS 序號, N.Sampletime_d, N.Samplevalue_d, T.TagDesc AS 注釋
FROM qd_data_temp AS N INNER JOIN TagDesc AS T ON N.Tagid_d = T.TagID
視圖可以像表一樣查詢
SELECT * FROM [DATATEST]
刪除視圖
SQL DROP VIEW [DATATEST]
3、時間函數(shù)
NOW()返回當前日期和時間
CURDATE()返回房前日期時間單獨部分
dateadd()日期時間加法
datediff()返回兩個日期之間的時間
convert()用不同的格式顯示日期時間
4、分組
按小時分組平均值
SELECT year(日期時間列名),MONTH(日期時間列名),DAY(日期時間列名),DATEPART(HOUR,(日期時間列名),AVG(值列)
FROM 表名
GROUP?。拢佟?year(日期時間列名),MONTH(日期時間列名),DAY(日期時間列名),DATEPART(HOUR,(日期時間列名)
select 序號,CONVERT(VARCHAR,Sampletime_d,111) as 日期,DATEPART(hh,Sampletime_d) as 時
,AVG(Samplevalue_d) as 值
from DATATEST WHERE CONVERT(VARCHAR,Sampletime_d,111)='2019/10/28'
group by 序號,CONVERT(VARCHAR,Sampletime_d,111),DATEPART(hh,Sampletime_d)
order by 日期,序號
先創(chuàng)建視圖
CREATE VIEW DATATEST AS
SELECT N.Tagid_d AS 序號, N.Sampletime_d, N.Samplevalue_d, T.TagDesc AS 注釋
FROM qd_data_temp AS N INNER JOIN TagDesc AS T ON N.Tagid_d = T.TagID
再查詢
select 序號,注釋,CONVERT(VARCHAR,Sampletime_d,111) as 日期,DATEPART(hh,Sampletime_d) as 時
,AVG(Samplevalue_d) as 值
from DATATEST WHERE Sampletime_d between '2019-5-6' AND '2019-10-29'
group by 序號,釋,CONVERT(VARCHAR,Sampletime_d,111),DATEPART(hh,Sampletime_d)
order by 日期,序號
時間間隔查詢方式
select 序號,注釋,CONVERT(VARCHAR,Sampletime_d,111) as 日期,DATEPART(hh,Sampletime_d) as 時
,AVG(Samplevalue_d) as 值
from DATATEST WHERE Sampletime_d between '2019-5-6' AND '2019-10-29'
group by 序號,釋,CONVERT(VARCHAR,Sampletime_d,111),DATEPART(hh,Sampletime_d)
order by 日期,序號
四、RDLC查詢報表的其他示例
1、全廠數(shù)據(jù)報表
1、展示
2、框架
3、查詢開始時間參數(shù)
4、時間間隔參數(shù)
5、車間選擇參數(shù)
6、變量選擇參數(shù)
7、PVNAMES參數(shù)
=join(Parameters!變量選擇.Value,";")
標簽和值相同
8、DATASET1腳本
declare @StartTime bigint
declare @EndTime bigint
declare @intervalTime bigint
declare @TagsID Varchar(1000)
declare @st Varchar(1000)
declare @temp1 Varchar(1000)
declare @temp2 Varchar(1000)
declare @temp3 Varchar(1000)
declare @temp4 Varchar(1000)
set @temp1=CONVERT(Varchar(1000),@開始日期, 23);
set @st =' '+@查詢開始時間;
set @temp2 =convert(datetime,@temp1+@st);
set @StartTime =dbo.ToBigInt(convert(datetime,@temp2));
set @temp3=CONVERT(Varchar(1000),@結(jié)束日期, 23);
set @st =' '+@查詢結(jié)束時間;
set @temp4 =convert(datetime,@temp3+@st);
set @EndTime =dbo.ToBigInt(convert(datetime,@temp4));
set @intervalTime =dbo.IntervalToBigInt(@時間間隔);
set @tagsID=dbo.CTTagnamesToTagIDs(@pvnames);
select dbo.TagDe(SampleDateTime) AS SampleDateTime,cast(SampleValue as decimal(10,1)) as SampleValue from fn_get_samples_matrix_byinterval(@TagsID,@StartTime,@EndTime,0,@intervalTime) WA
INNER JOIN dbo.TagDesc ON TagDe
9、DATASET2腳本
select TagID,TagName,TagDesc from TagDesc
where Locate=@車間選擇
10、DATASET3腳本
select chejian from qd_chejian_desc where Locate=@車間選擇
2、車間數(shù)據(jù)報表
use [sql]
declare @temp1 varchar(50)
declare @temp2 varchar(100)
declare @temp3 varchar(300)
declare @temp4 varchar(100)
declare @temp5 varchar(100)
declare @temp6 varchar(200)
declare @TagsID Varchar(2000)
if (LEN(CAST(month(@查詢?nèi)掌? as char(100)))>1)
begin
set @temp4=CAST(month(@查詢?nèi)掌? AS char(100))
end
else
begin
set @temp4='0'+CAST(month(@查詢?nèi)掌? AS char(100))
end
if (LEN(CAST(day(@查詢?nèi)掌? as char(100)))>1)
begin
set @temp5=CAST(day(@查詢?nèi)掌? AS char(100))
end
else
begin
set @temp5='0'+CAST(day(@查詢?nèi)掌? AS char(100))
end
set @temp2='qd_ymd'+convert(varchar(100),year(@查詢?nèi)掌?)+rtrim(@temp4)+ltrim(@temp5)
set @temp3='select TagDesc_d AS TagDesc,Sampletime_d AS 時間,Samplevalue_d AS 數(shù)值 from '+ @temp2
set @temp4=' where Locate_d='''+@車間選擇+''' and Jiezhi_d='''+@介質(zhì)選擇+''' and Pici_d='''+@計量參數(shù)+''' order by Sampletime_d'
EXEC(@temp3+@temp4)
3、一種餅圖展示
USE [SQL]
declare @temp1 varchar(50)
declare @temp2 varchar(100)
declare @temp3 varchar(300)
declare @temp4 varchar(300)
declare @temp5 varchar(300)
declare @temp6 varchar(300)
declare @temp7 Varchar(2000)
if (LEN(CAST(month(@月份選擇) as char(100)))>1)
begin
set @temp4=CAST(month(@月份選擇) AS char(100))
end
else
begin
set @temp4='0'+CAST(month(@月份選擇) AS char(100))
end
set @temp2='qd_ym'+convert(varchar(100),year(@月份選擇))+rtrim(@temp4)
set @temp6=convert(varchar(100),year(@月份選擇))+'年'+convert(varchar(100),month(@月份選擇))+'月 高溫水熱量月用量匯總 單位:GJ'
set @temp3='select Locate_m ,qd_c(Samplevalue_m) as total ,'''+@temp6+'''as yuefen from '+ @temp2
set @temp4=' INNER join qd_chejian_desc on ' + @temp2+'.Locate_m =qd_c where '
set @temp5='Jiezhi_m=''3'' and Pici_m=''2'' group by '+@temp2 +'.Locate_m,qd_c;
exec(@temp3+@temp4+@temp5)
五、利用SQL作業(yè)進行創(chuàng)建,修改,刪除等作業(yè)
1、創(chuàng)建作業(yè)
右鍵單擊作業(yè),選新建作業(yè)
2、修改名稱等信息,點擊步驟,創(chuàng)建腳本,以下腳本可以創(chuàng)建一個日報表
USE [SQL]
declare @temp1 varchar(100)
declare @temp2 varchar(100)
declare @temp3 varchar(100)
declare @temp4 varchar(80)
declare @createtable varchar(500)
set @temp1='qd_ymd'+CONVERT(Varchar(100),getdate(),112)
IF not EXISTS (SELECT name FROM sysobjects
WHERE name =@temp1 AND type = 'U')
begin
set @createtable='CREATE TABLE '+@temp1+'(Tagid_d int,TagDesc_d nvarchar(3000),Locate_d nvarchar(100),Sampletime_d datetime,Samplevalue_d float,Jiezhi_d nvarchar(50),Pici_d nvarchar(50))'
exec(@createtable)
set @temp2='CREATE INDEX Tagid_d on '+@temp1+'(Tagid_d)'
exec(@temp2)
end
3、設(shè)置計劃運行時間
4、腳本參考
1、創(chuàng)建日報表
USE [SQL]
declare @temp1 varchar(100)
declare @temp2 varchar(100)
declare @temp3 varchar(100)
declare @temp4 varchar(80)
declare @createtable varchar(500)
set @temp1='qd_ymd'+CONVERT(Varchar(100),getdate(),112)
IF not EXISTS (SELECT name FROM sysobjects
WHERE name =@temp1 AND type = 'U')
begin
set @createtable='CREATE TABLE '+@temp1+'(Tagid_d int,TagDesc_d nvarchar(3000),Locate_d nvarchar(100),Sampletime_d datetime,Samplevalue_d float,Jiezhi_d nvarchar(50),Pici_d nvarchar(50))'
exec(@createtable)
set @temp2='CREATE INDEX Tagid_d on '+@temp1+'(Tagid_d)'
exec(@temp2)
end
2、創(chuàng)建月報表
USE [SQL]
declare @temp1 varchar(50)
declare @temp2 varchar(50)
declare @temp3 varchar(50)
declare @temp4 varchar(100)
declare @createtable varchar(500)
if (LEN(CAST(month(GETDATE()) as char(100)))>1)
begin
set @temp1=CAST(month(GETDATE()) AS char(100))
end
else
begin
set @temp1='0'+CAST(month(GETDATE()) AS char(100))
end
set @temp2= CAST(year(GETDATE())as char(100));
set @temp3 ='qd_ym'+rtrim(@temp2)+ltrim(@temp1)
IF not EXISTS (SELECT name FROM sysobjects
WHERE name =@temp3 AND type = 'U')
begin
set @createtable='CREATE TABLE '+@temp3+'(Tagid_m int,TagDesc_m nvarchar(3000),Locate_m nvarchar(100),Sampletime_m datetime,Samplevalue_m float,Jiezhi_m nvarchar(50), Pici_m nvarchar(50))'
exec(@createtable)
set @temp4='CREATE INDEX Tagid_m on '+@temp3+'(Tagid_m)'
exec(@temp4)
end
3、匯總?cè)請蟊頂?shù)據(jù)到月報表
USE [SQL]
declare @temp1 varchar(50)
declare @temp2 varchar(50)
declare @temp3 varchar(100)
declare @temp4 varchar(100)
declare @temp5 varchar(80)
declare @temp6 varchar(200)
declare @temp7 varchar(100)
declare @temp8 varchar(50)
declare @temp9 varchar(50)
declare @temp10 varchar(50)
declare @temp11 varchar(100)
declare @temp12 varchar(100)
declare @temp13 varchar(1000)
declare @temp14 varchar(500)
declare @createtable varchar(500)
/*生成昨天的月份*/
if (LEN(CAST(month(dateadd(dd,-1, GETDATE()) )as char(100)))>1)
begin
set @temp1= CAST(month(dateadd(dd,-1, GETDATE()))AS char(100))
end
else
begin
set @temp1='0'+ CAST(month(dateadd(dd,-1, GETDATE()))AS char(100))
end
set @temp2= CAST(year(dateadd(dd,-1, GETDATE()) )as char(100));
set @temp3 ='qd_ym'+rtrim(@temp2)+ltrim(@temp1)
/*判斷月報表格是否存在*/
IF not EXISTS (SELECT name FROM sysobjects
WHERE name =@temp3 AND type = 'U')
begin
set @createtable='CREATE TABLE '+@temp3+'(Tagid_m int,TagDesc_m nvarchar(3000),Locate_m nvarchar(100),Sampletime_m datetime,Samplevalue_m float, Jiezhi_m nvarchar(50), Pici_m nvarchar(50))'
exec(@createtable)
set @temp4='CREATE INDEX Tagid_m on '+@temp3+'(Tagid_m)'
exec(@temp4)
end
/*匯總昨日總量到月報*/
/*判斷日報是否存在*/
set @temp6='qd_ymd'+ CONVERT(Varchar(1000),dateadd(dd,-1,getdate()),112)
IF not EXISTS (SELECT name FROM sysobjects
WHERE name =@temp6 AND type = 'U')
begin
set @createtable='CREATE TABLE '+@temp6+'(Tagid_d int,TagDesc_d nvarchar(2000),Locate_d nvarchar(100),Sampletime_d datetime,Samplevalue_d float, Jiezhi_d nvarchar(50), Pici_d nvarchar(50))'
exec(@createtable)
set @temp7='CREATE INDEX Tagid_d on '+@temp6+'(Tagid_d)'
exec(@temp7)
end
/*插入昨日的匯總數(shù)據(jù)*/
set @temp8=year(dateadd(dd,-1,getdate()))
set @temp9=month(dateadd(dd,-1,getdate()))
set @temp10=day(dateadd(dd,-1,getdate()))
set @temp11=CONVERT(Varchar(100), dateadd(dd,-1,getdate()),112)
set @temp12=CONVERT(varchar(50),year(@temp11))+'-'+convert(varchar(50),month(@temp11))+'-'+convert(varchar(50),day(@temp11))
set @temp13='insert into '+@temp3+'(Tagid_m,TagDesc_m,Locate_m,Sampletime_m,Samplevalue_m, Jiezhi_m,Pici_m)
select Tagid_d, TagDe, TagDe;''+@temp12+''',Samplevalue_d, TagDe, TagDe from
(select Tagid_d,cast(sum(Samplevalue_d) as decimal(10,1)) as Samplevalue_d from '+@temp6+' w
where year(Sampletime_d)='+ @temp8+' and month(Sampletime_d)='+ @temp9+' and day(Sampletime_d)='+ @temp10+'
group by Tagid_d ) a
inner join TagDesc on TagDe;
exec(@temp13)
/*把昨天的歷史數(shù)據(jù)清除*/
Set @temp14='DELETE from qd_data_temp where year(Sampletime_d)='+ @temp8+' and month(Sampletime_d)='+ @temp9+' and day(Sampletime_d)='+ @temp10+ ''
exec(@temp14)
4、日報表小時數(shù)據(jù)計算存入
說明:需要每小時建立一個作業(yè)。
USE [SQL]
/* 23點的*/
declare @temp1 varchar(100)
declare @temp2 varchar(100)
declare @temp3 varchar(200)
declare @temp4 varchar(100)
declare @temp5 varchar(100)
declare @temp6 datetime
declare @temp7 Varchar(100)
declare @temp8 Varchar(1000)
declare @temp9 varchar(1000)
declare @temp10 varchar(1000)
declare @temp11 datetime
declare @temp12 datetime
declare @temp13 varchar(100)
declare @temp14 varchar(1000)
declare @temp15 varchar(1000)
declare @temp16 varchar(100)
declare @temp17 varchar(100)
declare @output varchar(1000);
declare @createtable varchar(500)
declare @StartTime bigint
declare @EndTime bigint
declare @intervalTime bigint
declare @TagsID Varchar(2000)
declare @st Varchar(100)
/*把昨天的23點的數(shù)據(jù)和今天的0:0:05前的數(shù)據(jù)寫入qd_date_temp */
set @temp17=CONVERT(Varchar(100), dateadd(dd,-1,getdate()), 23);
set @temp1=CONVERT(Varchar(100), getdate() , 23);
set @temp2=@temp17+' '+ '23:00:00:00';
set @temp13=@temp1+' '+ '00:00:00:00';
set @StartTime=dbo.ToBigInt(@temp2);
set @EndTime =dbo.ToBigInt(@temp13);
set @intervalTime =dbo.IntervalToBigInt('30m');
select @TagsID =coalesce(@TagsID,'')+ cast(TagID as varchar(10))+';'
from TagDesc
insert into dbo. qd_data_temp(Tagid_d,Sampletime_d, Samplevalue_d ,Locate) select w.TagID,dbo.ToDate), convert(float,w.SampleValue) as SampleValue ,TagDe FROM (select TagID,SampleDateTime,SampleValue,QualityID from fn_get_samples_matrix_byinterval
(@TagsID,@StartTime,@EndTime,0,@intervalTime)) w
inner join TagDesc on w.TagID=TagDe
where (QualityID=192 and TagDe<>'shui' and year(SampleDateTime))= year(dateadd(dd,-1,getdate())) and month(SampleDateTime))= month(dateadd(dd,-1,getdate())) and day(SampleDateTime))= day(dateadd(dd,-1,getdate()))
and datepart(hh,dbo.ToDate(SampleDateTime))=23) or (QualityID=192 and TagDe<>'shui'and year(SampleDateTime))= year(getdate()) and month(SampleDateTime))= month(getdate()) and day(SampleDateTime))= day(getdate()))
/*判斷前一天的日報是否存在,沒有建立*/
set @temp3='qd_ymd'+CONVERT(Varchar(1000),dateadd(dd,-1,getdate()),112)
IF not EXISTS (SELECT name FROM sysobjects
WHERE name =@temp3 AND type = 'U')
begin
set @createtable='CREATE TABLE '+@temp3+'(Tagid_d int,TagDesc_d nvarchar(3000),Locate_d nvarchar(100),Sampletime_d datetime,Samplevalue_d float, Jiezhi_d nvarchar(50), Pici_d
nvarchar(50))'
exec(@createtable)
set @temp4='CREATE INDEX Tagid_d on '+@temp3+'(Tagid_d)'
exec(@temp4)
end
/*本小時數(shù)據(jù)整理,插入日報數(shù)據(jù)為前一小時數(shù)據(jù)和當前小時的 #:05:00數(shù)據(jù)*/
set @temp5=CONVERT(Varchar(100), getdate() , 20);
set @temp6=dateadd(dd,-1,convert(datetime,@temp5));
set @temp9='23:00:00'
set @temp10=CONVERT(varchar(100),year(@temp6))+'-'+convert(varchar(1000),month(@temp6))+'-'+convert(varchar(1000),day(@temp6))+' '+@temp9
set @temp11=convert(datetime,@temp10,20)
/*插入日報數(shù)據(jù), id大小寫規(guī)范,添加了一個取值不為0的情況,避免差值太大*/
set @temp14='insert into '+@temp3+'(Tagid_d,TagDesc_d,Locate_d,Jiezhi_d , Pici_d,Sampletime_d,Samplevalue_d)
select Tagid_d,TagDe,TagDe, TagDe, TagDe,'''+@temp10+''' as Sampletime_d,Samplevalue_d from ( select Tagid_d,cast(max(Samplevalue_d)-min(Samplevalue_d) as
decimal(10,1)) as Samplevalue_d from qd_data_temp w
where(datepart(hh,w.Sampletime_d)=23 and day(Sampletime_d)=day(dateadd(dd,-1,getdate())) and mon)=month(dateadd(dd,-1,getdate())) and Samplevalue_d<>0)
or (day)=day(getdate()) and mon)=month(getdate()) and (datepart(hh,w.Sampletime_d)=0) and Samplevalue_d<>0)
group by Tagid_d ) a
inner join TagDesc on TagDe;
exec(@temp14)
5、自動刪除過期數(shù)據(jù)
use [sql]
declare @tb varchar(100)
declare @tb1 varchar(100)
/* 刪除日報 */
while(EXISTS (SELECT name FROM sysobjects
WHERE type = 'U' AND name LIKE 'qd_ymd%' and datediff( day,convert(datetime,RIGHT(name,8),120),getdate())>=60))
BEGIN
SELECT @tb='drop table '+name
FROM sysobjects
WHERE type = 'U' AND name LIKE 'qd_ymd%' and datediff( day,convert(datetime,RIGHT(name,8),120),getdate())>=60
exec(@tb)
END
/* 刪除月報 */
while(EXISTS (SELECT name FROM sysobjects
WHERE type = 'U' AND name LIKE 'qd_ym2%' and datediff(month,convert(datetime,RIGHT(name,6)+'01',120),getdate())>=12))
BEGIN
SELECT @tb1='drop table '+name
FROM sysobjects
WHERE type = 'U' AND name LIKE 'qd_ym2%' and datediff(month,convert(datetime,RIGHT(name,6)+'01',120),getdate())>=12
exec(@tb1)
END
/* 刪除numsample, */
delete from NumericSamples where dbo.ToDate(SampleDateTime)<= dateadd(day,-60,getdate())
1.《怎么使用sqlserver?終于找到答案了組態(tài)軟件,使用SqlServer做報表的一種方式》援引自互聯(lián)網(wǎng),旨在傳遞更多網(wǎng)絡(luò)信息知識,僅代表作者本人觀點,與本網(wǎng)站無關(guān),侵刪請聯(lián)系頁腳下方聯(lián)系方式。
2.《怎么使用sqlserver?終于找到答案了組態(tài)軟件,使用SqlServer做報表的一種方式》僅供讀者參考,本網(wǎng)站未對該內(nèi)容進行證實,對其原創(chuàng)性、真實性、完整性、及時性不作任何保證。
3.文章轉(zhuǎn)載時請保留本站內(nèi)容來源地址,http://f99ss.com/gl/3060399.html