行转列
时间:2020-02-18 访问量:1174
1、access(固定列)—殡仪馆管理系统(放在SQL方式计算与验证,带参数)
select * from
(select sum(金额) as 遗体袋 from 收费明细
where 接尸单号=':接尸单号'and 费用项目='遗体袋') as A,
(select sum(金额) as 火化费 from 收费明细
where 接尸单号='接尸单号'and 费用项目='火化费') as B
2、sqlserver(固定列)—设备借用管理系统(放在SQL方式计算与验证,带参数)
SELECT 接收机 = stuff((
SELECT ',' + 机器编号
FROM 借用明细
WHERE 类别='接收机' and 借用编号=':借用编号'
FOR XML path('')
), 1, 1, '')
FROM 借用明细 a
where 类别='接收机' and 借用编号=':借用编号'
GROUP BY 类别
3、sqlserver(加其他条件)—欣然培训班(节点带参数)
declare @sql varchar(8000)
select
@sql=isnull(@sql+',','')
+'sum(case when convert(varchar(10),上课日期,120)='''+convert(varchar(10),上课日期,120)+''' then
(case when 考勤=1 then 1 else null end) else null end) as ['+convert(varchar(10),上课日期,120)+']'
from
(select distinct convert(varchar(10),上课日期,120) as 上课日期 FROM 学员上课明细 where 状态='√' and 学期=':学期' and 班级名称=':班级名称' and 星期=':星期' ) as A
order by convert(varchar(10),上课日期,120)
exec('select 学员姓名,学期,班级名称,星期,'
+@sql
+' from 学员上课明细 where 学期='':学期'' and 班级名称='':班级名称'' and 星期='':星期'' group by 学员姓名,学期,班级名称,星期'
)
4、sqlite 固定列 设备借用管理系统(放在SQL方式计算与验证,带参数)
select (select GROUP_CONCAT(机器编号)
from 借用明细 where 借用编号=':借用编号' and 类别='接收机' ) as 接收编号,
(select GROUP_CONCAT(机器编号)
from 借用明细 where 借用编号=':借用编号' and 类别='发射机' ) as 发射编号,
(select GROUP_CONCAT(机器编号)
from 借用明细 where 借用编号=':借用编号' and 类别='充电箱' ) as 充电编号
5、sqlite (行合并在一列,同表 )—松鹤陵园
select 墓穴编号,姓名,性别,出生日期,逝世日期 from
(select 墓穴编号,GROUP_CONCAT(故者姓名) as 姓名,GROUP_CONCAT(性别) as 性别
,GROUP_CONCAT(出生日期) as 出生日期,GROUP_CONCAT(逝世日期) as 逝世日期
from 故者信息
group by 墓穴编号) as ah