有一张EnumType 枚举表
EnumTypeIDEnumTypeNameEnumTypeValue
101停机 101
102关机 102
103占线 103
104不配合 104
105其他 105ExceptionInfo 产生异常信息表
IDtelExceptionStatus
1101
2102
3 102
4 101
5 102
6 103显示如下--(假设的 比例是总量除以生成数量)
不成功原因
停机 关机 占线 不配合 其他
数量 比例 数量 比例 数量 比例 数量 比例 数量 比例
19 6.96% 80 29.30% 13 4.76% 13 4.76% 2 0.73%
EnumTypeIDEnumTypeNameEnumTypeValue
101停机 101
102关机 102
103占线 103
104不配合 104
105其他 105ExceptionInfo 产生异常信息表
IDtelExceptionStatus
1101
2102
3 102
4 101
5 102
6 103显示如下--(假设的 比例是总量除以生成数量)
不成功原因
停机 关机 占线 不配合 其他
数量 比例 数量 比例 数量 比例 数量 比例 数量 比例
19 6.96% 80 29.30% 13 4.76% 13 4.76% 2 0.73%
解决方案 »
- 页面在不同浏览器中的外观不一致问题应该如何对待?
- 救命阿,asp.net 在设计那,不知点了什么和按了什么
- 截图来看,ASP.NET为什么无法获取Cookie
- SQL语句中的运算函数是什么? 比如2*3
- 页面上Cache的问题
- 我想把一个产品结构树呈现出来 怎么做?
- 关于NullReferenceException的问题
- 好心人进来看看,问个ActiveX问题
- 求两正则:提取域名与过滤.jp域名?
- 小问题:Response.Redirect如何跳出框架?
- asp.net中DropDownList的问题??
- Ajax自带的Calendar和text封闭成一个日期控件和Dropdownlist冲突??(刚给分了但没贴上?)
比例可在显示的时候计算.
交叉数据报表
有时候需要旋转结果以便在水平方向显示列,而在垂直方向显示行。这就是所谓的创建 PivotTable®、创建交叉数据报表或旋转数据。假定有一个表 Pivot,其中每季度占一行。对 Pivot 的 SELECT 操作在垂直方向上列出这些季度:Year Quarter Amount
---- ------- ------
1990 1 1.1
1990 2 1.2
1990 3 1.3
1990 4 1.4
1991 1 2.1
1991 2 2.2
1991 3 2.3
1991 4 2.4生成报表的表必须是这样的,其中每年占一行,每个季度的数值显示在一个单独的列中,如:Year
Q1
Q2
Q3
Q4
1990
1.1
1.2
1.3
1.4
1991
2.1
2.2
2.3
2.4
下面的语句用于创建 Pivot 表并在其中填入第一个表中的数据:USE Northwind
GOCREATE TABLE Pivot
( Year SMALLINT,
Quarter TINYINT,
Amount DECIMAL(2,1) )
GO
INSERT INTO Pivot VALUES (1990, 1, 1.1)
INSERT INTO Pivot VALUES (1990, 2, 1.2)
INSERT INTO Pivot VALUES (1990, 3, 1.3)
INSERT INTO Pivot VALUES (1990, 4, 1.4)
INSERT INTO Pivot VALUES (1991, 1, 2.1)
INSERT INTO Pivot VALUES (1991, 2, 2.2)
INSERT INTO Pivot VALUES (1991, 3, 2.3)
INSERT INTO Pivot VALUES (1991, 4, 2.4)
GO下面是用于创建旋转结果的 SELECT 语句:SELECT Year,
SUM(CASE Quarter WHEN 1 THEN Amount ELSE 0 END) AS Q1,
SUM(CASE Quarter WHEN 2 THEN Amount ELSE 0 END) AS Q2,
SUM(CASE Quarter WHEN 3 THEN Amount ELSE 0 END) AS Q3,
SUM(CASE Quarter WHEN 4 THEN Amount ELSE 0 END) AS Q4
FROM Northwind.dbo.Pivot
GROUP BY Year
GO该 SELECT 语句还处理其中每个季度占多行的表。GROUP BY 语句将 Pivot 中一年的所有行合并成一行输出。当执行分组操作时,SUM 聚合中的 CASE 函数的应用方式是这样的:将每季度的 Amount 值添加到结果集的适当列中,在其它季度的结果集列中添加 0。如果该 SELECT 语句的结果用作电子表格的输入,那么电子表格将很容易计算每年的合计。当从应用程序使用 SELECT 时,可能更易于增强 SELECT 语句来计算每年的合计。例如:SELECT P1.*, (P1.Q1 + P1.Q2 + P1.Q3 + P1.Q4) AS YearTotal
FROM (SELECT Year,
SUM(CASE P.Quarter WHEN 1 THEN P.Amount ELSE 0 END) AS Q1,
SUM(CASE P.Quarter WHEN 2 THEN P.Amount ELSE 0 END) AS Q2,
SUM(CASE P.Quarter WHEN 3 THEN P.Amount ELSE 0 END) AS Q3,
SUM(CASE P.Quarter WHEN 4 THEN P.Amount ELSE 0 END) AS Q4
FROM Pivot AS P
GROUP BY P.Year) AS P1
GO
insert into ta select 101,'停机',101
insert into ta select 102,'关机',102
insert into ta select 103,'占线',103
insert into ta select 104,'不配合',104
insert into ta select 105,'其他',105create table tb(ID int,telExceptionStatus int)
insert into tb select 1,101
insert into tb select 2,102
insert into tb select 3,103
insert into tb select 4,104
insert into tb select 5,105
insert into tb select 6,106select
sum(case when EnumTypeName='停机' then 1 else 0 end) as '停机数量',
ltrim(cast(sum(case when EnumTypeName='停机' then EnumTypeValue else 0 end)*100.0/sum(EnumTypeValue) as decimal(18,2)))+'%' as '停机比例',sum(case when EnumTypeName='关机' then 1 else 0 end) as '关机数量',
ltrim(cast(sum(case when EnumTypeName='关机' then EnumTypeValue else 0 end)*100.0/sum(EnumTypeValue) as decimal(18,2)))+'%' as '关机比例',sum(case when EnumTypeName='占线' then 1 else 0 end) as '占线数量',
ltrim(cast(sum(case when EnumTypeName='占线' then EnumTypeValue else 0 end)*100.0/sum(EnumTypeValue) as decimal(18,2)))+'%' as '占线比例',sum(case when EnumTypeName='不配合' then 1 else 0 end) as '不配合数量',
ltrim(cast(sum(case when EnumTypeName='不配合' then EnumTypeValue else 0 end)*100.0/sum(EnumTypeValue) as decimal(18,2)))+'%' as '不配合比例',sum(case when EnumTypeName='其他' then 1 else 0 end) as '其他数量',
ltrim(cast(sum(case when EnumTypeName='其他' then EnumTypeValue else 0 end)*100.0/sum(EnumTypeValue) as decimal(18,2)))+'%' as '其他比例'
from ta a join tb b on a.EnumTypeID=b.telExceptionStatus停机数量 停机比例 关机数量 关机比例 占线数量 占线比例 不配合数量 不配合比例 其他数量 其他比例
1 19.61% 1 19.81% 1 20.00% 1 20.19% 1 20.39%
declare @ta table (EnumTypeID int,EnumTypeName varchar(50),EnumTypeValue int)
insert into @ta select 101,'停机',101
insert into @ta select 102,'关机',102
insert into @ta select 103,'占线',103
insert into @ta select 104,'不配合',104
insert into @ta select 105,'其他',105declare @tb table(ID int,telExceptionStatus int)
insert into @tb select 1,101
insert into @tb select 2,102
insert into @tb select 3,103
insert into @tb select 4,104
insert into @tb select 5,105
insert into @tb select 6,101select sum(case when EnumTypeName='停机' then 1 else 0 end) as '停机数量',
ltrim(cast(sum(case when EnumTypeName='停机' then 1 else 0 end)*100.0/count(EnumTypeValue) as decimal(18,2)))+'%' as '停机比例',sum(case when EnumTypeName='关机' then 1 else 0 end) as '关机数量',
ltrim(cast(sum(case when EnumTypeName='关机' then 1 else 0 end)*100.0/count(EnumTypeValue) as decimal(18,2)))+'%' as '关机比例',sum(case when EnumTypeName='占线' then 1 else 0 end) as '占线数量',
ltrim(cast(sum(case when EnumTypeName='占线' then 1 else 0 end)*100.0/count(EnumTypeValue) as decimal(18,2)))+'%' as '占线比例',sum(case when EnumTypeName='不配合' then 1 else 0 end) as '不配合数量',
ltrim(cast(sum(case when EnumTypeName='不配合' then 1 else 0 end)*100.0/count(EnumTypeValue) as decimal(18,2)))+'%' as '不配合比例',sum(case when EnumTypeName='其他' then 1 else 0 end) as '其他数量',
ltrim(cast(sum(case when EnumTypeName='其他' then 1 else 0 end)*100.0/count(EnumTypeValue) as decimal(18,2)))+'%' as '其他比例'
from @ta a join @tb b on a.EnumTypeID=b.telExceptionStatus停机数量 停机比例 关机数量 关机比例 占线数量 占线比例 不配合数量 不配合比例 其他数量 其他比例
2 33.33% 1 16.67% 1 16.67% 1 16.67% 1 16.67%
drop table ta
go
if object_id('tb') is not null
drop table tb
go
create table ta(EnumTypeID int,EnumTypeName varchar(50),EnumTypeValue int)
insert into ta select 101,'停机',101
insert into ta select 102,'关机',102
insert into ta select 103,'占线',103
insert into ta select 104,'不配合',104
insert into ta select 105,'其他',105create table tb(ID int,telExceptionStatus int)
insert into tb select 1,101
insert into tb select 2,102
select
sum(case when EnumTypeName='停机' then 1 else 0 end) as '停机数量',
ltrim(cast(sum(case when EnumTypeName='停机' then 1 else 0 end)*100.0/count(EnumTypeValue) as decimal(18,2)))+'%' as '停机比例',sum(case when EnumTypeName='关机' then 1 else 0 end) as '关机数量',
ltrim(cast(sum(case when EnumTypeName='关机' then 1 else 0 end)*100.0/count(EnumTypeValue) as decimal(18,2)))+'%' as '关机比例',sum(case when EnumTypeName='占线' then 1 else 0 end) as '占线数量',
ltrim(cast(sum(case when EnumTypeName='占线' then 1 else 0 end)*100.0/count(EnumTypeValue) as decimal(18,2)))+'%' as '占线比例',sum(case when EnumTypeName='不配合' then 1 else 0 end) as '不配合数量',
ltrim(cast(sum(case when EnumTypeName='不配合' then 1 else 0 end)*100.0/count(EnumTypeValue) as decimal(18,2)))+'%' as '不配合比例',sum(case when EnumTypeName='其他' then 1 else 0 end) as '其他数量',
ltrim(cast(sum(case when EnumTypeName='其他' then 1 else 0 end)*100.0/count(EnumTypeValue) as decimal(18,2)))+'%' as '其他比例'
from ta a join tb b on a.EnumTypeID=b.telExceptionStatus
停机数量 停机比例 关机数量 关机比例 占线数量 占线比例 不配合数量 不配合比例 其他数量 其他比例
1 50.00% 1 50.00% 0 0.00% 0 0.00% 0 0.00%