统计与汇总1

转载注明出错,谢谢!!

--(1)数据统计与汇总 declare @Tb table ( ID int identity(1,1) primary key, EmpoyeeName varchar(50), Salary int ) insert into @Tb select 'aa',1200 union all select 'bb',1300 union all select 'cc',2400 union all select 'bb',900 union all select 'bb',1800 union all select 'cc',700 union all select 'aa',600 select EmpoyeeName, sum(Salary) Salary from @Tb where Salary>1000 group by EmpoyeeName --派生数据 --部门表 declare @Dept table ( ID int identity(1,1) primary key, DepartMent nvarchar(20) ) insert into @Dept select 'A部门' insert into @Dept select 'B部门' insert into @Dept select 'C部门' --员工表 declare @Employee table ( ID int identity(1,1) primary key, [Name] varchar(20), DeptID int--与部门表中的ID相关联 ) insert into @Employee select '胡果',1 insert into @Employee select '小梁',1 insert into @Employee select '近身剪',2 insert into @Employee select '树哥',3 --订单表 declare @Order table ( ID int identity(1,1) primary key, EmployeeID int,--与员工表中的ID相关联 SalePrice decimal(10,2), Date datetime ) insert into @Order select 1,1000.00,'2009-1-1' union all select 1,900.00,'2009-3-1' union all select 2,800.00,'2009-3-8' union all select 2,700.00,'2009-3-18' union all select 3,1200.00,'2009-3-10' union all select 3,1200.00,'2009-4-10' union all select 3,600.00,'2009-5-1' union all select 4,900.00,'2009-1-18' union all select 4,900.00,'2009-2-18' union all select 4,900.00,'2009-4-18' union all select 4,600.00,'2009-5-11' ;with hgo as ( select D.DepartMent,Convert(varchar(6),dateadd(month,D.[month],'20081201'),112) 'Month', isnull(sum(O.SalePrice),0) SalePrice from ( select D.ID,D.DepartMent,M.[month] from @Dept D cross join ( select [month]=1 union all select [month]=2 union all select [month]=3 union all select [month]=4 union all select [month]=5 union all select [month]=6 )M ) D left join ( select E.DeptID,O.SalePrice,O.Date from @Employee E join @Order O on E.ID=O.EmployeeID ) O on O.DeptID=D.ID and O.Date>=dateadd(month,D.[month],'20081201') and O.Date<dateadd(month,D.[month],'20090101') group by D.DepartMent,D.[month] ) select DepartMent, sum(case when Month='200901' then SalePrice else 0 end) '一月份销售', sum(case when Month='200902' then SalePrice else 0 end) '二月份销售', sum(case when Month='200903' then SalePrice else 0 end) '三月份销售', sum(case when Month='200904' then SalePrice else 0 end) '四月份销售', sum(case when Month='200905' then SalePrice else 0 end) '五月份销售', sum(case when Month='200906' then SalePrice else 0 end) '六月份销售', sum(SalePrice) '上半年总业绩' from hgo group by DepartMent /* DepartMent 一月份销售 二月份销售 三月份销售 四月份销售 五月份销售 六月份销售 上半年总业绩 --------- ---------- -------- ---------- ----------- ---------- ---------- ------------ A部门 1000.00 0.00 2400.00 0.00 0.00 0.00 3400.00 B部门 0.00 0.00 1200.00 1200.00 600.00 0.00 3000.00 C部门 900.00 900.00 0.00 900.00 600.00 0.00 3300.00 */ --(2)使用union all declare @TB table ( Item varchar(20), Color varchar(20), Quantity int ) insert into @TB select 'Table','Blue',124 union all select 'Table','Red',-23 union all select 'Chair','Blue',101 union all select 'Chair','Red',91 select Item,Color,Quantity from @TB where Item='Table' union all select '',Item as 'TableTotal',sum(Quantity) as TableQua from @TB where Item='Table'group by Item union all select Item,Color,Quantity from @TB where Item='Chair' union all select '',Item as 'ChairTotal',sum(Quantity) as TableQua from @TB where Item='Chair'group by Item union all select 'Total','',sum(Quantity) as Quantity from @TB --Results /* Item Color Quantity -------------------- -------------------- ----------- Table Blue 124 Table Red -23 Table 101 Chair Blue 101 Chair Red 91 Chair 192 Total 293 (7 行受影响) */ --使用with RollUp处理上述问题 declare @TB table ( Item varchar(20), Color varchar(20), Quantity int ) insert into @TB select 'Table','Blue',124 union all select 'Table','Red',-23 union all select 'Chair','Blue',101 union all select 'Chair','Red',91 select Item,Color,sum(Quantity) Quantity from @TB group by Item,Color with rollup --Results /* Item Color Quantity -------------------- -------------------- ----------- Chair Blue 101 Chair Red 91 Chair NULL 192 Table Blue 124 Table Red -23 Table NULL 101 NULL NULL 293 */ --比较Union all与with RollUP /*(1)union all 对每级的汇总都必须单独的处理,最后才生成结果;而RollUP的分级汇总是MSSQL内部直接处理的,所以效率比Union all高 (2)RollUP固定对Group by子句中的字段进行汇总,而Union all可以针对某个级别进行汇总 (3)从上面的两个结果可以看出,使用Union all的文字说明比RolUP更为方便 */ --(3)分级汇总过滤 /*由于RollUP产生的合计,小计汇总都可以通过Grouping函数来判读 */ declare @T table ( groups char(10), Item varchar(10), Color varchar(10), Quantity int ) insert into @T select 'aa','table','blue',124 union all select 'bb','table','red',-23 union all select 'bb','cup','green',-23 union all select 'aa','chair','blue',101 union all select 'aa','chair','red',-90 select groups,Item,color,sum(Quantity) Quantity, GP_groups=grouping(groups), GP_Item=grouping(Item), GP_Color=grouping(Color) from @T group by groups,Item,color with rollup having grouping(Item)=1 and grouping(groups)=0 /* groups Item color Quantity GP_groups GP_Item GP_Color ---------- ---------- ---------- ----------- --------- ------- -------- aa chair blue 101 0 0 0 aa chair red -90 0 0 0 aa chair NULL 11 0 0 1 aa table blue 124 0 0 0 aa table NULL 124 0 0 1 aa NULL NULL 135 0 1 1 bb cup green -23 0 0 0 bb cup NULL -23 0 0 1 bb table red -23 0 0 0 bb table NULL -23 0 0 1 bb NULL NULL -46 0 1 1 NULL NULL NULL 89 1 1 1 */ /* 观看GP_groups,GP_Item,GP_Color以上数据可以看出当结果为1的时候表示小计数据 为0的时候表示聚合行数据 */ --下面加上having grouping(Item)=1 and grouping(groups)=0在看 select groups,Item,color,sum(Quantity) Quantity, GP_groups=grouping(groups), GP_Item=grouping(Item), GP_Color=grouping(Color) from @T group by groups,Item,color with rollup having grouping(Item)=1 and grouping(groups)=0 /* groups Item color Quantity GP_groups GP_Item GP_Color ---------- ---------- ---------- ----------- --------- ------- -------- aa NULL NULL 135 0 1 1 bb NULL NULL -46 0 1 1 */ /* 现在我们要用with rollup实现分级汇总结果显示格式 可以先看下3实例分级汇总过滤,在查询的时候实现grouping(列名) */ declare @T table ( groups char(10), Item varchar(10), Color varchar(10), Quantity int ) insert into @T select 'aa','table','blue',124 union all select 'bb','table','red',-23 union all select 'bb','cup','green',-23 union all select 'aa','chair','blue',101 union all select 'aa','chair','red',-90 --select groups,Item,color,sum(Quantity) Quantity, -- grouping(groups) gp, -- grouping(Item) Item, -- grouping(Color) Color --from @T group by groups,Item,Color with rollup select case when grouping(groups)=1 then '总计' else groups end as 'groups', isnull(case when grouping(groups)=0 and grouping(Item)=1 then N'小计'+groups else Item end,'') as 'Item', isnull(case when grouping(groups)=0 and grouping(Color)=1 and grouping(Item)=0 then Item+N'小计' else Color end,'') as 'Color', sum(Quantity) Quantity from @T group by groups,Item,Color with rollup /* groups Item Color Quantity ---------- ------------ ------------ ----------- aa chair blue 101 aa chair red -90 aa chair chair小计 11 aa table blue 124 aa table table小计 124 aa 小计aa 135 bb cup green -23 bb cup cup小计 -23 bb table red -23 bb table table小计 -23 bb 小计bb -46 总计 89 */

