C# Winform 使用SUM CASE WHEN实现动态列交叉数据报表
C# Winform 使用SUM CASE WHEN实现动态列交叉数据报表
扫一扫加作者微信
使用SUM CASE WHEN实现动态列交叉报表
a) 根据查询条件返回不同字段的结果数据。
b) 根据查询条件自动创建表格的列。
1. 按年度生成动态列的SQL脚本
SQL Code:
SELECT Account,OperationType,
SUM(CASE WHEN YEAR(OperationTime)=2015 THEN 1 ELSE 0 END) AS Y2015,
SUM(CASE WHEN YEAR(OperationTime)= 2016 THEN 1 ELSE 0 END) AS Y2016,
SUM(CASE WHEN YEAR(OperationTime)= 2017 THEN 1 ELSE 0 END) AS Y2017,
SUM(CASE WHEN YEAR(OperationTime)= 2018 THEN 1 ELSE 0 END) AS Y2018,
SUM(CASE WHEN YEAR(OperationTime)= 2019 THEN 1 ELSE 0 END) AS Y2019,
SUM(CASE WHEN YEAR(OperationTime)= 2020 THEN 1 ELSE 0 END) AS Y2020,
SUM(CASE WHEN YEAR(OperationTime)= 2021 THEN 1 ELSE 0 END) AS Y2021
FROM sys_LogOperation
GROUP BY Account,OperationType;
//来源:C/S框架网 | www.csframework.com | QQ:23404761
SUM(CASE WHEN YEAR(OperationTime)=2015 THEN 1 ELSE 0 END) AS Y2015,
SUM(CASE WHEN YEAR(OperationTime)= 2016 THEN 1 ELSE 0 END) AS Y2016,
SUM(CASE WHEN YEAR(OperationTime)= 2017 THEN 1 ELSE 0 END) AS Y2017,
SUM(CASE WHEN YEAR(OperationTime)= 2018 THEN 1 ELSE 0 END) AS Y2018,
SUM(CASE WHEN YEAR(OperationTime)= 2019 THEN 1 ELSE 0 END) AS Y2019,
SUM(CASE WHEN YEAR(OperationTime)= 2020 THEN 1 ELSE 0 END) AS Y2020,
SUM(CASE WHEN YEAR(OperationTime)= 2021 THEN 1 ELSE 0 END) AS Y2021
FROM sys_LogOperation
GROUP BY Account,OperationType;
//来源:C/S框架网 | www.csframework.com | QQ:23404761
2. 按固定项目生成动态列的SQL脚本
SQL Code:
SELECT YEAR(OperationTime) AS YearItem,
SUM(CASE WHEN OperationType = 'OpenForm' THEN 1 ELSE 0 END) AS OpenForm,
SUM(CASE WHEN OperationType = 'Exception' THEN 1 ELSE 0 END) AS Exception,
SUM(CASE WHEN OperationType = 'CustomError' THEN 1 ELSE 0 END) AS CustomError
FROM sys_LogOperation
GROUP BY YEAR(OperationTime);
//来源:C/S框架网 | www.csframework.com | QQ:23404761
SUM(CASE WHEN OperationType = 'OpenForm' THEN 1 ELSE 0 END) AS OpenForm,
SUM(CASE WHEN OperationType = 'Exception' THEN 1 ELSE 0 END) AS Exception,
SUM(CASE WHEN OperationType = 'CustomError' THEN 1 ELSE 0 END) AS CustomError
FROM sys_LogOperation
GROUP BY YEAR(OperationTime);
//来源:C/S框架网 | www.csframework.com | QQ:23404761
3. 界面设计参考
4. 查询结果,动态创建列
C# Code:
/// <summary>
/// 动态窗体GridColumn实例
/// </summary>
/// <param name="years"></param>
private void CreateDynamicColumns(string[] years)
{
//清除所有列
gridView1.Columns.Clear();
//添加默认列OperationType,Account,左侧固定
gridView1.Columns.Add(colAccount);
gridView1.Columns.Add(colType);
colAccount.VisibleIndex = 0;
colType.VisibleIndex = 1;
int v = 2;
foreach (var year in years)
{
GridColumn col = new GridColumn();
col.Name = "col" + year;
col.Caption = year;
col.FieldName = "Y" + year;//字段名
col.Width = 100;
col.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;//数字合计
col.VisibleIndex = v;
col.Visible = true;
gridView1.Columns.Add(col);
v++;
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
/// <summary>
/// 动态窗体GridColumn实例
/// </summary>
/// <param name="years"></param>
private void CreateDynamicColumns(string[] years)
{
//清除所有列
gridView1.Columns.Clear();
//添加默认列OperationType,Account,左侧固定
gridView1.Columns.Add(colAccount);
gridView1.Columns.Add(colType);
colAccount.VisibleIndex = 0;
colType.VisibleIndex = 1;
int v = 2;
foreach (var year in years)
{
GridColumn col = new GridColumn();
col.Name = "col" + year;
col.Caption = year;
col.FieldName = "Y" + year;//字段名
col.Width = 100;
col.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;//数字合计
col.VisibleIndex = v;
col.Visible = true;
gridView1.Columns.Add(col);
v++;
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
5. 演示界面源码位置
CSFrameworkV5-ClientFoundation
\CSFrameworkV5.Report.frmDynamicReport
6. DAL层源码
C# Code:
public DataSet GetDynamicReport(string[] years)
{
//组合SQL1
StringBuilder sb = new StringBuilder();
sb.AppendLine($"SELECT Account,OperationType, ");
for (int i = 0; i <= years.Length - 1; i++)
{
string year = years[i].Trim();//必须去空格!
string item = $" SUM(CASE WHEN YEAR(OperationTime)={year} THEN 1 ELSE 0 END) AS Y{year} ";
if (i < years.Length - 1) item += ", ";
sb.AppendLine(item);
}
sb.AppendLine("FROM sys_LogOperation ");
sb.AppendLine("GROUP BY Account,OperationType; ");
string sql1 = sb.ToString();
//组合SQL2
string sql2 = "SELECT YEAR(OperationTime) AS YearItem, " +
" SUM(CASE WHEN OperationType = 'OpenForm' THEN 1 ELSE 0 END) AS OpenForm, " +
" SUM(CASE WHEN OperationType = 'Exception' THEN 1 ELSE 0 END) AS Exception, " +
" SUM(CASE WHEN OperationType = 'CustomError' THEN 1 ELSE 0 END) AS CustomError " +
"FROM sys_LogOperation " +
"GROUP BY YEAR(OperationTime); ";
DataSet ds = DatabaseProvider.SystemDatabase.GetDataSet(sql1 + sql2);
return ds;
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
public DataSet GetDynamicReport(string[] years)
{
//组合SQL1
StringBuilder sb = new StringBuilder();
sb.AppendLine($"SELECT Account,OperationType, ");
for (int i = 0; i <= years.Length - 1; i++)
{
string year = years[i].Trim();//必须去空格!
string item = $" SUM(CASE WHEN YEAR(OperationTime)={year} THEN 1 ELSE 0 END) AS Y{year} ";
if (i < years.Length - 1) item += ", ";
sb.AppendLine(item);
}
sb.AppendLine("FROM sys_LogOperation ");
sb.AppendLine("GROUP BY Account,OperationType; ");
string sql1 = sb.ToString();
//组合SQL2
string sql2 = "SELECT YEAR(OperationTime) AS YearItem, " +
" SUM(CASE WHEN OperationType = 'OpenForm' THEN 1 ELSE 0 END) AS OpenForm, " +
" SUM(CASE WHEN OperationType = 'Exception' THEN 1 ELSE 0 END) AS Exception, " +
" SUM(CASE WHEN OperationType = 'CustomError' THEN 1 ELSE 0 END) AS CustomError " +
"FROM sys_LogOperation " +
"GROUP BY YEAR(OperationTime); ";
DataSet ds = DatabaseProvider.SystemDatabase.GetDataSet(sql1 + sql2);
return ds;
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
扫一扫加作者微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网