(select deptName, case status when '状态1' then 1 else 0 end as status1, case status when '状态2' then 1 else 0 end as status2, case status when '状态3' then 1 else 0 end as status3, case status when '状态4' then 1 else 0 end as status4 from tableName where datepart(y, date)=2011 and datepart(m, date)=1) ta group by tadeptName
月份需要加上年份一起判断, 合计不能从这条语句中得到,可以通过另外一条语句或者通过程序中对数据分析得到,不知道你前台用什么开发的,是不是有控件可以直接生成
select count() as recordCount, sum(tbstatus1) as status1, sum(tbstatus2) as status2, sum(tbstatus3) as status3, sum(tbstatus4) as status4 from (select tadeptName, count(tadeptName) as personCount, sum(tastatus1) as status1, sum(tastatus2) as status2, sum(tastatus3) as status3, sum(tastatus4) as status4 from
(select deptName, case status when '状态1' then 1 else 0 end as status1, case status when '状态2' then 1 else 0 end as status2, case status when '状态3' then 1 else 0 end as status3, case status when '状态4' then 1 else 0 end as status4 from tableName where datepart(y, date)=2011 and datepart(m, date)=1) ta group by tadeptName) tb
这条语句可以得到合计值你这个如果是oracle或者支持伪列的SQL,可以这样做
SELECT 数量, 金额, FROM (SELECT 数量, 金额, rownum AS rk from tab) WHERE rk BETWEEN 0 AND 200
UNION ALL
SELECT 数量, 金额, FROM (SELECT 数量, 金额, rownum AS rk from tab) WHERE rk > 200 AND rk <= 400
UNION ALL
SELECT 数量, 金额, FROM (SELECT 数量, 金额, rownum AS rk from tab) WHERE rk > 400 AND rk <= 600
UNION ALL
SELECT 数量, 金额, FROM (SELECT 数量, 金额, rownum AS rk from tab) WHERE rk > 600 AND rk <= 800
UNION ALL
SELECT 数量, 金额, FROM (SELECT 数量, 金额, rownum AS rk from tab) WHERE rk > 800 AND rk <= 1000
但是这样效率比较低下。建议用存储过程来做。
存储过程,定义游标,然后通过读取每一行,然后做统计,如果够200行,就sum假如:这些数据在一张名为:Test 的数据表中
select from Test
union
select 5, '合计' , SUM(C) , SUM(D) from Test
其中:先把表中的所有数据查询出来,然后按照表的结构,select 主键列,'合计',SUM(C),SUM(D) from 数据表 使用SQL的数学函数 动态添加一行数据,最后使用 union 关键字 连接两次查询的结果。
--count(cproId) 也可能换成 未显示的字段 sum(cqty) 数量之和
select aspecId,aspecName,count(cproId) as '销量'from productSpec a inner join product b on aspecId=bspecId
inner join order c on bproId=cproId
where createTime<=dateadd(month,-1,getdate())
group by aspecId,aspecName
union
select aspecId,aspecName,count(cproId) as '销量'
from productSpec a inner join product b on aspecId=bspecId
inner join order c on bproId=cproId
where createTime<=dateadd(month,-2,getdate())
group by aspecId,aspecName
union
select aspecId,aspecName,count(cproId) as '销量'
from productSpec a inner join product b on aspecId=bspecId
inner join order c on bproId=cproId
where createTime<=dateadd(month,-3,getdate())
group by aspecId,aspecName
--或者
select t1specId,t1specName,
t1[1个月前的销量],t2[2个月前的销量],t3[3个月前的销量] from
(select aspecId,aspecName,count(cproId) as '1个月前的销量'
from productSpec a inner join product b on aspecId=bspecId
inner join order c on bproId=cproId
where createTime<=dateadd(month,-1,getdate())
group by aspecId,aspecName)t1,
(select aspecId,aspecName,count(cproId) as '2个月前的销量'
from productSpec a inner join product b on aspecId=bspecId
inner join order c on bproId=cproId
where createTime<=dateadd(month,-2,getdate())
group by aspecId,aspecName)t2,
(select aspecId,aspecName,count(cproId) as '3个月前的销量'
from productSpec a inner join product b on aspecId=bspecId
inner join order c on bproId=cproId
where createTime<=dateadd(month,-3,getdate())
group by aspecId,aspecName)t3
where t1specId=t2specId and t1specName=t2specName
and t3specId=t2specId and t3specName=t2specName
and t1specId=t3specId and t1specName=t3specName
-- and t1specName in ('A','B','C','D') --根据需要 再次过滤select from Table1
union all select name+' Toal' name ,sum(num1),sum(num2) from Table1
where name not in('Lab','R&D')
group by name
--order by name
union all select 'R&D & Lab' name,sum(num1),sum(num2) from Table1
where name in('Lab','R&D')
union all select 'Total' name,sum(num1),sum(num2) from Table1
order by name
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)