跟网型逆变器小干扰稳定性分析控制策略优化研究(Simulink仿真实现)内容概要:本文围绕跟网型逆变器的小干扰稳定性展开分析,重点研究其在电力系统中的动态响应特性及控制策略优化问题。通过构建基于Simulink的仿真模型,对逆变器在不同工况下的小信号稳定性进行建模分析,识别系统可能存在的振荡风险,并提出相应的控制优化方法以提升系统稳定性和动态性能。研究内容涵盖数学建模、稳定性判据分析、控制器设计参数优化,并结合仿真验证所提策略的有效性,为新能源并网系统的稳定运行提供理论支持和技术参考。; 适合人群:具备电力电子、自动控制或电力系统相关背景,熟悉Matlab/Simulink仿真工具,从事新能源并网、微电网或电力系统稳定性研究的研究生、科研人员及工程技术人员。; 使用场景及目标:① 分析跟网型逆变器在弱电网条件下的小干扰稳定性问题;② 设计并优化逆变器外环内环控制器以提升系统阻尼特性;③ 利用Simulink搭建仿真模型验证理论分析控制策略的有效性;④ 支持科研论文撰写、课题研究或工程项目中的稳定性评估改进。; 阅读建议:建议读者结合文中提供的Simulink仿真模型,深入理解状态空间建模、特征值分析及控制器设计过程,重点关注控制参数变化对系统极点分布的影响,并通过动手仿真加深对小干扰稳定性机理的认识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值