行转列吧应该可以的 select max(No) as '编号', Name as '姓名', max(case type when 2 then sum else 0 end ) as '类型2', max(case type when 3 then sum else 0 end ) as '类型3', max(Month) as '月份', max(OpDate) as '录入时间' from tableName group by Name 数据量大可能会影响效率,谨慎使用吧
我现在用sql 查询出来了列头如下: No Name Month OpData 类型1 类型2 类型3 类型N我现在在后台怎么循环取 类型1到类型N字段里的值?
我现在用sql 查询出来了列头如下: No Name Month OpData 类型1 类型2 类型3 类型N我现在在后台怎么循环取 类型1到类型N字段里的值?
我现在用sql 查询出来了列头如下: No Name Month OpData 类型1 类型2 类型3 类型N我现在在后台怎么循环取 类型1到类型N字段里的值? 需要把类型动态起来,不能写死了
---查询--- declare @sql varchar(max) select @sql=isnull(@sql+',','') +'Max(case when [type]='''+[type]+''' then [SUM] else 0 end) as [类型'+[type]+']' from (select distinct [type] from [Test]) t set @sql='select [No] as 编号,[Name] as 姓名, '+@sql+',convert(varchar(10),Month,120) as 月份,convert(varchar(10),OpData,120) as 录入时间 from [Test] group by [No],[Name],convert(varchar(10),Month,120),convert(varchar(10),OpData,120)' exec (@sql)
case type when 2 then sum else 0 end 这句是什么意思? 可以理解为 当 type =2 时候取 sum的值,显然是30
select
max(No) as '编号',
Name as '姓名',
max(case type when 2 then sum else 0 end ) as '类型2',
max(case type when 3 then sum else 0 end ) as '类型3',
max(Month) as '月份',
max(OpDate) as '录入时间'
from tableName group by Name
数据量大可能会影响效率,谨慎使用吧
case type when 2 then sum else 0 end
这句是什么意思?
sql别人写的 我现在获取到的数据是这种样子的
我放在了 DataSet
现在怎么在后台实现这个
No Name Month OpData 类型1 类型2 类型3 类型N我现在在后台怎么循环取 类型1到类型N字段里的值?
我现在用sql 查询出来了列头如下:
No Name Month OpData 类型1 类型2 类型3 类型N我现在在后台怎么循环取 类型1到类型N字段里的值?
我现在用sql 查询出来了列头如下:
No Name Month OpData 类型1 类型2 类型3 类型N我现在在后台怎么循环取 类型1到类型N字段里的值?
需要把类型动态起来,不能写死了
declare @sql varchar(max)
select
@sql=isnull(@sql+',','')
+'Max(case when [type]='''+[type]+''' then [SUM] else 0 end) as [类型'+[type]+']'
from (select distinct [type] from [Test]) t
set @sql='select [No] as 编号,[Name] as 姓名, '+@sql+',convert(varchar(10),Month,120) as 月份,convert(varchar(10),OpData,120) as 录入时间 from [Test] group by [No],[Name],convert(varchar(10),Month,120),convert(varchar(10),OpData,120)'
exec (@sql)
case type when 2 then sum else 0 end
这句是什么意思?
可以理解为 当 type =2 时候取 sum的值,显然是30
有没有办法循环取值?
把上面直接当sql语句执行,返回一个DataTable
得到了DataTable,你还不会呈现到前台么?
dr["列名"]="你的值";
for(int i = 0; i < dt.Rows.Count-1; i++)
{
for (int j = 0; j <dt.Columns.Count-1; j++)
{
dt.Rows[i][j]
}}遍历不行?